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.
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 (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).
# Necessary packages
# 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) # Warm up model
# Compute loss
mean(abs2(myrnn(x) - y) for (x, y) in zip(X[2:end], y[2:end]))
for epoch in 1:10 # Train the RNN for 10 epochs
Flux.reset!(myrnn) # Reset RNN
gs = gradient(ps) do # Compute gradients
Flux.update!(opt, ps, gs) # Update parameters
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!
not all heroes wear capes