I’m new to JUlia programming language. I was trying to create my own model for MNIST classification using Flux.
Below is my code:
model = Chain(
Conv((2,2), 1=>16, relu),
x -> maxpool(x, (2,2)),
Conv((2,2), 16=>8, relu),
x -> maxpool(x, (2,2)),
x -> reshape(x, :, size(x, 4)),
Dense(288, 10), softmax) |> gpu
My Graphics card is not compatible for Flux. Hence, i can only use cpu. If I run the above code it shows error regarding the GPU. But, when i change the gpu to cpu in the above code, it shows the following error:
invalid redefinition of constant model
Why is it showing this error? According to my knowledge, this code should run when it is changed to cpu.
You can just remove the |> gpu completely in the model definition. Note that |> is a pipe operator, so the code above is equivalent to model = gpu(Chain(...)) . If you just run model= Chain(...) the model is already on the CPU so there is no need to call cpu(model).
Actually I see that in my case gpu doesn’t do anything, because Flux knows that cuda is not available.
I’m not sure why you are getting the invalid redefinition error in any case as I don’t encounter it if I repeat your steps exactly, with Julia 1.3.1. Which versions of Flux and Julia are you using?
can you try to restart the REPL and just run the code above (model = ...) ? I suspect you had a different definition of model before which is causing this problem.
How can I check the version of Flux installed?
In the REPL type ] st Flux (typing ] enters the package manager mode).
Flux version is 0.10.1.
When I try running the code in REPL, it is running now. But, it is not working in jupyter notebook. I’m getting the following error when running in jupyter notebook:
MethodError: Cannot `convert` an object of type Zygote.Params to an object of type Float64
Closest candidates are:
convert(::Type{Float64}, !Matched::LLVM.ConstantFP) at /home/g2-test/.julia/packages/LLVM/DAnFH/src/core/value/constant.jl:85
convert(::Type{T}, !Matched::T) where T<:Number at number.jl:6
convert(::Type{T}, !Matched::Number) where T<:Number at number.jl:7
...
Stacktrace:
[1] ADAM(::Zygote.Params, ::Tuple{Float64,Float64}, ::IdDict{Any,Any}) at /home/g2-test/.julia/packages/Flux/2i5P1/src/optimise/optimisers.jl:160
[2] ADAM(::Zygote.Params, ::Tuple{Float64,Float64}) at /home/g2-test/.julia/packages/Flux/2i5P1/src/optimise/optimisers.jl:165 (repeats 2 times)
[3] top-level scope at In[1]:22
What is the difference in doing import Pkg; Pkg.add(“Flux”) and ] add Flux? Is both the same? I did it in both ways.
As answered by @DrChainsaw, you are using the ADAM constructor incorrectly, you shouldn’t pass the parameters of the model to it.
Try to have a look at the documentation or maybe in some MNIST example in the model-zoo .