Invalid redefinition of constant model

Hi everyone.

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.

I copied this code from:
https://www.linkedin.com/pulse/creating-deep-neural-network-model-learn-handwritten-digits-mike-gold/

Can someone help me.
Thanking you in advance.

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?

I’m using Julia 1.3.1 and the flux version I don’t know. When I type ‘using Flux’ in REPL, I’m getting

[ Info: CUDAdrv.jl failed to initialize, GPU functionality unavailable (set JULIA_CUDA_SILENT or JULIA_CUDA_VERBOSE to silence or expand this message)

I tried running the code removing |> But, still the same error is coming.
How can I check the version of Flux installed?

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.

When I try running the code in REPL, it is running now. But, it is not working in jupyter notebook.

Are you running exactly the same code? Are you using the same Julia/Flux/Zygote version in the notebook and REPL?

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 far as I know they are the same.

The error from the first post is probably because you have a function (or maybe struct) defined somewhere with the same name:

julia> ffff(x) = x
ffff (generic function with 1 method)

julia> ffff = 1
ERROR: invalid redefinition of constant ffff
Stacktrace:
 [1] top-level scope at none:0

The second error is probably because ADAM does not take a Zygote.Params as input to the constructor. First argument is the learning rate.

julia> using Flux

help?> ADAM
search: ADAM ADAMW AdaMax NADAM ADAGrad ADADelta readavailable isreadable broadcast Broadcast

  ADAM(η = 0.001, β = (0.9, 0.999))


  ADAM (https://arxiv.org/abs/1412.6980v8) optimiser.

julia> 
1 Like

I found that the second one is the problem. When I comment the ADAM portion, it is running. How can I solve this?

This 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) 
loss(x, y) = crossentropy(model(x), y)
opt = ADAM(params(model))

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 .

1 Like