2016-08-29 14:17:54 +00:00
|
|
|
|
type Delay
|
|
|
|
|
name::Symbol
|
2016-10-25 18:10:26 +00:00
|
|
|
|
default::Nullable{Param}
|
2016-08-29 14:17:54 +00:00
|
|
|
|
end
|
|
|
|
|
|
2016-10-25 18:10:26 +00:00
|
|
|
|
Delay(name) = Delay(name, nothing)
|
|
|
|
|
|
2016-08-29 14:17:54 +00:00
|
|
|
|
function liftloops!(ex, params)
|
|
|
|
|
e = Flow.normedges(ex)
|
|
|
|
|
hidden = intersect((b.args[1] for b in ex.args), params)
|
|
|
|
|
edges = Dict(h => gensym("edge") for h in hidden)
|
|
|
|
|
for b in ex.args
|
|
|
|
|
b.args[2] = MacroTools.postwalk(x -> get(edges, x, x), b.args[2])
|
|
|
|
|
end
|
|
|
|
|
for (h, e) in edges
|
|
|
|
|
unshift!(ex.args, :($e = $(Delay(h))($h)))
|
|
|
|
|
end
|
|
|
|
|
return ex
|
|
|
|
|
end
|
|
|
|
|
|
2016-08-31 01:37:53 +00:00
|
|
|
|
function hasloops(model)
|
|
|
|
|
g = graph(model)
|
|
|
|
|
g == nothing && return false
|
|
|
|
|
iscyclic(g) && return true
|
|
|
|
|
result = false
|
|
|
|
|
map(m -> hasloops(m) && (result = true), g)
|
|
|
|
|
return result
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function atomise(model)
|
|
|
|
|
postwalk(graph(model)) do v
|
|
|
|
|
hasloops(value(v)) || return v
|
|
|
|
|
spliceinputs(atomise(value(v)), inputs(v)...)
|
|
|
|
|
end
|
|
|
|
|
end
|
2016-08-29 14:17:54 +00:00
|
|
|
|
|
2016-08-31 01:37:53 +00:00
|
|
|
|
hinput(n) = vertex(getindex, constant(ModelInput(1)), constant(n))
|
2016-08-29 14:17:54 +00:00
|
|
|
|
|
2016-08-31 01:37:53 +00:00
|
|
|
|
function unroll!(delay::IVertex, n)
|
|
|
|
|
prewalk!(delay[1]) do v
|
|
|
|
|
v === delay ? hinput(n) : v
|
2016-08-29 15:23:43 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2016-08-31 01:37:53 +00:00
|
|
|
|
function break!(g::IVertex)
|
|
|
|
|
g = bumpinputs(g)
|
2016-08-29 15:23:43 +00:00
|
|
|
|
loops = []
|
2016-10-25 18:10:26 +00:00
|
|
|
|
defaults = []
|
2016-08-31 01:37:53 +00:00
|
|
|
|
g = prewalk!(g) do v
|
2016-08-29 15:23:43 +00:00
|
|
|
|
isa(value(v), Delay) || return v
|
2016-08-31 01:37:53 +00:00
|
|
|
|
n = length(loops)+1
|
|
|
|
|
push!(loops, unroll!(v, n))
|
2016-10-25 18:10:26 +00:00
|
|
|
|
push!(defaults, get(value(v).default))
|
2016-08-31 01:37:53 +00:00
|
|
|
|
hinput(n)
|
2016-08-29 15:23:43 +00:00
|
|
|
|
end
|
2016-10-25 18:10:26 +00:00
|
|
|
|
cse(vertex(tuple, vertex(tuple, loops...), g)), defaults
|
2016-08-29 14:17:54 +00:00
|
|
|
|
end
|
|
|
|
|
|
2016-10-25 20:10:04 +00:00
|
|
|
|
function unroll(model, n)
|
|
|
|
|
graph, defaults = break!(atomise(model))
|
|
|
|
|
outputs = [spliceinputs(graph, vertex(tuple, map(constant, defaults)...), constant(ModelInput(1)))]
|
|
|
|
|
for i = 2:n
|
|
|
|
|
push!(outputs, spliceinputs(graph, outputs[end][1], constant(ModelInput(i))))
|
|
|
|
|
end
|
|
|
|
|
state = outputs[end][1]
|
|
|
|
|
outputs = map(x -> x[2], outputs)
|
|
|
|
|
vertex(tuple, state, vertex(tuple, outputs...))
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# r = Chain(Recurrent(30,10), Recurrent(10, 20))
|
|
|
|
|
# unroll(r, 1) |> syntax |> prettify |> display
|
2016-08-29 14:17:54 +00:00
|
|
|
|
|
2016-10-12 15:28:16 +00:00
|
|
|
|
@net type Recurrent
|
2016-09-06 12:06:30 +00:00
|
|
|
|
Wx; Wh; B
|
2016-08-29 14:17:54 +00:00
|
|
|
|
hidden
|
2016-09-06 12:06:30 +00:00
|
|
|
|
|
2016-08-29 14:17:54 +00:00
|
|
|
|
function (x)
|
2016-09-06 12:06:30 +00:00
|
|
|
|
hidden = σ( Wx*x + Wh*hidden + B )
|
2016-08-29 14:17:54 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2016-08-31 01:37:53 +00:00
|
|
|
|
Recurrent(in::Integer, out::Integer; init = initn) =
|
2016-09-06 12:06:30 +00:00
|
|
|
|
Recurrent(init(out, in), init(out, out), init(out), zeros(out))
|
2016-08-29 14:17:54 +00:00
|
|
|
|
|
|
|
|
|
Base.show(io::IO, r::Recurrent) =
|
|
|
|
|
print(io, "Flux.Recurrent(...)")
|