2017-05-01 16:41:42 +00:00
|
|
|
using Flux: mapt, collectt, shapecheckt
|
2017-05-01 15:12:46 +00:00
|
|
|
|
|
|
|
struct Exec
|
2017-05-01 18:44:58 +00:00
|
|
|
session ::Session
|
|
|
|
input ::Any
|
|
|
|
output ::Any
|
|
|
|
grads ::Any
|
|
|
|
params ::Dict{Flux.Param,Tensor}
|
|
|
|
stacks ::Dict{Any,Any}
|
2016-10-26 13:25:10 +00:00
|
|
|
end
|
|
|
|
|
2016-12-20 17:18:40 +00:00
|
|
|
function makesession(model, inputs; session = Session(Graph()))
|
2017-05-01 16:41:42 +00:00
|
|
|
inputs = mapt(_ -> placeholder(Float32), inputs)
|
2016-12-20 17:32:33 +00:00
|
|
|
params, stacks, output = tograph(model, inputs...)
|
2017-05-01 18:44:58 +00:00
|
|
|
# grads = gradients(output, [collectt(inputs)..., values(params)...])
|
|
|
|
grads = placeholder(Float32)
|
2017-04-27 11:48:11 +00:00
|
|
|
run(session, global_variables_initializer())
|
2017-05-01 18:44:58 +00:00
|
|
|
Exec(session, inputs, output, grads, params, stacks)
|
2016-12-20 17:18:40 +00:00
|
|
|
end
|
|
|
|
|
2017-05-01 15:30:24 +00:00
|
|
|
retuple(xs) = xs
|
|
|
|
retuple(xs::AbstractArray{<:AbstractArray}) = (retuple.(xs)...,)
|
|
|
|
|
2017-05-01 16:41:42 +00:00
|
|
|
dictt(xs, ys) = Dict(zip(collectt(xs), collectt(ys)))
|
|
|
|
|
2017-05-01 18:44:58 +00:00
|
|
|
function params(m::Exec, args...)
|
2017-05-01 16:41:42 +00:00
|
|
|
shapecheckt(m.input, args)
|
2017-05-01 17:27:52 +00:00
|
|
|
idict = dictt(m.input, args)
|
|
|
|
pdict = Dict(t => p.x for (p, t) in m.params)
|
2017-05-01 18:44:58 +00:00
|
|
|
merge(idict, pdict)
|
|
|
|
end
|
|
|
|
|
|
|
|
function (m::Exec)(args...)
|
|
|
|
retuple(run(m.session, m.output, params(m, args...)))
|
|
|
|
end
|
|
|
|
|
|
|
|
pullt!(_, xs) = shift!(xs)
|
|
|
|
pullt!(x::Tuple, xs) = map(x -> pullt!(x, xs), x)
|
|
|
|
|
|
|
|
# TODO: gradients don't work yet
|
|
|
|
# `gradients` lacks support for `grad_y`s and multiple `y`s
|
|
|
|
|
|
|
|
function Flux.back!(m::Exec, Δ, args...)
|
|
|
|
Δps = run(m.session, m.grads, params(m, args...))
|
|
|
|
Δin = pullt!(m.input, Δps)
|
|
|
|
for (p, Δ) in zip(keys(m.params), Δps)
|
|
|
|
p.Δx .+= Δ
|
|
|
|
end
|
|
|
|
Δin
|
|
|
|
end
|
|
|
|
|
|
|
|
function Flux.update!(m::Exec, η)
|
|
|
|
for p in keys(m.params)
|
|
|
|
update!(p, η)
|
|
|
|
end
|
|
|
|
return m
|
2016-10-26 13:25:10 +00:00
|
|
|
end
|
|
|
|
|
2017-05-01 15:12:46 +00:00
|
|
|
mutable struct Model
|
|
|
|
model::Any
|
|
|
|
exec::Exec
|
|
|
|
Model(model) = new(model)
|
2016-10-26 13:25:10 +00:00
|
|
|
end
|
|
|
|
|
2017-05-01 15:12:46 +00:00
|
|
|
tf(model) = Model(model)
|
|
|
|
|
|
|
|
function (m::Model)(args...)
|
2017-05-01 17:27:52 +00:00
|
|
|
args = mapt(x->Float32.(x), args)
|
2017-05-30 17:04:31 +00:00
|
|
|
isdefined(m, :exec) || (m.exec = makesession(m.model, args))
|
2017-05-01 15:12:46 +00:00
|
|
|
@tferr m.exec.stacks m.exec(args...)
|
2016-10-28 19:50:27 +00:00
|
|
|
end
|
2016-10-26 13:25:10 +00:00
|
|
|
|
2017-05-01 18:44:58 +00:00
|
|
|
Flux.back!(m::Model, Δ, args...) = back!(m.exec, Δ, args...)
|
|
|
|
Flux.update!(m::Model, η) = (update!(m.exec, η); m)
|
2017-05-01 17:05:17 +00:00
|
|
|
|
|
|
|
# Recurrent Models
|
|
|
|
|
|
|
|
using Flux: Stateful, SeqModel
|
|
|
|
|
|
|
|
tf(m::Stateful) = Stateful(tf(m.model), m.istate, m.ostate)
|
|
|
|
tf(m::SeqModel) = SeqModel(tf(m.model), m.steps)
|