<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-2/"></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/15/neural-networks-exercises-part-2/?utm_source=rss&utm_medium=rss&utm_campaign=neural-networks-exercises-part-2"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<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. Today, we’ll practice how to use the <code>nnet</code> and <code>neuralnet</code> packages to create a feedforward neural networks, which we introduce in the <a rel="nofollow" href="http://www.r-exercises.com/2017/06/08/neural-networks-exercises-part-1/" target="_blank"> last set of exercises</a>. In this type of neural network, all the neuron from the input layer are linked to the neuron from the hidden layer and all of those neuron are linked to the output layer, like seen on this <a rel="nofollow" href="https://en.wikipedia.org/wiki/Feedforward_neural_network#/media/File:Feed_forward_neural_net.gif" target="_blank">image</a>. Since there’s no cycle in this network, the information flow in one direction from the input layer to the hidden layers to the output layer. For more information about those types of neural network you can <a rel="nofollow" href="http://www.iro.umontreal.ca/~bengioy/ift6266/H12/html/mlp_en.html" target="_blank">read this page</a>.</p>
<p>Answers to the exercises are available <a rel="nofollow" href="http://www.r-exercises.com/2017/06/15/neural-networks-solutions-part-2/" target="_blank">here</a>.</p>
<p><strong>Exercise 1</strong><br>
We’ll start by practicing what we’ve seen in the <a rel="nofollow" href="http://www.r-exercises.com/2017/06/08/neural-networks-exercises-part-1/" target="_blank"> last set of exercises</a>. Load the <code>MASS</code> package and the biopsy dataset, then prepare your data to be feed to a neural network.</p>
<p><strong>Exercise 2</strong><br>
We’ll use the <code>nnet()</code> function from the package of the same name to do a logistic regression on the biopsy data set using a feedforward neural network. If you remember the<br>
<a rel="nofollow" href="http://www.r-exercises.com/2017/06/08/neural-networks-exercises-part-1/" target="_blank"> last set of exercises</a> you know that we have to choose the number of neuron in the hidden layer of our feedforward neural network. There’s no rule or equation which can tell us the optimal number of neurons to use, so the best way to find the better model is to do a bunch of cross-validation of our model with different number of neurons in the hidden layer and choose the one who would fit best the data. A good range to test with this process is between one neuron and the number of input variables.</p>
<p>Write a function that take a train data set, a test data set and a range of integer corresponding to the number of neurons to be used as parameter. Then this function should, for each possible number of neuron in the hidden layer, train a neural network made with <code>nnet()</code>, make prediction on the test set and return the accuracy of the prediction.</p>
<p><strong>Exercise 3</strong><br>
Use your function on your data set and plot the result. Which should be the number of neurons to use in the hidden layer of your feedforward neural network.</p>
<p><strong>Exercise 4</strong><br>
The <code>nnet()</code> function is easy to use, but doesn’t give us a lot of option to customize our neural network. As a consequence, it’s a good package to use if you have to do a quick model to test a hypothesis, but for more complex model the <code>neuralnet</code> package is a lot more powerful. Documentation for this package can be found <a rel="nofollow" target="_blank">here</a>.</p>
<p>Use the <code>neuralnet()</code> function with the default parameter and the number of neuron in the hidden layer set to the answer of the last exercise. Note that this function can only handle numeric value and cannot deal with factors. Then use the <code>compute()</code> function to make prediction on the values of the test set and compute the accuracy of your model.</p>
<div>
<aside><img src="http://www.r-exercises.com/2017/06/15/neural-networks-exercises-part-2/?utm_source=rss&utm_medium=rss&utm_campaign=neural-networks-exercises-part-2"></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 5</strong><br>
The <code>nnet()</code> function use by default the <a rel="nofollow" href="https://en.wikipedia.org/wiki/Broyden%E2%80%93Fletcher%E2%80%93Goldfarb%E2%80%93Shanno_algorithm" target="_blank">BFGS algorithm</a> to adjust the value of the weights until the output values of our model are close to the values of our data set. The <code>neuralnet</code> package give us the option to use more efficient algorithm to compute those value which result in faster processing time and overall better estimation. For example, by default this function use the resilient backpropagation with weight backtracking.</p>
<p>Use the <code>neuralnet()</code> function with the parameter <code>algorithm</code> set to ‘rprop-‘, which stand for resilient backpropagation without weight backtracking.<br>
Then test your model and print the accuracy.</p>
<p><strong>Exercise 6</strong><br>
Two other algorithm can be used with the <code>neuralnet()</code> function: <code>'sag'</code> and <code>'slr'</code>. Those two strings tell the function to use the globally convergent algorithm (grprop) and to modify the learning rate associated with the smallest absolute gradient (sag) or the smallest learning rate (slr). When using those algorithm, it can be useful to pass a vector or list containing the lowest and highest limit for the learning rate to the learningrate.limit parameter.</p>
<p>Again, use the <code>neuralnet()</code> function twice, once with parameter <code>algorithm</code> set to <code>'sag'</code> and then to <code>'slr'</code>. In both cases set the <code>learningrate.limit</code> parameter to c(0.1,1) and change the <code>stepmax</code> parameter to <code>1e+06</code>.</p>
<p><strong>Exercise 7</strong><br>
The learning rate determine how much the backpropagation can affect the weight at each iteration. A high learning rate mean that during the training of the neural network, each iteration can strongly change the value of the weight or, to put in other way, the algorithm learn a lot of each observation in your data set. This mean that outlier could easily affect your weight and make your algorithm diverge from the path of the ideal weights for your problem. A small learning rate mean that the algorithm learn less from each observation in your data set, so your neural network is less affected by outlier, but this mean that you will need more observations to make a good model.</p>
<p>Use the <code>neuralnet()</code> function with parameter <code>algorithm</code> set to ‘rprop+’ twice: once with the <code>learningrate</code> parameter set to 0.01 and another time with the <code>learningrate</code> parameter set to 1. Notice the difference in running time in both cases.</p>
<p><strong>Exercise 8</strong><br>
The <code>neuralnet</code> package give us the ability of make a visual representation of the neural network you made. Use the <code>plot()</code> function to visualize one of the neural networks of the last exercise.</p>
<p><strong>Exercise 9</strong><br>
Until now, we’ve used feedfordward neural network with one hidden layer of neurons, but we could use more. In fact, the state of the art neural network use often 100 of hidden layer for modeling complex behavior. For basic regression problems or even basic digits recognition problems, one layer is enough, but if you want to use more, you can do so with the <code>neuralnet()</code> function by passing a vector of integer to the hidden parameter representing the number of neurons in each layer.</p>
<p>Create a feedforward neural network with three hidden layers of nine neurons and use it on your data.</p>
<p><strong>Exercise 10</strong><br>
Plot the feedforward neural network from the last exercise.</p>
<div>
<h3>Related exercise sets:</h3>
<ol>
<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/31/evaluate-your-model-with-r-exercises/" title="Evaluate your model with R Exercises" target="_blank">Evaluate your model with R Exercises </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/2016/12/02/model-evaluation-exercise-1/" title="Model Evaluation Exercises 1" target="_blank">Model Evaluation Exercises 1 </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/15/neural-networks-exercises-part-2/?utm_source=rss&utm_medium=rss&utm_campaign=neural-networks-exercises-part-2"> 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/VzrJxCoEpP4" height="1"></a></div>