2016-10-26 13:25:10 +00:00
|
|
|
|
type Model
|
2016-10-28 20:17:48 +00:00
|
|
|
|
model::Any
|
2016-10-26 13:25:10 +00:00
|
|
|
|
session::Session
|
2016-10-28 15:06:56 +00:00
|
|
|
|
params::Dict{Flux.Param,Tensor}
|
2016-10-26 13:25:10 +00:00
|
|
|
|
inputs::Vector{Tensor}
|
2016-10-28 20:17:48 +00:00
|
|
|
|
output::Any
|
2016-10-28 14:13:58 +00:00
|
|
|
|
gradients::Vector{Tensor}
|
2016-10-26 13:25:10 +00:00
|
|
|
|
end
|
|
|
|
|
|
2016-10-28 19:50:27 +00:00
|
|
|
|
ismultioutput(m::Model) = !isa(m.output, Tensor)
|
|
|
|
|
|
2016-10-26 13:25:10 +00:00
|
|
|
|
function tf(model)
|
2016-10-28 15:26:06 +00:00
|
|
|
|
sess = Session(Graph())
|
2016-10-26 13:25:10 +00:00
|
|
|
|
input = placeholder(Float32)
|
2016-10-28 15:06:56 +00:00
|
|
|
|
params, output = tograph(model, input)
|
2016-10-26 13:25:10 +00:00
|
|
|
|
run(sess, initialize_all_variables())
|
2016-10-28 15:06:56 +00:00
|
|
|
|
Model(model, sess, params,
|
2016-10-28 19:50:27 +00:00
|
|
|
|
[input], output,
|
2016-10-28 15:06:56 +00:00
|
|
|
|
[gradients(output, input)])
|
2016-10-26 13:25:10 +00:00
|
|
|
|
end
|
|
|
|
|
|
2016-10-28 16:00:31 +00:00
|
|
|
|
batchone(x) = Batch((x,))
|
2016-10-29 23:20:15 +00:00
|
|
|
|
batchone(x::Batch) = x
|
2016-10-28 16:00:31 +00:00
|
|
|
|
|
|
|
|
|
function batch(xs)
|
|
|
|
|
dims = ndims(xs)-1
|
|
|
|
|
T = Array{eltype(xs),dims}
|
|
|
|
|
B = Array{eltype(xs),dims+1}
|
|
|
|
|
Batch{T,B}(xs)
|
|
|
|
|
end
|
2016-10-26 13:25:10 +00:00
|
|
|
|
|
|
|
|
|
function (m::Model)(args::Batch...)
|
|
|
|
|
@assert length(args) == length(m.inputs)
|
2016-10-28 19:50:27 +00:00
|
|
|
|
output = run(m.session, m.output, Dict(zip(m.inputs, args)))
|
|
|
|
|
ismultioutput(m) ? (batch.(output)...,) : batch(output)
|
2016-10-26 13:25:10 +00:00
|
|
|
|
end
|
|
|
|
|
|
2016-10-28 19:50:27 +00:00
|
|
|
|
function (m::Model)(args...)
|
|
|
|
|
output = m(map(batchone, args)...)
|
|
|
|
|
ismultioutput(m) ? map(first, output) : first(output)
|
|
|
|
|
end
|
2016-10-26 13:25:10 +00:00
|
|
|
|
|
|
|
|
|
function Flux.back!(m::Model, Δ, args...)
|
|
|
|
|
@assert length(args) == length(m.inputs)
|
|
|
|
|
# TODO: keyword arguments to `gradients`
|
2016-10-28 14:13:58 +00:00
|
|
|
|
run(m.session, m.gradients[1], Dict(zip(m.inputs, args)))
|
2016-10-26 13:25:10 +00:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function Flux.update!(m::Model)
|
|
|
|
|
error("update! is not yet supported on TensorFlow models")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
import Juno: info
|
|
|
|
|
|
|
|
|
|
function Flux.train!(m::Model, train, test=[]; epoch = 1, η = 0.1,
|
|
|
|
|
loss = (y, y′) -> reduce_sum((y - y′).^2)/2,
|
|
|
|
|
opt = TensorFlow.train.GradientDescentOptimizer(η))
|
|
|
|
|
i = 0
|
|
|
|
|
Y = placeholder(Float32)
|
2016-10-28 14:13:58 +00:00
|
|
|
|
Loss = loss(m.outputs[1], Y)
|
2016-10-26 13:25:10 +00:00
|
|
|
|
minimize_op = TensorFlow.train.minimize(opt, Loss)
|
|
|
|
|
for e in 1:epoch
|
|
|
|
|
info("Epoch $e\n")
|
|
|
|
|
@progress for (x, y) in train
|
2016-10-28 14:13:58 +00:00
|
|
|
|
y, cur_loss, _ = run(m.session, vcat(m.outputs[1], Loss, minimize_op),
|
2016-10-28 16:00:31 +00:00
|
|
|
|
Dict(m.inputs[1]=>batchone(x), Y=>batchone(y)))
|
2016-10-26 13:25:10 +00:00
|
|
|
|
if i % 5000 == 0
|
|
|
|
|
@show y
|
|
|
|
|
@show accuracy(m, test)
|
|
|
|
|
end
|
|
|
|
|
i += 1
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|