dup params fix

This commit is contained in:
Mike J Innes 2017-06-08 10:49:39 +01:00
parent ab0f184d29
commit 4ccbbbb284
2 changed files with 13 additions and 5 deletions

View File

@ -91,9 +91,8 @@ end
register(ctx::Context, node) = node register(ctx::Context, node) = node
function var(ctx::Context, p::Union{Flux.Param{<:AbstractArray},AbstractArray,AlterParam}) function var(ctx::Context, p::Union{Flux.Param{<:AbstractArray},AbstractArray,AlterParam})
id = gensym() haskey(ctx[:params], p) && return ctx[:params][p]
ctx[:params][id] = p ctx[:params][p] = mx.Variable(gensym())
return mx.Variable(id)
end end
var(ctx::Context, x) = x var(ctx::Context, x) = x
@ -110,10 +109,11 @@ graph(ctx::Context, args...) = @icatch ctx graph(ctx, args...)
function tograph(model, args...; feedforward = false) function tograph(model, args...; feedforward = false)
ctx = Context(mux(iline, iconst, ilambda, iargs, ituple, graph), ctx = Context(mux(iline, iconst, ilambda, iargs, ituple, graph),
params = Dict(), stacks = Dict(), params = ObjectIdDict(), stacks = Dict(),
feedforward = feedforward) feedforward = feedforward)
out = @ithrow graph(ctx, model, mapt(mx.Variable, args)...) out = @ithrow graph(ctx, model, mapt(mx.Variable, args)...)
return Graph(args, out, ctx[:params], ctx[:stacks]) params = Dict(nodename(v) => p for (p, v) in ctx[:params])
return Graph(args, out, params, ctx[:stacks])
end end
# Error Handling # Error Handling

View File

@ -28,4 +28,12 @@ using Flux: MaxPool
@test mx.infer_shape(f.arch, data = (20, 20, 5, 1))[2] == [(10, 1)] @test mx.infer_shape(f.arch, data = (20, 20, 5, 1))[2] == [(10, 1)]
end end
@testset "Duplicate parameters" begin
a = Affine(10, 10)
d = Chain(a, a)
m = mxnet(d)
m(randn(1, 10))
@test length(m.graph.params) == 2
end
end end