# Invalid redefinition of constant model

**URL:** https://discourse.julialang.org/t/invalid-redefinition-of-constant-model/35162
**Category:** New to Julia
**Created:** [February 26, 2020, 10:59am UTC](https://discourse.julialang.org/t/invalid-redefinition-of-constant-model/35162 "2020-02-26T10:59:50Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Eldho.Ashna](https://avatars.discourse-cdn.com/v4/letter/e/bc8723/32.png) [@Eldho.Ashna](https://discourse.julialang.org/u/Eldho.Ashna)
#### Post date: [February 26, 2020, 10:59am UTC](https://discourse.julialang.org/t/invalid-redefinition-of-constant-model/35162/1 "2020-02-26T10:59:50Z")

</div>

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:

```julia
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:

```julia
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/](https://www.linkedin.com/pulse/creating-deep-neural-network-model-learn-handwritten-digits-mike-gold/)

Can someone help me.  
Thanking you in advance.

---

<div class="post-metadata">

### Author: ![orialb](https://avatars.discourse-cdn.com/v4/letter/o/65b543/32.png) [@orialb](https://discourse.julialang.org/u/orialb)
#### Post date: [February 26, 2020, 12:32pm UTC](https://discourse.julialang.org/t/invalid-redefinition-of-constant-model/35162/2 "2020-02-26T12:32:53Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![Eldho.Ashna](https://avatars.discourse-cdn.com/v4/letter/e/bc8723/32.png) [@Eldho.Ashna](https://discourse.julialang.org/u/Eldho.Ashna)
#### Post date: [February 26, 2020, 12:40pm UTC](https://discourse.julialang.org/t/invalid-redefinition-of-constant-model/35162/3 "2020-02-26T12:40:15Z")

</div>

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

```julia
[ 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?

---

<div class="post-metadata">

### Author: ![orialb](https://avatars.discourse-cdn.com/v4/letter/o/65b543/32.png) [@orialb](https://discourse.julialang.org/u/orialb)
#### Post date: [February 26, 2020, 12:43pm UTC](https://discourse.julialang.org/t/invalid-redefinition-of-constant-model/35162/4 "2020-02-26T12:43:41Z")

</div>

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).

---

<div class="post-metadata">

### Author: ![Eldho.Ashna](https://avatars.discourse-cdn.com/v4/letter/e/bc8723/32.png) [@Eldho.Ashna](https://discourse.julialang.org/u/Eldho.Ashna)
#### Post date: [February 26, 2020, 12:54pm UTC](https://discourse.julialang.org/t/invalid-redefinition-of-constant-model/35162/5 "2020-02-26T12:54:02Z")

</div>

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:

```julia
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.

---

<div class="post-metadata">

### Author: ![orialb](https://avatars.discourse-cdn.com/v4/letter/o/65b543/32.png) [@orialb](https://discourse.julialang.org/u/orialb)
#### Post date: [February 26, 2020, 12:58pm UTC](https://discourse.julialang.org/t/invalid-redefinition-of-constant-model/35162/6 "2020-02-26T12:58:48Z")

</div>

> 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.

---

<div class="post-metadata">

### Author: ![DrChainsaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drchainsaw/32/8497_2.png) [@DrChainsaw](https://discourse.julialang.org/u/DrChainsaw)
#### Post date: [February 26, 2020, 5:19pm UTC](https://discourse.julialang.org/t/invalid-redefinition-of-constant-model/35162/7 "2020-02-26T17:19:26Z")

</div>

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

```julia
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
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> 

```

---

<div class="post-metadata">

### Author: ![Eldho.Ashna](https://avatars.discourse-cdn.com/v4/letter/e/bc8723/32.png) [@Eldho.Ashna](https://discourse.julialang.org/u/Eldho.Ashna)
#### Post date: [February 27, 2020, 4:04am UTC](https://discourse.julialang.org/t/invalid-redefinition-of-constant-model/35162/8 "2020-02-27T04:04:44Z")

</div>

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:

```julia
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))

```

---

<div class="post-metadata">

### Author: ![orialb](https://avatars.discourse-cdn.com/v4/letter/o/65b543/32.png) [@orialb](https://discourse.julialang.org/u/orialb)
#### Post date: [February 27, 2020, 11:17am UTC](https://discourse.julialang.org/t/invalid-redefinition-of-constant-model/35162/10 "2020-02-27T11:17:09Z")

</div>

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](https://github.com/FluxML/model-zoo/) .
