denizyuret/Knet.jl

Issue with Knet.save: MethodError: no method matching +(::Array{UInt8,1}, ::Int64)

gsoykan opened this issue · 3 comments

I have been trying to save my NN architecture with Knet.save.
The structure consists of a 'Chain' and 'layers' within them.
However, it throws the below error.
Any help would be much appreciated.

Error encountered while saving "test.jld2".

Fatal error:
MethodError: no method matching +(::Array{UInt8,1}, ::Int64)
For element-wise addition, use broadcasting with dot syntax: array .+ scalar
Closest candidates are:
  +(::Any, ::Any, !Matched::Any, !Matched::Any...) at operators.jl:538
  +(!Matched::BigInt, ::Union{Int16, Int32, Int64, Int8}) at gmp.jl:530
  +(!Matched::Base.CoreLogging.LogLevel, ::Integer) at logging.jl:116
  ...

Stacktrace:
 [1] handle_error(::MethodError, ::File{DataFormat{:JLD2}}) at /kuacc/users/gsoykan20/.julia/packages/FileIO/TyKdX/src/error_handling.jl:82
 [2] handle_exceptions(::Array{Any,1}, ::String) at /kuacc/users/gsoykan20/.julia/packages/FileIO/TyKdX/src/error_handling.jl:77
 [3] save(::FileIO.Formatted, ::Any, ::Vararg{Any,N} where N; options::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at /kuacc/users/gsoykan20/.julia/packages/FileIO/TyKdX/src/loadsave.jl:238
 [4] save at /kuacc/users/gsoykan20/.julia/packages/FileIO/TyKdX/src/loadsave.jl:217 [inlined]
 [5] #save#19 at /kuacc/users/gsoykan20/.julia/packages/FileIO/TyKdX/src/loadsave.jl:139 [inlined]
 [6] save(::String, ::String, ::Chain) at /kuacc/users/gsoykan20/.julia/packages/FileIO/TyKdX/src/loadsave.jl:139
 [7] save(::String, ::String, ::Vararg{Any,N} where N; options::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at /kuacc/users/gsoykan20/.julia/packages/Knet/LdQyF/src/knetarrays/jld2.jl:14
 [8] save(::String, ::String, ::Vararg{Any,N} where N) at /kuacc/users/gsoykan20/.julia/packages/Knet/LdQyF/src/knetarrays/jld2.jl:13
 [9] top-level scope at In[148]:1
 [10] include_string(::Function, ::Module, ::String, ::String) at ./loading.jl:1091

Here is a minimum runnable code piece that could lead the error:

using Knet

struct Chain
    layers; lambda1; lambda2; loss;
    Chain(layers...; lambda1=0, lambda2=0, loss=nll) = new(layers, lambda1, lambda2, loss)
end

(c::Chain)(x) = (for l in c.layers; x = l(x); end; x)
function (c::Chain)(x, y)
    loss = c.loss(c(x), y)
    if training() 
        c.lambda1 != 0 && (loss += c.lambda1 * sum(sum(abs, l.w) for l in c.layers))
        c.lambda2 != 0 && (loss += c.lambda2 * sum(sum(abs2, l.w) for l in c.layers))
    end
    return loss
end
(c::Chain)(d::Data) = mean(c(x, y) for (x, y) in d)

struct Deconv; w; stride; padding; end
Deconv(w1, w2, nx, ny; stride=1, padding=0, atype=Knet.atype()) = Deconv(param(w1, w2, nx, ny;atype=atype), stride, padding)

(dc::Deconv)(x) = deconv4(dc.w, x; stride=dc.stride, padding=dc.padding)

test_chain = Chain( Deconv(4, 4, 14, 2048; padding=1, stride=2));
Knet.save("test.jld2", "deconv", test_chain)

Please use FileIO.save instead of Knet.save while I investigate this.

I pushed a fix to Knet#master, you can test with pkg"add Knet#master"

I tested it and now it works. Thank you.