<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/neural-networks-exercises-part-3/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.r-exercises.com/2017/06/21/neural-networks-exercises-part-3/?utm_source=rss&utm_medium=rss&utm_campaign=neural-networks-exercises-part-3"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>
<div class="wp-caption"><a rel="nofollow" href="https://i0.wp.com/www.r-exercises.com/wp-content/uploads/2017/06/300px-Colored_neural_network.svg_.png" target="_blank"><img sizes="(max-width: 249px) 100vw, 249px" src="https://i0.wp.com/www.r-exercises.com/wp-content/uploads/2017/06/300px-Colored_neural_network.svg_-249x300.png?resize=249%2C300" alt="" class="wp-image-6305" data-recalc-dims="1"></a>
<p class="wp-caption-text">Source: <a rel="nofollow" href="https://en.wikipedia.org/wiki/File:Colored_neural_network.svg" target="_blank">Wikipedia</a></p>
</div>
<p>
Neural network have become a corner stone of machine learning in the last decade. Created in the late 1940s with the intention to create computer programs who mimics the way neurons process information, those kinds of algorithm have long been believe to be only an academic curiosity, deprived of practical use since they require a lot of processing power and other machine learning algorithm outperform them. However since the mid 2000s, the creation of new neural network types and techniques, couple with the increase availability of fast computers made the neural network a powerful tool that every data analysts or programmer must know.</p>
<p>In this series of articles, we’ll see how to fit a neural network with R, we’ll learn the core concepts we need to know to well apply those algorithms and how to evaluate if our model is appropriate to use in production. In the <a rel="nofollow" href="http://www.r-exercises.com/2017/06/15/neural-networks-exercises-part-2/" target="_blank">last exercises sets</a>, we have seen how to implement a feed-forward neural network in R. That kind of neural network is quite useful to match a single input value to a specific output value, either a dependent variable in regression problems or a class in clustering problems. However sometime, a sequence of input can give a lot more of information to the network than a single value. For example, if you want to train a neural network to predict which letter will come next in a word based on which letters have been typed, making prediction based on the last letter entered can give good results, but if all the previous letter are used for making the predictions the results should be better since the arrangement of previous letter can give important information about the rest of the word.</p>
<p>In today’s exercise set, we will see a type of neural network that is design to make use of the information made available by using sequence of inputs. Those ”recurrent neural networks” do so by using a hidden state at time t-1 that influence the calculation of the weight at time t. For more information about this type of neural network, you can read this <a rel="nofollow" href="https://deeplearning4j.org/lstm.html" target="_blank">article</a> which is a good introduction on the subject.</p>
<p>Answers to the exercises are available <a rel="nofollow" href="http://www.r-exercises.com/2017/06/21/neural-networks-solutions-part-3/" target="_blank">here</a>.</p>
<p><strong>Exercise 1</strong><br>
We will start by using a recurrent neural network to predict the values of a time series. Load the <code>tsEuStockMarkets</code> dataset from the <code>dataset</code> package and save the first 1400 observations from the “DAX” time series as your working dataset.</p>
<p><strong>Exercise 2</strong><br>
Process the dataset so he can be used in a neural network.</p>
<p><strong>Exercise 3</strong><br>
Create two matrix containing 10 sequences of 140 observations from the previous dataset. The first one must be made of the original observations and will be the input of our neural network. The second one will be the output and since we want to predict the value of the stock market at time t+1 based on the value at time t, this matrix will be the same as the first one were all the elements are shifted from one position. Make sure that each sequence are coded as a row of each matrix.</p>
<p><strong>Exercise 4</strong><br>
Set the seed to 42 and choose randomly eight sequences to train your model and two sequences that will be used for validation later. Once it’s done, load the <code>rnn</code> package and use the <code>trainr()</code> function to train a recurrent neural network on the training dataset. For now, use a learning rate of 0.01, one hidden layer of one neuron and 500 epoch.</p>
<p><strong>Exercise 5</strong><br>
Use the function <code>predictr</code> to make prediction on all the 10 sequences of your original data matrix, then plot the real values and the predicted value on the same graph. Also draw the plot of the prediction on the test set and the real value of your dataset.</p>
<p><strong>Exercise 6</strong><br>
The last model seems to underestimate the stock values that are higher than 0.5. Repeat the step of exercise 3 and 4 but this time use 10 hidden layers. Once it’s done calculate the RMSE of your predictions. This will be the baseline model for the rest of this exercise set.</p>
<div>
<aside><img src="http://www.r-exercises.com/2017/06/21/neural-networks-exercises-part-3/?utm_source=rss&utm_medium=rss&utm_campaign=neural-networks-exercises-part-3"></aside>
<div><strong>Learn more</strong> about neural networks in the online course <a rel="nofollow" href="http://www.r-exercises.com/product/machine-learning-a-z-hands-on-python-r-in-data-science/" target="_blank">Machine Learning A-Z™: Hands-On Python &amp; R In Data Science</a>. In this course you will learn how to:
<ul>
<li>Work with Deep Learning networks and related packages in R</li>
<li>Create Natural Language Processing models</li>
<li>And much more</li>
</ul>
</div>
</div>
<p><strong>Exercise 7</strong><br>
One interesting method often used to accelerate the training of a neural network is the “Nesterov momentum”. This procedure is based on the fact that while trying to find the weights that minimize the cost function of your neural network, optimization algorithm like gradient descend “zigzag” around a straight path to the minimum value. By adding a momentum matrix, which keeps track of the general direction of the gradient, to the gradient we can minimize the deviation from this optimal path and speeding the convergence of the algorithm. You can see this <a rel="nofollow" href="https://www.youtube.com/watch?v=8yg2mRJx-z4" target="_blank">video</a> for more information about this concept.</p>
<p>Repeat the last exercise, but this time use 250 epochs and a momentum of 0.7.</p>
<p><strong>Exercise 8</strong><br>
As special type of recurrent neural network trained by backpropagation through time is called the Long Short-Term Memory (LSTM) network. This type of recurrent neural network is quite useful in a deep learning context, since this method is robust again the vanishing gradient problem. We will see both of those concepts more in detail in a future exercise set, but for now you can read about it <a rel="nofollow" href="https://en.wikipedia.org/wiki/Long_short-term_memory" target="_blank">here</a>.</p>
<p>The <code>trainr()</code> function give us the ability to train a LSTM network by setting the <code>network_type</code> parameter to “lstm”. Use this algorithm with 500 epochs and 20 neuron in the hidden layer to predict the value of your time series.</p>
<p><strong>Exercise 9</strong><br>
When working with a recurrent neural network it is important to choose an input sequence length that give the algorithm the maximum information possible without adding useless noise to the input. Until now we used 10 sequences of 140 observations. Train a recurrent neural network on 28 sequences of 50 observations, make prediction and compute the RMSE to see if this encoding had an effect on your predictions.</p>
<p><strong>Exercise 10</strong><br>
Try to use all of the 1860 observation in the “DAX” time series to train and test a recurrent neural network. Then post the setting you used for your model and why you choose them in the comments.</p>
<div>
<h3>Related exercise sets:</h3>
<ol>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/06/15/neural-networks-exercises-part-2/" title="Neural networks Exercises (Part-2)" target="_blank">Neural networks Exercises (Part-2) </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/06/08/neural-networks-exercises-part-1/" title="Neural networks Exercises (Part-1)" target="_blank">Neural networks Exercises (Part-1) </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/05/02/forecasting-for-small-business-exercises-part-4/" title="Forecasting for small business Exercises (Part-4)" target="_blank">Forecasting for small business Exercises (Part-4) </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/start-here-to-learn-r/" target="_blank">Explore all our (>1000) R exercises</a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/r-courses/" target="_blank">Find an R course using our R Course Finder</a> directory</li>
</ol>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.r-exercises.com/2017/06/21/neural-networks-exercises-part-3/?utm_source=rss&utm_medium=rss&utm_campaign=neural-networks-exercises-part-3"> R-exercises</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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/Am0wkoY4YKk" height="1"></a></div>