JuliaIO / BSON.jl

Other
158 stars 39 forks source link

BSON load Array{typeof(σ),1} problem #79

Open luboshanus opened 3 years ago

luboshanus commented 3 years ago

Hi,

I have a setup for modelling using Flux and I change activation functions. When I have my activation functions in an array and those are different:

julia> [relu, sigmoid]
2-element Array{Function,1}:
 relu (generic function with 2 methods)
 σ (generic function with 4 methods)

the type of the array is Function and when saved in BSON after working with the array then the type can be Any and this is fine for loading .bson.

But, when the array of activation functions is only sigmoids then the type of the array is typeof(σ), such as:

julia> [sigmoid, sigmoid]
2-element Array{typeof(σ),1}:
 σ (generic function with 4 methods)
 σ (generic function with 4 methods)

This works fine when having the dictionary with these but it does not work well when saved and then loaded. I got this error: (Also posted on slack: https://julialang.slack.com/archives/C7LFJTXV5/p1610360933246600 )

julia> BSON.load(file_with_sigmoids)
ERROR: MethodError: no method matching typeof(σ)()
Stacktrace:
 [1] (::BSON.var"#37#38")(::Dict{Symbol,Any}) at /Users/luboshanus/.julia/packages/BSON/XAts7/src/extensions.jl:76
 [2] _raise_recursive(::Dict{Symbol,Any}, ::IdDict{Any,Any}) at /Users/luboshanus/.julia/packages/BSON/XAts7/src/read.jl:79
 [3] raise_recursive(::Dict{Symbol,Any}, ::IdDict{Any,Any}) at /Users/luboshanus/.julia/packages/BSON/XAts7/src/read.jl:89
 [4] (::BSON.var"#39#40"{IdDict{Any,Any}})(::Dict{Symbol,Any}) at /Users/luboshanus/.julia/packages/BSON/XAts7/src/extensions.jl:124
 [5] iterate at ./generator.jl:47 [inlined]
 [6] collect_to!(::Array{Any,1}, ::Base.Generator{Array{Any,1},BSON.var"#39#40"{IdDict{Any,Any}}}, ::Int64, ::Int64) at ./array.jl:732
 [7] collect_to!(::Array{Real,1}, ::Base.Generator{Array{Any,1},BSON.var"#39#40"{IdDict{Any,Any}}}, ::Int64, ::Int64) at ./array.jl:740 (repeats 3 times)
 [8] collect_to_with_first!(::Array{Int64,1}, ::Int64, ::Base.Generator{Array{Any,1},BSON.var"#39#40"{IdDict{Any,Any}}}, ::Int64) at ./array.jl:710
 [9] _collect(::Array{Any,1}, ::Base.Generator{Array{Any,1},BSON.var"#39#40"{IdDict{Any,Any}}}, ::Base.EltypeUnknown, ::Base.HasShape{1}) at ./array.jl:704
 [10] collect_similar(::Array{Any,1}, ::Base.Generator{Array{Any,1},BSON.var"#39#40"{IdDict{Any,Any}}}) at ./array.jl:628
 [11] map(::Function, ::Array{Any,1}) at ./abstractarray.jl:2162
 [12] newstruct_raw(::IdDict{Any,Any}, ::Type{T} where T, ::Dict{Symbol,Any}) at /Users/luboshanus/.julia/packages/BSON/XAts7/src/extensions.jl:124
 [13] (::BSON.var"#43#44")(::Dict{Symbol,Any}, ::IdDict{Any,Any}) at /Users/luboshanus/.julia/packages/BSON/XAts7/src/extensions.jl:140
 [14] raise_recursive(::Dict{Symbol,Any}, ::IdDict{Any,Any}) at /Users/luboshanus/.julia/packages/BSON/XAts7/src/read.jl:88
 [15] (::BSON.var"#21#22"{IdDict{Any,Any}})(::Dict{Symbol,Any}) at /Users/luboshanus/.julia/packages/BSON/XAts7/src/read.jl:94
 [16] applychildren!(::BSON.var"#21#22"{IdDict{Any,Any}}, ::Array{Any,1}) at /Users/luboshanus/.julia/packages/BSON/XAts7/src/BSON.jl:28
 [17] raise_recursive at /Users/luboshanus/.julia/packages/BSON/XAts7/src/read.jl:94 [inlined]
 [18] (::BSON.var"#39#40"{IdDict{Any,Any}})(::Array{Any,1}) at /Users/luboshanus/.julia/packages/BSON/XAts7/src/extensions.jl:124
 [19] iterate at ./generator.jl:47 [inlined]
 [20] collect_to!(::Array{Array{Any,1},1}, ::Base.Generator{Array{Any,1},BSON.var"#39#40"{IdDict{Any,Any}}}, ::Int64, ::Int64) at ./array.jl:732
 [21] collect_to_with_first!(::Array{Array{Any,1},1}, ::Array{Any,1}, ::Base.Generator{Array{Any,1},BSON.var"#39#40"{IdDict{Any,Any}}}, ::Int64) at ./array.jl:710
 [22] _collect(::Array{Any,1}, ::Base.Generator{Array{Any,1},BSON.var"#39#40"{IdDict{Any,Any}}}, ::Base.EltypeUnknown, ::Base.HasShape{1}) at ./array.jl:704
 [23] collect_similar(::Array{Any,1}, ::Base.Generator{Array{Any,1},BSON.var"#39#40"{IdDict{Any,Any}}}) at ./array.jl:628
 [24] map(::Function, ::Array{Any,1}) at ./abstractarray.jl:2162
 [25] newstruct_raw(::IdDict{Any,Any}, ::Type{T} where T, ::Dict{Symbol,Any}) at /Users/luboshanus/.julia/packages/BSON/XAts7/src/extensions.jl:124
 [26] (::BSON.var"#43#44")(::Dict{Symbol,Any}, ::IdDict{Any,Any}) at /Users/luboshanus/.julia/packages/BSON/XAts7/src/extensions.jl:140
 [27] raise_recursive(::Dict{Symbol,Any}, ::IdDict{Any,Any}) at /Users/luboshanus/.julia/packages/BSON/XAts7/src/read.jl:88
 [28] raise_recursive at /Users/luboshanus/.julia/packages/BSON/XAts7/src/read.jl:99 [inlined]
 [29] load(::String) at /Users/luboshanus/.julia/packages/BSON/XAts7/src/read.jl:104
 [30] top-level scope at REPL[20]:1

I have figured a workaround for my case and I pushed the array to be saved as Any, but with the results I already have I don't know what to do. (Will recompute all.) Maybe I don't see my own mistake there in loading or saving, but, isn't there just an easy fix with the raise_recursive ? Thanks.