# Recurrent Neural Networks using Julia

**URL:** <https://discourse.julialang.org/t/recurrent-neural-networks-using-julia/79119>\
**Category:** New to Julia\
**Created:** [April 6, 2022, 6:35pm UTC](https://discourse.julialang.org/t/recurrent-neural-networks-using-julia/79119 "2022-04-06T18:35:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nash/32/14482_2.png) [@Nash](https://discourse.julialang.org/u/Nash)\
**Post date:** [April 6, 2022, 6:35pm UTC](https://discourse.julialang.org/t/recurrent-neural-networks-using-julia/79119/1 "2022-04-06T18:35:12Z")

</div>

Consider the following simple situation:

I have a timeseries of IBM stock (a simple array of 5000 prices) and I want to predict the next price given that data. How can I use Julia to create a Recurrent Neural Networks and solve my problem?

I would very much appreciate a step-by-step example, rather than general comments.

---

<div class="post-metadata">

**Author:** ![JLDC](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jldc/32/17627_2.png) [@JLDC](https://discourse.julialang.org/u/JLDC)\
**Post date:** [April 6, 2022, 8:29pm UTC](https://discourse.julialang.org/t/recurrent-neural-networks-using-julia/79119/2 "2022-04-06T20:29:24Z")

</div>

Hi Nash

A lot of details are missing to give a more specific answer, but here are some pointers.

I would first suggest you take a look at the [documentation on recurrent models in Flux](https://fluxml.ai/Flux.jl/stable/models/recurrence/) (there are also other libraries, I just happen to work with Flux personally). This will help you understand my example better.

In the following, I will assume your data is a vector of Float64 with a length of 5000. I will also assume that you use the full sequence length, which is probably not a good idea, and I will assume that your data is scaled in some way (e.g. normalization or min-max scaling).

```julia
# Necessary packages
using Flux
using Statistics
# Convert your data to Float32
ibm_data = Float32.(ibm_data)
# Reshape your data to the general RNN input format in Flux
# Note that this assumes a sequence length of 5000, i.e. the full sequence, which is not necessarily a good idea
X = [[x] for x in ibm_data[1:end-1]]
y = ibm_data[2:end]
# Create the RNN model
myrnn = Chain(RNN(1, 32), Dense(32, 1))
# Choose an optimizer
opt = ADAM(1e-2)
# Keep track of parameters for update
ps = Flux.params(myrnn)
# Define a loss function
function loss(X, y)
    myrnn(X[1]) # Warm up model
    # Compute loss
    mean(abs2(myrnn(x)[1] - y) for (x, y) in zip(X[2:end], y[2:end]))
end
for epoch in 1:10 # Train the RNN for 10 epochs
    Flux.reset!(myrnn) # Reset RNN
    gs = gradient(ps) do # Compute gradients
        loss(X, y)        
    end
    Flux.update!(opt, ps, gs) # Update parameters
end

```

That’s it, a very basic RNN to predict your IBM stock price series. Note that you won’t be able to do good predictions with it, a lot more fine tuning is needed but I hope this gets you on the right direction!

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [April 6, 2022, 8:30pm UTC](https://discourse.julialang.org/t/recurrent-neural-networks-using-julia/79119/3 "2022-04-06T20:30:50Z")

</div>

not all heroes wear capes
