2016-10-26 13:25:10 +00:00
|
|
|
|
type Model
|
2016-10-28 14:13:58 +00:00
|
|
|
|
model
|
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 14:13:58 +00:00
|
|
|
|
outputs::Vector{Tensor}
|
|
|
|
|
gradients::Vector{Tensor}
|
2016-10-26 13:25:10 +00:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
[input], [output],
|
|
|
|
|
[gradients(output, input)])
|
2016-10-26 13:25:10 +00:00
|
|
|
|
end
|
|
|
|
|
|
2016-10-28 16:00:31 +00:00
|
|
|
|
batchone(x) = Batch((x,))
|
|
|
|
|
|
|
|
|
|
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 16:00:31 +00:00
|
|
|
|
batch(run(m.session, m.outputs[1], Dict(zip(m.inputs, args))))
|
2016-10-26 13:25:10 +00:00
|
|
|
|
end
|
|
|
|
|
|
2016-10-28 16:00:31 +00:00
|
|
|
|
(m::Model)(args...) = first(m(map(batchone, args)...))
|
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
|