Flux.jl/src/backend/tensorflow/model.jl

60 lines
1.6 KiB
Julia
Raw Normal View History

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 14:13:58 +00:00
vars::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 14:13:58 +00:00
sess = Session()
vars = Dict{Flux.Param,Tensor}()
2016-10-26 13:25:10 +00:00
input = placeholder(Float32)
2016-10-28 14:13:58 +00:00
output = graph(model, input)
2016-10-26 13:25:10 +00:00
run(sess, initialize_all_variables())
2016-10-28 14:13:58 +00:00
Model(model, sess, vars, [input], [output], [gradients(output, input)])
2016-10-26 13:25:10 +00:00
end
batch(x) = Batch((x,))
function (m::Model)(args::Batch...)
@assert length(args) == length(m.inputs)
2016-10-28 14:13:58 +00:00
run(m.session, m.outputs[1], Dict(zip(m.inputs, args)))
2016-10-26 13:25:10 +00:00
end
(m::Model)(args...) = m(map(batch, args)...)
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-26 13:25:10 +00:00
Dict(m.inputs[1]=>batch(x), Y=>batch(y)))
if i % 5000 == 0
@show y
@show accuracy(m, test)
end
i += 1
end
end
end