added tensorflow

This commit is contained in:
mhjensen
2018-10-02 22:15:26 +02:00
parent 598d85b068
commit ff08e04b6e
64 changed files with 4579 additions and 154 deletions
+16 -3
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -261,7 +274,7 @@ MathJax.Hub.Config({
<li><a href="._NeuralNet-bs008.html">9</a></li>
<li><a href="._NeuralNet-bs009.html">10</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs001.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+16 -3
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -246,7 +259,7 @@ a weight variable.
<li><a href="._NeuralNet-bs009.html">10</a></li>
<li><a href="._NeuralNet-bs010.html">11</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs002.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+16 -3
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -296,7 +309,7 @@ humanities to life science and medicine.
<li><a href="._NeuralNet-bs010.html">11</a></li>
<li><a href="._NeuralNet-bs011.html">12</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs003.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+16 -3
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -261,7 +274,7 @@ methods we discussed earlier.
<li><a href="._NeuralNet-bs011.html">12</a></li>
<li><a href="._NeuralNet-bs012.html">13</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs004.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+16 -3
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -253,7 +266,7 @@ to <em>all</em> nodes in the subsequent layer, making this a so-called
<li><a href="._NeuralNet-bs012.html">13</a></li>
<li><a href="._NeuralNet-bs013.html">14</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs005.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+16 -3
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -262,7 +275,7 @@ recognition.
<li><a href="._NeuralNet-bs013.html">14</a></li>
<li><a href="._NeuralNet-bs014.html">15</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs006.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+16 -3
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -254,7 +267,7 @@ especially well-suited for handwriting and speech recognition.
<li><a href="._NeuralNet-bs014.html">15</a></li>
<li><a href="._NeuralNet-bs015.html">16</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs007.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+16 -3
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -255,7 +268,7 @@ type of NN due the unusual activation functions.
<li><a href="._NeuralNet-bs015.html">16</a></li>
<li><a href="._NeuralNet-bs016.html">17</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs008.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+16 -3
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -253,7 +266,7 @@ Such networks are often called <em>multilayer perceptrons</em> (MLPs).
<li><a href="._NeuralNet-bs016.html">17</a></li>
<li><a href="._NeuralNet-bs017.html">18</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs009.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+16 -3
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -258,7 +271,7 @@ as to not restrict the range of output values.
<li><a href="._NeuralNet-bs017.html">18</a></li>
<li><a href="._NeuralNet-bs018.html">19</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs010.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+16 -3
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -259,7 +272,7 @@ of the outputs of <em>all</em> neurons in the previous layer.
<li><a href="._NeuralNet-bs018.html">19</a></li>
<li><a href="._NeuralNet-bs019.html">20</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs011.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+16 -3
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -288,7 +301,7 @@ is obtained.
<li><a href="._NeuralNet-bs019.html">20</a></li>
<li><a href="._NeuralNet-bs020.html">21</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs012.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+16 -3
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -270,7 +283,7 @@ $$
<li><a href="._NeuralNet-bs020.html">21</a></li>
<li><a href="._NeuralNet-bs021.html">22</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs013.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+16 -3
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -261,7 +274,7 @@ variables are the input values \( x_n \).
<li><a href="._NeuralNet-bs021.html">22</a></li>
<li><a href="._NeuralNet-bs022.html">23</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs014.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+16 -3
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -270,7 +283,7 @@ flexibility of a neural network.
<li><a href="._NeuralNet-bs022.html">23</a></li>
<li><a href="._NeuralNet-bs023.html">24</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs015.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+16 -3
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -280,7 +293,7 @@ $$
<li><a href="._NeuralNet-bs023.html">24</a></li>
<li><a href="._NeuralNet-bs024.html">25</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs016.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+16 -3
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -264,7 +277,7 @@ used as input to the activation functions. For each operation
<li><a href="._NeuralNet-bs024.html">25</a></li>
<li><a href="._NeuralNet-bs025.html">26</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs017.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+16 -3
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -258,7 +271,7 @@ for a FFNN to fulfill the universal approximation theorem
<li><a href="._NeuralNet-bs025.html">26</a></li>
<li><a href="._NeuralNet-bs026.html">27</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs018.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+16 -3
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -266,7 +279,7 @@ $$
<li><a href="._NeuralNet-bs026.html">27</a></li>
<li><a href="._NeuralNet-bs027.html">28</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs019.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+16 -3
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -328,7 +341,7 @@ plt<span style="color: #666666">.</span>show()
<li><a href="._NeuralNet-bs027.html">28</a></li>
<li><a href="._NeuralNet-bs028.html">29</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs020.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+16 -3
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -279,7 +292,7 @@ like logistic regression or linear regression and their modifications on the oth
<li><a href="._NeuralNet-bs028.html">29</a></li>
<li><a href="._NeuralNet-bs029.html">30</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs021.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+16 -3
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -269,7 +282,7 @@ the potential of being universal approximators.
<li><a href="._NeuralNet-bs029.html">30</a></li>
<li><a href="._NeuralNet-bs030.html">31</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs022.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+16 -3
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -275,7 +288,7 @@ classes.
<li><a href="._NeuralNet-bs030.html">31</a></li>
<li><a href="._NeuralNet-bs031.html">32</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs023.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+16 -3
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -280,7 +293,7 @@ $$
<li><a href="._NeuralNet-bs031.html">32</a></li>
<li><a href="._NeuralNet-bs032.html">33</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs024.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+16 -3
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -263,7 +276,7 @@ $$
<li><a href="._NeuralNet-bs032.html">33</a></li>
<li><a href="._NeuralNet-bs033.html">34</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs025.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+16 -3
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -266,7 +279,7 @@ $$
<li><a href="._NeuralNet-bs033.html">34</a></li>
<li><a href="._NeuralNet-bs034.html">35</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs026.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+16 -3
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -291,7 +304,7 @@ $$
<li><a href="._NeuralNet-bs034.html">35</a></li>
<li><a href="._NeuralNet-bs035.html">36</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs027.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+16 -3
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -258,7 +271,7 @@ That is, the error \( \delta_j^L \) is exactly equal to the rate of change of th
<li><a href="._NeuralNet-bs035.html">36</a></li>
<li><a href="._NeuralNet-bs036.html">37</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs028.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+16 -3
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -301,7 +314,7 @@ one \( L-1 \) in terms of the errors in the final output layer.
<li><a href="._NeuralNet-bs036.html">37</a></li>
<li><a href="._NeuralNet-bs037.html">38</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs029.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+16 -3
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -273,7 +286,7 @@ We are now ready to set up the algorithm for back propagation and learning the w
<li><a href="._NeuralNet-bs037.html">38</a></li>
<li><a href="._NeuralNet-bs038.html">39</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs030.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+16 -3
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -317,7 +330,7 @@ Here it is convenient to use stochastic radient descent with mini-batches with a
<li><a href="._NeuralNet-bs038.html">39</a></li>
<li><a href="._NeuralNet-bs039.html">40</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs031.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+16 -3
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -279,7 +292,7 @@ of our network.
<li><a href="._NeuralNet-bs039.html">40</a></li>
<li><a href="._NeuralNet-bs040.html">41</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs032.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+16 -3
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -302,7 +315,7 @@ We leave it as an exercise in project 2 to derive these equations.
<li><a href="._NeuralNet-bs040.html">41</a></li>
<li><a href="._NeuralNet-bs041.html">42</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs033.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+16 -3
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -257,7 +270,7 @@ One can identify a set of key steps when using neural networks to solve supervis
<li><a href="._NeuralNet-bs041.html">42</a></li>
<li><a href="._NeuralNet-bs042.html">43</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs034.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+16 -3
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -336,7 +349,7 @@ plt<span style="color: #666666">.</span>show()
<li><a href="._NeuralNet-bs042.html">43</a></li>
<li><a href="._NeuralNet-bs043.html">44</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs035.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+16 -3
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -290,7 +303,7 @@ X_train, X_test, Y_train, Y_test <span style="color: #666666">=</span> train_tes
<li><a href="._NeuralNet-bs043.html">44</a></li>
<li><a href="._NeuralNet-bs044.html">45</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs036.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+16 -3
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -287,7 +300,7 @@ which is inspired by probability theory (see logistic regression) and was most c
<li><a href="._NeuralNet-bs044.html">45</a></li>
<li><a href="._NeuralNet-bs045.html">46</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs037.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+16 -3
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -286,7 +299,7 @@ weights to the output layer.
<li><a href="._NeuralNet-bs045.html">46</a></li>
<li><a href="._NeuralNet-bs046.html">47</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs038.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+16 -3
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -277,7 +290,7 @@ output_bias <span style="color: #666666">=</span> np<span style="color: #666666"
<li><a href="._NeuralNet-bs046.html">47</a></li>
<li><a href="._NeuralNet-bs047.html">48</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs039.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+16 -3
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -268,7 +281,7 @@ $$ a_{j}^{o} = \frac{\exp{(z_j^{o})}}
<li><a href="._NeuralNet-bs047.html">48</a></li>
<li><a href="._NeuralNet-bs048.html">49</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs040.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+16 -3
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -316,7 +329,7 @@ predictions <span style="color: #666666">=</span> predict(X_train)
<li><a href="._NeuralNet-bs048.html">49</a></li>
<li><a href="._NeuralNet-bs049.html">50</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs041.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+17 -2
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -282,6 +295,8 @@ A full derivation is given in the appendix at the end.
<li><a href="._NeuralNet-bs048.html">49</a></li>
<li><a href="._NeuralNet-bs049.html">50</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs042.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+18 -2
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -276,6 +289,9 @@ This has two important benefits:
<li><a href="._NeuralNet-bs048.html">49</a></li>
<li><a href="._NeuralNet-bs049.html">50</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs051.html">52</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs043.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+19 -2
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -271,6 +284,10 @@ calculate the gradient efficently.
<li><a href="._NeuralNet-bs048.html">49</a></li>
<li><a href="._NeuralNet-bs049.html">50</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs051.html">52</a></li>
<li><a href="._NeuralNet-bs052.html">53</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs044.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+20 -2
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -354,6 +367,11 @@ lmbd <span style="color: #666666">=</span> <span style="color: #666666">0.01</sp
<li><a href="._NeuralNet-bs048.html">49</a></li>
<li><a href="._NeuralNet-bs049.html">50</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs051.html">52</a></li>
<li><a href="._NeuralNet-bs052.html">53</a></li>
<li><a href="._NeuralNet-bs053.html">54</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs045.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+21 -2
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -256,6 +269,12 @@ Andrew Ng goes through some of these considerations in this <a href="https://you
<li><a href="._NeuralNet-bs048.html">49</a></li>
<li><a href="._NeuralNet-bs049.html">50</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs051.html">52</a></li>
<li><a href="._NeuralNet-bs052.html">53</a></li>
<li><a href="._NeuralNet-bs053.html">54</a></li>
<li><a href="._NeuralNet-bs054.html">55</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs046.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+20 -2
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -348,6 +361,11 @@ being realizations of this object with different hyperparameters. An implementat
<li><a href="._NeuralNet-bs048.html">49</a></li>
<li><a href="._NeuralNet-bs049.html">50</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs051.html">52</a></li>
<li><a href="._NeuralNet-bs052.html">53</a></li>
<li><a href="._NeuralNet-bs053.html">54</a></li>
<li><a href="._NeuralNet-bs054.html">55</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs047.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+20 -2
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -269,6 +282,11 @@ test_predict <span style="color: #666666">=</span> dnn<span style="color: #66666
<li><a href="._NeuralNet-bs048.html">49</a></li>
<li><a href="._NeuralNet-bs049.html">50</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs051.html">52</a></li>
<li><a href="._NeuralNet-bs052.html">53</a></li>
<li><a href="._NeuralNet-bs053.html">54</a></li>
<li><a href="._NeuralNet-bs054.html">55</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs048.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+20 -2
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -308,6 +321,11 @@ plt<span style="color: #666666">.</span>show()
<li class="active"><a href="._NeuralNet-bs048.html">49</a></li>
<li><a href="._NeuralNet-bs049.html">50</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs051.html">52</a></li>
<li><a href="._NeuralNet-bs052.html">53</a></li>
<li><a href="._NeuralNet-bs053.html">54</a></li>
<li><a href="._NeuralNet-bs054.html">55</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs049.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+20 -2
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -302,6 +315,11 @@ plt<span style="color: #666666">.</span>show()
<li><a href="._NeuralNet-bs048.html">49</a></li>
<li class="active"><a href="._NeuralNet-bs049.html">50</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs051.html">52</a></li>
<li><a href="._NeuralNet-bs052.html">53</a></li>
<li><a href="._NeuralNet-bs053.html">54</a></li>
<li><a href="._NeuralNet-bs054.html">55</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs050.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+31 -3
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -218,10 +231,19 @@ MathJax.Hub.Config({
<a name="part0050"></a>
<!-- !split -->
<h2 id="___sec49" class="anchor">And then with Tensorflow </h2>
<h2 id="___sec49" class="anchor">Building neural networks in Tensorflow and Keras </h2>
<p>
Now we want to build on the experience gained from our neural network implementation in NumPy and scikit-learn
and use it to construct a neural network in Tensorflow. Once we have constructed a neural network in NumPy
and Tensorflow, building one in Keras is really quite trivial, though the performance may suffer.
<p>
In our previous example we used only one hidden layer, and in this we will use two. From this it should be quite
clear how to build one using an arbitrary number of hidden layers, using data structures such as Python lists or
NumPy arrays.
<p>
<p>
<!-- navigation buttons at the bottom of the page -->
<ul class="pagination">
@@ -237,6 +259,12 @@ MathJax.Hub.Config({
<li><a href="._NeuralNet-bs048.html">49</a></li>
<li><a href="._NeuralNet-bs049.html">50</a></li>
<li class="active"><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs051.html">52</a></li>
<li><a href="._NeuralNet-bs052.html">53</a></li>
<li><a href="._NeuralNet-bs053.html">54</a></li>
<li><a href="._NeuralNet-bs054.html">55</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs051.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
@@ -0,0 +1,320 @@
<!--
Automatically generated HTML file from DocOnce source
(https://github.com/hplgit/doconce/)
-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
<meta name="description" content="Data Analysis and Machine Learning: Neural networks, from the simple perceptron to deep learning">
<title>Data Analysis and Machine Learning: Neural networks, from the simple perceptron to deep learning</title>
<!-- Bootstrap style: bootstrap -->
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
<!-- not necessary
<link href="https://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">
-->
<style type="text/css">
/* Add scrollbar to dropdown menus in bootstrap navigation bar */
.dropdown-menu {
height: auto;
max-height: 400px;
overflow-x: hidden;
}
/* Adds an invisible element before each target to offset for the navigation
bar */
.anchor::before {
content:"";
display:block;
height:50px; /* fixed header height for style bootstrap */
margin:-50px 0 0; /* negative fixed header height */
}
</style>
</head>
<!-- tocinfo
{'highest level': 2,
'sections': [('Neural networks', 2, None, '___sec0'),
('Artificial neurons', 2, None, '___sec1'),
('Neural network types', 2, None, '___sec2'),
('Feed-forward neural networks', 2, None, '___sec3'),
('Convolutional Neural Network', 2, None, '___sec4'),
('Recurrent neural networks', 2, None, '___sec5'),
('Other types of networks', 2, None, '___sec6'),
('Multilayer perceptrons', 2, None, '___sec7'),
('Why multilayer perceptrons?', 2, None, '___sec8'),
('Mathematical model', 2, None, '___sec9'),
('Mathematical model', 2, None, '___sec10'),
('Mathematical model', 2, None, '___sec11'),
('Mathematical model', 2, None, '___sec12'),
('Mathematical model', 2, None, '___sec13'),
('Matrix-vector notation', 3, None, '___sec14'),
('Matrix-vector notation and activation', 3, None, '___sec15'),
('Activation functions', 3, None, '___sec16'),
('Activation functions, Logistic and Hyperbolic ones',
3,
None,
'___sec17'),
('Relevance', 3, None, '___sec18'),
('The multilayer perceptron (MLP)', 2, None, '___sec19'),
('From one to many layers, the universal approximation theorem',
2,
None,
'___sec20'),
('Deriving the back propagation code for a multilayer perceptron '
'model',
2,
None,
'___sec21'),
('Definitions', 2, None, '___sec22'),
('Derivatives and the chain rule', 2, None, '___sec23'),
('Derivative of the cost function', 2, None, '___sec24'),
('Bringing it together, first back propagation equation',
2,
None,
'___sec25'),
('Derivatives in terms of $z_j^L$', 2, None, '___sec26'),
('Bringing it together', 2, None, '___sec27'),
('Final back propagating equation', 2, None, '___sec28'),
('Setting up the Back propagation algorithm',
2,
None,
'___sec29'),
('Setting up a Multi-layer perceptron model for classification',
2,
None,
'___sec30'),
('Defining the cost function', 2, None, '___sec31'),
('Developing a code for doing neural networks with back '
'propagation',
2,
None,
'___sec32'),
('Collect and pre-process data', 2, None, '___sec33'),
('Train and test datasets', 2, None, '___sec34'),
('Define model and architecture', 2, None, '___sec35'),
('Layers', 2, None, '___sec36'),
('Weights and biases', 2, None, '___sec37'),
('Feed-forward pass', 2, None, '___sec38'),
('Matrix multiplication', 2, None, '___sec39'),
('Choose cost function and optimizer', 2, None, '___sec40'),
('Optimizing the cost function', 2, None, '___sec41'),
('Regularization', 2, None, '___sec42'),
('Matrix multiplication', 2, None, '___sec43'),
('Improving performance', 2, None, '___sec44'),
('Full object-oriented implementation', 2, None, '___sec45'),
('Evaluate model performance on test data', 2, None, '___sec46'),
('Adjust hyperparameters (if necessary, network architecture',
2,
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
TeX: {
equationNumbers: { autoNumber: "none" },
extensions: ["AMSmath.js", "AMSsymbols.js", "autobold.js", "color.js"]
}
});
</script>
<script type="text/javascript" async
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<!-- Bootstrap navigation bar -->
<div class="navbar navbar-default navbar-fixed-top">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-responsive-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="NeuralNet-bs.html">Data Analysis and Machine Learning: Neural networks, from the simple perceptron to deep learning</a>
</div>
<div class="navbar-collapse collapse navbar-responsive-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
<ul class="dropdown-menu">
<!-- navigation toc: --> <li><a href="._NeuralNet-bs001.html#___sec0" style="font-size: 80%;"><b>Neural networks</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs002.html#___sec1" style="font-size: 80%;"><b>Artificial neurons</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs003.html#___sec2" style="font-size: 80%;"><b>Neural network types</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs004.html#___sec3" style="font-size: 80%;"><b>Feed-forward neural networks</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs005.html#___sec4" style="font-size: 80%;"><b>Convolutional Neural Network</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs006.html#___sec5" style="font-size: 80%;"><b>Recurrent neural networks</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs007.html#___sec6" style="font-size: 80%;"><b>Other types of networks</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs008.html#___sec7" style="font-size: 80%;"><b>Multilayer perceptrons</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs009.html#___sec8" style="font-size: 80%;"><b>Why multilayer perceptrons?</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs010.html#___sec9" style="font-size: 80%;"><b>Mathematical model</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs011.html#___sec10" style="font-size: 80%;"><b>Mathematical model</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs012.html#___sec11" style="font-size: 80%;"><b>Mathematical model</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs013.html#___sec12" style="font-size: 80%;"><b>Mathematical model</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs014.html#___sec13" style="font-size: 80%;"><b>Mathematical model</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs015.html#___sec14" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Matrix-vector notation</a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs016.html#___sec15" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Matrix-vector notation and activation</a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs017.html#___sec16" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Activation functions</a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs018.html#___sec17" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Activation functions, Logistic and Hyperbolic ones</a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs019.html#___sec18" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Relevance</a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs020.html#___sec19" style="font-size: 80%;"><b>The multilayer perceptron (MLP)</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs021.html#___sec20" style="font-size: 80%;"><b>From one to many layers, the universal approximation theorem</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs022.html#___sec21" style="font-size: 80%;"><b>Deriving the back propagation code for a multilayer perceptron model</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs023.html#___sec22" style="font-size: 80%;"><b>Definitions</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs024.html#___sec23" style="font-size: 80%;"><b>Derivatives and the chain rule</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs025.html#___sec24" style="font-size: 80%;"><b>Derivative of the cost function</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs026.html#___sec25" style="font-size: 80%;"><b>Bringing it together, first back propagation equation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs027.html#___sec26" style="font-size: 80%;"><b>Derivatives in terms of \( z_j^L \)</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs028.html#___sec27" style="font-size: 80%;"><b>Bringing it together</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs029.html#___sec28" style="font-size: 80%;"><b>Final back propagating equation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs030.html#___sec29" style="font-size: 80%;"><b>Setting up the Back propagation algorithm</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs031.html#___sec30" style="font-size: 80%;"><b>Setting up a Multi-layer perceptron model for classification</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs032.html#___sec31" style="font-size: 80%;"><b>Defining the cost function</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs033.html#___sec32" style="font-size: 80%;"><b>Developing a code for doing neural networks with back propagation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs034.html#___sec33" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs035.html#___sec34" style="font-size: 80%;"><b>Train and test datasets</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs036.html#___sec35" style="font-size: 80%;"><b>Define model and architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs037.html#___sec36" style="font-size: 80%;"><b>Layers</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs038.html#___sec37" style="font-size: 80%;"><b>Weights and biases</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs039.html#___sec38" style="font-size: 80%;"><b>Feed-forward pass</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs040.html#___sec39" style="font-size: 80%;"><b>Matrix multiplication</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs041.html#___sec40" style="font-size: 80%;"><b>Choose cost function and optimizer</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs042.html#___sec41" style="font-size: 80%;"><b>Optimizing the cost function</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs043.html#___sec42" style="font-size: 80%;"><b>Regularization</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs044.html#___sec43" style="font-size: 80%;"><b>Matrix multiplication</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs045.html#___sec44" style="font-size: 80%;"><b>Improving performance</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs046.html#___sec45" style="font-size: 80%;"><b>Full object-oriented implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
</ul>
</div>
</div>
</div> <!-- end of navigation bar -->
<div class="container">
<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p> <!-- add vertical space -->
<a name="part0051"></a>
<!-- !split -->
<h2 id="___sec50" class="anchor">Tensorflow </h2>
<p>
Tensorflow is an open source library machine learning library
developed by the Google Brain team for internal use. It was released
under the Apache 2.0 open source license in November 9, 2015.
<p>
Tensorflow is a computational framework that allows you to construct
machine learning models at different levels of abstraction, from
high-level, object-oriented APIs like Keras, down to the C++ kernels
that Tensorflow is built upon. The higher levels of abstraction are
simpler to use, but less flexible, and our choice of implementation
should reflect the problems we are trying to solve.
<p>
<a href="https://www.tensorflow.org/guide/graphs" target="_self">Tensorflow uses</a> so-called graphs to represent your computation
in terms of the dependencies between individual operations, such that you first build a Tensorflow <em>graph</em>
to represent your model, and then create a Tensorflow <em>session</em> to run the graph.
<p>
In this guide we will analyze the same data as we did in our NumPy and
scikit-learn tutorial, gathered from the MNIST database of images. We
will give an introduction to the lower level Python Application
Program Interfaces (APIs), and see how we use them to build our graph.
Then we will build (effectively) the same graph in Keras, to see just
how simple solving a machine learning problem can be.
<p>
To install tensorflow on Unix/Linux systems, use pip as
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>pip3 install tensorflow
</pre></div>
<p>
and/or if you use <b>anaconda</b>, just write (or install from the graphical user interface)
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>conda install tensorflow
</pre></div>
<p>
<p>
<!-- navigation buttons at the bottom of the page -->
<ul class="pagination">
<li><a href="._NeuralNet-bs050.html">&laquo;</a></li>
<li><a href="._NeuralNet-bs000.html">1</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs043.html">44</a></li>
<li><a href="._NeuralNet-bs044.html">45</a></li>
<li><a href="._NeuralNet-bs045.html">46</a></li>
<li><a href="._NeuralNet-bs046.html">47</a></li>
<li><a href="._NeuralNet-bs047.html">48</a></li>
<li><a href="._NeuralNet-bs048.html">49</a></li>
<li><a href="._NeuralNet-bs049.html">50</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li class="active"><a href="._NeuralNet-bs051.html">52</a></li>
<li><a href="._NeuralNet-bs052.html">53</a></li>
<li><a href="._NeuralNet-bs053.html">54</a></li>
<li><a href="._NeuralNet-bs054.html">55</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs052.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
</div> <!-- end container -->
<!-- include javascript, jQuery *first* -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<!-- Bootstrap footer
<footer>
<a href="http://..."><img width="250" align=right src="http://..."></a>
</footer>
-->
<center style="font-size:80%">
<!-- copyright only on the titlepage -->
</center>
</body>
</html>
@@ -0,0 +1,340 @@
<!--
Automatically generated HTML file from DocOnce source
(https://github.com/hplgit/doconce/)
-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
<meta name="description" content="Data Analysis and Machine Learning: Neural networks, from the simple perceptron to deep learning">
<title>Data Analysis and Machine Learning: Neural networks, from the simple perceptron to deep learning</title>
<!-- Bootstrap style: bootstrap -->
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
<!-- not necessary
<link href="https://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">
-->
<style type="text/css">
/* Add scrollbar to dropdown menus in bootstrap navigation bar */
.dropdown-menu {
height: auto;
max-height: 400px;
overflow-x: hidden;
}
/* Adds an invisible element before each target to offset for the navigation
bar */
.anchor::before {
content:"";
display:block;
height:50px; /* fixed header height for style bootstrap */
margin:-50px 0 0; /* negative fixed header height */
}
</style>
</head>
<!-- tocinfo
{'highest level': 2,
'sections': [('Neural networks', 2, None, '___sec0'),
('Artificial neurons', 2, None, '___sec1'),
('Neural network types', 2, None, '___sec2'),
('Feed-forward neural networks', 2, None, '___sec3'),
('Convolutional Neural Network', 2, None, '___sec4'),
('Recurrent neural networks', 2, None, '___sec5'),
('Other types of networks', 2, None, '___sec6'),
('Multilayer perceptrons', 2, None, '___sec7'),
('Why multilayer perceptrons?', 2, None, '___sec8'),
('Mathematical model', 2, None, '___sec9'),
('Mathematical model', 2, None, '___sec10'),
('Mathematical model', 2, None, '___sec11'),
('Mathematical model', 2, None, '___sec12'),
('Mathematical model', 2, None, '___sec13'),
('Matrix-vector notation', 3, None, '___sec14'),
('Matrix-vector notation and activation', 3, None, '___sec15'),
('Activation functions', 3, None, '___sec16'),
('Activation functions, Logistic and Hyperbolic ones',
3,
None,
'___sec17'),
('Relevance', 3, None, '___sec18'),
('The multilayer perceptron (MLP)', 2, None, '___sec19'),
('From one to many layers, the universal approximation theorem',
2,
None,
'___sec20'),
('Deriving the back propagation code for a multilayer perceptron '
'model',
2,
None,
'___sec21'),
('Definitions', 2, None, '___sec22'),
('Derivatives and the chain rule', 2, None, '___sec23'),
('Derivative of the cost function', 2, None, '___sec24'),
('Bringing it together, first back propagation equation',
2,
None,
'___sec25'),
('Derivatives in terms of $z_j^L$', 2, None, '___sec26'),
('Bringing it together', 2, None, '___sec27'),
('Final back propagating equation', 2, None, '___sec28'),
('Setting up the Back propagation algorithm',
2,
None,
'___sec29'),
('Setting up a Multi-layer perceptron model for classification',
2,
None,
'___sec30'),
('Defining the cost function', 2, None, '___sec31'),
('Developing a code for doing neural networks with back '
'propagation',
2,
None,
'___sec32'),
('Collect and pre-process data', 2, None, '___sec33'),
('Train and test datasets', 2, None, '___sec34'),
('Define model and architecture', 2, None, '___sec35'),
('Layers', 2, None, '___sec36'),
('Weights and biases', 2, None, '___sec37'),
('Feed-forward pass', 2, None, '___sec38'),
('Matrix multiplication', 2, None, '___sec39'),
('Choose cost function and optimizer', 2, None, '___sec40'),
('Optimizing the cost function', 2, None, '___sec41'),
('Regularization', 2, None, '___sec42'),
('Matrix multiplication', 2, None, '___sec43'),
('Improving performance', 2, None, '___sec44'),
('Full object-oriented implementation', 2, None, '___sec45'),
('Evaluate model performance on test data', 2, None, '___sec46'),
('Adjust hyperparameters (if necessary, network architecture',
2,
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
TeX: {
equationNumbers: { autoNumber: "none" },
extensions: ["AMSmath.js", "AMSsymbols.js", "autobold.js", "color.js"]
}
});
</script>
<script type="text/javascript" async
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<!-- Bootstrap navigation bar -->
<div class="navbar navbar-default navbar-fixed-top">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-responsive-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="NeuralNet-bs.html">Data Analysis and Machine Learning: Neural networks, from the simple perceptron to deep learning</a>
</div>
<div class="navbar-collapse collapse navbar-responsive-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
<ul class="dropdown-menu">
<!-- navigation toc: --> <li><a href="._NeuralNet-bs001.html#___sec0" style="font-size: 80%;"><b>Neural networks</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs002.html#___sec1" style="font-size: 80%;"><b>Artificial neurons</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs003.html#___sec2" style="font-size: 80%;"><b>Neural network types</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs004.html#___sec3" style="font-size: 80%;"><b>Feed-forward neural networks</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs005.html#___sec4" style="font-size: 80%;"><b>Convolutional Neural Network</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs006.html#___sec5" style="font-size: 80%;"><b>Recurrent neural networks</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs007.html#___sec6" style="font-size: 80%;"><b>Other types of networks</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs008.html#___sec7" style="font-size: 80%;"><b>Multilayer perceptrons</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs009.html#___sec8" style="font-size: 80%;"><b>Why multilayer perceptrons?</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs010.html#___sec9" style="font-size: 80%;"><b>Mathematical model</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs011.html#___sec10" style="font-size: 80%;"><b>Mathematical model</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs012.html#___sec11" style="font-size: 80%;"><b>Mathematical model</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs013.html#___sec12" style="font-size: 80%;"><b>Mathematical model</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs014.html#___sec13" style="font-size: 80%;"><b>Mathematical model</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs015.html#___sec14" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Matrix-vector notation</a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs016.html#___sec15" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Matrix-vector notation and activation</a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs017.html#___sec16" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Activation functions</a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs018.html#___sec17" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Activation functions, Logistic and Hyperbolic ones</a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs019.html#___sec18" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Relevance</a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs020.html#___sec19" style="font-size: 80%;"><b>The multilayer perceptron (MLP)</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs021.html#___sec20" style="font-size: 80%;"><b>From one to many layers, the universal approximation theorem</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs022.html#___sec21" style="font-size: 80%;"><b>Deriving the back propagation code for a multilayer perceptron model</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs023.html#___sec22" style="font-size: 80%;"><b>Definitions</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs024.html#___sec23" style="font-size: 80%;"><b>Derivatives and the chain rule</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs025.html#___sec24" style="font-size: 80%;"><b>Derivative of the cost function</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs026.html#___sec25" style="font-size: 80%;"><b>Bringing it together, first back propagation equation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs027.html#___sec26" style="font-size: 80%;"><b>Derivatives in terms of \( z_j^L \)</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs028.html#___sec27" style="font-size: 80%;"><b>Bringing it together</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs029.html#___sec28" style="font-size: 80%;"><b>Final back propagating equation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs030.html#___sec29" style="font-size: 80%;"><b>Setting up the Back propagation algorithm</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs031.html#___sec30" style="font-size: 80%;"><b>Setting up a Multi-layer perceptron model for classification</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs032.html#___sec31" style="font-size: 80%;"><b>Defining the cost function</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs033.html#___sec32" style="font-size: 80%;"><b>Developing a code for doing neural networks with back propagation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs034.html#___sec33" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs035.html#___sec34" style="font-size: 80%;"><b>Train and test datasets</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs036.html#___sec35" style="font-size: 80%;"><b>Define model and architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs037.html#___sec36" style="font-size: 80%;"><b>Layers</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs038.html#___sec37" style="font-size: 80%;"><b>Weights and biases</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs039.html#___sec38" style="font-size: 80%;"><b>Feed-forward pass</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs040.html#___sec39" style="font-size: 80%;"><b>Matrix multiplication</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs041.html#___sec40" style="font-size: 80%;"><b>Choose cost function and optimizer</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs042.html#___sec41" style="font-size: 80%;"><b>Optimizing the cost function</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs043.html#___sec42" style="font-size: 80%;"><b>Regularization</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs044.html#___sec43" style="font-size: 80%;"><b>Matrix multiplication</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs045.html#___sec44" style="font-size: 80%;"><b>Improving performance</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs046.html#___sec45" style="font-size: 80%;"><b>Full object-oriented implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
</ul>
</div>
</div>
</div> <!-- end of navigation bar -->
<div class="container">
<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p> <!-- add vertical space -->
<a name="part0052"></a>
<!-- !split -->
<h2 id="___sec51" class="anchor">Collect and pre-process data </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># import necessary packages</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn</span> <span style="color: #008000; font-weight: bold">import</span> datasets
<span style="color: #408080; font-style: italic"># ensure the same random numbers appear every time</span>
np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>seed(<span style="color: #666666">0</span>)
<span style="color: #408080; font-style: italic"># display images in notebook</span>
<span style="color: #666666">%</span>matplotlib inline
plt<span style="color: #666666">.</span>rcParams[<span style="color: #BA2121">&#39;figure.figsize&#39;</span>] <span style="color: #666666">=</span> (<span style="color: #666666">12</span>,<span style="color: #666666">12</span>)
<span style="color: #408080; font-style: italic"># download MNIST dataset</span>
digits <span style="color: #666666">=</span> datasets<span style="color: #666666">.</span>load_digits()
<span style="color: #408080; font-style: italic"># define inputs and labels</span>
inputs <span style="color: #666666">=</span> digits<span style="color: #666666">.</span>images
labels <span style="color: #666666">=</span> digits<span style="color: #666666">.</span>target
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;inputs = (n_inputs, pixel_width, pixel_height) = &quot;</span> <span style="color: #666666">+</span> <span style="color: #008000">str</span>(inputs<span style="color: #666666">.</span>shape))
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;labels = (n_inputs) = &quot;</span> <span style="color: #666666">+</span> <span style="color: #008000">str</span>(labels<span style="color: #666666">.</span>shape))
<span style="color: #408080; font-style: italic"># flatten the image</span>
<span style="color: #408080; font-style: italic"># the value -1 means dimension is inferred from the remaining dimensions: 8x8 = 64</span>
n_inputs <span style="color: #666666">=</span> <span style="color: #008000">len</span>(inputs)
inputs <span style="color: #666666">=</span> inputs<span style="color: #666666">.</span>reshape(n_inputs, <span style="color: #666666">-1</span>)
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;X = (n_inputs, n_features) = &quot;</span> <span style="color: #666666">+</span> <span style="color: #008000">str</span>(inputs<span style="color: #666666">.</span>shape))
<span style="color: #408080; font-style: italic"># choose some random images to display</span>
indices <span style="color: #666666">=</span> np<span style="color: #666666">.</span>arange(n_inputs)
random_indices <span style="color: #666666">=</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>choice(indices, size<span style="color: #666666">=5</span>)
<span style="color: #008000; font-weight: bold">for</span> i, image <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">enumerate</span>(digits<span style="color: #666666">.</span>images[random_indices]):
plt<span style="color: #666666">.</span>subplot(<span style="color: #666666">1</span>, <span style="color: #666666">5</span>, i<span style="color: #666666">+1</span>)
plt<span style="color: #666666">.</span>axis(<span style="color: #BA2121">&#39;off&#39;</span>)
plt<span style="color: #666666">.</span>imshow(image, cmap<span style="color: #666666">=</span>plt<span style="color: #666666">.</span>cm<span style="color: #666666">.</span>gray_r, interpolation<span style="color: #666666">=</span><span style="color: #BA2121">&#39;nearest&#39;</span>)
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">&quot;Label: </span><span style="color: #BB6688; font-weight: bold">%d</span><span style="color: #BA2121">&quot;</span> <span style="color: #666666">%</span> digits<span style="color: #666666">.</span>target[random_indices[i]])
plt<span style="color: #666666">.</span>show()
</pre></div>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">keras.utils</span> <span style="color: #008000; font-weight: bold">import</span> to_categorical
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> train_test_split
<span style="color: #408080; font-style: italic"># one-hot representation of labels</span>
labels <span style="color: #666666">=</span> to_categorical(labels)
<span style="color: #408080; font-style: italic"># split into train and test data</span>
train_size <span style="color: #666666">=</span> <span style="color: #666666">0.8</span>
test_size <span style="color: #666666">=</span> <span style="color: #666666">1</span> <span style="color: #666666">-</span> train_size
X_train, X_test, Y_train, Y_test <span style="color: #666666">=</span> train_test_split(inputs, labels, train_size<span style="color: #666666">=</span>train_size,
test_size<span style="color: #666666">=</span>test_size)
</pre></div>
<p>
<p>
<!-- navigation buttons at the bottom of the page -->
<ul class="pagination">
<li><a href="._NeuralNet-bs051.html">&laquo;</a></li>
<li><a href="._NeuralNet-bs000.html">1</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs044.html">45</a></li>
<li><a href="._NeuralNet-bs045.html">46</a></li>
<li><a href="._NeuralNet-bs046.html">47</a></li>
<li><a href="._NeuralNet-bs047.html">48</a></li>
<li><a href="._NeuralNet-bs048.html">49</a></li>
<li><a href="._NeuralNet-bs049.html">50</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs051.html">52</a></li>
<li class="active"><a href="._NeuralNet-bs052.html">53</a></li>
<li><a href="._NeuralNet-bs053.html">54</a></li>
<li><a href="._NeuralNet-bs054.html">55</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs053.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
</div> <!-- end container -->
<!-- include javascript, jQuery *first* -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<!-- Bootstrap footer
<footer>
<a href="http://..."><img width="250" align=right src="http://..."></a>
</footer>
-->
<center style="font-size:80%">
<!-- copyright only on the titlepage -->
</center>
</body>
</html>
@@ -0,0 +1,418 @@
<!--
Automatically generated HTML file from DocOnce source
(https://github.com/hplgit/doconce/)
-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
<meta name="description" content="Data Analysis and Machine Learning: Neural networks, from the simple perceptron to deep learning">
<title>Data Analysis and Machine Learning: Neural networks, from the simple perceptron to deep learning</title>
<!-- Bootstrap style: bootstrap -->
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
<!-- not necessary
<link href="https://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">
-->
<style type="text/css">
/* Add scrollbar to dropdown menus in bootstrap navigation bar */
.dropdown-menu {
height: auto;
max-height: 400px;
overflow-x: hidden;
}
/* Adds an invisible element before each target to offset for the navigation
bar */
.anchor::before {
content:"";
display:block;
height:50px; /* fixed header height for style bootstrap */
margin:-50px 0 0; /* negative fixed header height */
}
</style>
</head>
<!-- tocinfo
{'highest level': 2,
'sections': [('Neural networks', 2, None, '___sec0'),
('Artificial neurons', 2, None, '___sec1'),
('Neural network types', 2, None, '___sec2'),
('Feed-forward neural networks', 2, None, '___sec3'),
('Convolutional Neural Network', 2, None, '___sec4'),
('Recurrent neural networks', 2, None, '___sec5'),
('Other types of networks', 2, None, '___sec6'),
('Multilayer perceptrons', 2, None, '___sec7'),
('Why multilayer perceptrons?', 2, None, '___sec8'),
('Mathematical model', 2, None, '___sec9'),
('Mathematical model', 2, None, '___sec10'),
('Mathematical model', 2, None, '___sec11'),
('Mathematical model', 2, None, '___sec12'),
('Mathematical model', 2, None, '___sec13'),
('Matrix-vector notation', 3, None, '___sec14'),
('Matrix-vector notation and activation', 3, None, '___sec15'),
('Activation functions', 3, None, '___sec16'),
('Activation functions, Logistic and Hyperbolic ones',
3,
None,
'___sec17'),
('Relevance', 3, None, '___sec18'),
('The multilayer perceptron (MLP)', 2, None, '___sec19'),
('From one to many layers, the universal approximation theorem',
2,
None,
'___sec20'),
('Deriving the back propagation code for a multilayer perceptron '
'model',
2,
None,
'___sec21'),
('Definitions', 2, None, '___sec22'),
('Derivatives and the chain rule', 2, None, '___sec23'),
('Derivative of the cost function', 2, None, '___sec24'),
('Bringing it together, first back propagation equation',
2,
None,
'___sec25'),
('Derivatives in terms of $z_j^L$', 2, None, '___sec26'),
('Bringing it together', 2, None, '___sec27'),
('Final back propagating equation', 2, None, '___sec28'),
('Setting up the Back propagation algorithm',
2,
None,
'___sec29'),
('Setting up a Multi-layer perceptron model for classification',
2,
None,
'___sec30'),
('Defining the cost function', 2, None, '___sec31'),
('Developing a code for doing neural networks with back '
'propagation',
2,
None,
'___sec32'),
('Collect and pre-process data', 2, None, '___sec33'),
('Train and test datasets', 2, None, '___sec34'),
('Define model and architecture', 2, None, '___sec35'),
('Layers', 2, None, '___sec36'),
('Weights and biases', 2, None, '___sec37'),
('Feed-forward pass', 2, None, '___sec38'),
('Matrix multiplication', 2, None, '___sec39'),
('Choose cost function and optimizer', 2, None, '___sec40'),
('Optimizing the cost function', 2, None, '___sec41'),
('Regularization', 2, None, '___sec42'),
('Matrix multiplication', 2, None, '___sec43'),
('Improving performance', 2, None, '___sec44'),
('Full object-oriented implementation', 2, None, '___sec45'),
('Evaluate model performance on test data', 2, None, '___sec46'),
('Adjust hyperparameters (if necessary, network architecture',
2,
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
TeX: {
equationNumbers: { autoNumber: "none" },
extensions: ["AMSmath.js", "AMSsymbols.js", "autobold.js", "color.js"]
}
});
</script>
<script type="text/javascript" async
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<!-- Bootstrap navigation bar -->
<div class="navbar navbar-default navbar-fixed-top">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-responsive-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="NeuralNet-bs.html">Data Analysis and Machine Learning: Neural networks, from the simple perceptron to deep learning</a>
</div>
<div class="navbar-collapse collapse navbar-responsive-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
<ul class="dropdown-menu">
<!-- navigation toc: --> <li><a href="._NeuralNet-bs001.html#___sec0" style="font-size: 80%;"><b>Neural networks</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs002.html#___sec1" style="font-size: 80%;"><b>Artificial neurons</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs003.html#___sec2" style="font-size: 80%;"><b>Neural network types</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs004.html#___sec3" style="font-size: 80%;"><b>Feed-forward neural networks</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs005.html#___sec4" style="font-size: 80%;"><b>Convolutional Neural Network</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs006.html#___sec5" style="font-size: 80%;"><b>Recurrent neural networks</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs007.html#___sec6" style="font-size: 80%;"><b>Other types of networks</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs008.html#___sec7" style="font-size: 80%;"><b>Multilayer perceptrons</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs009.html#___sec8" style="font-size: 80%;"><b>Why multilayer perceptrons?</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs010.html#___sec9" style="font-size: 80%;"><b>Mathematical model</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs011.html#___sec10" style="font-size: 80%;"><b>Mathematical model</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs012.html#___sec11" style="font-size: 80%;"><b>Mathematical model</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs013.html#___sec12" style="font-size: 80%;"><b>Mathematical model</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs014.html#___sec13" style="font-size: 80%;"><b>Mathematical model</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs015.html#___sec14" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Matrix-vector notation</a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs016.html#___sec15" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Matrix-vector notation and activation</a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs017.html#___sec16" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Activation functions</a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs018.html#___sec17" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Activation functions, Logistic and Hyperbolic ones</a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs019.html#___sec18" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Relevance</a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs020.html#___sec19" style="font-size: 80%;"><b>The multilayer perceptron (MLP)</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs021.html#___sec20" style="font-size: 80%;"><b>From one to many layers, the universal approximation theorem</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs022.html#___sec21" style="font-size: 80%;"><b>Deriving the back propagation code for a multilayer perceptron model</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs023.html#___sec22" style="font-size: 80%;"><b>Definitions</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs024.html#___sec23" style="font-size: 80%;"><b>Derivatives and the chain rule</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs025.html#___sec24" style="font-size: 80%;"><b>Derivative of the cost function</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs026.html#___sec25" style="font-size: 80%;"><b>Bringing it together, first back propagation equation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs027.html#___sec26" style="font-size: 80%;"><b>Derivatives in terms of \( z_j^L \)</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs028.html#___sec27" style="font-size: 80%;"><b>Bringing it together</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs029.html#___sec28" style="font-size: 80%;"><b>Final back propagating equation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs030.html#___sec29" style="font-size: 80%;"><b>Setting up the Back propagation algorithm</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs031.html#___sec30" style="font-size: 80%;"><b>Setting up a Multi-layer perceptron model for classification</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs032.html#___sec31" style="font-size: 80%;"><b>Defining the cost function</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs033.html#___sec32" style="font-size: 80%;"><b>Developing a code for doing neural networks with back propagation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs034.html#___sec33" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs035.html#___sec34" style="font-size: 80%;"><b>Train and test datasets</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs036.html#___sec35" style="font-size: 80%;"><b>Define model and architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs037.html#___sec36" style="font-size: 80%;"><b>Layers</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs038.html#___sec37" style="font-size: 80%;"><b>Weights and biases</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs039.html#___sec38" style="font-size: 80%;"><b>Feed-forward pass</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs040.html#___sec39" style="font-size: 80%;"><b>Matrix multiplication</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs041.html#___sec40" style="font-size: 80%;"><b>Choose cost function and optimizer</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs042.html#___sec41" style="font-size: 80%;"><b>Optimizing the cost function</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs043.html#___sec42" style="font-size: 80%;"><b>Regularization</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs044.html#___sec43" style="font-size: 80%;"><b>Matrix multiplication</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs045.html#___sec44" style="font-size: 80%;"><b>Improving performance</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs046.html#___sec45" style="font-size: 80%;"><b>Full object-oriented implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
</ul>
</div>
</div>
</div> <!-- end of navigation bar -->
<div class="container">
<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p> <!-- add vertical space -->
<a name="part0053"></a>
<!-- !split -->
<h2 id="___sec52" class="anchor">Using TensorFlow backend </h2>
<ol>
<li> Define model and architecture</li>
<li> Choose cost function and optimizer</li>
</ol>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">tensorflow</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">tf</span>
<span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">NeuralNetworkTensorflow</span>:
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">__init__</span>(
<span style="color: #008000">self</span>,
X_train,
Y_train,
X_test,
Y_test,
n_neurons_layer1<span style="color: #666666">=100</span>,
n_neurons_layer2<span style="color: #666666">=50</span>,
n_categories<span style="color: #666666">=2</span>,
epochs<span style="color: #666666">=10</span>,
batch_size<span style="color: #666666">=100</span>,
eta<span style="color: #666666">=0.1</span>,
lmbd<span style="color: #666666">=0.0</span>,
):
<span style="color: #408080; font-style: italic"># keep track of number of steps</span>
<span style="color: #008000">self</span><span style="color: #666666">.</span>global_step <span style="color: #666666">=</span> tf<span style="color: #666666">.</span>Variable(<span style="color: #666666">0</span>, dtype<span style="color: #666666">=</span>tf<span style="color: #666666">.</span>int32, trainable<span style="color: #666666">=</span><span style="color: #008000">False</span>, name<span style="color: #666666">=</span><span style="color: #BA2121">&#39;global_step&#39;</span>)
<span style="color: #008000">self</span><span style="color: #666666">.</span>X_train <span style="color: #666666">=</span> X_train
<span style="color: #008000">self</span><span style="color: #666666">.</span>Y_train <span style="color: #666666">=</span> Y_train
<span style="color: #008000">self</span><span style="color: #666666">.</span>X_test <span style="color: #666666">=</span> X_test
<span style="color: #008000">self</span><span style="color: #666666">.</span>Y_test <span style="color: #666666">=</span> Y_test
<span style="color: #008000">self</span><span style="color: #666666">.</span>n_inputs <span style="color: #666666">=</span> X_train<span style="color: #666666">.</span>shape[<span style="color: #666666">0</span>]
<span style="color: #008000">self</span><span style="color: #666666">.</span>n_features <span style="color: #666666">=</span> X_train<span style="color: #666666">.</span>shape[<span style="color: #666666">1</span>]
<span style="color: #008000">self</span><span style="color: #666666">.</span>n_neurons_layer1 <span style="color: #666666">=</span> n_neurons_layer1
<span style="color: #008000">self</span><span style="color: #666666">.</span>n_neurons_layer2 <span style="color: #666666">=</span> n_neurons_layer2
<span style="color: #008000">self</span><span style="color: #666666">.</span>n_categories <span style="color: #666666">=</span> n_categories
<span style="color: #008000">self</span><span style="color: #666666">.</span>epochs <span style="color: #666666">=</span> epochs
<span style="color: #008000">self</span><span style="color: #666666">.</span>batch_size <span style="color: #666666">=</span> batch_size
<span style="color: #008000">self</span><span style="color: #666666">.</span>iterations <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>n_inputs <span style="color: #666666">//</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>batch_size
<span style="color: #008000">self</span><span style="color: #666666">.</span>eta <span style="color: #666666">=</span> eta
<span style="color: #008000">self</span><span style="color: #666666">.</span>lmbd <span style="color: #666666">=</span> lmbd
<span style="color: #408080; font-style: italic"># build network piece by piece</span>
<span style="color: #408080; font-style: italic"># name scopes (with) are used to enforce creation of new variables</span>
<span style="color: #408080; font-style: italic"># https://www.tensorflow.org/guide/variables</span>
<span style="color: #008000">self</span><span style="color: #666666">.</span>create_placeholders()
<span style="color: #008000">self</span><span style="color: #666666">.</span>create_DNN()
<span style="color: #008000">self</span><span style="color: #666666">.</span>create_loss()
<span style="color: #008000">self</span><span style="color: #666666">.</span>create_optimiser()
<span style="color: #008000">self</span><span style="color: #666666">.</span>create_accuracy()
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">create_placeholders</span>(<span style="color: #008000">self</span>):
<span style="color: #408080; font-style: italic"># placeholders are fine here, but &quot;Datasets&quot; are the preferred method</span>
<span style="color: #408080; font-style: italic"># of streaming data into a model</span>
<span style="color: #008000; font-weight: bold">with</span> tf<span style="color: #666666">.</span>name_scope(<span style="color: #BA2121">&#39;data&#39;</span>):
<span style="color: #008000">self</span><span style="color: #666666">.</span>X <span style="color: #666666">=</span> tf<span style="color: #666666">.</span>placeholder(tf<span style="color: #666666">.</span>float32, shape<span style="color: #666666">=</span>(<span style="color: #008000">None</span>, <span style="color: #008000">self</span><span style="color: #666666">.</span>n_features), name<span style="color: #666666">=</span><span style="color: #BA2121">&#39;X_data&#39;</span>)
<span style="color: #008000">self</span><span style="color: #666666">.</span>Y <span style="color: #666666">=</span> tf<span style="color: #666666">.</span>placeholder(tf<span style="color: #666666">.</span>float32, shape<span style="color: #666666">=</span>(<span style="color: #008000">None</span>, <span style="color: #008000">self</span><span style="color: #666666">.</span>n_categories), name<span style="color: #666666">=</span><span style="color: #BA2121">&#39;Y_data&#39;</span>)
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">create_DNN</span>(<span style="color: #008000">self</span>):
<span style="color: #008000; font-weight: bold">with</span> tf<span style="color: #666666">.</span>name_scope(<span style="color: #BA2121">&#39;DNN&#39;</span>):
<span style="color: #408080; font-style: italic"># the weights are stored to calculate regularization loss later</span>
<span style="color: #408080; font-style: italic"># Fully connected layer 1</span>
<span style="color: #008000">self</span><span style="color: #666666">.</span>W_fc1 <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>weight_variable([<span style="color: #008000">self</span><span style="color: #666666">.</span>n_features, <span style="color: #008000">self</span><span style="color: #666666">.</span>n_neurons_layer1], name<span style="color: #666666">=</span><span style="color: #BA2121">&#39;fc1&#39;</span>, dtype<span style="color: #666666">=</span>tf<span style="color: #666666">.</span>float32)
b_fc1 <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>bias_variable([<span style="color: #008000">self</span><span style="color: #666666">.</span>n_neurons_layer1], name<span style="color: #666666">=</span><span style="color: #BA2121">&#39;fc1&#39;</span>, dtype<span style="color: #666666">=</span>tf<span style="color: #666666">.</span>float32)
a_fc1 <span style="color: #666666">=</span> tf<span style="color: #666666">.</span>nn<span style="color: #666666">.</span>sigmoid(tf<span style="color: #666666">.</span>matmul(<span style="color: #008000">self</span><span style="color: #666666">.</span>X, <span style="color: #008000">self</span><span style="color: #666666">.</span>W_fc1) <span style="color: #666666">+</span> b_fc1)
<span style="color: #408080; font-style: italic"># Fully connected layer 2</span>
<span style="color: #008000">self</span><span style="color: #666666">.</span>W_fc2 <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>weight_variable([<span style="color: #008000">self</span><span style="color: #666666">.</span>n_neurons_layer1, <span style="color: #008000">self</span><span style="color: #666666">.</span>n_neurons_layer2], name<span style="color: #666666">=</span><span style="color: #BA2121">&#39;fc2&#39;</span>, dtype<span style="color: #666666">=</span>tf<span style="color: #666666">.</span>float32)
b_fc2 <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>bias_variable([<span style="color: #008000">self</span><span style="color: #666666">.</span>n_neurons_layer2], name<span style="color: #666666">=</span><span style="color: #BA2121">&#39;fc2&#39;</span>, dtype<span style="color: #666666">=</span>tf<span style="color: #666666">.</span>float32)
a_fc2 <span style="color: #666666">=</span> tf<span style="color: #666666">.</span>nn<span style="color: #666666">.</span>sigmoid(tf<span style="color: #666666">.</span>matmul(a_fc1, <span style="color: #008000">self</span><span style="color: #666666">.</span>W_fc2) <span style="color: #666666">+</span> b_fc2)
<span style="color: #408080; font-style: italic"># Output layer</span>
<span style="color: #008000">self</span><span style="color: #666666">.</span>W_out <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>weight_variable([<span style="color: #008000">self</span><span style="color: #666666">.</span>n_neurons_layer2, <span style="color: #008000">self</span><span style="color: #666666">.</span>n_categories], name<span style="color: #666666">=</span><span style="color: #BA2121">&#39;out&#39;</span>, dtype<span style="color: #666666">=</span>tf<span style="color: #666666">.</span>float32)
b_out <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>bias_variable([<span style="color: #008000">self</span><span style="color: #666666">.</span>n_categories], name<span style="color: #666666">=</span><span style="color: #BA2121">&#39;out&#39;</span>, dtype<span style="color: #666666">=</span>tf<span style="color: #666666">.</span>float32)
<span style="color: #008000">self</span><span style="color: #666666">.</span>z_out <span style="color: #666666">=</span> tf<span style="color: #666666">.</span>matmul(a_fc2, <span style="color: #008000">self</span><span style="color: #666666">.</span>W_out) <span style="color: #666666">+</span> b_out
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">create_loss</span>(<span style="color: #008000">self</span>):
<span style="color: #008000; font-weight: bold">with</span> tf<span style="color: #666666">.</span>name_scope(<span style="color: #BA2121">&#39;loss&#39;</span>):
softmax_loss <span style="color: #666666">=</span> tf<span style="color: #666666">.</span>reduce_mean(tf<span style="color: #666666">.</span>nn<span style="color: #666666">.</span>softmax_cross_entropy_with_logits_v2(labels<span style="color: #666666">=</span><span style="color: #008000">self</span><span style="color: #666666">.</span>Y, logits<span style="color: #666666">=</span><span style="color: #008000">self</span><span style="color: #666666">.</span>z_out))
regularizer_loss_fc1 <span style="color: #666666">=</span> tf<span style="color: #666666">.</span>nn<span style="color: #666666">.</span>l2_loss(<span style="color: #008000">self</span><span style="color: #666666">.</span>W_fc1)
regularizer_loss_fc2 <span style="color: #666666">=</span> tf<span style="color: #666666">.</span>nn<span style="color: #666666">.</span>l2_loss(<span style="color: #008000">self</span><span style="color: #666666">.</span>W_fc2)
regularizer_loss_out <span style="color: #666666">=</span> tf<span style="color: #666666">.</span>nn<span style="color: #666666">.</span>l2_loss(<span style="color: #008000">self</span><span style="color: #666666">.</span>W_out)
regularizer_loss <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>lmbd<span style="color: #666666">*</span>(regularizer_loss_fc1 <span style="color: #666666">+</span> regularizer_loss_fc2 <span style="color: #666666">+</span> regularizer_loss_out)
<span style="color: #008000">self</span><span style="color: #666666">.</span>loss <span style="color: #666666">=</span> softmax_loss <span style="color: #666666">+</span> regularizer_loss
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">create_accuracy</span>(<span style="color: #008000">self</span>):
<span style="color: #008000; font-weight: bold">with</span> tf<span style="color: #666666">.</span>name_scope(<span style="color: #BA2121">&#39;accuracy&#39;</span>):
probabilities <span style="color: #666666">=</span> tf<span style="color: #666666">.</span>nn<span style="color: #666666">.</span>softmax(<span style="color: #008000">self</span><span style="color: #666666">.</span>z_out)
predictions <span style="color: #666666">=</span> tf<span style="color: #666666">.</span>argmax(probabilities, axis<span style="color: #666666">=1</span>)
labels <span style="color: #666666">=</span> tf<span style="color: #666666">.</span>argmax(<span style="color: #008000">self</span><span style="color: #666666">.</span>Y, axis<span style="color: #666666">=1</span>)
correct_predictions <span style="color: #666666">=</span> tf<span style="color: #666666">.</span>equal(predictions, labels)
correct_predictions <span style="color: #666666">=</span> tf<span style="color: #666666">.</span>cast(correct_predictions, tf<span style="color: #666666">.</span>float32)
<span style="color: #008000">self</span><span style="color: #666666">.</span>accuracy <span style="color: #666666">=</span> tf<span style="color: #666666">.</span>reduce_mean(correct_predictions)
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">create_optimiser</span>(<span style="color: #008000">self</span>):
<span style="color: #008000; font-weight: bold">with</span> tf<span style="color: #666666">.</span>name_scope(<span style="color: #BA2121">&#39;optimizer&#39;</span>):
<span style="color: #008000">self</span><span style="color: #666666">.</span>optimizer <span style="color: #666666">=</span> tf<span style="color: #666666">.</span>train<span style="color: #666666">.</span>GradientDescentOptimizer(learning_rate<span style="color: #666666">=</span><span style="color: #008000">self</span><span style="color: #666666">.</span>eta)<span style="color: #666666">.</span>minimize(<span style="color: #008000">self</span><span style="color: #666666">.</span>loss, global_step<span style="color: #666666">=</span><span style="color: #008000">self</span><span style="color: #666666">.</span>global_step)
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">weight_variable</span>(<span style="color: #008000">self</span>, shape, name<span style="color: #666666">=</span><span style="color: #BA2121">&#39;&#39;</span>, dtype<span style="color: #666666">=</span>tf<span style="color: #666666">.</span>float32):
initial <span style="color: #666666">=</span> tf<span style="color: #666666">.</span>truncated_normal(shape, stddev<span style="color: #666666">=0.1</span>)
<span style="color: #008000; font-weight: bold">return</span> tf<span style="color: #666666">.</span>Variable(initial, name<span style="color: #666666">=</span>name, dtype<span style="color: #666666">=</span>dtype)
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">bias_variable</span>(<span style="color: #008000">self</span>, shape, name<span style="color: #666666">=</span><span style="color: #BA2121">&#39;&#39;</span>, dtype<span style="color: #666666">=</span>tf<span style="color: #666666">.</span>float32):
initial <span style="color: #666666">=</span> tf<span style="color: #666666">.</span>constant(<span style="color: #666666">0.1</span>, shape<span style="color: #666666">=</span>shape)
<span style="color: #008000; font-weight: bold">return</span> tf<span style="color: #666666">.</span>Variable(initial, name<span style="color: #666666">=</span>name, dtype<span style="color: #666666">=</span>dtype)
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">fit</span>(<span style="color: #008000">self</span>):
data_indices <span style="color: #666666">=</span> np<span style="color: #666666">.</span>arange(<span style="color: #008000">self</span><span style="color: #666666">.</span>n_inputs)
<span style="color: #008000; font-weight: bold">with</span> tf<span style="color: #666666">.</span>Session() <span style="color: #008000; font-weight: bold">as</span> sess:
sess<span style="color: #666666">.</span>run(tf<span style="color: #666666">.</span>global_variables_initializer())
<span style="color: #008000; font-weight: bold">for</span> i <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #008000">self</span><span style="color: #666666">.</span>epochs):
<span style="color: #008000; font-weight: bold">for</span> j <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #008000">self</span><span style="color: #666666">.</span>iterations):
chosen_datapoints <span style="color: #666666">=</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>choice(data_indices, size<span style="color: #666666">=</span><span style="color: #008000">self</span><span style="color: #666666">.</span>batch_size, replace<span style="color: #666666">=</span><span style="color: #008000">False</span>)
batch_X, batch_Y <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>X_train[chosen_datapoints], <span style="color: #008000">self</span><span style="color: #666666">.</span>Y_train[chosen_datapoints]
sess<span style="color: #666666">.</span>run([DNN<span style="color: #666666">.</span>loss, DNN<span style="color: #666666">.</span>optimizer],
feed_dict<span style="color: #666666">=</span>{DNN<span style="color: #666666">.</span>X: batch_X,
DNN<span style="color: #666666">.</span>Y: batch_Y})
accuracy <span style="color: #666666">=</span> sess<span style="color: #666666">.</span>run(DNN<span style="color: #666666">.</span>accuracy,
feed_dict<span style="color: #666666">=</span>{DNN<span style="color: #666666">.</span>X: batch_X,
DNN<span style="color: #666666">.</span>Y: batch_Y})
step <span style="color: #666666">=</span> sess<span style="color: #666666">.</span>run(DNN<span style="color: #666666">.</span>global_step)
<span style="color: #008000">self</span><span style="color: #666666">.</span>train_loss, <span style="color: #008000">self</span><span style="color: #666666">.</span>train_accuracy <span style="color: #666666">=</span> sess<span style="color: #666666">.</span>run([DNN<span style="color: #666666">.</span>loss, DNN<span style="color: #666666">.</span>accuracy],
feed_dict<span style="color: #666666">=</span>{DNN<span style="color: #666666">.</span>X: <span style="color: #008000">self</span><span style="color: #666666">.</span>X_train,
DNN<span style="color: #666666">.</span>Y: <span style="color: #008000">self</span><span style="color: #666666">.</span>Y_train})
<span style="color: #008000">self</span><span style="color: #666666">.</span>test_loss, <span style="color: #008000">self</span><span style="color: #666666">.</span>test_accuracy <span style="color: #666666">=</span> sess<span style="color: #666666">.</span>run([DNN<span style="color: #666666">.</span>loss, DNN<span style="color: #666666">.</span>accuracy],
feed_dict<span style="color: #666666">=</span>{DNN<span style="color: #666666">.</span>X: <span style="color: #008000">self</span><span style="color: #666666">.</span>X_test,
DNN<span style="color: #666666">.</span>Y: <span style="color: #008000">self</span><span style="color: #666666">.</span>Y_test})
</pre></div>
<p>
<p>
<!-- navigation buttons at the bottom of the page -->
<ul class="pagination">
<li><a href="._NeuralNet-bs052.html">&laquo;</a></li>
<li><a href="._NeuralNet-bs000.html">1</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs045.html">46</a></li>
<li><a href="._NeuralNet-bs046.html">47</a></li>
<li><a href="._NeuralNet-bs047.html">48</a></li>
<li><a href="._NeuralNet-bs048.html">49</a></li>
<li><a href="._NeuralNet-bs049.html">50</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs051.html">52</a></li>
<li><a href="._NeuralNet-bs052.html">53</a></li>
<li class="active"><a href="._NeuralNet-bs053.html">54</a></li>
<li><a href="._NeuralNet-bs054.html">55</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs054.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
</div> <!-- end container -->
<!-- include javascript, jQuery *first* -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<!-- Bootstrap footer
<footer>
<a href="http://..."><img width="250" align=right src="http://..."></a>
</footer>
-->
<center style="font-size:80%">
<!-- copyright only on the titlepage -->
</center>
</body>
</html>
@@ -0,0 +1,352 @@
<!--
Automatically generated HTML file from DocOnce source
(https://github.com/hplgit/doconce/)
-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
<meta name="description" content="Data Analysis and Machine Learning: Neural networks, from the simple perceptron to deep learning">
<title>Data Analysis and Machine Learning: Neural networks, from the simple perceptron to deep learning</title>
<!-- Bootstrap style: bootstrap -->
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
<!-- not necessary
<link href="https://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">
-->
<style type="text/css">
/* Add scrollbar to dropdown menus in bootstrap navigation bar */
.dropdown-menu {
height: auto;
max-height: 400px;
overflow-x: hidden;
}
/* Adds an invisible element before each target to offset for the navigation
bar */
.anchor::before {
content:"";
display:block;
height:50px; /* fixed header height for style bootstrap */
margin:-50px 0 0; /* negative fixed header height */
}
</style>
</head>
<!-- tocinfo
{'highest level': 2,
'sections': [('Neural networks', 2, None, '___sec0'),
('Artificial neurons', 2, None, '___sec1'),
('Neural network types', 2, None, '___sec2'),
('Feed-forward neural networks', 2, None, '___sec3'),
('Convolutional Neural Network', 2, None, '___sec4'),
('Recurrent neural networks', 2, None, '___sec5'),
('Other types of networks', 2, None, '___sec6'),
('Multilayer perceptrons', 2, None, '___sec7'),
('Why multilayer perceptrons?', 2, None, '___sec8'),
('Mathematical model', 2, None, '___sec9'),
('Mathematical model', 2, None, '___sec10'),
('Mathematical model', 2, None, '___sec11'),
('Mathematical model', 2, None, '___sec12'),
('Mathematical model', 2, None, '___sec13'),
('Matrix-vector notation', 3, None, '___sec14'),
('Matrix-vector notation and activation', 3, None, '___sec15'),
('Activation functions', 3, None, '___sec16'),
('Activation functions, Logistic and Hyperbolic ones',
3,
None,
'___sec17'),
('Relevance', 3, None, '___sec18'),
('The multilayer perceptron (MLP)', 2, None, '___sec19'),
('From one to many layers, the universal approximation theorem',
2,
None,
'___sec20'),
('Deriving the back propagation code for a multilayer perceptron '
'model',
2,
None,
'___sec21'),
('Definitions', 2, None, '___sec22'),
('Derivatives and the chain rule', 2, None, '___sec23'),
('Derivative of the cost function', 2, None, '___sec24'),
('Bringing it together, first back propagation equation',
2,
None,
'___sec25'),
('Derivatives in terms of $z_j^L$', 2, None, '___sec26'),
('Bringing it together', 2, None, '___sec27'),
('Final back propagating equation', 2, None, '___sec28'),
('Setting up the Back propagation algorithm',
2,
None,
'___sec29'),
('Setting up a Multi-layer perceptron model for classification',
2,
None,
'___sec30'),
('Defining the cost function', 2, None, '___sec31'),
('Developing a code for doing neural networks with back '
'propagation',
2,
None,
'___sec32'),
('Collect and pre-process data', 2, None, '___sec33'),
('Train and test datasets', 2, None, '___sec34'),
('Define model and architecture', 2, None, '___sec35'),
('Layers', 2, None, '___sec36'),
('Weights and biases', 2, None, '___sec37'),
('Feed-forward pass', 2, None, '___sec38'),
('Matrix multiplication', 2, None, '___sec39'),
('Choose cost function and optimizer', 2, None, '___sec40'),
('Optimizing the cost function', 2, None, '___sec41'),
('Regularization', 2, None, '___sec42'),
('Matrix multiplication', 2, None, '___sec43'),
('Improving performance', 2, None, '___sec44'),
('Full object-oriented implementation', 2, None, '___sec45'),
('Evaluate model performance on test data', 2, None, '___sec46'),
('Adjust hyperparameters (if necessary, network architecture',
2,
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
TeX: {
equationNumbers: { autoNumber: "none" },
extensions: ["AMSmath.js", "AMSsymbols.js", "autobold.js", "color.js"]
}
});
</script>
<script type="text/javascript" async
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<!-- Bootstrap navigation bar -->
<div class="navbar navbar-default navbar-fixed-top">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-responsive-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="NeuralNet-bs.html">Data Analysis and Machine Learning: Neural networks, from the simple perceptron to deep learning</a>
</div>
<div class="navbar-collapse collapse navbar-responsive-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
<ul class="dropdown-menu">
<!-- navigation toc: --> <li><a href="._NeuralNet-bs001.html#___sec0" style="font-size: 80%;"><b>Neural networks</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs002.html#___sec1" style="font-size: 80%;"><b>Artificial neurons</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs003.html#___sec2" style="font-size: 80%;"><b>Neural network types</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs004.html#___sec3" style="font-size: 80%;"><b>Feed-forward neural networks</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs005.html#___sec4" style="font-size: 80%;"><b>Convolutional Neural Network</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs006.html#___sec5" style="font-size: 80%;"><b>Recurrent neural networks</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs007.html#___sec6" style="font-size: 80%;"><b>Other types of networks</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs008.html#___sec7" style="font-size: 80%;"><b>Multilayer perceptrons</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs009.html#___sec8" style="font-size: 80%;"><b>Why multilayer perceptrons?</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs010.html#___sec9" style="font-size: 80%;"><b>Mathematical model</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs011.html#___sec10" style="font-size: 80%;"><b>Mathematical model</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs012.html#___sec11" style="font-size: 80%;"><b>Mathematical model</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs013.html#___sec12" style="font-size: 80%;"><b>Mathematical model</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs014.html#___sec13" style="font-size: 80%;"><b>Mathematical model</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs015.html#___sec14" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Matrix-vector notation</a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs016.html#___sec15" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Matrix-vector notation and activation</a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs017.html#___sec16" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Activation functions</a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs018.html#___sec17" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Activation functions, Logistic and Hyperbolic ones</a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs019.html#___sec18" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Relevance</a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs020.html#___sec19" style="font-size: 80%;"><b>The multilayer perceptron (MLP)</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs021.html#___sec20" style="font-size: 80%;"><b>From one to many layers, the universal approximation theorem</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs022.html#___sec21" style="font-size: 80%;"><b>Deriving the back propagation code for a multilayer perceptron model</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs023.html#___sec22" style="font-size: 80%;"><b>Definitions</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs024.html#___sec23" style="font-size: 80%;"><b>Derivatives and the chain rule</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs025.html#___sec24" style="font-size: 80%;"><b>Derivative of the cost function</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs026.html#___sec25" style="font-size: 80%;"><b>Bringing it together, first back propagation equation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs027.html#___sec26" style="font-size: 80%;"><b>Derivatives in terms of \( z_j^L \)</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs028.html#___sec27" style="font-size: 80%;"><b>Bringing it together</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs029.html#___sec28" style="font-size: 80%;"><b>Final back propagating equation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs030.html#___sec29" style="font-size: 80%;"><b>Setting up the Back propagation algorithm</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs031.html#___sec30" style="font-size: 80%;"><b>Setting up a Multi-layer perceptron model for classification</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs032.html#___sec31" style="font-size: 80%;"><b>Defining the cost function</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs033.html#___sec32" style="font-size: 80%;"><b>Developing a code for doing neural networks with back propagation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs034.html#___sec33" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs035.html#___sec34" style="font-size: 80%;"><b>Train and test datasets</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs036.html#___sec35" style="font-size: 80%;"><b>Define model and architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs037.html#___sec36" style="font-size: 80%;"><b>Layers</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs038.html#___sec37" style="font-size: 80%;"><b>Weights and biases</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs039.html#___sec38" style="font-size: 80%;"><b>Feed-forward pass</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs040.html#___sec39" style="font-size: 80%;"><b>Matrix multiplication</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs041.html#___sec40" style="font-size: 80%;"><b>Choose cost function and optimizer</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs042.html#___sec41" style="font-size: 80%;"><b>Optimizing the cost function</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs043.html#___sec42" style="font-size: 80%;"><b>Regularization</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs044.html#___sec43" style="font-size: 80%;"><b>Matrix multiplication</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs045.html#___sec44" style="font-size: 80%;"><b>Improving performance</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs046.html#___sec45" style="font-size: 80%;"><b>Full object-oriented implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
</ul>
</div>
</div>
</div> <!-- end of navigation bar -->
<div class="container">
<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p> <!-- add vertical space -->
<a name="part0054"></a>
<!-- !split -->
<h2 id="___sec53" class="anchor">Optimizing and using gradient descent </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>epochs <span style="color: #666666">=</span> <span style="color: #666666">100</span>
batch_size <span style="color: #666666">=</span> <span style="color: #666666">100</span>
n_neurons_layer1 <span style="color: #666666">=</span> <span style="color: #666666">100</span>
n_neurons_layer2 <span style="color: #666666">=</span> <span style="color: #666666">50</span>
n_categories <span style="color: #666666">=</span> <span style="color: #666666">10</span>
eta_vals <span style="color: #666666">=</span> np<span style="color: #666666">.</span>logspace(<span style="color: #666666">-5</span>, <span style="color: #666666">1</span>, <span style="color: #666666">7</span>)
lmbd_vals <span style="color: #666666">=</span> np<span style="color: #666666">.</span>logspace(<span style="color: #666666">-5</span>, <span style="color: #666666">1</span>, <span style="color: #666666">7</span>)
</pre></div>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>DNN_tf <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros((<span style="color: #008000">len</span>(eta_vals), <span style="color: #008000">len</span>(lmbd_vals)), dtype<span style="color: #666666">=</span><span style="color: #008000">object</span>)
<span style="color: #008000; font-weight: bold">for</span> i, eta <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">enumerate</span>(eta_vals):
<span style="color: #008000; font-weight: bold">for</span> j, lmbd <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">enumerate</span>(lmbd_vals):
DNN <span style="color: #666666">=</span> NeuralNetworkTensorflow(X_train, Y_train, X_test, Y_test,
n_neurons_layer1, n_neurons_layer2, n_categories,
epochs<span style="color: #666666">=</span>epochs, batch_size<span style="color: #666666">=</span>batch_size, eta<span style="color: #666666">=</span>eta, lmbd<span style="color: #666666">=</span>lmbd)
DNN<span style="color: #666666">.</span>fit()
DNN_tf[i][j] <span style="color: #666666">=</span> DNN
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;Learning rate = &quot;</span>, eta)
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;Lambda = &quot;</span>, lmbd)
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;Test accuracy: </span><span style="color: #BB6688; font-weight: bold">%.3f</span><span style="color: #BA2121">&quot;</span> <span style="color: #666666">%</span> DNN<span style="color: #666666">.</span>test_accuracy)
<span style="color: #008000; font-weight: bold">print</span>()
</pre></div>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># optional</span>
<span style="color: #408080; font-style: italic"># visual representation of grid search</span>
<span style="color: #408080; font-style: italic"># uses seaborn heatmap, could probably do this in matplotlib</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">seaborn</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">sns</span>
sns<span style="color: #666666">.</span>set()
train_accuracy <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros((<span style="color: #008000">len</span>(eta_vals), <span style="color: #008000">len</span>(lmbd_vals)))
test_accuracy <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros((<span style="color: #008000">len</span>(eta_vals), <span style="color: #008000">len</span>(lmbd_vals)))
<span style="color: #008000; font-weight: bold">for</span> i <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #008000">len</span>(eta_vals)):
<span style="color: #008000; font-weight: bold">for</span> j <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #008000">len</span>(lmbd_vals)):
DNN <span style="color: #666666">=</span> DNN_tf[i][j]
train_accuracy[i][j] <span style="color: #666666">=</span> DNN<span style="color: #666666">.</span>train_accuracy
test_accuracy[i][j] <span style="color: #666666">=</span> DNN<span style="color: #666666">.</span>test_accuracy
fig, ax <span style="color: #666666">=</span> plt<span style="color: #666666">.</span>subplots(figsize <span style="color: #666666">=</span> (<span style="color: #666666">10</span>, <span style="color: #666666">10</span>))
sns<span style="color: #666666">.</span>heatmap(train_accuracy, annot<span style="color: #666666">=</span><span style="color: #008000">True</span>, ax<span style="color: #666666">=</span>ax, cmap<span style="color: #666666">=</span><span style="color: #BA2121">&quot;viridis&quot;</span>)
ax<span style="color: #666666">.</span>set_title(<span style="color: #BA2121">&quot;Training Accuracy&quot;</span>)
ax<span style="color: #666666">.</span>set_ylabel(<span style="color: #BA2121">&quot;$\eta$&quot;</span>)
ax<span style="color: #666666">.</span>set_xlabel(<span style="color: #BA2121">&quot;$\lambda$&quot;</span>)
plt<span style="color: #666666">.</span>show()
fig, ax <span style="color: #666666">=</span> plt<span style="color: #666666">.</span>subplots(figsize <span style="color: #666666">=</span> (<span style="color: #666666">10</span>, <span style="color: #666666">10</span>))
sns<span style="color: #666666">.</span>heatmap(test_accuracy, annot<span style="color: #666666">=</span><span style="color: #008000">True</span>, ax<span style="color: #666666">=</span>ax, cmap<span style="color: #666666">=</span><span style="color: #BA2121">&quot;viridis&quot;</span>)
ax<span style="color: #666666">.</span>set_title(<span style="color: #BA2121">&quot;Test Accuracy&quot;</span>)
ax<span style="color: #666666">.</span>set_ylabel(<span style="color: #BA2121">&quot;$\eta$&quot;</span>)
ax<span style="color: #666666">.</span>set_xlabel(<span style="color: #BA2121">&quot;$\lambda$&quot;</span>)
plt<span style="color: #666666">.</span>show()
</pre></div>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># optional</span>
<span style="color: #408080; font-style: italic"># we can use log files to visualize our graph in Tensorboard</span>
writer <span style="color: #666666">=</span> tf<span style="color: #666666">.</span>summary<span style="color: #666666">.</span>FileWriter(<span style="color: #BA2121">&#39;logs/&#39;</span>)
writer<span style="color: #666666">.</span>add_graph(tf<span style="color: #666666">.</span>get_default_graph())
</pre></div>
<p>
<p>
<!-- navigation buttons at the bottom of the page -->
<ul class="pagination">
<li><a href="._NeuralNet-bs053.html">&laquo;</a></li>
<li><a href="._NeuralNet-bs000.html">1</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs046.html">47</a></li>
<li><a href="._NeuralNet-bs047.html">48</a></li>
<li><a href="._NeuralNet-bs048.html">49</a></li>
<li><a href="._NeuralNet-bs049.html">50</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs051.html">52</a></li>
<li><a href="._NeuralNet-bs052.html">53</a></li>
<li><a href="._NeuralNet-bs053.html">54</a></li>
<li class="active"><a href="._NeuralNet-bs054.html">55</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs055.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
</div> <!-- end container -->
<!-- include javascript, jQuery *first* -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<!-- Bootstrap footer
<footer>
<a href="http://..."><img width="250" align=right src="http://..."></a>
</footer>
-->
<center style="font-size:80%">
<!-- copyright only on the titlepage -->
</center>
</body>
</html>
@@ -0,0 +1,370 @@
<!--
Automatically generated HTML file from DocOnce source
(https://github.com/hplgit/doconce/)
-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
<meta name="description" content="Data Analysis and Machine Learning: Neural networks, from the simple perceptron to deep learning">
<title>Data Analysis and Machine Learning: Neural networks, from the simple perceptron to deep learning</title>
<!-- Bootstrap style: bootstrap -->
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
<!-- not necessary
<link href="https://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">
-->
<style type="text/css">
/* Add scrollbar to dropdown menus in bootstrap navigation bar */
.dropdown-menu {
height: auto;
max-height: 400px;
overflow-x: hidden;
}
/* Adds an invisible element before each target to offset for the navigation
bar */
.anchor::before {
content:"";
display:block;
height:50px; /* fixed header height for style bootstrap */
margin:-50px 0 0; /* negative fixed header height */
}
</style>
</head>
<!-- tocinfo
{'highest level': 2,
'sections': [('Neural networks', 2, None, '___sec0'),
('Artificial neurons', 2, None, '___sec1'),
('Neural network types', 2, None, '___sec2'),
('Feed-forward neural networks', 2, None, '___sec3'),
('Convolutional Neural Network', 2, None, '___sec4'),
('Recurrent neural networks', 2, None, '___sec5'),
('Other types of networks', 2, None, '___sec6'),
('Multilayer perceptrons', 2, None, '___sec7'),
('Why multilayer perceptrons?', 2, None, '___sec8'),
('Mathematical model', 2, None, '___sec9'),
('Mathematical model', 2, None, '___sec10'),
('Mathematical model', 2, None, '___sec11'),
('Mathematical model', 2, None, '___sec12'),
('Mathematical model', 2, None, '___sec13'),
('Matrix-vector notation', 3, None, '___sec14'),
('Matrix-vector notation and activation', 3, None, '___sec15'),
('Activation functions', 3, None, '___sec16'),
('Activation functions, Logistic and Hyperbolic ones',
3,
None,
'___sec17'),
('Relevance', 3, None, '___sec18'),
('The multilayer perceptron (MLP)', 2, None, '___sec19'),
('From one to many layers, the universal approximation theorem',
2,
None,
'___sec20'),
('Deriving the back propagation code for a multilayer perceptron '
'model',
2,
None,
'___sec21'),
('Definitions', 2, None, '___sec22'),
('Derivatives and the chain rule', 2, None, '___sec23'),
('Derivative of the cost function', 2, None, '___sec24'),
('Bringing it together, first back propagation equation',
2,
None,
'___sec25'),
('Derivatives in terms of $z_j^L$', 2, None, '___sec26'),
('Bringing it together', 2, None, '___sec27'),
('Final back propagating equation', 2, None, '___sec28'),
('Setting up the Back propagation algorithm',
2,
None,
'___sec29'),
('Setting up a Multi-layer perceptron model for classification',
2,
None,
'___sec30'),
('Defining the cost function', 2, None, '___sec31'),
('Developing a code for doing neural networks with back '
'propagation',
2,
None,
'___sec32'),
('Collect and pre-process data', 2, None, '___sec33'),
('Train and test datasets', 2, None, '___sec34'),
('Define model and architecture', 2, None, '___sec35'),
('Layers', 2, None, '___sec36'),
('Weights and biases', 2, None, '___sec37'),
('Feed-forward pass', 2, None, '___sec38'),
('Matrix multiplication', 2, None, '___sec39'),
('Choose cost function and optimizer', 2, None, '___sec40'),
('Optimizing the cost function', 2, None, '___sec41'),
('Regularization', 2, None, '___sec42'),
('Matrix multiplication', 2, None, '___sec43'),
('Improving performance', 2, None, '___sec44'),
('Full object-oriented implementation', 2, None, '___sec45'),
('Evaluate model performance on test data', 2, None, '___sec46'),
('Adjust hyperparameters (if necessary, network architecture',
2,
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
TeX: {
equationNumbers: { autoNumber: "none" },
extensions: ["AMSmath.js", "AMSsymbols.js", "autobold.js", "color.js"]
}
});
</script>
<script type="text/javascript" async
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<!-- Bootstrap navigation bar -->
<div class="navbar navbar-default navbar-fixed-top">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-responsive-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="NeuralNet-bs.html">Data Analysis and Machine Learning: Neural networks, from the simple perceptron to deep learning</a>
</div>
<div class="navbar-collapse collapse navbar-responsive-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
<ul class="dropdown-menu">
<!-- navigation toc: --> <li><a href="._NeuralNet-bs001.html#___sec0" style="font-size: 80%;"><b>Neural networks</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs002.html#___sec1" style="font-size: 80%;"><b>Artificial neurons</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs003.html#___sec2" style="font-size: 80%;"><b>Neural network types</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs004.html#___sec3" style="font-size: 80%;"><b>Feed-forward neural networks</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs005.html#___sec4" style="font-size: 80%;"><b>Convolutional Neural Network</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs006.html#___sec5" style="font-size: 80%;"><b>Recurrent neural networks</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs007.html#___sec6" style="font-size: 80%;"><b>Other types of networks</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs008.html#___sec7" style="font-size: 80%;"><b>Multilayer perceptrons</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs009.html#___sec8" style="font-size: 80%;"><b>Why multilayer perceptrons?</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs010.html#___sec9" style="font-size: 80%;"><b>Mathematical model</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs011.html#___sec10" style="font-size: 80%;"><b>Mathematical model</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs012.html#___sec11" style="font-size: 80%;"><b>Mathematical model</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs013.html#___sec12" style="font-size: 80%;"><b>Mathematical model</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs014.html#___sec13" style="font-size: 80%;"><b>Mathematical model</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs015.html#___sec14" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Matrix-vector notation</a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs016.html#___sec15" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Matrix-vector notation and activation</a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs017.html#___sec16" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Activation functions</a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs018.html#___sec17" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Activation functions, Logistic and Hyperbolic ones</a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs019.html#___sec18" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Relevance</a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs020.html#___sec19" style="font-size: 80%;"><b>The multilayer perceptron (MLP)</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs021.html#___sec20" style="font-size: 80%;"><b>From one to many layers, the universal approximation theorem</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs022.html#___sec21" style="font-size: 80%;"><b>Deriving the back propagation code for a multilayer perceptron model</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs023.html#___sec22" style="font-size: 80%;"><b>Definitions</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs024.html#___sec23" style="font-size: 80%;"><b>Derivatives and the chain rule</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs025.html#___sec24" style="font-size: 80%;"><b>Derivative of the cost function</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs026.html#___sec25" style="font-size: 80%;"><b>Bringing it together, first back propagation equation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs027.html#___sec26" style="font-size: 80%;"><b>Derivatives in terms of \( z_j^L \)</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs028.html#___sec27" style="font-size: 80%;"><b>Bringing it together</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs029.html#___sec28" style="font-size: 80%;"><b>Final back propagating equation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs030.html#___sec29" style="font-size: 80%;"><b>Setting up the Back propagation algorithm</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs031.html#___sec30" style="font-size: 80%;"><b>Setting up a Multi-layer perceptron model for classification</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs032.html#___sec31" style="font-size: 80%;"><b>Defining the cost function</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs033.html#___sec32" style="font-size: 80%;"><b>Developing a code for doing neural networks with back propagation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs034.html#___sec33" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs035.html#___sec34" style="font-size: 80%;"><b>Train and test datasets</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs036.html#___sec35" style="font-size: 80%;"><b>Define model and architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs037.html#___sec36" style="font-size: 80%;"><b>Layers</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs038.html#___sec37" style="font-size: 80%;"><b>Weights and biases</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs039.html#___sec38" style="font-size: 80%;"><b>Feed-forward pass</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs040.html#___sec39" style="font-size: 80%;"><b>Matrix multiplication</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs041.html#___sec40" style="font-size: 80%;"><b>Choose cost function and optimizer</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs042.html#___sec41" style="font-size: 80%;"><b>Optimizing the cost function</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs043.html#___sec42" style="font-size: 80%;"><b>Regularization</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs044.html#___sec43" style="font-size: 80%;"><b>Matrix multiplication</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs045.html#___sec44" style="font-size: 80%;"><b>Improving performance</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs046.html#___sec45" style="font-size: 80%;"><b>Full object-oriented implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
</ul>
</div>
</div>
</div> <!-- end of navigation bar -->
<div class="container">
<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p> <!-- add vertical space -->
<a name="part0055"></a>
<!-- !split -->
<h2 id="___sec54" class="anchor">Using Keras </h2>
<p>
Keras is a high level <a href="https://en.wikipedia.org/wiki/Application_programming_interface" target="_self">neural network</a>
that supports Tensorflow, CTNK and Theano as backends.
If you have Tensorflow installed Keras is available through the <em>tf.keras</em> module.
If you have Anaconda installed you may run the following command
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>conda install keras
</pre></div>
<p>
Alternatively, if you have Tensorflow or one of the other supported backends install you may use the pip package manager:
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>pip3 install keras
</pre></div>
<p>
or look up the <a href="https://keras.io/" target="_self">instructions here</a>.
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">keras.models</span> <span style="color: #008000; font-weight: bold">import</span> Sequential
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">keras.layers</span> <span style="color: #008000; font-weight: bold">import</span> Dense
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">keras.regularizers</span> <span style="color: #008000; font-weight: bold">import</span> l2
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">keras.optimizers</span> <span style="color: #008000; font-weight: bold">import</span> SGD
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">create_neural_network_keras</span>(n_neurons_layer1, n_neurons_layer2, n_categories, eta, lmbd):
model <span style="color: #666666">=</span> Sequential()
model<span style="color: #666666">.</span>add(Dense(n_neurons_layer1, activation<span style="color: #666666">=</span><span style="color: #BA2121">&#39;sigmoid&#39;</span>, kernel_regularizer<span style="color: #666666">=</span>l2(lmbd)))
model<span style="color: #666666">.</span>add(Dense(n_neurons_layer2, activation<span style="color: #666666">=</span><span style="color: #BA2121">&#39;sigmoid&#39;</span>, kernel_regularizer<span style="color: #666666">=</span>l2(lmbd)))
model<span style="color: #666666">.</span>add(Dense(n_categories, activation<span style="color: #666666">=</span><span style="color: #BA2121">&#39;softmax&#39;</span>))
sgd <span style="color: #666666">=</span> SGD(lr<span style="color: #666666">=</span>eta)
model<span style="color: #666666">.</span>compile(loss<span style="color: #666666">=</span><span style="color: #BA2121">&#39;categorical_crossentropy&#39;</span>, optimizer<span style="color: #666666">=</span>sgd, metrics<span style="color: #666666">=</span>[<span style="color: #BA2121">&#39;accuracy&#39;</span>])
<span style="color: #008000; font-weight: bold">return</span> model
</pre></div>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>DNN_keras <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros((<span style="color: #008000">len</span>(eta_vals), <span style="color: #008000">len</span>(lmbd_vals)), dtype<span style="color: #666666">=</span><span style="color: #008000">object</span>)
<span style="color: #008000; font-weight: bold">for</span> i, eta <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">enumerate</span>(eta_vals):
<span style="color: #008000; font-weight: bold">for</span> j, lmbd <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">enumerate</span>(lmbd_vals):
DNN <span style="color: #666666">=</span> create_neural_network_keras(n_neurons_layer1, n_neurons_layer2, n_categories,
eta<span style="color: #666666">=</span>eta, lmbd<span style="color: #666666">=</span>lmbd)
DNN<span style="color: #666666">.</span>fit(X_train, Y_train, epochs<span style="color: #666666">=</span>epochs, batch_size<span style="color: #666666">=</span>batch_size, verbose<span style="color: #666666">=0</span>)
scores <span style="color: #666666">=</span> DNN<span style="color: #666666">.</span>evaluate(X_test, Y_test)
DNN_keras[i][j] <span style="color: #666666">=</span> DNN
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;Learning rate = &quot;</span>, eta)
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;Lambda = &quot;</span>, lmbd)
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;Test accuracy: </span><span style="color: #BB6688; font-weight: bold">%.3f</span><span style="color: #BA2121">&quot;</span> <span style="color: #666666">%</span> scores[<span style="color: #666666">1</span>])
<span style="color: #008000; font-weight: bold">print</span>()
</pre></div>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># optional</span>
<span style="color: #408080; font-style: italic"># visual representation of grid search</span>
<span style="color: #408080; font-style: italic"># uses seaborn heatmap, could probably do this in matplotlib</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">seaborn</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">sns</span>
sns<span style="color: #666666">.</span>set()
train_accuracy <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros((<span style="color: #008000">len</span>(eta_vals), <span style="color: #008000">len</span>(lmbd_vals)))
test_accuracy <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros((<span style="color: #008000">len</span>(eta_vals), <span style="color: #008000">len</span>(lmbd_vals)))
<span style="color: #008000; font-weight: bold">for</span> i <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #008000">len</span>(eta_vals)):
<span style="color: #008000; font-weight: bold">for</span> j <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #008000">len</span>(lmbd_vals)):
DNN <span style="color: #666666">=</span> DNN_keras[i][j]
train_accuracy[i][j] <span style="color: #666666">=</span> DNN<span style="color: #666666">.</span>evaluate(X_train, Y_train)[<span style="color: #666666">1</span>]
test_accuracy[i][j] <span style="color: #666666">=</span> DNN<span style="color: #666666">.</span>evaluate(X_test, Y_test)[<span style="color: #666666">1</span>]
fig, ax <span style="color: #666666">=</span> plt<span style="color: #666666">.</span>subplots(figsize <span style="color: #666666">=</span> (<span style="color: #666666">10</span>, <span style="color: #666666">10</span>))
sns<span style="color: #666666">.</span>heatmap(train_accuracy, annot<span style="color: #666666">=</span><span style="color: #008000">True</span>, ax<span style="color: #666666">=</span>ax, cmap<span style="color: #666666">=</span><span style="color: #BA2121">&quot;viridis&quot;</span>)
ax<span style="color: #666666">.</span>set_title(<span style="color: #BA2121">&quot;Training Accuracy&quot;</span>)
ax<span style="color: #666666">.</span>set_ylabel(<span style="color: #BA2121">&quot;$\eta$&quot;</span>)
ax<span style="color: #666666">.</span>set_xlabel(<span style="color: #BA2121">&quot;$\lambda$&quot;</span>)
plt<span style="color: #666666">.</span>show()
fig, ax <span style="color: #666666">=</span> plt<span style="color: #666666">.</span>subplots(figsize <span style="color: #666666">=</span> (<span style="color: #666666">10</span>, <span style="color: #666666">10</span>))
sns<span style="color: #666666">.</span>heatmap(test_accuracy, annot<span style="color: #666666">=</span><span style="color: #008000">True</span>, ax<span style="color: #666666">=</span>ax, cmap<span style="color: #666666">=</span><span style="color: #BA2121">&quot;viridis&quot;</span>)
ax<span style="color: #666666">.</span>set_title(<span style="color: #BA2121">&quot;Test Accuracy&quot;</span>)
ax<span style="color: #666666">.</span>set_ylabel(<span style="color: #BA2121">&quot;$\eta$&quot;</span>)
ax<span style="color: #666666">.</span>set_xlabel(<span style="color: #BA2121">&quot;$\lambda$&quot;</span>)
plt<span style="color: #666666">.</span>show()
</pre></div>
<p>
<p>
<!-- navigation buttons at the bottom of the page -->
<ul class="pagination">
<li><a href="._NeuralNet-bs054.html">&laquo;</a></li>
<li><a href="._NeuralNet-bs000.html">1</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs047.html">48</a></li>
<li><a href="._NeuralNet-bs048.html">49</a></li>
<li><a href="._NeuralNet-bs049.html">50</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs051.html">52</a></li>
<li><a href="._NeuralNet-bs052.html">53</a></li>
<li><a href="._NeuralNet-bs053.html">54</a></li>
<li><a href="._NeuralNet-bs054.html">55</a></li>
<li class="active"><a href="._NeuralNet-bs055.html">56</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
</div> <!-- end container -->
<!-- include javascript, jQuery *first* -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<!-- Bootstrap footer
<footer>
<a href="http://..."><img width="250" align=right src="http://..."></a>
</footer>
-->
<center style="font-size:80%">
<!-- copyright only on the titlepage -->
</center>
</body>
</html>
+16 -3
View File
@@ -115,7 +115,15 @@ Automatically generated HTML file from DocOnce source
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -202,7 +210,12 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._NeuralNet-bs047.html#___sec46" style="font-size: 80%;"><b>Evaluate model performance on test data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs048.html#___sec47" style="font-size: 80%;"><b>Adjust hyperparameters (if necessary, network architecture</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs049.html#___sec48" style="font-size: 80%;"><b>scikit-learn implementation</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>And then with Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs050.html#___sec49" style="font-size: 80%;"><b>Building neural networks in Tensorflow and Keras</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs051.html#___sec50" style="font-size: 80%;"><b>Tensorflow</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs052.html#___sec51" style="font-size: 80%;"><b>Collect and pre-process data</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs053.html#___sec52" style="font-size: 80%;"><b>Using TensorFlow backend</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs054.html#___sec53" style="font-size: 80%;"><b>Optimizing and using gradient descent</b></a></li>
<!-- navigation toc: --> <li><a href="._NeuralNet-bs055.html#___sec54" style="font-size: 80%;"><b>Using Keras</b></a></li>
</ul>
</li>
@@ -261,7 +274,7 @@ MathJax.Hub.Config({
<li><a href="._NeuralNet-bs008.html">9</a></li>
<li><a href="._NeuralNet-bs009.html">10</a></li>
<li><a href="">...</a></li>
<li><a href="._NeuralNet-bs050.html">51</a></li>
<li><a href="._NeuralNet-bs055.html">56</a></li>
<li><a href="._NeuralNet-bs001.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+450 -1
View File
@@ -2334,7 +2334,456 @@ plt.show()
<section>
<h2 id="___sec49">And then with Tensorflow </h2>
<h2 id="___sec49">Building neural networks in Tensorflow and Keras </h2>
<p>
Now we want to build on the experience gained from our neural network implementation in NumPy and scikit-learn
and use it to construct a neural network in Tensorflow. Once we have constructed a neural network in NumPy
and Tensorflow, building one in Keras is really quite trivial, though the performance may suffer.
<p>
In our previous example we used only one hidden layer, and in this we will use two. From this it should be quite
clear how to build one using an arbitrary number of hidden layers, using data structures such as Python lists or
NumPy arrays.
</section>
<section>
<h2 id="___sec50">Tensorflow </h2>
<p>
Tensorflow is an open source library machine learning library
developed by the Google Brain team for internal use. It was released
under the Apache 2.0 open source license in November 9, 2015.
<p>
Tensorflow is a computational framework that allows you to construct
machine learning models at different levels of abstraction, from
high-level, object-oriented APIs like Keras, down to the C++ kernels
that Tensorflow is built upon. The higher levels of abstraction are
simpler to use, but less flexible, and our choice of implementation
should reflect the problems we are trying to solve.
<p>
<a href="https://www.tensorflow.org/guide/graphs" target="_blank">Tensorflow uses</a> so-called graphs to represent your computation
in terms of the dependencies between individual operations, such that you first build a Tensorflow <em>graph</em>
to represent your model, and then create a Tensorflow <em>session</em> to run the graph.
<p>
In this guide we will analyze the same data as we did in our NumPy and
scikit-learn tutorial, gathered from the MNIST database of images. We
will give an introduction to the lower level Python Application
Program Interfaces (APIs), and see how we use them to build our graph.
Then we will build (effectively) the same graph in Keras, to see just
how simple solving a machine learning problem can be.
<p>
To install tensorflow on Unix/Linux systems, use pip as
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="font-size: 80%; line-height: 125%"><span></span>pip3 install tensorflow
</pre></div>
<p>
and/or if you use <b>anaconda</b>, just write (or install from the graphical user interface)
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="font-size: 80%; line-height: 125%"><span></span>conda install tensorflow
</pre></div>
</section>
<section>
<h2 id="___sec51">Collect and pre-process data </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="font-size: 80%; line-height: 125%"><span></span><span style="color: #228B22"># import necessary packages</span>
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">numpy</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">np</span>
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">matplotlib.pyplot</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">plt</span>
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn</span> <span style="color: #8B008B; font-weight: bold">import</span> datasets
<span style="color: #228B22"># ensure the same random numbers appear every time</span>
np.random.seed(<span style="color: #B452CD">0</span>)
<span style="color: #228B22"># display images in notebook</span>
%matplotlib inline
plt.rcParams[<span style="color: #CD5555">&#39;figure.figsize&#39;</span>] = (<span style="color: #B452CD">12</span>,<span style="color: #B452CD">12</span>)
<span style="color: #228B22"># download MNIST dataset</span>
digits = datasets.load_digits()
<span style="color: #228B22"># define inputs and labels</span>
inputs = digits.images
labels = digits.target
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&quot;inputs = (n_inputs, pixel_width, pixel_height) = &quot;</span> + <span style="color: #658b00">str</span>(inputs.shape))
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&quot;labels = (n_inputs) = &quot;</span> + <span style="color: #658b00">str</span>(labels.shape))
<span style="color: #228B22"># flatten the image</span>
<span style="color: #228B22"># the value -1 means dimension is inferred from the remaining dimensions: 8x8 = 64</span>
n_inputs = <span style="color: #658b00">len</span>(inputs)
inputs = inputs.reshape(n_inputs, -<span style="color: #B452CD">1</span>)
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&quot;X = (n_inputs, n_features) = &quot;</span> + <span style="color: #658b00">str</span>(inputs.shape))
<span style="color: #228B22"># choose some random images to display</span>
indices = np.arange(n_inputs)
random_indices = np.random.choice(indices, size=<span style="color: #B452CD">5</span>)
<span style="color: #8B008B; font-weight: bold">for</span> i, image <span style="color: #8B008B">in</span> <span style="color: #658b00">enumerate</span>(digits.images[random_indices]):
plt.subplot(<span style="color: #B452CD">1</span>, <span style="color: #B452CD">5</span>, i+<span style="color: #B452CD">1</span>)
plt.axis(<span style="color: #CD5555">&#39;off&#39;</span>)
plt.imshow(image, cmap=plt.cm.gray_r, interpolation=<span style="color: #CD5555">&#39;nearest&#39;</span>)
plt.title(<span style="color: #CD5555">&quot;Label: %d&quot;</span> % digits.target[random_indices[i]])
plt.show()
</pre></div>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="font-size: 80%; line-height: 125%"><span></span><span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">keras.utils</span> <span style="color: #8B008B; font-weight: bold">import</span> to_categorical
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.model_selection</span> <span style="color: #8B008B; font-weight: bold">import</span> train_test_split
<span style="color: #228B22"># one-hot representation of labels</span>
labels = to_categorical(labels)
<span style="color: #228B22"># split into train and test data</span>
train_size = <span style="color: #B452CD">0.8</span>
test_size = <span style="color: #B452CD">1</span> - train_size
X_train, X_test, Y_train, Y_test = train_test_split(inputs, labels, train_size=train_size,
test_size=test_size)
</pre></div>
</section>
<section>
<h2 id="___sec52">Using TensorFlow backend </h2>
<ol>
<p><li> Define model and architecture</li>
<p><li> Choose cost function and optimizer</li>
</ol>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="font-size: 80%; line-height: 125%"><span></span><span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">tensorflow</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">tf</span>
<span style="color: #8B008B; font-weight: bold">class</span> <span style="color: #008b45; font-weight: bold">NeuralNetworkTensorflow</span>:
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">__init__</span>(
<span style="color: #658b00">self</span>,
X_train,
Y_train,
X_test,
Y_test,
n_neurons_layer1=<span style="color: #B452CD">100</span>,
n_neurons_layer2=<span style="color: #B452CD">50</span>,
n_categories=<span style="color: #B452CD">2</span>,
epochs=<span style="color: #B452CD">10</span>,
batch_size=<span style="color: #B452CD">100</span>,
eta=<span style="color: #B452CD">0.1</span>,
lmbd=<span style="color: #B452CD">0.0</span>,
):
<span style="color: #228B22"># keep track of number of steps</span>
<span style="color: #658b00">self</span>.global_step = tf.Variable(<span style="color: #B452CD">0</span>, dtype=tf.int32, trainable=<span style="color: #658b00">False</span>, name=<span style="color: #CD5555">&#39;global_step&#39;</span>)
<span style="color: #658b00">self</span>.X_train = X_train
<span style="color: #658b00">self</span>.Y_train = Y_train
<span style="color: #658b00">self</span>.X_test = X_test
<span style="color: #658b00">self</span>.Y_test = Y_test
<span style="color: #658b00">self</span>.n_inputs = X_train.shape[<span style="color: #B452CD">0</span>]
<span style="color: #658b00">self</span>.n_features = X_train.shape[<span style="color: #B452CD">1</span>]
<span style="color: #658b00">self</span>.n_neurons_layer1 = n_neurons_layer1
<span style="color: #658b00">self</span>.n_neurons_layer2 = n_neurons_layer2
<span style="color: #658b00">self</span>.n_categories = n_categories
<span style="color: #658b00">self</span>.epochs = epochs
<span style="color: #658b00">self</span>.batch_size = batch_size
<span style="color: #658b00">self</span>.iterations = <span style="color: #658b00">self</span>.n_inputs // <span style="color: #658b00">self</span>.batch_size
<span style="color: #658b00">self</span>.eta = eta
<span style="color: #658b00">self</span>.lmbd = lmbd
<span style="color: #228B22"># build network piece by piece</span>
<span style="color: #228B22"># name scopes (with) are used to enforce creation of new variables</span>
<span style="color: #228B22"># https://www.tensorflow.org/guide/variables</span>
<span style="color: #658b00">self</span>.create_placeholders()
<span style="color: #658b00">self</span>.create_DNN()
<span style="color: #658b00">self</span>.create_loss()
<span style="color: #658b00">self</span>.create_optimiser()
<span style="color: #658b00">self</span>.create_accuracy()
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">create_placeholders</span>(<span style="color: #658b00">self</span>):
<span style="color: #228B22"># placeholders are fine here, but &quot;Datasets&quot; are the preferred method</span>
<span style="color: #228B22"># of streaming data into a model</span>
<span style="color: #8B008B; font-weight: bold">with</span> tf.name_scope(<span style="color: #CD5555">&#39;data&#39;</span>):
<span style="color: #658b00">self</span>.X = tf.placeholder(tf.float32, shape=(<span style="color: #658b00">None</span>, <span style="color: #658b00">self</span>.n_features), name=<span style="color: #CD5555">&#39;X_data&#39;</span>)
<span style="color: #658b00">self</span>.Y = tf.placeholder(tf.float32, shape=(<span style="color: #658b00">None</span>, <span style="color: #658b00">self</span>.n_categories), name=<span style="color: #CD5555">&#39;Y_data&#39;</span>)
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">create_DNN</span>(<span style="color: #658b00">self</span>):
<span style="color: #8B008B; font-weight: bold">with</span> tf.name_scope(<span style="color: #CD5555">&#39;DNN&#39;</span>):
<span style="color: #228B22"># the weights are stored to calculate regularization loss later</span>
<span style="color: #228B22"># Fully connected layer 1</span>
<span style="color: #658b00">self</span>.W_fc1 = <span style="color: #658b00">self</span>.weight_variable([<span style="color: #658b00">self</span>.n_features, <span style="color: #658b00">self</span>.n_neurons_layer1], name=<span style="color: #CD5555">&#39;fc1&#39;</span>, dtype=tf.float32)
b_fc1 = <span style="color: #658b00">self</span>.bias_variable([<span style="color: #658b00">self</span>.n_neurons_layer1], name=<span style="color: #CD5555">&#39;fc1&#39;</span>, dtype=tf.float32)
a_fc1 = tf.nn.sigmoid(tf.matmul(<span style="color: #658b00">self</span>.X, <span style="color: #658b00">self</span>.W_fc1) + b_fc1)
<span style="color: #228B22"># Fully connected layer 2</span>
<span style="color: #658b00">self</span>.W_fc2 = <span style="color: #658b00">self</span>.weight_variable([<span style="color: #658b00">self</span>.n_neurons_layer1, <span style="color: #658b00">self</span>.n_neurons_layer2], name=<span style="color: #CD5555">&#39;fc2&#39;</span>, dtype=tf.float32)
b_fc2 = <span style="color: #658b00">self</span>.bias_variable([<span style="color: #658b00">self</span>.n_neurons_layer2], name=<span style="color: #CD5555">&#39;fc2&#39;</span>, dtype=tf.float32)
a_fc2 = tf.nn.sigmoid(tf.matmul(a_fc1, <span style="color: #658b00">self</span>.W_fc2) + b_fc2)
<span style="color: #228B22"># Output layer</span>
<span style="color: #658b00">self</span>.W_out = <span style="color: #658b00">self</span>.weight_variable([<span style="color: #658b00">self</span>.n_neurons_layer2, <span style="color: #658b00">self</span>.n_categories], name=<span style="color: #CD5555">&#39;out&#39;</span>, dtype=tf.float32)
b_out = <span style="color: #658b00">self</span>.bias_variable([<span style="color: #658b00">self</span>.n_categories], name=<span style="color: #CD5555">&#39;out&#39;</span>, dtype=tf.float32)
<span style="color: #658b00">self</span>.z_out = tf.matmul(a_fc2, <span style="color: #658b00">self</span>.W_out) + b_out
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">create_loss</span>(<span style="color: #658b00">self</span>):
<span style="color: #8B008B; font-weight: bold">with</span> tf.name_scope(<span style="color: #CD5555">&#39;loss&#39;</span>):
softmax_loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=<span style="color: #658b00">self</span>.Y, logits=<span style="color: #658b00">self</span>.z_out))
regularizer_loss_fc1 = tf.nn.l2_loss(<span style="color: #658b00">self</span>.W_fc1)
regularizer_loss_fc2 = tf.nn.l2_loss(<span style="color: #658b00">self</span>.W_fc2)
regularizer_loss_out = tf.nn.l2_loss(<span style="color: #658b00">self</span>.W_out)
regularizer_loss = <span style="color: #658b00">self</span>.lmbd*(regularizer_loss_fc1 + regularizer_loss_fc2 + regularizer_loss_out)
<span style="color: #658b00">self</span>.loss = softmax_loss + regularizer_loss
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">create_accuracy</span>(<span style="color: #658b00">self</span>):
<span style="color: #8B008B; font-weight: bold">with</span> tf.name_scope(<span style="color: #CD5555">&#39;accuracy&#39;</span>):
probabilities = tf.nn.softmax(<span style="color: #658b00">self</span>.z_out)
predictions = tf.argmax(probabilities, axis=<span style="color: #B452CD">1</span>)
labels = tf.argmax(<span style="color: #658b00">self</span>.Y, axis=<span style="color: #B452CD">1</span>)
correct_predictions = tf.equal(predictions, labels)
correct_predictions = tf.cast(correct_predictions, tf.float32)
<span style="color: #658b00">self</span>.accuracy = tf.reduce_mean(correct_predictions)
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">create_optimiser</span>(<span style="color: #658b00">self</span>):
<span style="color: #8B008B; font-weight: bold">with</span> tf.name_scope(<span style="color: #CD5555">&#39;optimizer&#39;</span>):
<span style="color: #658b00">self</span>.optimizer = tf.train.GradientDescentOptimizer(learning_rate=<span style="color: #658b00">self</span>.eta).minimize(<span style="color: #658b00">self</span>.loss, global_step=<span style="color: #658b00">self</span>.global_step)
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">weight_variable</span>(<span style="color: #658b00">self</span>, shape, name=<span style="color: #CD5555">&#39;&#39;</span>, dtype=tf.float32):
initial = tf.truncated_normal(shape, stddev=<span style="color: #B452CD">0.1</span>)
<span style="color: #8B008B; font-weight: bold">return</span> tf.Variable(initial, name=name, dtype=dtype)
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">bias_variable</span>(<span style="color: #658b00">self</span>, shape, name=<span style="color: #CD5555">&#39;&#39;</span>, dtype=tf.float32):
initial = tf.constant(<span style="color: #B452CD">0.1</span>, shape=shape)
<span style="color: #8B008B; font-weight: bold">return</span> tf.Variable(initial, name=name, dtype=dtype)
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">fit</span>(<span style="color: #658b00">self</span>):
data_indices = np.arange(<span style="color: #658b00">self</span>.n_inputs)
<span style="color: #8B008B; font-weight: bold">with</span> tf.Session() <span style="color: #8B008B; font-weight: bold">as</span> sess:
sess.run(tf.global_variables_initializer())
<span style="color: #8B008B; font-weight: bold">for</span> i <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(<span style="color: #658b00">self</span>.epochs):
<span style="color: #8B008B; font-weight: bold">for</span> j <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(<span style="color: #658b00">self</span>.iterations):
chosen_datapoints = np.random.choice(data_indices, size=<span style="color: #658b00">self</span>.batch_size, replace=<span style="color: #658b00">False</span>)
batch_X, batch_Y = <span style="color: #658b00">self</span>.X_train[chosen_datapoints], <span style="color: #658b00">self</span>.Y_train[chosen_datapoints]
sess.run([DNN.loss, DNN.optimizer],
feed_dict={DNN.X: batch_X,
DNN.Y: batch_Y})
accuracy = sess.run(DNN.accuracy,
feed_dict={DNN.X: batch_X,
DNN.Y: batch_Y})
step = sess.run(DNN.global_step)
<span style="color: #658b00">self</span>.train_loss, <span style="color: #658b00">self</span>.train_accuracy = sess.run([DNN.loss, DNN.accuracy],
feed_dict={DNN.X: <span style="color: #658b00">self</span>.X_train,
DNN.Y: <span style="color: #658b00">self</span>.Y_train})
<span style="color: #658b00">self</span>.test_loss, <span style="color: #658b00">self</span>.test_accuracy = sess.run([DNN.loss, DNN.accuracy],
feed_dict={DNN.X: <span style="color: #658b00">self</span>.X_test,
DNN.Y: <span style="color: #658b00">self</span>.Y_test})
</pre></div>
</section>
<section>
<h2 id="___sec53">Optimizing and using gradient descent </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="font-size: 80%; line-height: 125%"><span></span>epochs = <span style="color: #B452CD">100</span>
batch_size = <span style="color: #B452CD">100</span>
n_neurons_layer1 = <span style="color: #B452CD">100</span>
n_neurons_layer2 = <span style="color: #B452CD">50</span>
n_categories = <span style="color: #B452CD">10</span>
eta_vals = np.logspace(-<span style="color: #B452CD">5</span>, <span style="color: #B452CD">1</span>, <span style="color: #B452CD">7</span>)
lmbd_vals = np.logspace(-<span style="color: #B452CD">5</span>, <span style="color: #B452CD">1</span>, <span style="color: #B452CD">7</span>)
</pre></div>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="font-size: 80%; line-height: 125%"><span></span>DNN_tf = np.zeros((<span style="color: #658b00">len</span>(eta_vals), <span style="color: #658b00">len</span>(lmbd_vals)), dtype=<span style="color: #658b00">object</span>)
<span style="color: #8B008B; font-weight: bold">for</span> i, eta <span style="color: #8B008B">in</span> <span style="color: #658b00">enumerate</span>(eta_vals):
<span style="color: #8B008B; font-weight: bold">for</span> j, lmbd <span style="color: #8B008B">in</span> <span style="color: #658b00">enumerate</span>(lmbd_vals):
DNN = NeuralNetworkTensorflow(X_train, Y_train, X_test, Y_test,
n_neurons_layer1, n_neurons_layer2, n_categories,
epochs=epochs, batch_size=batch_size, eta=eta, lmbd=lmbd)
DNN.fit()
DNN_tf[i][j] = DNN
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&quot;Learning rate = &quot;</span>, eta)
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&quot;Lambda = &quot;</span>, lmbd)
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&quot;Test accuracy: %.3f&quot;</span> % DNN.test_accuracy)
<span style="color: #8B008B; font-weight: bold">print</span>()
</pre></div>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="font-size: 80%; line-height: 125%"><span></span><span style="color: #228B22"># optional</span>
<span style="color: #228B22"># visual representation of grid search</span>
<span style="color: #228B22"># uses seaborn heatmap, could probably do this in matplotlib</span>
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">seaborn</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">sns</span>
sns.set()
train_accuracy = np.zeros((<span style="color: #658b00">len</span>(eta_vals), <span style="color: #658b00">len</span>(lmbd_vals)))
test_accuracy = np.zeros((<span style="color: #658b00">len</span>(eta_vals), <span style="color: #658b00">len</span>(lmbd_vals)))
<span style="color: #8B008B; font-weight: bold">for</span> i <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(<span style="color: #658b00">len</span>(eta_vals)):
<span style="color: #8B008B; font-weight: bold">for</span> j <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(<span style="color: #658b00">len</span>(lmbd_vals)):
DNN = DNN_tf[i][j]
train_accuracy[i][j] = DNN.train_accuracy
test_accuracy[i][j] = DNN.test_accuracy
fig, ax = plt.subplots(figsize = (<span style="color: #B452CD">10</span>, <span style="color: #B452CD">10</span>))
sns.heatmap(train_accuracy, annot=<span style="color: #658b00">True</span>, ax=ax, cmap=<span style="color: #CD5555">&quot;viridis&quot;</span>)
ax.set_title(<span style="color: #CD5555">&quot;Training Accuracy&quot;</span>)
ax.set_ylabel(<span style="color: #CD5555">&quot;$\eta$&quot;</span>)
ax.set_xlabel(<span style="color: #CD5555">&quot;$\lambda$&quot;</span>)
plt.show()
fig, ax = plt.subplots(figsize = (<span style="color: #B452CD">10</span>, <span style="color: #B452CD">10</span>))
sns.heatmap(test_accuracy, annot=<span style="color: #658b00">True</span>, ax=ax, cmap=<span style="color: #CD5555">&quot;viridis&quot;</span>)
ax.set_title(<span style="color: #CD5555">&quot;Test Accuracy&quot;</span>)
ax.set_ylabel(<span style="color: #CD5555">&quot;$\eta$&quot;</span>)
ax.set_xlabel(<span style="color: #CD5555">&quot;$\lambda$&quot;</span>)
plt.show()
</pre></div>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="font-size: 80%; line-height: 125%"><span></span><span style="color: #228B22"># optional</span>
<span style="color: #228B22"># we can use log files to visualize our graph in Tensorboard</span>
writer = tf.summary.FileWriter(<span style="color: #CD5555">&#39;logs/&#39;</span>)
writer.add_graph(tf.get_default_graph())
</pre></div>
</section>
<section>
<h2 id="___sec54">Using Keras </h2>
<p>
Keras is a high level <a href="https://en.wikipedia.org/wiki/Application_programming_interface" target="_blank">neural network</a>
that supports Tensorflow, CTNK and Theano as backends.
If you have Tensorflow installed Keras is available through the <em>tf.keras</em> module.
If you have Anaconda installed you may run the following command
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="font-size: 80%; line-height: 125%"><span></span>conda install keras
</pre></div>
<p>
Alternatively, if you have Tensorflow or one of the other supported backends install you may use the pip package manager:
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="font-size: 80%; line-height: 125%"><span></span>pip3 install keras
</pre></div>
<p>
or look up the <a href="https://keras.io/" target="_blank">instructions here</a>.
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="font-size: 80%; line-height: 125%"><span></span><span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">keras.models</span> <span style="color: #8B008B; font-weight: bold">import</span> Sequential
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">keras.layers</span> <span style="color: #8B008B; font-weight: bold">import</span> Dense
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">keras.regularizers</span> <span style="color: #8B008B; font-weight: bold">import</span> l2
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">keras.optimizers</span> <span style="color: #8B008B; font-weight: bold">import</span> SGD
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">create_neural_network_keras</span>(n_neurons_layer1, n_neurons_layer2, n_categories, eta, lmbd):
model = Sequential()
model.add(Dense(n_neurons_layer1, activation=<span style="color: #CD5555">&#39;sigmoid&#39;</span>, kernel_regularizer=l2(lmbd)))
model.add(Dense(n_neurons_layer2, activation=<span style="color: #CD5555">&#39;sigmoid&#39;</span>, kernel_regularizer=l2(lmbd)))
model.add(Dense(n_categories, activation=<span style="color: #CD5555">&#39;softmax&#39;</span>))
sgd = SGD(lr=eta)
model.compile(loss=<span style="color: #CD5555">&#39;categorical_crossentropy&#39;</span>, optimizer=sgd, metrics=[<span style="color: #CD5555">&#39;accuracy&#39;</span>])
<span style="color: #8B008B; font-weight: bold">return</span> model
</pre></div>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="font-size: 80%; line-height: 125%"><span></span>DNN_keras = np.zeros((<span style="color: #658b00">len</span>(eta_vals), <span style="color: #658b00">len</span>(lmbd_vals)), dtype=<span style="color: #658b00">object</span>)
<span style="color: #8B008B; font-weight: bold">for</span> i, eta <span style="color: #8B008B">in</span> <span style="color: #658b00">enumerate</span>(eta_vals):
<span style="color: #8B008B; font-weight: bold">for</span> j, lmbd <span style="color: #8B008B">in</span> <span style="color: #658b00">enumerate</span>(lmbd_vals):
DNN = create_neural_network_keras(n_neurons_layer1, n_neurons_layer2, n_categories,
eta=eta, lmbd=lmbd)
DNN.fit(X_train, Y_train, epochs=epochs, batch_size=batch_size, verbose=<span style="color: #B452CD">0</span>)
scores = DNN.evaluate(X_test, Y_test)
DNN_keras[i][j] = DNN
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&quot;Learning rate = &quot;</span>, eta)
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&quot;Lambda = &quot;</span>, lmbd)
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&quot;Test accuracy: %.3f&quot;</span> % scores[<span style="color: #B452CD">1</span>])
<span style="color: #8B008B; font-weight: bold">print</span>()
</pre></div>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="font-size: 80%; line-height: 125%"><span></span><span style="color: #228B22"># optional</span>
<span style="color: #228B22"># visual representation of grid search</span>
<span style="color: #228B22"># uses seaborn heatmap, could probably do this in matplotlib</span>
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">seaborn</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">sns</span>
sns.set()
train_accuracy = np.zeros((<span style="color: #658b00">len</span>(eta_vals), <span style="color: #658b00">len</span>(lmbd_vals)))
test_accuracy = np.zeros((<span style="color: #658b00">len</span>(eta_vals), <span style="color: #658b00">len</span>(lmbd_vals)))
<span style="color: #8B008B; font-weight: bold">for</span> i <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(<span style="color: #658b00">len</span>(eta_vals)):
<span style="color: #8B008B; font-weight: bold">for</span> j <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(<span style="color: #658b00">len</span>(lmbd_vals)):
DNN = DNN_keras[i][j]
train_accuracy[i][j] = DNN.evaluate(X_train, Y_train)[<span style="color: #B452CD">1</span>]
test_accuracy[i][j] = DNN.evaluate(X_test, Y_test)[<span style="color: #B452CD">1</span>]
fig, ax = plt.subplots(figsize = (<span style="color: #B452CD">10</span>, <span style="color: #B452CD">10</span>))
sns.heatmap(train_accuracy, annot=<span style="color: #658b00">True</span>, ax=ax, cmap=<span style="color: #CD5555">&quot;viridis&quot;</span>)
ax.set_title(<span style="color: #CD5555">&quot;Training Accuracy&quot;</span>)
ax.set_ylabel(<span style="color: #CD5555">&quot;$\eta$&quot;</span>)
ax.set_xlabel(<span style="color: #CD5555">&quot;$\lambda$&quot;</span>)
plt.show()
fig, ax = plt.subplots(figsize = (<span style="color: #B452CD">10</span>, <span style="color: #B452CD">10</span>))
sns.heatmap(test_accuracy, annot=<span style="color: #658b00">True</span>, ax=ax, cmap=<span style="color: #CD5555">&quot;viridis&quot;</span>)
ax.set_title(<span style="color: #CD5555">&quot;Test Accuracy&quot;</span>)
ax.set_ylabel(<span style="color: #CD5555">&quot;$\eta$&quot;</span>)
ax.set_xlabel(<span style="color: #CD5555">&quot;$\lambda$&quot;</span>)
plt.show()
</pre></div>
</section>
+455 -2
View File
@@ -135,7 +135,15 @@ div { text-align: justify; text-justify: inter-word; }
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -2175,8 +2183,453 @@ plt.show()
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec49">And then with Tensorflow </h2>
<h2 id="___sec49">Building neural networks in Tensorflow and Keras </h2>
<p>
Now we want to build on the experience gained from our neural network implementation in NumPy and scikit-learn
and use it to construct a neural network in Tensorflow. Once we have constructed a neural network in NumPy
and Tensorflow, building one in Keras is really quite trivial, though the performance may suffer.
<p>
In our previous example we used only one hidden layer, and in this we will use two. From this it should be quite
clear how to build one using an arbitrary number of hidden layers, using data structures such as Python lists or
NumPy arrays.
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec50">Tensorflow </h2>
<p>
Tensorflow is an open source library machine learning library
developed by the Google Brain team for internal use. It was released
under the Apache 2.0 open source license in November 9, 2015.
<p>
Tensorflow is a computational framework that allows you to construct
machine learning models at different levels of abstraction, from
high-level, object-oriented APIs like Keras, down to the C++ kernels
that Tensorflow is built upon. The higher levels of abstraction are
simpler to use, but less flexible, and our choice of implementation
should reflect the problems we are trying to solve.
<p>
<a href="https://www.tensorflow.org/guide/graphs" target="_blank">Tensorflow uses</a> so-called graphs to represent your computation
in terms of the dependencies between individual operations, such that you first build a Tensorflow <em>graph</em>
to represent your model, and then create a Tensorflow <em>session</em> to run the graph.
<p>
In this guide we will analyze the same data as we did in our NumPy and
scikit-learn tutorial, gathered from the MNIST database of images. We
will give an introduction to the lower level Python Application
Program Interfaces (APIs), and see how we use them to build our graph.
Then we will build (effectively) the same graph in Keras, to see just
how simple solving a machine learning problem can be.
<p>
To install tensorflow on Unix/Linux systems, use pip as
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span>pip3 install tensorflow
</pre></div>
<p>
and/or if you use <b>anaconda</b>, just write (or install from the graphical user interface)
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span>conda install tensorflow
</pre></div>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec51">Collect and pre-process data </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span><span style="color: #228B22"># import necessary packages</span>
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">numpy</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">np</span>
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">matplotlib.pyplot</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">plt</span>
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn</span> <span style="color: #8B008B; font-weight: bold">import</span> datasets
<span style="color: #228B22"># ensure the same random numbers appear every time</span>
np.random.seed(<span style="color: #B452CD">0</span>)
<span style="color: #228B22"># display images in notebook</span>
%matplotlib inline
plt.rcParams[<span style="color: #CD5555">&#39;figure.figsize&#39;</span>] = (<span style="color: #B452CD">12</span>,<span style="color: #B452CD">12</span>)
<span style="color: #228B22"># download MNIST dataset</span>
digits = datasets.load_digits()
<span style="color: #228B22"># define inputs and labels</span>
inputs = digits.images
labels = digits.target
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&quot;inputs = (n_inputs, pixel_width, pixel_height) = &quot;</span> + <span style="color: #658b00">str</span>(inputs.shape))
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&quot;labels = (n_inputs) = &quot;</span> + <span style="color: #658b00">str</span>(labels.shape))
<span style="color: #228B22"># flatten the image</span>
<span style="color: #228B22"># the value -1 means dimension is inferred from the remaining dimensions: 8x8 = 64</span>
n_inputs = <span style="color: #658b00">len</span>(inputs)
inputs = inputs.reshape(n_inputs, -<span style="color: #B452CD">1</span>)
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&quot;X = (n_inputs, n_features) = &quot;</span> + <span style="color: #658b00">str</span>(inputs.shape))
<span style="color: #228B22"># choose some random images to display</span>
indices = np.arange(n_inputs)
random_indices = np.random.choice(indices, size=<span style="color: #B452CD">5</span>)
<span style="color: #8B008B; font-weight: bold">for</span> i, image <span style="color: #8B008B">in</span> <span style="color: #658b00">enumerate</span>(digits.images[random_indices]):
plt.subplot(<span style="color: #B452CD">1</span>, <span style="color: #B452CD">5</span>, i+<span style="color: #B452CD">1</span>)
plt.axis(<span style="color: #CD5555">&#39;off&#39;</span>)
plt.imshow(image, cmap=plt.cm.gray_r, interpolation=<span style="color: #CD5555">&#39;nearest&#39;</span>)
plt.title(<span style="color: #CD5555">&quot;Label: %d&quot;</span> % digits.target[random_indices[i]])
plt.show()
</pre></div>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span><span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">keras.utils</span> <span style="color: #8B008B; font-weight: bold">import</span> to_categorical
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.model_selection</span> <span style="color: #8B008B; font-weight: bold">import</span> train_test_split
<span style="color: #228B22"># one-hot representation of labels</span>
labels = to_categorical(labels)
<span style="color: #228B22"># split into train and test data</span>
train_size = <span style="color: #B452CD">0.8</span>
test_size = <span style="color: #B452CD">1</span> - train_size
X_train, X_test, Y_train, Y_test = train_test_split(inputs, labels, train_size=train_size,
test_size=test_size)
</pre></div>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec52">Using TensorFlow backend </h2>
<ol>
<li> Define model and architecture</li>
<li> Choose cost function and optimizer</li>
</ol>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span><span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">tensorflow</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">tf</span>
<span style="color: #8B008B; font-weight: bold">class</span> <span style="color: #008b45; font-weight: bold">NeuralNetworkTensorflow</span>:
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">__init__</span>(
<span style="color: #658b00">self</span>,
X_train,
Y_train,
X_test,
Y_test,
n_neurons_layer1=<span style="color: #B452CD">100</span>,
n_neurons_layer2=<span style="color: #B452CD">50</span>,
n_categories=<span style="color: #B452CD">2</span>,
epochs=<span style="color: #B452CD">10</span>,
batch_size=<span style="color: #B452CD">100</span>,
eta=<span style="color: #B452CD">0.1</span>,
lmbd=<span style="color: #B452CD">0.0</span>,
):
<span style="color: #228B22"># keep track of number of steps</span>
<span style="color: #658b00">self</span>.global_step = tf.Variable(<span style="color: #B452CD">0</span>, dtype=tf.int32, trainable=<span style="color: #658b00">False</span>, name=<span style="color: #CD5555">&#39;global_step&#39;</span>)
<span style="color: #658b00">self</span>.X_train = X_train
<span style="color: #658b00">self</span>.Y_train = Y_train
<span style="color: #658b00">self</span>.X_test = X_test
<span style="color: #658b00">self</span>.Y_test = Y_test
<span style="color: #658b00">self</span>.n_inputs = X_train.shape[<span style="color: #B452CD">0</span>]
<span style="color: #658b00">self</span>.n_features = X_train.shape[<span style="color: #B452CD">1</span>]
<span style="color: #658b00">self</span>.n_neurons_layer1 = n_neurons_layer1
<span style="color: #658b00">self</span>.n_neurons_layer2 = n_neurons_layer2
<span style="color: #658b00">self</span>.n_categories = n_categories
<span style="color: #658b00">self</span>.epochs = epochs
<span style="color: #658b00">self</span>.batch_size = batch_size
<span style="color: #658b00">self</span>.iterations = <span style="color: #658b00">self</span>.n_inputs // <span style="color: #658b00">self</span>.batch_size
<span style="color: #658b00">self</span>.eta = eta
<span style="color: #658b00">self</span>.lmbd = lmbd
<span style="color: #228B22"># build network piece by piece</span>
<span style="color: #228B22"># name scopes (with) are used to enforce creation of new variables</span>
<span style="color: #228B22"># https://www.tensorflow.org/guide/variables</span>
<span style="color: #658b00">self</span>.create_placeholders()
<span style="color: #658b00">self</span>.create_DNN()
<span style="color: #658b00">self</span>.create_loss()
<span style="color: #658b00">self</span>.create_optimiser()
<span style="color: #658b00">self</span>.create_accuracy()
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">create_placeholders</span>(<span style="color: #658b00">self</span>):
<span style="color: #228B22"># placeholders are fine here, but &quot;Datasets&quot; are the preferred method</span>
<span style="color: #228B22"># of streaming data into a model</span>
<span style="color: #8B008B; font-weight: bold">with</span> tf.name_scope(<span style="color: #CD5555">&#39;data&#39;</span>):
<span style="color: #658b00">self</span>.X = tf.placeholder(tf.float32, shape=(<span style="color: #658b00">None</span>, <span style="color: #658b00">self</span>.n_features), name=<span style="color: #CD5555">&#39;X_data&#39;</span>)
<span style="color: #658b00">self</span>.Y = tf.placeholder(tf.float32, shape=(<span style="color: #658b00">None</span>, <span style="color: #658b00">self</span>.n_categories), name=<span style="color: #CD5555">&#39;Y_data&#39;</span>)
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">create_DNN</span>(<span style="color: #658b00">self</span>):
<span style="color: #8B008B; font-weight: bold">with</span> tf.name_scope(<span style="color: #CD5555">&#39;DNN&#39;</span>):
<span style="color: #228B22"># the weights are stored to calculate regularization loss later</span>
<span style="color: #228B22"># Fully connected layer 1</span>
<span style="color: #658b00">self</span>.W_fc1 = <span style="color: #658b00">self</span>.weight_variable([<span style="color: #658b00">self</span>.n_features, <span style="color: #658b00">self</span>.n_neurons_layer1], name=<span style="color: #CD5555">&#39;fc1&#39;</span>, dtype=tf.float32)
b_fc1 = <span style="color: #658b00">self</span>.bias_variable([<span style="color: #658b00">self</span>.n_neurons_layer1], name=<span style="color: #CD5555">&#39;fc1&#39;</span>, dtype=tf.float32)
a_fc1 = tf.nn.sigmoid(tf.matmul(<span style="color: #658b00">self</span>.X, <span style="color: #658b00">self</span>.W_fc1) + b_fc1)
<span style="color: #228B22"># Fully connected layer 2</span>
<span style="color: #658b00">self</span>.W_fc2 = <span style="color: #658b00">self</span>.weight_variable([<span style="color: #658b00">self</span>.n_neurons_layer1, <span style="color: #658b00">self</span>.n_neurons_layer2], name=<span style="color: #CD5555">&#39;fc2&#39;</span>, dtype=tf.float32)
b_fc2 = <span style="color: #658b00">self</span>.bias_variable([<span style="color: #658b00">self</span>.n_neurons_layer2], name=<span style="color: #CD5555">&#39;fc2&#39;</span>, dtype=tf.float32)
a_fc2 = tf.nn.sigmoid(tf.matmul(a_fc1, <span style="color: #658b00">self</span>.W_fc2) + b_fc2)
<span style="color: #228B22"># Output layer</span>
<span style="color: #658b00">self</span>.W_out = <span style="color: #658b00">self</span>.weight_variable([<span style="color: #658b00">self</span>.n_neurons_layer2, <span style="color: #658b00">self</span>.n_categories], name=<span style="color: #CD5555">&#39;out&#39;</span>, dtype=tf.float32)
b_out = <span style="color: #658b00">self</span>.bias_variable([<span style="color: #658b00">self</span>.n_categories], name=<span style="color: #CD5555">&#39;out&#39;</span>, dtype=tf.float32)
<span style="color: #658b00">self</span>.z_out = tf.matmul(a_fc2, <span style="color: #658b00">self</span>.W_out) + b_out
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">create_loss</span>(<span style="color: #658b00">self</span>):
<span style="color: #8B008B; font-weight: bold">with</span> tf.name_scope(<span style="color: #CD5555">&#39;loss&#39;</span>):
softmax_loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=<span style="color: #658b00">self</span>.Y, logits=<span style="color: #658b00">self</span>.z_out))
regularizer_loss_fc1 = tf.nn.l2_loss(<span style="color: #658b00">self</span>.W_fc1)
regularizer_loss_fc2 = tf.nn.l2_loss(<span style="color: #658b00">self</span>.W_fc2)
regularizer_loss_out = tf.nn.l2_loss(<span style="color: #658b00">self</span>.W_out)
regularizer_loss = <span style="color: #658b00">self</span>.lmbd*(regularizer_loss_fc1 + regularizer_loss_fc2 + regularizer_loss_out)
<span style="color: #658b00">self</span>.loss = softmax_loss + regularizer_loss
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">create_accuracy</span>(<span style="color: #658b00">self</span>):
<span style="color: #8B008B; font-weight: bold">with</span> tf.name_scope(<span style="color: #CD5555">&#39;accuracy&#39;</span>):
probabilities = tf.nn.softmax(<span style="color: #658b00">self</span>.z_out)
predictions = tf.argmax(probabilities, axis=<span style="color: #B452CD">1</span>)
labels = tf.argmax(<span style="color: #658b00">self</span>.Y, axis=<span style="color: #B452CD">1</span>)
correct_predictions = tf.equal(predictions, labels)
correct_predictions = tf.cast(correct_predictions, tf.float32)
<span style="color: #658b00">self</span>.accuracy = tf.reduce_mean(correct_predictions)
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">create_optimiser</span>(<span style="color: #658b00">self</span>):
<span style="color: #8B008B; font-weight: bold">with</span> tf.name_scope(<span style="color: #CD5555">&#39;optimizer&#39;</span>):
<span style="color: #658b00">self</span>.optimizer = tf.train.GradientDescentOptimizer(learning_rate=<span style="color: #658b00">self</span>.eta).minimize(<span style="color: #658b00">self</span>.loss, global_step=<span style="color: #658b00">self</span>.global_step)
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">weight_variable</span>(<span style="color: #658b00">self</span>, shape, name=<span style="color: #CD5555">&#39;&#39;</span>, dtype=tf.float32):
initial = tf.truncated_normal(shape, stddev=<span style="color: #B452CD">0.1</span>)
<span style="color: #8B008B; font-weight: bold">return</span> tf.Variable(initial, name=name, dtype=dtype)
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">bias_variable</span>(<span style="color: #658b00">self</span>, shape, name=<span style="color: #CD5555">&#39;&#39;</span>, dtype=tf.float32):
initial = tf.constant(<span style="color: #B452CD">0.1</span>, shape=shape)
<span style="color: #8B008B; font-weight: bold">return</span> tf.Variable(initial, name=name, dtype=dtype)
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">fit</span>(<span style="color: #658b00">self</span>):
data_indices = np.arange(<span style="color: #658b00">self</span>.n_inputs)
<span style="color: #8B008B; font-weight: bold">with</span> tf.Session() <span style="color: #8B008B; font-weight: bold">as</span> sess:
sess.run(tf.global_variables_initializer())
<span style="color: #8B008B; font-weight: bold">for</span> i <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(<span style="color: #658b00">self</span>.epochs):
<span style="color: #8B008B; font-weight: bold">for</span> j <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(<span style="color: #658b00">self</span>.iterations):
chosen_datapoints = np.random.choice(data_indices, size=<span style="color: #658b00">self</span>.batch_size, replace=<span style="color: #658b00">False</span>)
batch_X, batch_Y = <span style="color: #658b00">self</span>.X_train[chosen_datapoints], <span style="color: #658b00">self</span>.Y_train[chosen_datapoints]
sess.run([DNN.loss, DNN.optimizer],
feed_dict={DNN.X: batch_X,
DNN.Y: batch_Y})
accuracy = sess.run(DNN.accuracy,
feed_dict={DNN.X: batch_X,
DNN.Y: batch_Y})
step = sess.run(DNN.global_step)
<span style="color: #658b00">self</span>.train_loss, <span style="color: #658b00">self</span>.train_accuracy = sess.run([DNN.loss, DNN.accuracy],
feed_dict={DNN.X: <span style="color: #658b00">self</span>.X_train,
DNN.Y: <span style="color: #658b00">self</span>.Y_train})
<span style="color: #658b00">self</span>.test_loss, <span style="color: #658b00">self</span>.test_accuracy = sess.run([DNN.loss, DNN.accuracy],
feed_dict={DNN.X: <span style="color: #658b00">self</span>.X_test,
DNN.Y: <span style="color: #658b00">self</span>.Y_test})
</pre></div>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec53">Optimizing and using gradient descent </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span>epochs = <span style="color: #B452CD">100</span>
batch_size = <span style="color: #B452CD">100</span>
n_neurons_layer1 = <span style="color: #B452CD">100</span>
n_neurons_layer2 = <span style="color: #B452CD">50</span>
n_categories = <span style="color: #B452CD">10</span>
eta_vals = np.logspace(-<span style="color: #B452CD">5</span>, <span style="color: #B452CD">1</span>, <span style="color: #B452CD">7</span>)
lmbd_vals = np.logspace(-<span style="color: #B452CD">5</span>, <span style="color: #B452CD">1</span>, <span style="color: #B452CD">7</span>)
</pre></div>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span>DNN_tf = np.zeros((<span style="color: #658b00">len</span>(eta_vals), <span style="color: #658b00">len</span>(lmbd_vals)), dtype=<span style="color: #658b00">object</span>)
<span style="color: #8B008B; font-weight: bold">for</span> i, eta <span style="color: #8B008B">in</span> <span style="color: #658b00">enumerate</span>(eta_vals):
<span style="color: #8B008B; font-weight: bold">for</span> j, lmbd <span style="color: #8B008B">in</span> <span style="color: #658b00">enumerate</span>(lmbd_vals):
DNN = NeuralNetworkTensorflow(X_train, Y_train, X_test, Y_test,
n_neurons_layer1, n_neurons_layer2, n_categories,
epochs=epochs, batch_size=batch_size, eta=eta, lmbd=lmbd)
DNN.fit()
DNN_tf[i][j] = DNN
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&quot;Learning rate = &quot;</span>, eta)
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&quot;Lambda = &quot;</span>, lmbd)
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&quot;Test accuracy: %.3f&quot;</span> % DNN.test_accuracy)
<span style="color: #8B008B; font-weight: bold">print</span>()
</pre></div>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span><span style="color: #228B22"># optional</span>
<span style="color: #228B22"># visual representation of grid search</span>
<span style="color: #228B22"># uses seaborn heatmap, could probably do this in matplotlib</span>
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">seaborn</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">sns</span>
sns.set()
train_accuracy = np.zeros((<span style="color: #658b00">len</span>(eta_vals), <span style="color: #658b00">len</span>(lmbd_vals)))
test_accuracy = np.zeros((<span style="color: #658b00">len</span>(eta_vals), <span style="color: #658b00">len</span>(lmbd_vals)))
<span style="color: #8B008B; font-weight: bold">for</span> i <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(<span style="color: #658b00">len</span>(eta_vals)):
<span style="color: #8B008B; font-weight: bold">for</span> j <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(<span style="color: #658b00">len</span>(lmbd_vals)):
DNN = DNN_tf[i][j]
train_accuracy[i][j] = DNN.train_accuracy
test_accuracy[i][j] = DNN.test_accuracy
fig, ax = plt.subplots(figsize = (<span style="color: #B452CD">10</span>, <span style="color: #B452CD">10</span>))
sns.heatmap(train_accuracy, annot=<span style="color: #658b00">True</span>, ax=ax, cmap=<span style="color: #CD5555">&quot;viridis&quot;</span>)
ax.set_title(<span style="color: #CD5555">&quot;Training Accuracy&quot;</span>)
ax.set_ylabel(<span style="color: #CD5555">&quot;$\eta$&quot;</span>)
ax.set_xlabel(<span style="color: #CD5555">&quot;$\lambda$&quot;</span>)
plt.show()
fig, ax = plt.subplots(figsize = (<span style="color: #B452CD">10</span>, <span style="color: #B452CD">10</span>))
sns.heatmap(test_accuracy, annot=<span style="color: #658b00">True</span>, ax=ax, cmap=<span style="color: #CD5555">&quot;viridis&quot;</span>)
ax.set_title(<span style="color: #CD5555">&quot;Test Accuracy&quot;</span>)
ax.set_ylabel(<span style="color: #CD5555">&quot;$\eta$&quot;</span>)
ax.set_xlabel(<span style="color: #CD5555">&quot;$\lambda$&quot;</span>)
plt.show()
</pre></div>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span><span style="color: #228B22"># optional</span>
<span style="color: #228B22"># we can use log files to visualize our graph in Tensorboard</span>
writer = tf.summary.FileWriter(<span style="color: #CD5555">&#39;logs/&#39;</span>)
writer.add_graph(tf.get_default_graph())
</pre></div>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec54">Using Keras </h2>
<p>
Keras is a high level <a href="https://en.wikipedia.org/wiki/Application_programming_interface" target="_blank">neural network</a>
that supports Tensorflow, CTNK and Theano as backends.
If you have Tensorflow installed Keras is available through the <em>tf.keras</em> module.
If you have Anaconda installed you may run the following command
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span>conda install keras
</pre></div>
<p>
Alternatively, if you have Tensorflow or one of the other supported backends install you may use the pip package manager:
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span>pip3 install keras
</pre></div>
<p>
or look up the <a href="https://keras.io/" target="_blank">instructions here</a>.
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span><span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">keras.models</span> <span style="color: #8B008B; font-weight: bold">import</span> Sequential
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">keras.layers</span> <span style="color: #8B008B; font-weight: bold">import</span> Dense
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">keras.regularizers</span> <span style="color: #8B008B; font-weight: bold">import</span> l2
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">keras.optimizers</span> <span style="color: #8B008B; font-weight: bold">import</span> SGD
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">create_neural_network_keras</span>(n_neurons_layer1, n_neurons_layer2, n_categories, eta, lmbd):
model = Sequential()
model.add(Dense(n_neurons_layer1, activation=<span style="color: #CD5555">&#39;sigmoid&#39;</span>, kernel_regularizer=l2(lmbd)))
model.add(Dense(n_neurons_layer2, activation=<span style="color: #CD5555">&#39;sigmoid&#39;</span>, kernel_regularizer=l2(lmbd)))
model.add(Dense(n_categories, activation=<span style="color: #CD5555">&#39;softmax&#39;</span>))
sgd = SGD(lr=eta)
model.compile(loss=<span style="color: #CD5555">&#39;categorical_crossentropy&#39;</span>, optimizer=sgd, metrics=[<span style="color: #CD5555">&#39;accuracy&#39;</span>])
<span style="color: #8B008B; font-weight: bold">return</span> model
</pre></div>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span>DNN_keras = np.zeros((<span style="color: #658b00">len</span>(eta_vals), <span style="color: #658b00">len</span>(lmbd_vals)), dtype=<span style="color: #658b00">object</span>)
<span style="color: #8B008B; font-weight: bold">for</span> i, eta <span style="color: #8B008B">in</span> <span style="color: #658b00">enumerate</span>(eta_vals):
<span style="color: #8B008B; font-weight: bold">for</span> j, lmbd <span style="color: #8B008B">in</span> <span style="color: #658b00">enumerate</span>(lmbd_vals):
DNN = create_neural_network_keras(n_neurons_layer1, n_neurons_layer2, n_categories,
eta=eta, lmbd=lmbd)
DNN.fit(X_train, Y_train, epochs=epochs, batch_size=batch_size, verbose=<span style="color: #B452CD">0</span>)
scores = DNN.evaluate(X_test, Y_test)
DNN_keras[i][j] = DNN
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&quot;Learning rate = &quot;</span>, eta)
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&quot;Lambda = &quot;</span>, lmbd)
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&quot;Test accuracy: %.3f&quot;</span> % scores[<span style="color: #B452CD">1</span>])
<span style="color: #8B008B; font-weight: bold">print</span>()
</pre></div>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span><span style="color: #228B22"># optional</span>
<span style="color: #228B22"># visual representation of grid search</span>
<span style="color: #228B22"># uses seaborn heatmap, could probably do this in matplotlib</span>
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">seaborn</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">sns</span>
sns.set()
train_accuracy = np.zeros((<span style="color: #658b00">len</span>(eta_vals), <span style="color: #658b00">len</span>(lmbd_vals)))
test_accuracy = np.zeros((<span style="color: #658b00">len</span>(eta_vals), <span style="color: #658b00">len</span>(lmbd_vals)))
<span style="color: #8B008B; font-weight: bold">for</span> i <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(<span style="color: #658b00">len</span>(eta_vals)):
<span style="color: #8B008B; font-weight: bold">for</span> j <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(<span style="color: #658b00">len</span>(lmbd_vals)):
DNN = DNN_keras[i][j]
train_accuracy[i][j] = DNN.evaluate(X_train, Y_train)[<span style="color: #B452CD">1</span>]
test_accuracy[i][j] = DNN.evaluate(X_test, Y_test)[<span style="color: #B452CD">1</span>]
fig, ax = plt.subplots(figsize = (<span style="color: #B452CD">10</span>, <span style="color: #B452CD">10</span>))
sns.heatmap(train_accuracy, annot=<span style="color: #658b00">True</span>, ax=ax, cmap=<span style="color: #CD5555">&quot;viridis&quot;</span>)
ax.set_title(<span style="color: #CD5555">&quot;Training Accuracy&quot;</span>)
ax.set_ylabel(<span style="color: #CD5555">&quot;$\eta$&quot;</span>)
ax.set_xlabel(<span style="color: #CD5555">&quot;$\lambda$&quot;</span>)
plt.show()
fig, ax = plt.subplots(figsize = (<span style="color: #B452CD">10</span>, <span style="color: #B452CD">10</span>))
sns.heatmap(test_accuracy, annot=<span style="color: #658b00">True</span>, ax=ax, cmap=<span style="color: #CD5555">&quot;viridis&quot;</span>)
ax.set_title(<span style="color: #CD5555">&quot;Test Accuracy&quot;</span>)
ax.set_ylabel(<span style="color: #CD5555">&quot;$\eta$&quot;</span>)
ax.set_xlabel(<span style="color: #CD5555">&quot;$\lambda$&quot;</span>)
plt.show()
</pre></div>
<p>
<!-- ------------------- end of main content --------------- -->
+455 -2
View File
@@ -140,7 +140,15 @@ div { text-align: justify; text-justify: inter-word; }
None,
'___sec47'),
('scikit-learn implementation', 2, None, '___sec48'),
('And then with Tensorflow', 2, None, '___sec49')]}
('Building neural networks in Tensorflow and Keras',
2,
None,
'___sec49'),
('Tensorflow', 2, None, '___sec50'),
('Collect and pre-process data', 2, None, '___sec51'),
('Using TensorFlow backend', 2, None, '___sec52'),
('Optimizing and using gradient descent', 2, None, '___sec53'),
('Using Keras', 2, None, '___sec54')]}
end of tocinfo -->
<body>
@@ -2180,8 +2188,453 @@ plt<span style="color: #666666">.</span>show()
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec49">And then with Tensorflow </h2>
<h2 id="___sec49">Building neural networks in Tensorflow and Keras </h2>
<p>
Now we want to build on the experience gained from our neural network implementation in NumPy and scikit-learn
and use it to construct a neural network in Tensorflow. Once we have constructed a neural network in NumPy
and Tensorflow, building one in Keras is really quite trivial, though the performance may suffer.
<p>
In our previous example we used only one hidden layer, and in this we will use two. From this it should be quite
clear how to build one using an arbitrary number of hidden layers, using data structures such as Python lists or
NumPy arrays.
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec50">Tensorflow </h2>
<p>
Tensorflow is an open source library machine learning library
developed by the Google Brain team for internal use. It was released
under the Apache 2.0 open source license in November 9, 2015.
<p>
Tensorflow is a computational framework that allows you to construct
machine learning models at different levels of abstraction, from
high-level, object-oriented APIs like Keras, down to the C++ kernels
that Tensorflow is built upon. The higher levels of abstraction are
simpler to use, but less flexible, and our choice of implementation
should reflect the problems we are trying to solve.
<p>
<a href="https://www.tensorflow.org/guide/graphs" target="_blank">Tensorflow uses</a> so-called graphs to represent your computation
in terms of the dependencies between individual operations, such that you first build a Tensorflow <em>graph</em>
to represent your model, and then create a Tensorflow <em>session</em> to run the graph.
<p>
In this guide we will analyze the same data as we did in our NumPy and
scikit-learn tutorial, gathered from the MNIST database of images. We
will give an introduction to the lower level Python Application
Program Interfaces (APIs), and see how we use them to build our graph.
Then we will build (effectively) the same graph in Keras, to see just
how simple solving a machine learning problem can be.
<p>
To install tensorflow on Unix/Linux systems, use pip as
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>pip3 install tensorflow
</pre></div>
<p>
and/or if you use <b>anaconda</b>, just write (or install from the graphical user interface)
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>conda install tensorflow
</pre></div>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec51">Collect and pre-process data </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># import necessary packages</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn</span> <span style="color: #008000; font-weight: bold">import</span> datasets
<span style="color: #408080; font-style: italic"># ensure the same random numbers appear every time</span>
np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>seed(<span style="color: #666666">0</span>)
<span style="color: #408080; font-style: italic"># display images in notebook</span>
<span style="color: #666666">%</span>matplotlib inline
plt<span style="color: #666666">.</span>rcParams[<span style="color: #BA2121">&#39;figure.figsize&#39;</span>] <span style="color: #666666">=</span> (<span style="color: #666666">12</span>,<span style="color: #666666">12</span>)
<span style="color: #408080; font-style: italic"># download MNIST dataset</span>
digits <span style="color: #666666">=</span> datasets<span style="color: #666666">.</span>load_digits()
<span style="color: #408080; font-style: italic"># define inputs and labels</span>
inputs <span style="color: #666666">=</span> digits<span style="color: #666666">.</span>images
labels <span style="color: #666666">=</span> digits<span style="color: #666666">.</span>target
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;inputs = (n_inputs, pixel_width, pixel_height) = &quot;</span> <span style="color: #666666">+</span> <span style="color: #008000">str</span>(inputs<span style="color: #666666">.</span>shape))
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;labels = (n_inputs) = &quot;</span> <span style="color: #666666">+</span> <span style="color: #008000">str</span>(labels<span style="color: #666666">.</span>shape))
<span style="color: #408080; font-style: italic"># flatten the image</span>
<span style="color: #408080; font-style: italic"># the value -1 means dimension is inferred from the remaining dimensions: 8x8 = 64</span>
n_inputs <span style="color: #666666">=</span> <span style="color: #008000">len</span>(inputs)
inputs <span style="color: #666666">=</span> inputs<span style="color: #666666">.</span>reshape(n_inputs, <span style="color: #666666">-1</span>)
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;X = (n_inputs, n_features) = &quot;</span> <span style="color: #666666">+</span> <span style="color: #008000">str</span>(inputs<span style="color: #666666">.</span>shape))
<span style="color: #408080; font-style: italic"># choose some random images to display</span>
indices <span style="color: #666666">=</span> np<span style="color: #666666">.</span>arange(n_inputs)
random_indices <span style="color: #666666">=</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>choice(indices, size<span style="color: #666666">=5</span>)
<span style="color: #008000; font-weight: bold">for</span> i, image <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">enumerate</span>(digits<span style="color: #666666">.</span>images[random_indices]):
plt<span style="color: #666666">.</span>subplot(<span style="color: #666666">1</span>, <span style="color: #666666">5</span>, i<span style="color: #666666">+1</span>)
plt<span style="color: #666666">.</span>axis(<span style="color: #BA2121">&#39;off&#39;</span>)
plt<span style="color: #666666">.</span>imshow(image, cmap<span style="color: #666666">=</span>plt<span style="color: #666666">.</span>cm<span style="color: #666666">.</span>gray_r, interpolation<span style="color: #666666">=</span><span style="color: #BA2121">&#39;nearest&#39;</span>)
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">&quot;Label: </span><span style="color: #BB6688; font-weight: bold">%d</span><span style="color: #BA2121">&quot;</span> <span style="color: #666666">%</span> digits<span style="color: #666666">.</span>target[random_indices[i]])
plt<span style="color: #666666">.</span>show()
</pre></div>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">keras.utils</span> <span style="color: #008000; font-weight: bold">import</span> to_categorical
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> train_test_split
<span style="color: #408080; font-style: italic"># one-hot representation of labels</span>
labels <span style="color: #666666">=</span> to_categorical(labels)
<span style="color: #408080; font-style: italic"># split into train and test data</span>
train_size <span style="color: #666666">=</span> <span style="color: #666666">0.8</span>
test_size <span style="color: #666666">=</span> <span style="color: #666666">1</span> <span style="color: #666666">-</span> train_size
X_train, X_test, Y_train, Y_test <span style="color: #666666">=</span> train_test_split(inputs, labels, train_size<span style="color: #666666">=</span>train_size,
test_size<span style="color: #666666">=</span>test_size)
</pre></div>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec52">Using TensorFlow backend </h2>
<ol>
<li> Define model and architecture</li>
<li> Choose cost function and optimizer</li>
</ol>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">tensorflow</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">tf</span>
<span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">NeuralNetworkTensorflow</span>:
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">__init__</span>(
<span style="color: #008000">self</span>,
X_train,
Y_train,
X_test,
Y_test,
n_neurons_layer1<span style="color: #666666">=100</span>,
n_neurons_layer2<span style="color: #666666">=50</span>,
n_categories<span style="color: #666666">=2</span>,
epochs<span style="color: #666666">=10</span>,
batch_size<span style="color: #666666">=100</span>,
eta<span style="color: #666666">=0.1</span>,
lmbd<span style="color: #666666">=0.0</span>,
):
<span style="color: #408080; font-style: italic"># keep track of number of steps</span>
<span style="color: #008000">self</span><span style="color: #666666">.</span>global_step <span style="color: #666666">=</span> tf<span style="color: #666666">.</span>Variable(<span style="color: #666666">0</span>, dtype<span style="color: #666666">=</span>tf<span style="color: #666666">.</span>int32, trainable<span style="color: #666666">=</span><span style="color: #008000">False</span>, name<span style="color: #666666">=</span><span style="color: #BA2121">&#39;global_step&#39;</span>)
<span style="color: #008000">self</span><span style="color: #666666">.</span>X_train <span style="color: #666666">=</span> X_train
<span style="color: #008000">self</span><span style="color: #666666">.</span>Y_train <span style="color: #666666">=</span> Y_train
<span style="color: #008000">self</span><span style="color: #666666">.</span>X_test <span style="color: #666666">=</span> X_test
<span style="color: #008000">self</span><span style="color: #666666">.</span>Y_test <span style="color: #666666">=</span> Y_test
<span style="color: #008000">self</span><span style="color: #666666">.</span>n_inputs <span style="color: #666666">=</span> X_train<span style="color: #666666">.</span>shape[<span style="color: #666666">0</span>]
<span style="color: #008000">self</span><span style="color: #666666">.</span>n_features <span style="color: #666666">=</span> X_train<span style="color: #666666">.</span>shape[<span style="color: #666666">1</span>]
<span style="color: #008000">self</span><span style="color: #666666">.</span>n_neurons_layer1 <span style="color: #666666">=</span> n_neurons_layer1
<span style="color: #008000">self</span><span style="color: #666666">.</span>n_neurons_layer2 <span style="color: #666666">=</span> n_neurons_layer2
<span style="color: #008000">self</span><span style="color: #666666">.</span>n_categories <span style="color: #666666">=</span> n_categories
<span style="color: #008000">self</span><span style="color: #666666">.</span>epochs <span style="color: #666666">=</span> epochs
<span style="color: #008000">self</span><span style="color: #666666">.</span>batch_size <span style="color: #666666">=</span> batch_size
<span style="color: #008000">self</span><span style="color: #666666">.</span>iterations <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>n_inputs <span style="color: #666666">//</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>batch_size
<span style="color: #008000">self</span><span style="color: #666666">.</span>eta <span style="color: #666666">=</span> eta
<span style="color: #008000">self</span><span style="color: #666666">.</span>lmbd <span style="color: #666666">=</span> lmbd
<span style="color: #408080; font-style: italic"># build network piece by piece</span>
<span style="color: #408080; font-style: italic"># name scopes (with) are used to enforce creation of new variables</span>
<span style="color: #408080; font-style: italic"># https://www.tensorflow.org/guide/variables</span>
<span style="color: #008000">self</span><span style="color: #666666">.</span>create_placeholders()
<span style="color: #008000">self</span><span style="color: #666666">.</span>create_DNN()
<span style="color: #008000">self</span><span style="color: #666666">.</span>create_loss()
<span style="color: #008000">self</span><span style="color: #666666">.</span>create_optimiser()
<span style="color: #008000">self</span><span style="color: #666666">.</span>create_accuracy()
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">create_placeholders</span>(<span style="color: #008000">self</span>):
<span style="color: #408080; font-style: italic"># placeholders are fine here, but &quot;Datasets&quot; are the preferred method</span>
<span style="color: #408080; font-style: italic"># of streaming data into a model</span>
<span style="color: #008000; font-weight: bold">with</span> tf<span style="color: #666666">.</span>name_scope(<span style="color: #BA2121">&#39;data&#39;</span>):
<span style="color: #008000">self</span><span style="color: #666666">.</span>X <span style="color: #666666">=</span> tf<span style="color: #666666">.</span>placeholder(tf<span style="color: #666666">.</span>float32, shape<span style="color: #666666">=</span>(<span style="color: #008000">None</span>, <span style="color: #008000">self</span><span style="color: #666666">.</span>n_features), name<span style="color: #666666">=</span><span style="color: #BA2121">&#39;X_data&#39;</span>)
<span style="color: #008000">self</span><span style="color: #666666">.</span>Y <span style="color: #666666">=</span> tf<span style="color: #666666">.</span>placeholder(tf<span style="color: #666666">.</span>float32, shape<span style="color: #666666">=</span>(<span style="color: #008000">None</span>, <span style="color: #008000">self</span><span style="color: #666666">.</span>n_categories), name<span style="color: #666666">=</span><span style="color: #BA2121">&#39;Y_data&#39;</span>)
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">create_DNN</span>(<span style="color: #008000">self</span>):
<span style="color: #008000; font-weight: bold">with</span> tf<span style="color: #666666">.</span>name_scope(<span style="color: #BA2121">&#39;DNN&#39;</span>):
<span style="color: #408080; font-style: italic"># the weights are stored to calculate regularization loss later</span>
<span style="color: #408080; font-style: italic"># Fully connected layer 1</span>
<span style="color: #008000">self</span><span style="color: #666666">.</span>W_fc1 <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>weight_variable([<span style="color: #008000">self</span><span style="color: #666666">.</span>n_features, <span style="color: #008000">self</span><span style="color: #666666">.</span>n_neurons_layer1], name<span style="color: #666666">=</span><span style="color: #BA2121">&#39;fc1&#39;</span>, dtype<span style="color: #666666">=</span>tf<span style="color: #666666">.</span>float32)
b_fc1 <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>bias_variable([<span style="color: #008000">self</span><span style="color: #666666">.</span>n_neurons_layer1], name<span style="color: #666666">=</span><span style="color: #BA2121">&#39;fc1&#39;</span>, dtype<span style="color: #666666">=</span>tf<span style="color: #666666">.</span>float32)
a_fc1 <span style="color: #666666">=</span> tf<span style="color: #666666">.</span>nn<span style="color: #666666">.</span>sigmoid(tf<span style="color: #666666">.</span>matmul(<span style="color: #008000">self</span><span style="color: #666666">.</span>X, <span style="color: #008000">self</span><span style="color: #666666">.</span>W_fc1) <span style="color: #666666">+</span> b_fc1)
<span style="color: #408080; font-style: italic"># Fully connected layer 2</span>
<span style="color: #008000">self</span><span style="color: #666666">.</span>W_fc2 <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>weight_variable([<span style="color: #008000">self</span><span style="color: #666666">.</span>n_neurons_layer1, <span style="color: #008000">self</span><span style="color: #666666">.</span>n_neurons_layer2], name<span style="color: #666666">=</span><span style="color: #BA2121">&#39;fc2&#39;</span>, dtype<span style="color: #666666">=</span>tf<span style="color: #666666">.</span>float32)
b_fc2 <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>bias_variable([<span style="color: #008000">self</span><span style="color: #666666">.</span>n_neurons_layer2], name<span style="color: #666666">=</span><span style="color: #BA2121">&#39;fc2&#39;</span>, dtype<span style="color: #666666">=</span>tf<span style="color: #666666">.</span>float32)
a_fc2 <span style="color: #666666">=</span> tf<span style="color: #666666">.</span>nn<span style="color: #666666">.</span>sigmoid(tf<span style="color: #666666">.</span>matmul(a_fc1, <span style="color: #008000">self</span><span style="color: #666666">.</span>W_fc2) <span style="color: #666666">+</span> b_fc2)
<span style="color: #408080; font-style: italic"># Output layer</span>
<span style="color: #008000">self</span><span style="color: #666666">.</span>W_out <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>weight_variable([<span style="color: #008000">self</span><span style="color: #666666">.</span>n_neurons_layer2, <span style="color: #008000">self</span><span style="color: #666666">.</span>n_categories], name<span style="color: #666666">=</span><span style="color: #BA2121">&#39;out&#39;</span>, dtype<span style="color: #666666">=</span>tf<span style="color: #666666">.</span>float32)
b_out <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>bias_variable([<span style="color: #008000">self</span><span style="color: #666666">.</span>n_categories], name<span style="color: #666666">=</span><span style="color: #BA2121">&#39;out&#39;</span>, dtype<span style="color: #666666">=</span>tf<span style="color: #666666">.</span>float32)
<span style="color: #008000">self</span><span style="color: #666666">.</span>z_out <span style="color: #666666">=</span> tf<span style="color: #666666">.</span>matmul(a_fc2, <span style="color: #008000">self</span><span style="color: #666666">.</span>W_out) <span style="color: #666666">+</span> b_out
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">create_loss</span>(<span style="color: #008000">self</span>):
<span style="color: #008000; font-weight: bold">with</span> tf<span style="color: #666666">.</span>name_scope(<span style="color: #BA2121">&#39;loss&#39;</span>):
softmax_loss <span style="color: #666666">=</span> tf<span style="color: #666666">.</span>reduce_mean(tf<span style="color: #666666">.</span>nn<span style="color: #666666">.</span>softmax_cross_entropy_with_logits_v2(labels<span style="color: #666666">=</span><span style="color: #008000">self</span><span style="color: #666666">.</span>Y, logits<span style="color: #666666">=</span><span style="color: #008000">self</span><span style="color: #666666">.</span>z_out))
regularizer_loss_fc1 <span style="color: #666666">=</span> tf<span style="color: #666666">.</span>nn<span style="color: #666666">.</span>l2_loss(<span style="color: #008000">self</span><span style="color: #666666">.</span>W_fc1)
regularizer_loss_fc2 <span style="color: #666666">=</span> tf<span style="color: #666666">.</span>nn<span style="color: #666666">.</span>l2_loss(<span style="color: #008000">self</span><span style="color: #666666">.</span>W_fc2)
regularizer_loss_out <span style="color: #666666">=</span> tf<span style="color: #666666">.</span>nn<span style="color: #666666">.</span>l2_loss(<span style="color: #008000">self</span><span style="color: #666666">.</span>W_out)
regularizer_loss <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>lmbd<span style="color: #666666">*</span>(regularizer_loss_fc1 <span style="color: #666666">+</span> regularizer_loss_fc2 <span style="color: #666666">+</span> regularizer_loss_out)
<span style="color: #008000">self</span><span style="color: #666666">.</span>loss <span style="color: #666666">=</span> softmax_loss <span style="color: #666666">+</span> regularizer_loss
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">create_accuracy</span>(<span style="color: #008000">self</span>):
<span style="color: #008000; font-weight: bold">with</span> tf<span style="color: #666666">.</span>name_scope(<span style="color: #BA2121">&#39;accuracy&#39;</span>):
probabilities <span style="color: #666666">=</span> tf<span style="color: #666666">.</span>nn<span style="color: #666666">.</span>softmax(<span style="color: #008000">self</span><span style="color: #666666">.</span>z_out)
predictions <span style="color: #666666">=</span> tf<span style="color: #666666">.</span>argmax(probabilities, axis<span style="color: #666666">=1</span>)
labels <span style="color: #666666">=</span> tf<span style="color: #666666">.</span>argmax(<span style="color: #008000">self</span><span style="color: #666666">.</span>Y, axis<span style="color: #666666">=1</span>)
correct_predictions <span style="color: #666666">=</span> tf<span style="color: #666666">.</span>equal(predictions, labels)
correct_predictions <span style="color: #666666">=</span> tf<span style="color: #666666">.</span>cast(correct_predictions, tf<span style="color: #666666">.</span>float32)
<span style="color: #008000">self</span><span style="color: #666666">.</span>accuracy <span style="color: #666666">=</span> tf<span style="color: #666666">.</span>reduce_mean(correct_predictions)
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">create_optimiser</span>(<span style="color: #008000">self</span>):
<span style="color: #008000; font-weight: bold">with</span> tf<span style="color: #666666">.</span>name_scope(<span style="color: #BA2121">&#39;optimizer&#39;</span>):
<span style="color: #008000">self</span><span style="color: #666666">.</span>optimizer <span style="color: #666666">=</span> tf<span style="color: #666666">.</span>train<span style="color: #666666">.</span>GradientDescentOptimizer(learning_rate<span style="color: #666666">=</span><span style="color: #008000">self</span><span style="color: #666666">.</span>eta)<span style="color: #666666">.</span>minimize(<span style="color: #008000">self</span><span style="color: #666666">.</span>loss, global_step<span style="color: #666666">=</span><span style="color: #008000">self</span><span style="color: #666666">.</span>global_step)
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">weight_variable</span>(<span style="color: #008000">self</span>, shape, name<span style="color: #666666">=</span><span style="color: #BA2121">&#39;&#39;</span>, dtype<span style="color: #666666">=</span>tf<span style="color: #666666">.</span>float32):
initial <span style="color: #666666">=</span> tf<span style="color: #666666">.</span>truncated_normal(shape, stddev<span style="color: #666666">=0.1</span>)
<span style="color: #008000; font-weight: bold">return</span> tf<span style="color: #666666">.</span>Variable(initial, name<span style="color: #666666">=</span>name, dtype<span style="color: #666666">=</span>dtype)
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">bias_variable</span>(<span style="color: #008000">self</span>, shape, name<span style="color: #666666">=</span><span style="color: #BA2121">&#39;&#39;</span>, dtype<span style="color: #666666">=</span>tf<span style="color: #666666">.</span>float32):
initial <span style="color: #666666">=</span> tf<span style="color: #666666">.</span>constant(<span style="color: #666666">0.1</span>, shape<span style="color: #666666">=</span>shape)
<span style="color: #008000; font-weight: bold">return</span> tf<span style="color: #666666">.</span>Variable(initial, name<span style="color: #666666">=</span>name, dtype<span style="color: #666666">=</span>dtype)
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">fit</span>(<span style="color: #008000">self</span>):
data_indices <span style="color: #666666">=</span> np<span style="color: #666666">.</span>arange(<span style="color: #008000">self</span><span style="color: #666666">.</span>n_inputs)
<span style="color: #008000; font-weight: bold">with</span> tf<span style="color: #666666">.</span>Session() <span style="color: #008000; font-weight: bold">as</span> sess:
sess<span style="color: #666666">.</span>run(tf<span style="color: #666666">.</span>global_variables_initializer())
<span style="color: #008000; font-weight: bold">for</span> i <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #008000">self</span><span style="color: #666666">.</span>epochs):
<span style="color: #008000; font-weight: bold">for</span> j <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #008000">self</span><span style="color: #666666">.</span>iterations):
chosen_datapoints <span style="color: #666666">=</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>choice(data_indices, size<span style="color: #666666">=</span><span style="color: #008000">self</span><span style="color: #666666">.</span>batch_size, replace<span style="color: #666666">=</span><span style="color: #008000">False</span>)
batch_X, batch_Y <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>X_train[chosen_datapoints], <span style="color: #008000">self</span><span style="color: #666666">.</span>Y_train[chosen_datapoints]
sess<span style="color: #666666">.</span>run([DNN<span style="color: #666666">.</span>loss, DNN<span style="color: #666666">.</span>optimizer],
feed_dict<span style="color: #666666">=</span>{DNN<span style="color: #666666">.</span>X: batch_X,
DNN<span style="color: #666666">.</span>Y: batch_Y})
accuracy <span style="color: #666666">=</span> sess<span style="color: #666666">.</span>run(DNN<span style="color: #666666">.</span>accuracy,
feed_dict<span style="color: #666666">=</span>{DNN<span style="color: #666666">.</span>X: batch_X,
DNN<span style="color: #666666">.</span>Y: batch_Y})
step <span style="color: #666666">=</span> sess<span style="color: #666666">.</span>run(DNN<span style="color: #666666">.</span>global_step)
<span style="color: #008000">self</span><span style="color: #666666">.</span>train_loss, <span style="color: #008000">self</span><span style="color: #666666">.</span>train_accuracy <span style="color: #666666">=</span> sess<span style="color: #666666">.</span>run([DNN<span style="color: #666666">.</span>loss, DNN<span style="color: #666666">.</span>accuracy],
feed_dict<span style="color: #666666">=</span>{DNN<span style="color: #666666">.</span>X: <span style="color: #008000">self</span><span style="color: #666666">.</span>X_train,
DNN<span style="color: #666666">.</span>Y: <span style="color: #008000">self</span><span style="color: #666666">.</span>Y_train})
<span style="color: #008000">self</span><span style="color: #666666">.</span>test_loss, <span style="color: #008000">self</span><span style="color: #666666">.</span>test_accuracy <span style="color: #666666">=</span> sess<span style="color: #666666">.</span>run([DNN<span style="color: #666666">.</span>loss, DNN<span style="color: #666666">.</span>accuracy],
feed_dict<span style="color: #666666">=</span>{DNN<span style="color: #666666">.</span>X: <span style="color: #008000">self</span><span style="color: #666666">.</span>X_test,
DNN<span style="color: #666666">.</span>Y: <span style="color: #008000">self</span><span style="color: #666666">.</span>Y_test})
</pre></div>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec53">Optimizing and using gradient descent </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>epochs <span style="color: #666666">=</span> <span style="color: #666666">100</span>
batch_size <span style="color: #666666">=</span> <span style="color: #666666">100</span>
n_neurons_layer1 <span style="color: #666666">=</span> <span style="color: #666666">100</span>
n_neurons_layer2 <span style="color: #666666">=</span> <span style="color: #666666">50</span>
n_categories <span style="color: #666666">=</span> <span style="color: #666666">10</span>
eta_vals <span style="color: #666666">=</span> np<span style="color: #666666">.</span>logspace(<span style="color: #666666">-5</span>, <span style="color: #666666">1</span>, <span style="color: #666666">7</span>)
lmbd_vals <span style="color: #666666">=</span> np<span style="color: #666666">.</span>logspace(<span style="color: #666666">-5</span>, <span style="color: #666666">1</span>, <span style="color: #666666">7</span>)
</pre></div>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>DNN_tf <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros((<span style="color: #008000">len</span>(eta_vals), <span style="color: #008000">len</span>(lmbd_vals)), dtype<span style="color: #666666">=</span><span style="color: #008000">object</span>)
<span style="color: #008000; font-weight: bold">for</span> i, eta <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">enumerate</span>(eta_vals):
<span style="color: #008000; font-weight: bold">for</span> j, lmbd <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">enumerate</span>(lmbd_vals):
DNN <span style="color: #666666">=</span> NeuralNetworkTensorflow(X_train, Y_train, X_test, Y_test,
n_neurons_layer1, n_neurons_layer2, n_categories,
epochs<span style="color: #666666">=</span>epochs, batch_size<span style="color: #666666">=</span>batch_size, eta<span style="color: #666666">=</span>eta, lmbd<span style="color: #666666">=</span>lmbd)
DNN<span style="color: #666666">.</span>fit()
DNN_tf[i][j] <span style="color: #666666">=</span> DNN
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;Learning rate = &quot;</span>, eta)
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;Lambda = &quot;</span>, lmbd)
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;Test accuracy: </span><span style="color: #BB6688; font-weight: bold">%.3f</span><span style="color: #BA2121">&quot;</span> <span style="color: #666666">%</span> DNN<span style="color: #666666">.</span>test_accuracy)
<span style="color: #008000; font-weight: bold">print</span>()
</pre></div>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># optional</span>
<span style="color: #408080; font-style: italic"># visual representation of grid search</span>
<span style="color: #408080; font-style: italic"># uses seaborn heatmap, could probably do this in matplotlib</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">seaborn</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">sns</span>
sns<span style="color: #666666">.</span>set()
train_accuracy <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros((<span style="color: #008000">len</span>(eta_vals), <span style="color: #008000">len</span>(lmbd_vals)))
test_accuracy <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros((<span style="color: #008000">len</span>(eta_vals), <span style="color: #008000">len</span>(lmbd_vals)))
<span style="color: #008000; font-weight: bold">for</span> i <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #008000">len</span>(eta_vals)):
<span style="color: #008000; font-weight: bold">for</span> j <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #008000">len</span>(lmbd_vals)):
DNN <span style="color: #666666">=</span> DNN_tf[i][j]
train_accuracy[i][j] <span style="color: #666666">=</span> DNN<span style="color: #666666">.</span>train_accuracy
test_accuracy[i][j] <span style="color: #666666">=</span> DNN<span style="color: #666666">.</span>test_accuracy
fig, ax <span style="color: #666666">=</span> plt<span style="color: #666666">.</span>subplots(figsize <span style="color: #666666">=</span> (<span style="color: #666666">10</span>, <span style="color: #666666">10</span>))
sns<span style="color: #666666">.</span>heatmap(train_accuracy, annot<span style="color: #666666">=</span><span style="color: #008000">True</span>, ax<span style="color: #666666">=</span>ax, cmap<span style="color: #666666">=</span><span style="color: #BA2121">&quot;viridis&quot;</span>)
ax<span style="color: #666666">.</span>set_title(<span style="color: #BA2121">&quot;Training Accuracy&quot;</span>)
ax<span style="color: #666666">.</span>set_ylabel(<span style="color: #BA2121">&quot;$\eta$&quot;</span>)
ax<span style="color: #666666">.</span>set_xlabel(<span style="color: #BA2121">&quot;$\lambda$&quot;</span>)
plt<span style="color: #666666">.</span>show()
fig, ax <span style="color: #666666">=</span> plt<span style="color: #666666">.</span>subplots(figsize <span style="color: #666666">=</span> (<span style="color: #666666">10</span>, <span style="color: #666666">10</span>))
sns<span style="color: #666666">.</span>heatmap(test_accuracy, annot<span style="color: #666666">=</span><span style="color: #008000">True</span>, ax<span style="color: #666666">=</span>ax, cmap<span style="color: #666666">=</span><span style="color: #BA2121">&quot;viridis&quot;</span>)
ax<span style="color: #666666">.</span>set_title(<span style="color: #BA2121">&quot;Test Accuracy&quot;</span>)
ax<span style="color: #666666">.</span>set_ylabel(<span style="color: #BA2121">&quot;$\eta$&quot;</span>)
ax<span style="color: #666666">.</span>set_xlabel(<span style="color: #BA2121">&quot;$\lambda$&quot;</span>)
plt<span style="color: #666666">.</span>show()
</pre></div>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># optional</span>
<span style="color: #408080; font-style: italic"># we can use log files to visualize our graph in Tensorboard</span>
writer <span style="color: #666666">=</span> tf<span style="color: #666666">.</span>summary<span style="color: #666666">.</span>FileWriter(<span style="color: #BA2121">&#39;logs/&#39;</span>)
writer<span style="color: #666666">.</span>add_graph(tf<span style="color: #666666">.</span>get_default_graph())
</pre></div>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec54">Using Keras </h2>
<p>
Keras is a high level <a href="https://en.wikipedia.org/wiki/Application_programming_interface" target="_blank">neural network</a>
that supports Tensorflow, CTNK and Theano as backends.
If you have Tensorflow installed Keras is available through the <em>tf.keras</em> module.
If you have Anaconda installed you may run the following command
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>conda install keras
</pre></div>
<p>
Alternatively, if you have Tensorflow or one of the other supported backends install you may use the pip package manager:
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>pip3 install keras
</pre></div>
<p>
or look up the <a href="https://keras.io/" target="_blank">instructions here</a>.
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">keras.models</span> <span style="color: #008000; font-weight: bold">import</span> Sequential
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">keras.layers</span> <span style="color: #008000; font-weight: bold">import</span> Dense
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">keras.regularizers</span> <span style="color: #008000; font-weight: bold">import</span> l2
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">keras.optimizers</span> <span style="color: #008000; font-weight: bold">import</span> SGD
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">create_neural_network_keras</span>(n_neurons_layer1, n_neurons_layer2, n_categories, eta, lmbd):
model <span style="color: #666666">=</span> Sequential()
model<span style="color: #666666">.</span>add(Dense(n_neurons_layer1, activation<span style="color: #666666">=</span><span style="color: #BA2121">&#39;sigmoid&#39;</span>, kernel_regularizer<span style="color: #666666">=</span>l2(lmbd)))
model<span style="color: #666666">.</span>add(Dense(n_neurons_layer2, activation<span style="color: #666666">=</span><span style="color: #BA2121">&#39;sigmoid&#39;</span>, kernel_regularizer<span style="color: #666666">=</span>l2(lmbd)))
model<span style="color: #666666">.</span>add(Dense(n_categories, activation<span style="color: #666666">=</span><span style="color: #BA2121">&#39;softmax&#39;</span>))
sgd <span style="color: #666666">=</span> SGD(lr<span style="color: #666666">=</span>eta)
model<span style="color: #666666">.</span>compile(loss<span style="color: #666666">=</span><span style="color: #BA2121">&#39;categorical_crossentropy&#39;</span>, optimizer<span style="color: #666666">=</span>sgd, metrics<span style="color: #666666">=</span>[<span style="color: #BA2121">&#39;accuracy&#39;</span>])
<span style="color: #008000; font-weight: bold">return</span> model
</pre></div>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>DNN_keras <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros((<span style="color: #008000">len</span>(eta_vals), <span style="color: #008000">len</span>(lmbd_vals)), dtype<span style="color: #666666">=</span><span style="color: #008000">object</span>)
<span style="color: #008000; font-weight: bold">for</span> i, eta <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">enumerate</span>(eta_vals):
<span style="color: #008000; font-weight: bold">for</span> j, lmbd <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">enumerate</span>(lmbd_vals):
DNN <span style="color: #666666">=</span> create_neural_network_keras(n_neurons_layer1, n_neurons_layer2, n_categories,
eta<span style="color: #666666">=</span>eta, lmbd<span style="color: #666666">=</span>lmbd)
DNN<span style="color: #666666">.</span>fit(X_train, Y_train, epochs<span style="color: #666666">=</span>epochs, batch_size<span style="color: #666666">=</span>batch_size, verbose<span style="color: #666666">=0</span>)
scores <span style="color: #666666">=</span> DNN<span style="color: #666666">.</span>evaluate(X_test, Y_test)
DNN_keras[i][j] <span style="color: #666666">=</span> DNN
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;Learning rate = &quot;</span>, eta)
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;Lambda = &quot;</span>, lmbd)
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;Test accuracy: </span><span style="color: #BB6688; font-weight: bold">%.3f</span><span style="color: #BA2121">&quot;</span> <span style="color: #666666">%</span> scores[<span style="color: #666666">1</span>])
<span style="color: #008000; font-weight: bold">print</span>()
</pre></div>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># optional</span>
<span style="color: #408080; font-style: italic"># visual representation of grid search</span>
<span style="color: #408080; font-style: italic"># uses seaborn heatmap, could probably do this in matplotlib</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">seaborn</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">sns</span>
sns<span style="color: #666666">.</span>set()
train_accuracy <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros((<span style="color: #008000">len</span>(eta_vals), <span style="color: #008000">len</span>(lmbd_vals)))
test_accuracy <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros((<span style="color: #008000">len</span>(eta_vals), <span style="color: #008000">len</span>(lmbd_vals)))
<span style="color: #008000; font-weight: bold">for</span> i <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #008000">len</span>(eta_vals)):
<span style="color: #008000; font-weight: bold">for</span> j <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #008000">len</span>(lmbd_vals)):
DNN <span style="color: #666666">=</span> DNN_keras[i][j]
train_accuracy[i][j] <span style="color: #666666">=</span> DNN<span style="color: #666666">.</span>evaluate(X_train, Y_train)[<span style="color: #666666">1</span>]
test_accuracy[i][j] <span style="color: #666666">=</span> DNN<span style="color: #666666">.</span>evaluate(X_test, Y_test)[<span style="color: #666666">1</span>]
fig, ax <span style="color: #666666">=</span> plt<span style="color: #666666">.</span>subplots(figsize <span style="color: #666666">=</span> (<span style="color: #666666">10</span>, <span style="color: #666666">10</span>))
sns<span style="color: #666666">.</span>heatmap(train_accuracy, annot<span style="color: #666666">=</span><span style="color: #008000">True</span>, ax<span style="color: #666666">=</span>ax, cmap<span style="color: #666666">=</span><span style="color: #BA2121">&quot;viridis&quot;</span>)
ax<span style="color: #666666">.</span>set_title(<span style="color: #BA2121">&quot;Training Accuracy&quot;</span>)
ax<span style="color: #666666">.</span>set_ylabel(<span style="color: #BA2121">&quot;$\eta$&quot;</span>)
ax<span style="color: #666666">.</span>set_xlabel(<span style="color: #BA2121">&quot;$\lambda$&quot;</span>)
plt<span style="color: #666666">.</span>show()
fig, ax <span style="color: #666666">=</span> plt<span style="color: #666666">.</span>subplots(figsize <span style="color: #666666">=</span> (<span style="color: #666666">10</span>, <span style="color: #666666">10</span>))
sns<span style="color: #666666">.</span>heatmap(test_accuracy, annot<span style="color: #666666">=</span><span style="color: #008000">True</span>, ax<span style="color: #666666">=</span>ax, cmap<span style="color: #666666">=</span><span style="color: #BA2121">&quot;viridis&quot;</span>)
ax<span style="color: #666666">.</span>set_title(<span style="color: #BA2121">&quot;Test Accuracy&quot;</span>)
ax<span style="color: #666666">.</span>set_ylabel(<span style="color: #BA2121">&quot;$\eta$&quot;</span>)
ax<span style="color: #666666">.</span>set_xlabel(<span style="color: #BA2121">&quot;$\lambda$&quot;</span>)
plt<span style="color: #666666">.</span>show()
</pre></div>
<p>
<!-- ------------------- end of main content --------------- -->
+540 -1
View File
@@ -2447,7 +2447,546 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## And then with Tensorflow"
"## Building neural networks in Tensorflow and Keras\n",
"\n",
"Now we want to build on the experience gained from our neural network implementation in NumPy and scikit-learn\n",
"and use it to construct a neural network in Tensorflow. Once we have constructed a neural network in NumPy\n",
"and Tensorflow, building one in Keras is really quite trivial, though the performance may suffer. \n",
"\n",
"In our previous example we used only one hidden layer, and in this we will use two. From this it should be quite\n",
"clear how to build one using an arbitrary number of hidden layers, using data structures such as Python lists or\n",
"NumPy arrays.\n",
"\n",
"## Tensorflow\n",
"\n",
"Tensorflow is an open source library machine learning library\n",
"developed by the Google Brain team for internal use. It was released\n",
"under the Apache 2.0 open source license in November 9, 2015.\n",
"\n",
"Tensorflow is a computational framework that allows you to construct\n",
"machine learning models at different levels of abstraction, from\n",
"high-level, object-oriented APIs like Keras, down to the C++ kernels\n",
"that Tensorflow is built upon. The higher levels of abstraction are\n",
"simpler to use, but less flexible, and our choice of implementation\n",
"should reflect the problems we are trying to solve.\n",
"\n",
"[Tensorflow uses](https://www.tensorflow.org/guide/graphs) so-called graphs to represent your computation\n",
"in terms of the dependencies between individual operations, such that you first build a Tensorflow *graph*\n",
"to represent your model, and then create a Tensorflow *session* to run the graph.\n",
"\n",
"In this guide we will analyze the same data as we did in our NumPy and\n",
"scikit-learn tutorial, gathered from the MNIST database of images. We\n",
"will give an introduction to the lower level Python Application\n",
"Program Interfaces (APIs), and see how we use them to build our graph.\n",
"Then we will build (effectively) the same graph in Keras, to see just\n",
"how simple solving a machine learning problem can be.\n",
"\n",
"To install tensorflow on Unix/Linux systems, use pip as"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"pip3 install tensorflow"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"and/or if you use **anaconda**, just write (or install from the graphical user interface)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"conda install tensorflow"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Collect and pre-process data"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# import necessary packages\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from sklearn import datasets\n",
"\n",
"\n",
"# ensure the same random numbers appear every time\n",
"np.random.seed(0)\n",
"\n",
"# display images in notebook\n",
"%matplotlib inline\n",
"plt.rcParams['figure.figsize'] = (12,12)\n",
"\n",
"\n",
"# download MNIST dataset\n",
"digits = datasets.load_digits()\n",
"\n",
"# define inputs and labels\n",
"inputs = digits.images\n",
"labels = digits.target\n",
"\n",
"print(\"inputs = (n_inputs, pixel_width, pixel_height) = \" + str(inputs.shape))\n",
"print(\"labels = (n_inputs) = \" + str(labels.shape))\n",
"\n",
"\n",
"# flatten the image\n",
"# the value -1 means dimension is inferred from the remaining dimensions: 8x8 = 64\n",
"n_inputs = len(inputs)\n",
"inputs = inputs.reshape(n_inputs, -1)\n",
"print(\"X = (n_inputs, n_features) = \" + str(inputs.shape))\n",
"\n",
"\n",
"# choose some random images to display\n",
"indices = np.arange(n_inputs)\n",
"random_indices = np.random.choice(indices, size=5)\n",
"\n",
"for i, image in enumerate(digits.images[random_indices]):\n",
" plt.subplot(1, 5, i+1)\n",
" plt.axis('off')\n",
" plt.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest')\n",
" plt.title(\"Label: %d\" % digits.target[random_indices[i]])\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from keras.utils import to_categorical\n",
"from sklearn.model_selection import train_test_split\n",
"\n",
"# one-hot representation of labels\n",
"labels = to_categorical(labels)\n",
"\n",
"# split into train and test data\n",
"train_size = 0.8\n",
"test_size = 1 - train_size\n",
"X_train, X_test, Y_train, Y_test = train_test_split(inputs, labels, train_size=train_size,\n",
" test_size=test_size)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Using TensorFlow backend\n",
"\n",
"1. Define model and architecture\n",
"\n",
"2. Choose cost function and optimizer"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import tensorflow as tf\n",
"\n",
"class NeuralNetworkTensorflow:\n",
" def __init__(\n",
" self,\n",
" X_train,\n",
" Y_train,\n",
" X_test,\n",
" Y_test,\n",
" n_neurons_layer1=100,\n",
" n_neurons_layer2=50,\n",
" n_categories=2,\n",
" epochs=10,\n",
" batch_size=100,\n",
" eta=0.1,\n",
" lmbd=0.0,\n",
" ):\n",
" \n",
" # keep track of number of steps\n",
" self.global_step = tf.Variable(0, dtype=tf.int32, trainable=False, name='global_step')\n",
" \n",
" self.X_train = X_train\n",
" self.Y_train = Y_train\n",
" self.X_test = X_test\n",
" self.Y_test = Y_test\n",
" \n",
" self.n_inputs = X_train.shape[0]\n",
" self.n_features = X_train.shape[1]\n",
" self.n_neurons_layer1 = n_neurons_layer1\n",
" self.n_neurons_layer2 = n_neurons_layer2\n",
" self.n_categories = n_categories\n",
" \n",
" self.epochs = epochs\n",
" self.batch_size = batch_size\n",
" self.iterations = self.n_inputs // self.batch_size\n",
" self.eta = eta\n",
" self.lmbd = lmbd\n",
" \n",
" # build network piece by piece\n",
" # name scopes (with) are used to enforce creation of new variables\n",
" # https://www.tensorflow.org/guide/variables\n",
" self.create_placeholders()\n",
" self.create_DNN()\n",
" self.create_loss()\n",
" self.create_optimiser()\n",
" self.create_accuracy()\n",
" \n",
" def create_placeholders(self):\n",
" # placeholders are fine here, but \"Datasets\" are the preferred method\n",
" # of streaming data into a model\n",
" with tf.name_scope('data'):\n",
" self.X = tf.placeholder(tf.float32, shape=(None, self.n_features), name='X_data')\n",
" self.Y = tf.placeholder(tf.float32, shape=(None, self.n_categories), name='Y_data')\n",
" \n",
" def create_DNN(self):\n",
" with tf.name_scope('DNN'):\n",
" # the weights are stored to calculate regularization loss later\n",
" \n",
" # Fully connected layer 1\n",
" self.W_fc1 = self.weight_variable([self.n_features, self.n_neurons_layer1], name='fc1', dtype=tf.float32)\n",
" b_fc1 = self.bias_variable([self.n_neurons_layer1], name='fc1', dtype=tf.float32)\n",
" a_fc1 = tf.nn.sigmoid(tf.matmul(self.X, self.W_fc1) + b_fc1)\n",
" \n",
" # Fully connected layer 2\n",
" self.W_fc2 = self.weight_variable([self.n_neurons_layer1, self.n_neurons_layer2], name='fc2', dtype=tf.float32)\n",
" b_fc2 = self.bias_variable([self.n_neurons_layer2], name='fc2', dtype=tf.float32)\n",
" a_fc2 = tf.nn.sigmoid(tf.matmul(a_fc1, self.W_fc2) + b_fc2)\n",
" \n",
" # Output layer\n",
" self.W_out = self.weight_variable([self.n_neurons_layer2, self.n_categories], name='out', dtype=tf.float32)\n",
" b_out = self.bias_variable([self.n_categories], name='out', dtype=tf.float32)\n",
" self.z_out = tf.matmul(a_fc2, self.W_out) + b_out\n",
" \n",
" def create_loss(self):\n",
" with tf.name_scope('loss'):\n",
" softmax_loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=self.Y, logits=self.z_out))\n",
" \n",
" regularizer_loss_fc1 = tf.nn.l2_loss(self.W_fc1)\n",
" regularizer_loss_fc2 = tf.nn.l2_loss(self.W_fc2)\n",
" regularizer_loss_out = tf.nn.l2_loss(self.W_out)\n",
" regularizer_loss = self.lmbd*(regularizer_loss_fc1 + regularizer_loss_fc2 + regularizer_loss_out)\n",
" \n",
" self.loss = softmax_loss + regularizer_loss\n",
"\n",
" def create_accuracy(self):\n",
" with tf.name_scope('accuracy'):\n",
" probabilities = tf.nn.softmax(self.z_out)\n",
" predictions = tf.argmax(probabilities, axis=1)\n",
" labels = tf.argmax(self.Y, axis=1)\n",
" \n",
" correct_predictions = tf.equal(predictions, labels)\n",
" correct_predictions = tf.cast(correct_predictions, tf.float32)\n",
" self.accuracy = tf.reduce_mean(correct_predictions)\n",
" \n",
" def create_optimiser(self):\n",
" with tf.name_scope('optimizer'):\n",
" self.optimizer = tf.train.GradientDescentOptimizer(learning_rate=self.eta).minimize(self.loss, global_step=self.global_step)\n",
" \n",
" def weight_variable(self, shape, name='', dtype=tf.float32):\n",
" initial = tf.truncated_normal(shape, stddev=0.1)\n",
" return tf.Variable(initial, name=name, dtype=dtype)\n",
" \n",
" def bias_variable(self, shape, name='', dtype=tf.float32):\n",
" initial = tf.constant(0.1, shape=shape)\n",
" return tf.Variable(initial, name=name, dtype=dtype)\n",
" \n",
" def fit(self):\n",
" data_indices = np.arange(self.n_inputs)\n",
"\n",
" with tf.Session() as sess:\n",
" sess.run(tf.global_variables_initializer())\n",
" for i in range(self.epochs):\n",
" for j in range(self.iterations):\n",
" chosen_datapoints = np.random.choice(data_indices, size=self.batch_size, replace=False)\n",
" batch_X, batch_Y = self.X_train[chosen_datapoints], self.Y_train[chosen_datapoints]\n",
" \n",
" sess.run([DNN.loss, DNN.optimizer],\n",
" feed_dict={DNN.X: batch_X,\n",
" DNN.Y: batch_Y})\n",
" accuracy = sess.run(DNN.accuracy,\n",
" feed_dict={DNN.X: batch_X,\n",
" DNN.Y: batch_Y})\n",
" step = sess.run(DNN.global_step)\n",
" \n",
" self.train_loss, self.train_accuracy = sess.run([DNN.loss, DNN.accuracy],\n",
" feed_dict={DNN.X: self.X_train,\n",
" DNN.Y: self.Y_train})\n",
" \n",
" self.test_loss, self.test_accuracy = sess.run([DNN.loss, DNN.accuracy],\n",
" feed_dict={DNN.X: self.X_test,\n",
" DNN.Y: self.Y_test})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Optimizing and using gradient descent"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"epochs = 100\n",
"batch_size = 100\n",
"n_neurons_layer1 = 100\n",
"n_neurons_layer2 = 50\n",
"n_categories = 10\n",
"\n",
"eta_vals = np.logspace(-5, 1, 7)\n",
"lmbd_vals = np.logspace(-5, 1, 7)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"DNN_tf = np.zeros((len(eta_vals), len(lmbd_vals)), dtype=object)\n",
" \n",
"for i, eta in enumerate(eta_vals):\n",
" for j, lmbd in enumerate(lmbd_vals):\n",
" DNN = NeuralNetworkTensorflow(X_train, Y_train, X_test, Y_test,\n",
" n_neurons_layer1, n_neurons_layer2, n_categories,\n",
" epochs=epochs, batch_size=batch_size, eta=eta, lmbd=lmbd)\n",
" DNN.fit()\n",
" \n",
" DNN_tf[i][j] = DNN\n",
" \n",
" print(\"Learning rate = \", eta)\n",
" print(\"Lambda = \", lmbd)\n",
" print(\"Test accuracy: %.3f\" % DNN.test_accuracy)\n",
" print()"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# optional\n",
"# visual representation of grid search\n",
"# uses seaborn heatmap, could probably do this in matplotlib\n",
"import seaborn as sns\n",
"\n",
"sns.set()\n",
"\n",
"train_accuracy = np.zeros((len(eta_vals), len(lmbd_vals)))\n",
"test_accuracy = np.zeros((len(eta_vals), len(lmbd_vals)))\n",
"\n",
"for i in range(len(eta_vals)):\n",
" for j in range(len(lmbd_vals)):\n",
" DNN = DNN_tf[i][j]\n",
"\n",
" train_accuracy[i][j] = DNN.train_accuracy\n",
" test_accuracy[i][j] = DNN.test_accuracy\n",
"\n",
" \n",
"fig, ax = plt.subplots(figsize = (10, 10))\n",
"sns.heatmap(train_accuracy, annot=True, ax=ax, cmap=\"viridis\")\n",
"ax.set_title(\"Training Accuracy\")\n",
"ax.set_ylabel(\"$\\eta$\")\n",
"ax.set_xlabel(\"$\\lambda$\")\n",
"plt.show()\n",
"\n",
"fig, ax = plt.subplots(figsize = (10, 10))\n",
"sns.heatmap(test_accuracy, annot=True, ax=ax, cmap=\"viridis\")\n",
"ax.set_title(\"Test Accuracy\")\n",
"ax.set_ylabel(\"$\\eta$\")\n",
"ax.set_xlabel(\"$\\lambda$\")\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# optional\n",
"# we can use log files to visualize our graph in Tensorboard\n",
"writer = tf.summary.FileWriter('logs/')\n",
"writer.add_graph(tf.get_default_graph())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Using Keras\n",
"\n",
"Keras is a high level [neural network](https://en.wikipedia.org/wiki/Application_programming_interface)\n",
"that supports Tensorflow, CTNK and Theano as backends. \n",
"If you have Tensorflow installed Keras is available through the *tf.keras* module. \n",
"If you have Anaconda installed you may run the following command"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"conda install keras"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Alternatively, if you have Tensorflow or one of the other supported backends install you may use the pip package manager:"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"pip3 install keras"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"or look up the [instructions here](https://keras.io/)."
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from keras.models import Sequential\n",
"from keras.layers import Dense\n",
"from keras.regularizers import l2\n",
"from keras.optimizers import SGD\n",
"\n",
"def create_neural_network_keras(n_neurons_layer1, n_neurons_layer2, n_categories, eta, lmbd):\n",
" model = Sequential()\n",
" model.add(Dense(n_neurons_layer1, activation='sigmoid', kernel_regularizer=l2(lmbd)))\n",
" model.add(Dense(n_neurons_layer2, activation='sigmoid', kernel_regularizer=l2(lmbd)))\n",
" model.add(Dense(n_categories, activation='softmax'))\n",
" \n",
" sgd = SGD(lr=eta)\n",
" model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])\n",
" \n",
" return model"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"DNN_keras = np.zeros((len(eta_vals), len(lmbd_vals)), dtype=object)\n",
" \n",
"for i, eta in enumerate(eta_vals):\n",
" for j, lmbd in enumerate(lmbd_vals):\n",
" DNN = create_neural_network_keras(n_neurons_layer1, n_neurons_layer2, n_categories,\n",
" eta=eta, lmbd=lmbd)\n",
" DNN.fit(X_train, Y_train, epochs=epochs, batch_size=batch_size, verbose=0)\n",
" scores = DNN.evaluate(X_test, Y_test)\n",
" \n",
" DNN_keras[i][j] = DNN\n",
" \n",
" print(\"Learning rate = \", eta)\n",
" print(\"Lambda = \", lmbd)\n",
" print(\"Test accuracy: %.3f\" % scores[1])\n",
" print()"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# optional\n",
"# visual representation of grid search\n",
"# uses seaborn heatmap, could probably do this in matplotlib\n",
"import seaborn as sns\n",
"\n",
"sns.set()\n",
"\n",
"train_accuracy = np.zeros((len(eta_vals), len(lmbd_vals)))\n",
"test_accuracy = np.zeros((len(eta_vals), len(lmbd_vals)))\n",
"\n",
"for i in range(len(eta_vals)):\n",
" for j in range(len(lmbd_vals)):\n",
" DNN = DNN_keras[i][j]\n",
"\n",
" train_accuracy[i][j] = DNN.evaluate(X_train, Y_train)[1]\n",
" test_accuracy[i][j] = DNN.evaluate(X_test, Y_test)[1]\n",
"\n",
" \n",
"fig, ax = plt.subplots(figsize = (10, 10))\n",
"sns.heatmap(train_accuracy, annot=True, ax=ax, cmap=\"viridis\")\n",
"ax.set_title(\"Training Accuracy\")\n",
"ax.set_ylabel(\"$\\eta$\")\n",
"ax.set_xlabel(\"$\\lambda$\")\n",
"plt.show()\n",
"\n",
"fig, ax = plt.subplots(figsize = (10, 10))\n",
"sns.heatmap(test_accuracy, annot=True, ax=ax, cmap=\"viridis\")\n",
"ax.set_title(\"Test Accuracy\")\n",
"ax.set_ylabel(\"$\\eta$\")\n",
"ax.set_xlabel(\"$\\lambda$\")\n",
"plt.show()"
]
}
],
Binary file not shown.
Binary file not shown.
+1 -1
View File
@@ -1824,7 +1824,7 @@ conda install tensorflow
!split
===== Collect and pre-process data =====
bc pycod
!bc pycod
# import necessary packages
import numpy as np
import matplotlib.pyplot as plt