https://www.r-bloggers.com/plain-vanilla-recurrent-neural-networks-in-r-waves-prediction/
While continuing my study of neural networks and deep learning, I inevitably meet up with recurrent neural networks.
Recurrent neural networks (RNN) are a particular kind of neural networks usually very good at predicting sequences due to their inner working. If your task is to predict a sequence or a periodic signal, then using a RNN might be a good starting point. Plain vanilla RNN work fine but they have a little problem when trying to “keep in memory” events occured, say for instance, more than 20 steps back. The solution to this problem has been addressed with the development of a model called LSTM network. As far as I know, LSTM should usually be preferred to a plain vanilla RNN when possible as it yields better results.
In this post however, I am going to work on a plain vanilla RNN model. The reasons for doing this are two. First of all this is one of my first experience with RNN and I would like to get comfortable with them before going deeper; secondly, R provides a simple and very user friendly package named “rnn” for working with recurrent neural networks. I am going to dive in LSTM using MXNET and Tensorflow later.
Task description The task I am going to address is trying to predict a cosine from a noisy sine wave. Here below you can see the plot of the predictor X sequence and the Y sequence to be predicted.
# Clear workspace rm(list=ls()) # Load libraries require(rnn) # Set seed for reproducibility purposes set.seed(10) # Set frequency f <- 5 w <- 2*pi*f # Create sequences t <- seq(0.005,2,by=0.005) x <- sin(t*w) + rnorm(200, 0, 0.25) y <- cos(t*w) # Samples of 20 time series X <- matrix(x, nrow = 40) Y <- matrix(y, nrow = 40) # Plot noisy waves plot(as.vector(X), col='blue', type='l', ylab = "X,Y", main = "Noisy waves") lines(as.vector(Y), col = "red") legend("topright", c("X", "Y"), col = c("blue","red"), lty = c(1,1), lwd = c(1,1))
# Standardize in the interval 0 - 1 X <- (X - min(X)) / (max(X) - min(X)) Y <- (Y - min(Y)) / (max(Y) - min(Y)) # Transpose X <- t(X) Y <- t(Y) # Training-testing sets train <- 1:8 test <- 9:10
# Train model. Keep out the last two sequences. model <- trainr(Y = Y[train,], X = X[train,], learningrate = 0.05, hidden_dim = 16, numepochs = 1500) # Predicted values Yp <- predictr(model, X) # Plot predicted vs actual. Training set + testing set plot(as.vector(t(Y)), col = 'red', type = 'l', main = "Actual vs predicted", ylab = "Y,Yp") lines(as.vector(t(Yp)), type = 'l', col = 'blue') legend("topright", c("Predicted", "Real"), col = c("blue","red"), lty = c(1,1), lwd = c(1,1)) # Plot predicted vs actual. Testing set only. plot(as.vector(t(Y[test,])), col = 'red', type='l', main = "Actual vs predicted: testing set", ylab = "Y,Yp") lines(as.vector(t(Yp[test,])), type = 'l', col = 'blue') legend("topright", c("Predicted", "Real"), col = c("blue","red"), lty = c(1,1), lwd = c(1,1))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.