<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/plain-vanilla-recurrent-neural-networks-in-r-waves-prediction/"></div></div><div><a data-url="https://www.r-bloggers.com/plain-vanilla-recurrent-neural-networks-in-r-waves-prediction/" data-counturl="https://www.r-bloggers.com/plain-vanilla-recurrent-neural-networks-in-r-waves-prediction/" data-count="horizontal" data-text="Plain vanilla recurrent neural networks in R: waves prediction" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://firsttimeprogrammer.blogspot.com/2016/08/plain-vanilla-recurrent-neural-networks.html"> The Beginner Programmer</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>While continuing my study of neural networks and deep learning, I inevitably meet up with recurrent neural networks.</p>
<p><a target="_blank" href="https://en.wikipedia.org/wiki/Recurrent_neural_network"><strong>Recurrent neural networks</strong></a><strong> (RNN)</strong> 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<strong> </strong><a target="_blank" href="https://en.wikipedia.org/wiki/Long_short-term_memory"><strong>LSTM network</strong></a><strong>.</strong> As far as I know, LSTM should usually be preferred to a plain vanilla RNN when possible as it yields better results.</p>
<p> <a name="more"></a>
<p>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 “<strong><a target="_blank" href="https://cran.r-project.org/web/packages/rnn/index.html">rnn</a></strong>” for working with recurrent neural networks. I am going to dive in LSTM using <a target="_blank" href="https://github.com/dmlc/mxnet">MXNET</a> and <a target="_blank" href="https://www.tensorflow.org/">Tensorflow</a> later.</p>
<h2>Task description</h2>
<p>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.</p>
<p><a target="_blank" href="https://i0.wp.com/lh3.googleusercontent.com/-gZKBQ0vxYVo/V6SpIF_8U9I/AAAAAAAABsM/GTSm60jDxgo/s1600-h/Rplot01%25255B3%25255D.png?ssl=1"><img title="Rplot01" alt="Rplot01" src="https://i1.wp.com/lh3.googleusercontent.com/-sHooecJS3c4/V6SpIoxHANI/AAAAAAAABsQ/p4rgC6m_bgo/Rplot01_thumb%25255B1%25255D.png?resize=456%2C231&ssl=1" border="0" data-recalc-dims="1"></a></p>
<p>X is essentially a sine wave with some normally distributed noise, while Y is a straightforward smooth cosine wave.</p>
<p>You can clearly see that what I expect the model to do is to capture the phase shift of 90 degrees between the two waves and to throw away the noise in the input.</p>
<p>I chose to use a 5Hz frequency for both the waves but you can play around maybe trying to obtain similar results changing the frequency. Be aware though that the higher the frequency, the more datapoints you need to avoid problems that comes with the <a target="_blank" href="https://en.wikipedia.org/wiki/Nyquist%E2%80%93Shannon_sampling_theorem">sampling theorem</a>.</p>
<h2>Preprocessing</h2>
<p>The artificial dataset I created for this task is a set of 10 sequences each of which consists of 40 observations. The X matrix contains 10 sequences of a noisy sine wave while the Y matrix contains the corresponding 10 sequences of a clear cosine wave.</p>
<p>Before fitting the model I standardized all the data in the $[0 – 1]$ interval. When using any neural network model with real valued data, make sure not to avoid this step because if you do, then you might spend the next hour trying to figure out why the model did not converge or spitted out weird results. I am not an expert but I know from personal painful experience that this step is usually crucial, nevertheless I may occasionally forget to do it and then wander around like a fool looking for why I did not get what I was expecting <img alt="Smile" src="https://i1.wp.com/lh3.googleusercontent.com/-LcGhfpZbiqA/V6SpI_gfrQI/AAAAAAAABsU/X4G0oacSyOc/wlEmoticon-smile%25255B2%25255D.png?w=456&ssl=1" data-recalc-dims="1"></p>
<h2>Model</h2>
<p>As far as the model is concerned, I decided to use 16 hidden neurons, mostly because the other configurations that I tried all ended up with weird spikes in the valleys and peaks of the waves. This is the most notable problem I have encountered while trying to address this task: it is very easy to predict the upwards and downwards paths of the wave, while the peaks and valleys may raise some problems and be predicted as sudden spikes. 16 hidden units and about 1500 epochs seem to fix this problem. </p>
<h2>Results</h2>
<p>These below are the results I obtained after some experiments:</p>
<p>This is the full prediction for the entire predictor matrix X</p>
<p><a target="_blank" href="https://i0.wp.com/lh3.googleusercontent.com/-7Nvgd7mc4Nw/V6SpJFjT2-I/AAAAAAAABsY/4IxVEKRMRf0/s1600-h/Rplot02%25255B5%25255D.png?ssl=1"><img title="Rplot02" alt="Rplot02" src="https://i2.wp.com/lh3.googleusercontent.com/-ybp2Nt98po0/V6SpJR0G6CI/AAAAAAAABsc/4ZXQkVsNUMg/Rplot02_thumb%25255B3%25255D.png?resize=452%2C237&ssl=1" border="0" data-recalc-dims="1"></a></p>
<p>While this one is the prediction for the test set</p>
<p><a target="_blank" href="https://i0.wp.com/lh3.googleusercontent.com/-o6AcKHGcNf4/V6SpJpL4RVI/AAAAAAAABsg/eY67kZCoGnM/s1600-h/Rplot03%25255B5%25255D.png?ssl=1"><img title="Rplot03" alt="Rplot03" src="https://i1.wp.com/lh3.googleusercontent.com/-tunxH2YYqjc/V6SpJ9sHpdI/AAAAAAAABsk/hvuQAsWiKgc/Rplot03_thumb%25255B3%25255D.png?resize=434%2C258&ssl=1" border="0" data-recalc-dims="1"></a></p>
<p>I would say they look pretty good. I encourage you to try and play with this to look for the limits of the model. For instance I tryied to double the frequency of the cosine wave to 10Hz and still, the predictions look pretty good. Below you can see the X sequence (no change here) and the doubled frequency Y sequence. The last plot shows the prediction on the testing set vs the real values.</p>
<p><a target="_blank" href="https://i0.wp.com/lh3.googleusercontent.com/-PWRu3GggDeo/V6SpKEsG3XI/AAAAAAAABso/DXid6DJeRVU/s1600-h/Rplot05%25255B3%25255D.png?ssl=1"><img title="Rplot05" alt="Rplot05" src="https://i1.wp.com/lh3.googleusercontent.com/-AvA9jz6QyhA/V6SpKb27x3I/AAAAAAAABss/SHEqFydIRz0/Rplot05_thumb%25255B1%25255D.png?resize=453%2C230&ssl=1" border="0" data-recalc-dims="1"></a></p>
<p><a target="_blank" href="https://i2.wp.com/lh3.googleusercontent.com/-t8nTdCsH9vA/V6SpKqI6mdI/AAAAAAAABsw/H_Pe0EcUB4Y/s1600-h/Rplot04%25255B3%25255D.png?ssl=1"><img title="Rplot04" alt="Rplot04" src="https://i2.wp.com/lh3.googleusercontent.com/-s14U1WJeLjA/V6SpK-P_iHI/AAAAAAAABs0/Cup1ssBafhg/Rplot04_thumb%25255B1%25255D.png?resize=455%2C231&ssl=1" border="0" data-recalc-dims="1"></a></p>
<p>The code I used for this simple experiment is showed in the gist below. In order to get the plots for the doubled frequency example just put $f = 10$.</p>
<p>
<p>Thank you for reading this post, I hope you’ve found it interesting and useful. If you have any question, please do leave a comment.</p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/plain-vanilla-recurrent-neural-networks-in-r-waves-prediction/"></div></div><div><a data-url="https://www.r-bloggers.com/plain-vanilla-recurrent-neural-networks-in-r-waves-prediction/" data-counturl="https://www.r-bloggers.com/plain-vanilla-recurrent-neural-networks-in-r-waves-prediction/" data-count="vertical" data-text="Plain vanilla recurrent neural networks in R: waves prediction" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://firsttimeprogrammer.blogspot.com/2016/08/plain-vanilla-recurrent-neural-networks.html"> The Beginner Programmer</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/LgZINm4izMM"></a></div>