Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why not define this function in a normal way? #141

Open
hurricane007 opened this issue May 11, 2023 · 3 comments
Open

Why not define this function in a normal way? #141

hurricane007 opened this issue May 11, 2023 · 3 comments

Comments

@hurricane007
Copy link
Contributor

Hello guys,
I just started using SimpleChains.jl.
For me, Julia is a tool and I didn't learn the language very carefully so I don't know all the syntaxes. It's a little bit hard for me to understand the function below, which is found in the first example of SimpleChains.jl. I think there must be a lot of users like me who don't have sophisticated knowledge of Julia.
So could the authors write documentation and examples in a simpler/common way?
Thanks!

report = let mtrain = mlpdloss, X=X, Xtest=Xtest, mtest = mlpdtest
  p -> begin
    let train = mlpdloss(X, p), test = mlpdtest(Xtest, p)
      @info "Loss:" train test
    end
  end
end
@chriselrod
Copy link
Contributor

Julia has bad semantics around capturing variables in closures, causing frequent bugs like this

julia> x = rand(2,3);

julia> f = y -> y + x
#3 (generic function with 1 method)

julia> a = rand(2,3);

julia> f(a)
2×3 Matrix{Float64}:
 1.39317   1.18612  1.0031
 0.303721  1.17959  0.689349

julia> x = 4 # I'm now doing something else, so redefining x
4

julia> f(a) # let me call f again... oops!
ERROR: MethodError: no method matching +(::Matrix{Float64}, ::Int64)
For element-wise addition, use broadcasting with dot syntax: array .+ scalar

Closest candidates are:
  +(::Any, ::Any, ::Any, ::Any...)
   @ Base operators.jl:585
  +(::T, ::T) where T<:Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8}
   @ Base int.jl:87
  +(::Rational, ::Integer)
   @ Base rational.jl:332
  ...

Stacktrace:
 [1] (::var"#3#4")(y::Matrix{Float64})
   @ Main ./REPL[2]:1
 [2] top-level scope
   @ REPL[6]:1

Using let in this way lets us avoid the problem:

julia> x = rand(2,3);

julia> f = let x = x; y -> y + x; end
#5 (generic function with 1 method)

julia> a = rand(2,3);

julia> f(a)
2×3 Matrix{Float64}:
 0.600399  1.78474  1.07068
 0.416237  1.025    0.708488

julia> x = 4
4

julia> f(a) # f still works!
2×3 Matrix{Float64}:
 0.600399  1.78474  1.07068
 0.416237  1.025    0.708488

@hurricane007
Copy link
Contributor Author

If I understand correctly, report = let mtrain = mlpdloss, X=X, Xtest=Xtest, mtest = mlpdtest uses mtrain = mlpdloss to avoid bugs when mlpdloss is redefined. I am confused as mtrain is not used in the function.
Or do I understand it wrong?

@chriselrod
Copy link
Contributor

I am confused as mtrain is not used in the function.
Or do I understand it wrong?

Ha ha, no, you understand it correctly. That is a bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants