# Replicate Python code in Julia

**URL:** <https://discourse.julialang.org/t/replicate-python-code-in-julia/98434>\
**Category:** New to Julia\
**Created:** [May 7, 2023, 10:02am UTC](https://discourse.julialang.org/t/replicate-python-code-in-julia/98434 "2023-05-07T10:02:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vahagn](https://avatars.discourse-cdn.com/v4/letter/v/cc9497/32.png) [@Vahagn](https://discourse.julialang.org/u/Vahagn)\
**Post date:** [May 7, 2023, 10:02am UTC](https://discourse.julialang.org/t/replicate-python-code-in-julia/98434/1 "2023-05-07T10:02:14Z")

</div>

Hi everyone,  
I want to replicate Python code in Julia but I get an error.

#Neural network construction  
using Flux, Plots

model = Chain(Dense(5 =\> 32), Dense(32 =\> 32), Dense(32 =\> 2, bias=false))

function dr(a::Vector, z::Vector, e\_r::Vector, R::Vector, C::Vector)  
# create two empty vectors of length n  
n = length(a)  
x = zeros(n)  
y = zeros(n)

```
# we normalize exogenous state variables by their 2 standard deviations 
# so that they are typically between -1 and 1 
a = a/sigma_e_a/2
z = z/sigma_e_z/2
e_r = e_r/sigma_e_r/2

# we normalze interest rate and consumtpion to be between -1 and 1
R = (R-Rmin)/(Rmax-Rmin)*2.0-1.0
C = (C-Cmin)/(Cmax-Cmin)*2.0-1.0

s = cat([_e[:, 1] for _e in [a, z, e_r, R, C]], dims=2)

x = model(s) # n x 2 matrix

# consumption share is always in [0,1]
lambda_desicion = exp.x[:,1]

# expectation of marginal consumption is always positive
pie_desicion = x[:,2]

return (lambda_desicion, pie_desicion)

```

end

Rvec = LinRange(Rmin, Rmax, 100)  
Cvec = LinRange(cmin, cmax, 100)  
Rvec = vcat(Rvec)  
Cvec = vcat(Cvec)  
lambdavec,pievec = dr(Rvec_0, Rvec_0, Rvec\*0, Rvec, Cvec)

Can anyone, please, tell me what’s wrong with my code?

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [May 7, 2023, 10:24am UTC](https://discourse.julialang.org/t/replicate-python-code-in-julia/98434/2 "2023-05-07T10:24:38Z")

</div>

Hi there! What error messages do you see? The messages often contain the answers. 😀

---

<div class="post-metadata">

**Author:** ![Vahagn](https://avatars.discourse-cdn.com/v4/letter/v/cc9497/32.png) [@Vahagn](https://discourse.julialang.org/u/Vahagn)\
**Post date:** [May 7, 2023, 10:30am UTC](https://discourse.julialang.org/t/replicate-python-code-in-julia/98434/3 "2023-05-07T10:30:39Z")

</div>

MethodError: no method matching -(::Vector{Float64}, ::Int64)  
For element-wise subtraction, use broadcasting with dot syntax: array .- scalar  
Closest candidates are:  
-(::T, ::T) where T\<:Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8} at int.jl:86  
-(::UniformScaling, ::Number) at /Applications/Julia-1.8.app/Contents/Resources/julia/share/julia/stdlib/v1.8/LinearAlgebra/src/uniformscaling.jl:146  
-(::Core.LLVMPtr, ::Integer) at ~/.julia/packages/LLVM/HykgZ/src/interop/pointer.jl:111  
…

Stacktrace:  
[1] dr(a::Vector{Float64}, z::Vector{Float64}, e\_r::Vector{Float64}, R::Vector{Float64}, C::Vector{Float64})  
@ Main ./In[16]:14  
[2] top-level scope  
@ In[30]:5

---

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [May 7, 2023, 10:47am UTC](https://discourse.julialang.org/t/replicate-python-code-in-julia/98434/4 "2023-05-07T10:47:40Z")

</div>

Funny, the error message literally contains the answer: _“For element-wise subtraction, use broadcasting with dot syntax: array .- scalar”_.

So if you have a vector `v` and want to subtract a scalar `s` from every element of `v`, use `v .- s`.

---

<div class="post-metadata">

**Author:** ![Vahagn](https://avatars.discourse-cdn.com/v4/letter/v/cc9497/32.png) [@Vahagn](https://discourse.julialang.org/u/Vahagn)\
**Post date:** [May 7, 2023, 11:17am UTC](https://discourse.julialang.org/t/replicate-python-code-in-julia/98434/5 "2023-05-07T11:17:28Z")

</div>

Thanks, I corrected the code, but now I am getting another error message. It is similar to first one but I can’t figure it out

---

<div class="post-metadata">

**Author:** ![Vahagn](https://avatars.discourse-cdn.com/v4/letter/v/cc9497/32.png) [@Vahagn](https://discourse.julialang.org/u/Vahagn)\
**Post date:** [May 7, 2023, 11:17am UTC](https://discourse.julialang.org/t/replicate-python-code-in-julia/98434/6 "2023-05-07T11:17:49Z")

</div>

MethodError: no method matching +(::Vector{Float64}, ::Float32)  
For element-wise addition, use broadcasting with dot syntax: array .+ scalar  
Closest candidates are:  
+(::Any, ::Any, ::Any, ::Any…) at operators.jl:591  
+(::T, ::T) where T\<:Union{Float16, Float32, Float64} at float.jl:383  
+(::Union{InitialValues.NonspecificInitialValue, InitialValues.SpecificInitialValue{typeof(+)}}, ::Any) at ~/.julia/packages/InitialValues/OWP8V/src/InitialValues.jl:154  
…

Stacktrace:  
[1] \_broadcast\_getindex\_evalf  
@ ./broadcast.jl:670 [inlined]  
[2] \_broadcast\_getindex  
@ ./broadcast.jl:643 [inlined]  
[3] \_getindex  
@ ./broadcast.jl:667 [inlined]  
[4] \_broadcast\_getindex  
@ ./broadcast.jl:642 [inlined]  
[5] getindex  
@ ./broadcast.jl:597 [inlined]  
[6] copy  
@ ./broadcast.jl:899 [inlined]  
[7] materialize  
@ ./broadcast.jl:860 [inlined]  
[8] (::Dense{typeof(identity), Matrix{Float32}, Vector{Float32}})(x::Matrix{Vector{Float64}})  
@ Flux ~/.julia/packages/Flux/uCLgc/src/layers/basic.jl:174  
[9] macro expansion  
@ ~/.julia/packages/Flux/uCLgc/src/layers/basic.jl:53 [inlined]  
[10] \_applychain  
@ ~/.julia/packages/Flux/uCLgc/src/layers/basic.jl:53 [inlined]  
[11] (::Chain{Tuple{Dense{typeof(identity), Matrix{Float32}, Vector{Float32}}, Dense{typeof(identity), Matrix{Float32}, Vector{Float32}}, Dense{typeof(identity), Matrix{Float32}, Bool}}})(x::Matrix{Vector{Float64}})  
@ Flux ~/.julia/packages/Flux/uCLgc/src/layers/basic.jl:51  
[12] dr(a::Vector{Float64}, z::Vector{Float64}, e\_r::Vector{Float64}, R::Vector{Float64}, C::Vector{Float64})  
@ Main ./In[46]:19  
[13] top-level scope  
@ In[47]:5

---

<div class="post-metadata">

**Author:** ![Vahagn](https://avatars.discourse-cdn.com/v4/letter/v/cc9497/32.png) [@Vahagn](https://discourse.julialang.org/u/Vahagn)\
**Post date:** [May 7, 2023, 11:19am UTC](https://discourse.julialang.org/t/replicate-python-code-in-julia/98434/7 "2023-05-07T11:19:01Z")

</div>

The error is in neural network part of my code and I don’t have any addition in it. So I don’t know what is wrong.

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [May 7, 2023, 11:19am UTC](https://discourse.julialang.org/t/replicate-python-code-in-julia/98434/8 "2023-05-07T11:19:47Z")

</div>

Two more things: try to format your posts so that the code is easier to read:

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

Also, please tell us if you’re using ChatGPT (and try to avoid using its code if you don’t understand it.
