refactor input model
This commit is contained in:
parent
10761a4bee
commit
eb78f67a93
|
@ -1,8 +1,8 @@
|
|||
module Flux
|
||||
|
||||
using MacroTools, Lazy, Flow, Juno
|
||||
import Flow: graphm, syntax, prewalk, iscyclic, Constant, constant, isconstant,
|
||||
value, inputs, thread!, value, inputs
|
||||
import Flow: graphm, syntax, prewalk, postwalk, iscyclic, Constant, constant,
|
||||
isconstant, value, inputs, thread!, value, inputs, Split, Group
|
||||
import Juno: Tree, Row
|
||||
|
||||
# Zero Flux Given
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
module TF
|
||||
|
||||
using ..Flux, Flow, TensorFlow, Juno
|
||||
import Flow: Constant, postwalk, value, inputs
|
||||
import Flux: accuracy
|
||||
import Flow: Constant, postwalk, value, inputs, constant
|
||||
import Flux: accuracy, spliceinputs, detuple
|
||||
import TensorFlow: RawTensor
|
||||
import Juno: info
|
||||
|
||||
|
@ -20,9 +20,7 @@ graph{T<:AArray}(p::Flux.Param{T}) = Variable(p.x)
|
|||
function graph(model::Model, args...)
|
||||
g = Flux.graph(model)
|
||||
g ≠ nothing || error("No graph for $model")
|
||||
g = Flow.mapconst(g) do x
|
||||
isa(x, Flux.ModelInput) ? args[x.n] : x
|
||||
end
|
||||
g = spliceinputs(g, map(constant, args)...) |> detuple
|
||||
postwalk(g) do v
|
||||
vertex(graph(cvalue(v), cvalue.(inputs(v))...))
|
||||
end |> value
|
||||
|
|
|
@ -14,7 +14,7 @@ end
|
|||
function makegraph(graph, args)
|
||||
@assert length(args) == 1
|
||||
mapconst(graph) do x
|
||||
x == args[1] ? ModelInput(1) :
|
||||
x == args[1] ? inputnode(1) :
|
||||
isa(x, Delay) ? :(Delay($(Expr(:quote, x.name)), self.$(x.name))) :
|
||||
x
|
||||
end
|
||||
|
|
|
@ -1,19 +1,30 @@
|
|||
# TODO: change the input approach
|
||||
immutable ModelInput
|
||||
n::Int
|
||||
end
|
||||
immutable ModelInput end
|
||||
|
||||
isinput(x) = isa(x, Constant) && isa(x.value, ModelInput)
|
||||
inputnode(n) = vertex(Split(n), constant(ModelInput()))
|
||||
|
||||
bumpinput(i::ModelInput) = ModelInput(i.n + 1)
|
||||
bumpinput(x) = x
|
||||
# isinput(x) = isa(x, Constant) && value(x) == Input()
|
||||
|
||||
bumpinputs(v::IVertex) = mapconst(bumpinput, v)
|
||||
# function bumpinput(v::IVertex)
|
||||
# prewalk(v) do v
|
||||
# isa(value(v), Split) && value(v[1]) == Input() ?
|
||||
# end
|
||||
# end
|
||||
|
||||
function spliceinputs(v::IVertex, inputs::IVertex...)
|
||||
postwalk(v) do v
|
||||
isinput(value(v)) ?
|
||||
inputs[value(v).value.n] :
|
||||
v
|
||||
function spliceinput(v::IVertex, input::IVertex)
|
||||
prewalk(v) do v
|
||||
value(v) == Constant(ModelInput()) ? input : v
|
||||
end
|
||||
end
|
||||
|
||||
spliceinputs(v::IVertex, inputs::Vertex...) =
|
||||
spliceinput(v, vertex(Group(), inputs...))
|
||||
|
||||
function detuple(v::IVertex)
|
||||
prewalk(v) do v
|
||||
if isa(value(v), Split) && isa(value(v[1]), Group)
|
||||
v[1][value(v).n]
|
||||
else
|
||||
v
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -34,11 +34,11 @@ function atomise(model)
|
|||
end
|
||||
end
|
||||
|
||||
hinput(n) = vertex(getindex, constant(ModelInput(1)), constant(n))
|
||||
hiddeninput(n) = vertex(Split(n), inputnode(1))
|
||||
|
||||
function unroll!(delay::IVertex, n)
|
||||
prewalk!(delay[1]) do v
|
||||
v === delay ? hinput(n) : v
|
||||
v === delay ? hiddeninput(n) : v
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -51,23 +51,23 @@ function break!(g::IVertex)
|
|||
n = length(loops)+1
|
||||
push!(loops, unroll!(v, n))
|
||||
push!(defaults, get(value(v).default))
|
||||
hinput(n)
|
||||
hiddeninput(n)
|
||||
end
|
||||
cse(vertex(tuple, vertex(tuple, loops...), g)), defaults
|
||||
end
|
||||
|
||||
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
|
||||
# function unroll(model, n)
|
||||
# graph, defaults = break!(atomise(model))
|
||||
# outputs = [spliceinputs(graph, vertex(tuple, map(constant, defaults)...), inputnode(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))
|
||||
# r = Recurrent(10,10)
|
||||
# unroll(r, 1) |> syntax |> prettify |> display
|
||||
|
||||
@net type Recurrent
|
||||
|
|
|
@ -33,6 +33,6 @@ function Base.show(io::IO, c::Chain)
|
|||
end
|
||||
|
||||
graph(s::Chain) =
|
||||
foldl((v, m) -> vertex(m, v), constant(ModelInput(1)), s.layers)
|
||||
foldl((v, m) -> vertex(m, v), constant(inputnode(1)), s.layers)
|
||||
|
||||
shape(c::Chain, in) = c.shape
|
||||
|
|
Loading…
Reference in New Issue