week 43
This commit is contained in:
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/doconce/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Convolutional Neural Networks and Recurrent Neural Networks">
|
||||
<meta name="description" content="Week 43: Deep Learning: Recurrent Neural Networks and other methods">
|
||||
|
||||
<title>Week 43: Convolutional Neural Networks and Recurrent Neural Networks</title>
|
||||
<title>Week 43: Deep Learning: Recurrent Neural Networks and other methods</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
@@ -42,7 +42,12 @@ Automatically generated HTML file from DocOnce source
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Plans for week 43', 2, None, 'plans-for-week-43'),
|
||||
('Summary on CNNs', 2, None, 'summary-on-cnns'),
|
||||
('Reading Recommendations', 2, None, 'reading-recommendations'),
|
||||
('Summary on Deep Learning Methods',
|
||||
2,
|
||||
None,
|
||||
'summary-on-deep-learning-methods'),
|
||||
('CNNs in brief', 2, None, 'cnns-in-brief'),
|
||||
('Recurrent neural networks: Overarching view',
|
||||
2,
|
||||
None,
|
||||
@@ -73,7 +78,7 @@ end of tocinfo -->
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Convolutional Neural Networks and Recurrent Neural Networks</a>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Deep Learning: Recurrent Neural Networks and other methods</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
@@ -82,15 +87,17 @@ end of tocinfo -->
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs001.html#plans-for-week-43" style="font-size: 80%;">Plans for week 43</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#summary-on-cnns" style="font-size: 80%;">Summary on CNNs</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#recurrent-neural-networks-overarching-view" style="font-size: 80%;">Recurrent neural networks: Overarching view</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#set-up-of-an-rnn" style="font-size: 80%;">Set up of an RNN</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#a-simple-example" style="font-size: 80%;">A simple example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#an-extrapolation-example" style="font-size: 80%;">An extrapolation example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#formatting-the-data" style="font-size: 80%;">Formatting the Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;">Predicting New Points With A Trained Recurrent Neural Network</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#other-things-to-try" style="font-size: 80%;">Other Things to Try</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;">Other Types of Recurrent Neural Networks</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#reading-recommendations" style="font-size: 80%;">Reading Recommendations</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#summary-on-deep-learning-methods" style="font-size: 80%;">Summary on Deep Learning Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#cnns-in-brief" style="font-size: 80%;">CNNs in brief</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#recurrent-neural-networks-overarching-view" style="font-size: 80%;">Recurrent neural networks: Overarching view</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#set-up-of-an-rnn" style="font-size: 80%;">Set up of an RNN</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#a-simple-example" style="font-size: 80%;">A simple example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#an-extrapolation-example" style="font-size: 80%;">An extrapolation example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#formatting-the-data" style="font-size: 80%;">Formatting the Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;">Predicting New Points With A Trained Recurrent Neural Network</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#other-things-to-try" style="font-size: 80%;">Other Things to Try</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;">Other Types of Recurrent Neural Networks</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -109,7 +116,7 @@ end of tocinfo -->
|
||||
|
||||
|
||||
<div class="jumbotron">
|
||||
<center><h1>Week 43: Convolutional Neural Networks and Recurrent Neural Networks</h1></center> <!-- document title -->
|
||||
<center><h1>Week 43: Deep Learning: Recurrent Neural Networks and other methods</h1></center> <!-- document title -->
|
||||
|
||||
<p>
|
||||
<!-- author(s): Morten Hjorth-Jensen -->
|
||||
@@ -125,7 +132,7 @@ end of tocinfo -->
|
||||
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
|
||||
<br>
|
||||
<p>
|
||||
<center><h4>Oct 24, 2021</h4></center> <!-- date -->
|
||||
<center><h4>Oct 26, 2021</h4></center> <!-- date -->
|
||||
<br>
|
||||
<p>
|
||||
|
||||
@@ -149,7 +156,7 @@ end of tocinfo -->
|
||||
<li><a href="._week43-bs008.html">9</a></li>
|
||||
<li><a href="._week43-bs009.html">10</a></li>
|
||||
<li><a href="">...</a></li>
|
||||
<li><a href="._week43-bs010.html">11</a></li>
|
||||
<li><a href="._week43-bs012.html">13</a></li>
|
||||
<li><a href="._week43-bs001.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/doconce/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Convolutional Neural Networks and Recurrent Neural Networks">
|
||||
<meta name="description" content="Week 43: Deep Learning: Recurrent Neural Networks and other methods">
|
||||
|
||||
<title>Week 43: Convolutional Neural Networks and Recurrent Neural Networks</title>
|
||||
<title>Week 43: Deep Learning: Recurrent Neural Networks and other methods</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
@@ -42,7 +42,12 @@ Automatically generated HTML file from DocOnce source
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Plans for week 43', 2, None, 'plans-for-week-43'),
|
||||
('Summary on CNNs', 2, None, 'summary-on-cnns'),
|
||||
('Reading Recommendations', 2, None, 'reading-recommendations'),
|
||||
('Summary on Deep Learning Methods',
|
||||
2,
|
||||
None,
|
||||
'summary-on-deep-learning-methods'),
|
||||
('CNNs in brief', 2, None, 'cnns-in-brief'),
|
||||
('Recurrent neural networks: Overarching view',
|
||||
2,
|
||||
None,
|
||||
@@ -73,7 +78,7 @@ end of tocinfo -->
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Convolutional Neural Networks and Recurrent Neural Networks</a>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Deep Learning: Recurrent Neural Networks and other methods</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
@@ -82,15 +87,17 @@ end of tocinfo -->
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="#plans-for-week-43" style="font-size: 80%;">Plans for week 43</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#summary-on-cnns" style="font-size: 80%;">Summary on CNNs</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#recurrent-neural-networks-overarching-view" style="font-size: 80%;">Recurrent neural networks: Overarching view</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#set-up-of-an-rnn" style="font-size: 80%;">Set up of an RNN</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#a-simple-example" style="font-size: 80%;">A simple example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#an-extrapolation-example" style="font-size: 80%;">An extrapolation example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#formatting-the-data" style="font-size: 80%;">Formatting the Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;">Predicting New Points With A Trained Recurrent Neural Network</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#other-things-to-try" style="font-size: 80%;">Other Things to Try</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;">Other Types of Recurrent Neural Networks</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#reading-recommendations" style="font-size: 80%;">Reading Recommendations</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#summary-on-deep-learning-methods" style="font-size: 80%;">Summary on Deep Learning Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#cnns-in-brief" style="font-size: 80%;">CNNs in brief</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#recurrent-neural-networks-overarching-view" style="font-size: 80%;">Recurrent neural networks: Overarching view</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#set-up-of-an-rnn" style="font-size: 80%;">Set up of an RNN</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#a-simple-example" style="font-size: 80%;">A simple example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#an-extrapolation-example" style="font-size: 80%;">An extrapolation example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#formatting-the-data" style="font-size: 80%;">Formatting the Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;">Predicting New Points With A Trained Recurrent Neural Network</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#other-things-to-try" style="font-size: 80%;">Other Things to Try</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;">Other Types of Recurrent Neural Networks</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -110,7 +117,7 @@ end of tocinfo -->
|
||||
|
||||
<ul>
|
||||
<li> Thursday: Summary of Convolutional Neural Networks from week 42 and Recurrent Neural Networks</li>
|
||||
<li> Friday: Recurrent Neural Networks and other Deep Learning methods</li>
|
||||
<li> Friday: Recurrent Neural Networks and other Deep Learning methods such as Generalized Adversarial Neural Networks</li>
|
||||
</ul>
|
||||
|
||||
<div class="panel panel-default">
|
||||
@@ -132,10 +139,9 @@ end of tocinfo -->
|
||||
<p> <!-- subsequent paragraphs come in larger fonts, so start with a paragraph -->
|
||||
|
||||
<ul>
|
||||
<li> <a href="https://www.uio.no/studier/emner/matnat/ifi/IN5400/v20/material/week10/in5400_2020_week10_recurrent_neural_network.pdf" target="_self">IN5400 at UiO Lecture</a></li>
|
||||
<li> <a href="https://www.uio.no/studier/emner/matnat/ifi/IN5400/v20/material/week10/in5400_2020_week10_recurrent_neural_network.pdf" target="_self">IN5400 at UiO Lecture</a></li>
|
||||
<li> <a href="https://www.youtube.com/watch?v=6niqTuYFZLQ&list=PLzUTmXVwsnXod6WNdg57Yc3zFx_f-RYsq&index=10&ab_channel=StanfordUniversitySchoolofEngineering" target="_self">CS231 at Stanford Lecture</a></li>
|
||||
</ul>
|
||||
|
||||
<li> <a href="https://www.youtube.com/watch?v=6niqTuYFZLQ&list=PLzUTmXVwsnXod6WNdg57Yc3zFx_f-RYsq&index=10&ab_channel=StanfordUniversitySchoolofEngineering" target="_self">CS231 at Stanford Lecture</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -156,6 +162,8 @@ end of tocinfo -->
|
||||
<li><a href="._week43-bs008.html">9</a></li>
|
||||
<li><a href="._week43-bs009.html">10</a></li>
|
||||
<li><a href="._week43-bs010.html">11</a></li>
|
||||
<li><a href="">...</a></li>
|
||||
<li><a href="._week43-bs012.html">13</a></li>
|
||||
<li><a href="._week43-bs002.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/doconce/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Convolutional Neural Networks and Recurrent Neural Networks">
|
||||
<meta name="description" content="Week 43: Deep Learning: Recurrent Neural Networks and other methods">
|
||||
|
||||
<title>Week 43: Convolutional Neural Networks and Recurrent Neural Networks</title>
|
||||
<title>Week 43: Deep Learning: Recurrent Neural Networks and other methods</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
@@ -42,7 +42,12 @@ Automatically generated HTML file from DocOnce source
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Plans for week 43', 2, None, 'plans-for-week-43'),
|
||||
('Summary on CNNs', 2, None, 'summary-on-cnns'),
|
||||
('Reading Recommendations', 2, None, 'reading-recommendations'),
|
||||
('Summary on Deep Learning Methods',
|
||||
2,
|
||||
None,
|
||||
'summary-on-deep-learning-methods'),
|
||||
('CNNs in brief', 2, None, 'cnns-in-brief'),
|
||||
('Recurrent neural networks: Overarching view',
|
||||
2,
|
||||
None,
|
||||
@@ -73,7 +78,7 @@ end of tocinfo -->
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Convolutional Neural Networks and Recurrent Neural Networks</a>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Deep Learning: Recurrent Neural Networks and other methods</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
@@ -82,15 +87,17 @@ end of tocinfo -->
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs001.html#plans-for-week-43" style="font-size: 80%;">Plans for week 43</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#summary-on-cnns" style="font-size: 80%;">Summary on CNNs</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#recurrent-neural-networks-overarching-view" style="font-size: 80%;">Recurrent neural networks: Overarching view</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#set-up-of-an-rnn" style="font-size: 80%;">Set up of an RNN</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#a-simple-example" style="font-size: 80%;">A simple example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#an-extrapolation-example" style="font-size: 80%;">An extrapolation example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#formatting-the-data" style="font-size: 80%;">Formatting the Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;">Predicting New Points With A Trained Recurrent Neural Network</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#other-things-to-try" style="font-size: 80%;">Other Things to Try</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;">Other Types of Recurrent Neural Networks</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#reading-recommendations" style="font-size: 80%;">Reading Recommendations</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#summary-on-deep-learning-methods" style="font-size: 80%;">Summary on Deep Learning Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#cnns-in-brief" style="font-size: 80%;">CNNs in brief</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#recurrent-neural-networks-overarching-view" style="font-size: 80%;">Recurrent neural networks: Overarching view</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#set-up-of-an-rnn" style="font-size: 80%;">Set up of an RNN</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#a-simple-example" style="font-size: 80%;">A simple example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#an-extrapolation-example" style="font-size: 80%;">An extrapolation example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#formatting-the-data" style="font-size: 80%;">Formatting the Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;">Predicting New Points With A Trained Recurrent Neural Network</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#other-things-to-try" style="font-size: 80%;">Other Things to Try</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;">Other Types of Recurrent Neural Networks</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -106,12 +113,13 @@ end of tocinfo -->
|
||||
<a name="part0002"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="summary-on-cnns" class="anchor">Summary on CNNs </h2>
|
||||
<h2 id="reading-recommendations" class="anchor">Reading Recommendations </h2>
|
||||
|
||||
<p>
|
||||
Material to be added
|
||||
<ul>
|
||||
<li> Goodfellow et al, chapter 10 on Recurrent NNs, chapters 11 and 12 on various practicalities around deep learning are also recommended.</li>
|
||||
<li> Aurelien Geron, chapter 14 on RNNs.</li>
|
||||
</ul>
|
||||
|
||||
<p>
|
||||
<p>
|
||||
<!-- navigation buttons at the bottom of the page -->
|
||||
<ul class="pagination">
|
||||
@@ -127,6 +135,9 @@ Material to be added
|
||||
<li><a href="._week43-bs008.html">9</a></li>
|
||||
<li><a href="._week43-bs009.html">10</a></li>
|
||||
<li><a href="._week43-bs010.html">11</a></li>
|
||||
<li><a href="._week43-bs011.html">12</a></li>
|
||||
<li><a href="">...</a></li>
|
||||
<li><a href="._week43-bs012.html">13</a></li>
|
||||
<li><a href="._week43-bs003.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/doconce/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Convolutional Neural Networks and Recurrent Neural Networks">
|
||||
<meta name="description" content="Week 43: Deep Learning: Recurrent Neural Networks and other methods">
|
||||
|
||||
<title>Week 43: Convolutional Neural Networks and Recurrent Neural Networks</title>
|
||||
<title>Week 43: Deep Learning: Recurrent Neural Networks and other methods</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
@@ -42,7 +42,12 @@ Automatically generated HTML file from DocOnce source
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Plans for week 43', 2, None, 'plans-for-week-43'),
|
||||
('Summary on CNNs', 2, None, 'summary-on-cnns'),
|
||||
('Reading Recommendations', 2, None, 'reading-recommendations'),
|
||||
('Summary on Deep Learning Methods',
|
||||
2,
|
||||
None,
|
||||
'summary-on-deep-learning-methods'),
|
||||
('CNNs in brief', 2, None, 'cnns-in-brief'),
|
||||
('Recurrent neural networks: Overarching view',
|
||||
2,
|
||||
None,
|
||||
@@ -73,7 +78,7 @@ end of tocinfo -->
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Convolutional Neural Networks and Recurrent Neural Networks</a>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Deep Learning: Recurrent Neural Networks and other methods</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
@@ -82,15 +87,17 @@ end of tocinfo -->
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs001.html#plans-for-week-43" style="font-size: 80%;">Plans for week 43</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#summary-on-cnns" style="font-size: 80%;">Summary on CNNs</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#recurrent-neural-networks-overarching-view" style="font-size: 80%;">Recurrent neural networks: Overarching view</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#set-up-of-an-rnn" style="font-size: 80%;">Set up of an RNN</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#a-simple-example" style="font-size: 80%;">A simple example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#an-extrapolation-example" style="font-size: 80%;">An extrapolation example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#formatting-the-data" style="font-size: 80%;">Formatting the Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;">Predicting New Points With A Trained Recurrent Neural Network</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#other-things-to-try" style="font-size: 80%;">Other Things to Try</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;">Other Types of Recurrent Neural Networks</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#reading-recommendations" style="font-size: 80%;">Reading Recommendations</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#summary-on-deep-learning-methods" style="font-size: 80%;">Summary on Deep Learning Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#cnns-in-brief" style="font-size: 80%;">CNNs in brief</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#recurrent-neural-networks-overarching-view" style="font-size: 80%;">Recurrent neural networks: Overarching view</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#set-up-of-an-rnn" style="font-size: 80%;">Set up of an RNN</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#a-simple-example" style="font-size: 80%;">A simple example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#an-extrapolation-example" style="font-size: 80%;">An extrapolation example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#formatting-the-data" style="font-size: 80%;">Formatting the Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;">Predicting New Points With A Trained Recurrent Neural Network</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#other-things-to-try" style="font-size: 80%;">Other Things to Try</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;">Other Types of Recurrent Neural Networks</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -106,27 +113,13 @@ end of tocinfo -->
|
||||
<a name="part0003"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="recurrent-neural-networks-overarching-view" class="anchor">Recurrent neural networks: Overarching view </h2>
|
||||
<h2 id="summary-on-deep-learning-methods" class="anchor">Summary on Deep Learning Methods </h2>
|
||||
|
||||
<p>
|
||||
Till now our focus has been, including convolutional neural networks
|
||||
as well, on feedforward neural networks. The output or the activations
|
||||
flow only in one direction, from the input layer to the output layer.
|
||||
We have studied fully connected neural networks (also called artifical nueral networks) and convolutional neural networks (CNNs).
|
||||
|
||||
<p>
|
||||
A recurrent neural network (RNN) looks very much like a feedforward
|
||||
neural network, except that it also has connections pointing
|
||||
backward.
|
||||
|
||||
<p>
|
||||
RNNs are used to analyze time series data such as stock prices, and
|
||||
tell you when to buy or sell. In autonomous driving systems, they can
|
||||
anticipate car trajectories and help avoid accidents. More generally,
|
||||
they can work on sequences of arbitrary lengths, rather than on
|
||||
fixed-sized inputs like all the nets we have discussed so far. For
|
||||
example, they can take sentences, documents, or audio samples as
|
||||
input, making them extremely useful for natural language processing
|
||||
systems such as automatic translation and speech-to-text.
|
||||
The first type of deep learning networks work very well on homogeneous and structured input data while CCNs are normally tailored to recognizing images.
|
||||
|
||||
<p>
|
||||
<p>
|
||||
@@ -144,6 +137,8 @@ systems such as automatic translation and speech-to-text.
|
||||
<li><a href="._week43-bs008.html">9</a></li>
|
||||
<li><a href="._week43-bs009.html">10</a></li>
|
||||
<li><a href="._week43-bs010.html">11</a></li>
|
||||
<li><a href="._week43-bs011.html">12</a></li>
|
||||
<li><a href="._week43-bs012.html">13</a></li>
|
||||
<li><a href="._week43-bs004.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/doconce/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Convolutional Neural Networks and Recurrent Neural Networks">
|
||||
<meta name="description" content="Week 43: Deep Learning: Recurrent Neural Networks and other methods">
|
||||
|
||||
<title>Week 43: Convolutional Neural Networks and Recurrent Neural Networks</title>
|
||||
<title>Week 43: Deep Learning: Recurrent Neural Networks and other methods</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
@@ -42,7 +42,12 @@ Automatically generated HTML file from DocOnce source
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Plans for week 43', 2, None, 'plans-for-week-43'),
|
||||
('Summary on CNNs', 2, None, 'summary-on-cnns'),
|
||||
('Reading Recommendations', 2, None, 'reading-recommendations'),
|
||||
('Summary on Deep Learning Methods',
|
||||
2,
|
||||
None,
|
||||
'summary-on-deep-learning-methods'),
|
||||
('CNNs in brief', 2, None, 'cnns-in-brief'),
|
||||
('Recurrent neural networks: Overarching view',
|
||||
2,
|
||||
None,
|
||||
@@ -73,7 +78,7 @@ end of tocinfo -->
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Convolutional Neural Networks and Recurrent Neural Networks</a>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Deep Learning: Recurrent Neural Networks and other methods</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
@@ -82,15 +87,17 @@ end of tocinfo -->
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs001.html#plans-for-week-43" style="font-size: 80%;">Plans for week 43</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#summary-on-cnns" style="font-size: 80%;">Summary on CNNs</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#recurrent-neural-networks-overarching-view" style="font-size: 80%;">Recurrent neural networks: Overarching view</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#set-up-of-an-rnn" style="font-size: 80%;">Set up of an RNN</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#a-simple-example" style="font-size: 80%;">A simple example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#an-extrapolation-example" style="font-size: 80%;">An extrapolation example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#formatting-the-data" style="font-size: 80%;">Formatting the Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;">Predicting New Points With A Trained Recurrent Neural Network</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#other-things-to-try" style="font-size: 80%;">Other Things to Try</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;">Other Types of Recurrent Neural Networks</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#reading-recommendations" style="font-size: 80%;">Reading Recommendations</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#summary-on-deep-learning-methods" style="font-size: 80%;">Summary on Deep Learning Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#cnns-in-brief" style="font-size: 80%;">CNNs in brief</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#recurrent-neural-networks-overarching-view" style="font-size: 80%;">Recurrent neural networks: Overarching view</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#set-up-of-an-rnn" style="font-size: 80%;">Set up of an RNN</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#a-simple-example" style="font-size: 80%;">A simple example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#an-extrapolation-example" style="font-size: 80%;">An extrapolation example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#formatting-the-data" style="font-size: 80%;">Formatting the Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;">Predicting New Points With A Trained Recurrent Neural Network</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#other-things-to-try" style="font-size: 80%;">Other Things to Try</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;">Other Types of Recurrent Neural Networks</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -106,10 +113,29 @@ end of tocinfo -->
|
||||
<a name="part0004"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="set-up-of-an-rnn" class="anchor">Set up of an RNN </h2>
|
||||
<h2 id="cnns-in-brief" class="anchor">CNNs in brief </h2>
|
||||
|
||||
<p>
|
||||
Text to come.
|
||||
In summary:
|
||||
|
||||
<ul>
|
||||
<li> A CNN architecture is in the simplest case a list of Layers that transform the image volume into an output volume (e.g. holding the class scores)</li>
|
||||
<li> There are a few distinct types of Layers (e.g. CONV/FC/RELU/POOL are by far the most popular)</li>
|
||||
<li> Each Layer accepts an input 3D volume and transforms it to an output 3D volume through a differentiable function</li>
|
||||
<li> Each Layer may or may not have parameters (e.g. CONV/FC do, RELU/POOL don’t)</li>
|
||||
<li> Each Layer may or may not have additional hyperparameters (e.g. CONV/FC/POOL do, RELU doesn’t)</li>
|
||||
</ul>
|
||||
|
||||
For more material on convolutional networks, we strongly recommend
|
||||
the course
|
||||
<a href="https://www.uio.no/studier/emner/matnat/ifi/IN5400/index-eng.html" target="_self">IN5400 – Machine Learning for Image Analysis</a>
|
||||
and the slides of <a href="http://cs231n.github.io/convolutional-networks/" target="_self">CS231</a> which is taught at Stanford University (consistently ranked as one of the top computer science programs in the world). <a href="http://neuralnetworksanddeeplearning.com/chap6.html" target="_self">Michael Nielsen's book is a must read, in particular chapter 6 which deals with CNNs</a>.
|
||||
|
||||
<p>
|
||||
However, both standard feed forwards networks and CNNs perform well on data with unknown length.
|
||||
|
||||
<p>
|
||||
This is where recurrent nueral networks (RNNs) come to our rescue.
|
||||
|
||||
<p>
|
||||
<p>
|
||||
@@ -127,6 +153,8 @@ Text to come.
|
||||
<li><a href="._week43-bs008.html">9</a></li>
|
||||
<li><a href="._week43-bs009.html">10</a></li>
|
||||
<li><a href="._week43-bs010.html">11</a></li>
|
||||
<li><a href="._week43-bs011.html">12</a></li>
|
||||
<li><a href="._week43-bs012.html">13</a></li>
|
||||
<li><a href="._week43-bs005.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/doconce/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Convolutional Neural Networks and Recurrent Neural Networks">
|
||||
<meta name="description" content="Week 43: Deep Learning: Recurrent Neural Networks and other methods">
|
||||
|
||||
<title>Week 43: Convolutional Neural Networks and Recurrent Neural Networks</title>
|
||||
<title>Week 43: Deep Learning: Recurrent Neural Networks and other methods</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
@@ -42,7 +42,12 @@ Automatically generated HTML file from DocOnce source
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Plans for week 43', 2, None, 'plans-for-week-43'),
|
||||
('Summary on CNNs', 2, None, 'summary-on-cnns'),
|
||||
('Reading Recommendations', 2, None, 'reading-recommendations'),
|
||||
('Summary on Deep Learning Methods',
|
||||
2,
|
||||
None,
|
||||
'summary-on-deep-learning-methods'),
|
||||
('CNNs in brief', 2, None, 'cnns-in-brief'),
|
||||
('Recurrent neural networks: Overarching view',
|
||||
2,
|
||||
None,
|
||||
@@ -73,7 +78,7 @@ end of tocinfo -->
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Convolutional Neural Networks and Recurrent Neural Networks</a>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Deep Learning: Recurrent Neural Networks and other methods</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
@@ -82,15 +87,17 @@ end of tocinfo -->
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs001.html#plans-for-week-43" style="font-size: 80%;">Plans for week 43</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#summary-on-cnns" style="font-size: 80%;">Summary on CNNs</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#recurrent-neural-networks-overarching-view" style="font-size: 80%;">Recurrent neural networks: Overarching view</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#set-up-of-an-rnn" style="font-size: 80%;">Set up of an RNN</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#a-simple-example" style="font-size: 80%;">A simple example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#an-extrapolation-example" style="font-size: 80%;">An extrapolation example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#formatting-the-data" style="font-size: 80%;">Formatting the Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;">Predicting New Points With A Trained Recurrent Neural Network</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#other-things-to-try" style="font-size: 80%;">Other Things to Try</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;">Other Types of Recurrent Neural Networks</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#reading-recommendations" style="font-size: 80%;">Reading Recommendations</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#summary-on-deep-learning-methods" style="font-size: 80%;">Summary on Deep Learning Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#cnns-in-brief" style="font-size: 80%;">CNNs in brief</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#recurrent-neural-networks-overarching-view" style="font-size: 80%;">Recurrent neural networks: Overarching view</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#set-up-of-an-rnn" style="font-size: 80%;">Set up of an RNN</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#a-simple-example" style="font-size: 80%;">A simple example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#an-extrapolation-example" style="font-size: 80%;">An extrapolation example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#formatting-the-data" style="font-size: 80%;">Formatting the Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;">Predicting New Points With A Trained Recurrent Neural Network</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#other-things-to-try" style="font-size: 80%;">Other Things to Try</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;">Other Types of Recurrent Neural Networks</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -106,80 +113,31 @@ end of tocinfo -->
|
||||
<a name="part0005"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="a-simple-example" class="anchor">A simple example </h2>
|
||||
<h2 id="recurrent-neural-networks-overarching-view" class="anchor">Recurrent neural networks: Overarching view </h2>
|
||||
|
||||
<p>
|
||||
Till now our focus has been, including convolutional neural networks
|
||||
as well, on feedforward neural networks. The output or the activations
|
||||
flow only in one direction, from the input layer to the output layer.
|
||||
|
||||
<!-- 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"># Start importing packages</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">pandas</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">pd</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">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">from</span> <span style="color: #0000FF; font-weight: bold">tensorflow.keras</span> <span style="color: #008000; font-weight: bold">import</span> datasets, layers, models
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">tensorflow.keras.layers</span> <span style="color: #008000; font-weight: bold">import</span> Input
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">tensorflow.keras.models</span> <span style="color: #008000; font-weight: bold">import</span> Model, Sequential
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">tensorflow.keras.layers</span> <span style="color: #008000; font-weight: bold">import</span> Dense, SimpleRNN, LSTM, GRU
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">tensorflow.keras</span> <span style="color: #008000; font-weight: bold">import</span> optimizers
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">tensorflow.keras</span> <span style="color: #008000; font-weight: bold">import</span> regularizers
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">tensorflow.keras.utils</span> <span style="color: #008000; font-weight: bold">import</span> to_categorical
|
||||
<p>
|
||||
A recurrent neural network (RNN) looks very much like a feedforward
|
||||
neural network, except that it also has connections pointing
|
||||
backward.
|
||||
|
||||
<p>
|
||||
RNNs are used to analyze time series data such as stock prices, and
|
||||
tell you when to buy or sell. In autonomous driving systems, they can
|
||||
anticipate car trajectories and help avoid accidents. More generally,
|
||||
they can work on sequences of arbitrary lengths, rather than on
|
||||
fixed-sized inputs like all the nets we have discussed so far. For
|
||||
example, they can take sentences, documents, or audio samples as
|
||||
input, making them extremely useful for natural language processing
|
||||
systems such as automatic translation and speech-to-text.
|
||||
|
||||
<p>
|
||||
RNNs are also used in "text classification":"Text Classification Algorithms: A Survey"
|
||||
|
||||
<span style="color: #408080; font-style: italic"># convert into dataset matrix</span>
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">convertToMatrix</span>(data, step):
|
||||
X, Y <span style="color: #666666">=</span>[], []
|
||||
<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>(data)<span style="color: #666666">-</span>step):
|
||||
d<span style="color: #666666">=</span>i<span style="color: #666666">+</span>step
|
||||
X<span style="color: #666666">.</span>append(data[i:d,])
|
||||
Y<span style="color: #666666">.</span>append(data[d,])
|
||||
<span style="color: #008000; font-weight: bold">return</span> np<span style="color: #666666">.</span>array(X), np<span style="color: #666666">.</span>array(Y)
|
||||
|
||||
step <span style="color: #666666">=</span> <span style="color: #666666">4</span>
|
||||
N <span style="color: #666666">=</span> <span style="color: #666666">1000</span>
|
||||
Tp <span style="color: #666666">=</span> <span style="color: #666666">800</span>
|
||||
|
||||
t<span style="color: #666666">=</span>np<span style="color: #666666">.</span>arange(<span style="color: #666666">0</span>,N)
|
||||
x<span style="color: #666666">=</span>np<span style="color: #666666">.</span>sin(<span style="color: #666666">0.02*</span>t)<span style="color: #666666">+2*</span>np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>rand(N)
|
||||
df <span style="color: #666666">=</span> pd<span style="color: #666666">.</span>DataFrame(x)
|
||||
df<span style="color: #666666">.</span>head()
|
||||
|
||||
plt<span style="color: #666666">.</span>plot(df)
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
|
||||
values<span style="color: #666666">=</span>df<span style="color: #666666">.</span>values
|
||||
train,test <span style="color: #666666">=</span> values[<span style="color: #666666">0</span>:Tp,:], values[Tp:N,:]
|
||||
|
||||
<span style="color: #408080; font-style: italic"># add step elements into train and test</span>
|
||||
test <span style="color: #666666">=</span> np<span style="color: #666666">.</span>append(test,np<span style="color: #666666">.</span>repeat(test[<span style="color: #666666">-1</span>,],step))
|
||||
train <span style="color: #666666">=</span> np<span style="color: #666666">.</span>append(train,np<span style="color: #666666">.</span>repeat(train[<span style="color: #666666">-1</span>,],step))
|
||||
|
||||
trainX,trainY <span style="color: #666666">=</span>convertToMatrix(train,step)
|
||||
testX,testY <span style="color: #666666">=</span>convertToMatrix(test,step)
|
||||
trainX <span style="color: #666666">=</span> np<span style="color: #666666">.</span>reshape(trainX, (trainX<span style="color: #666666">.</span>shape[<span style="color: #666666">0</span>], <span style="color: #666666">1</span>, trainX<span style="color: #666666">.</span>shape[<span style="color: #666666">1</span>]))
|
||||
testX <span style="color: #666666">=</span> np<span style="color: #666666">.</span>reshape(testX, (testX<span style="color: #666666">.</span>shape[<span style="color: #666666">0</span>], <span style="color: #666666">1</span>, testX<span style="color: #666666">.</span>shape[<span style="color: #666666">1</span>]))
|
||||
|
||||
model <span style="color: #666666">=</span> Sequential()
|
||||
model<span style="color: #666666">.</span>add(SimpleRNN(units<span style="color: #666666">=32</span>, input_shape<span style="color: #666666">=</span>(<span style="color: #666666">1</span>,step), activation<span style="color: #666666">=</span><span style="color: #BA2121">"relu"</span>))
|
||||
model<span style="color: #666666">.</span>add(Dense(<span style="color: #666666">8</span>, activation<span style="color: #666666">=</span><span style="color: #BA2121">"relu"</span>))
|
||||
model<span style="color: #666666">.</span>add(Dense(<span style="color: #666666">1</span>))
|
||||
model<span style="color: #666666">.</span>compile(loss<span style="color: #666666">=</span><span style="color: #BA2121">'mean_squared_error'</span>, optimizer<span style="color: #666666">=</span><span style="color: #BA2121">'rmsprop'</span>)
|
||||
model<span style="color: #666666">.</span>summary()
|
||||
|
||||
model<span style="color: #666666">.</span>fit(trainX,trainY, epochs<span style="color: #666666">=100</span>, batch_size<span style="color: #666666">=16</span>, verbose<span style="color: #666666">=2</span>)
|
||||
trainPredict <span style="color: #666666">=</span> model<span style="color: #666666">.</span>predict(trainX)
|
||||
testPredict<span style="color: #666666">=</span> model<span style="color: #666666">.</span>predict(testX)
|
||||
predicted<span style="color: #666666">=</span>np<span style="color: #666666">.</span>concatenate((trainPredict,testPredict),axis<span style="color: #666666">=0</span>)
|
||||
|
||||
trainScore <span style="color: #666666">=</span> model<span style="color: #666666">.</span>evaluate(trainX, trainY, verbose<span style="color: #666666">=0</span>)
|
||||
<span style="color: #008000">print</span>(trainScore)
|
||||
|
||||
index <span style="color: #666666">=</span> df<span style="color: #666666">.</span>index<span style="color: #666666">.</span>values
|
||||
plt<span style="color: #666666">.</span>plot(index,df)
|
||||
plt<span style="color: #666666">.</span>plot(index,predicted)
|
||||
plt<span style="color: #666666">.</span>axvline(df<span style="color: #666666">.</span>index[Tp], c<span style="color: #666666">=</span><span style="color: #BA2121">"r"</span>)
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
</pre></div>
|
||||
<p>
|
||||
<p>
|
||||
<!-- navigation buttons at the bottom of the page -->
|
||||
@@ -196,6 +154,8 @@ plt<span style="color: #666666">.</span>show()
|
||||
<li><a href="._week43-bs008.html">9</a></li>
|
||||
<li><a href="._week43-bs009.html">10</a></li>
|
||||
<li><a href="._week43-bs010.html">11</a></li>
|
||||
<li><a href="._week43-bs011.html">12</a></li>
|
||||
<li><a href="._week43-bs012.html">13</a></li>
|
||||
<li><a href="._week43-bs006.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/doconce/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Convolutional Neural Networks and Recurrent Neural Networks">
|
||||
<meta name="description" content="Week 43: Deep Learning: Recurrent Neural Networks and other methods">
|
||||
|
||||
<title>Week 43: Convolutional Neural Networks and Recurrent Neural Networks</title>
|
||||
<title>Week 43: Deep Learning: Recurrent Neural Networks and other methods</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
@@ -42,7 +42,12 @@ Automatically generated HTML file from DocOnce source
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Plans for week 43', 2, None, 'plans-for-week-43'),
|
||||
('Summary on CNNs', 2, None, 'summary-on-cnns'),
|
||||
('Reading Recommendations', 2, None, 'reading-recommendations'),
|
||||
('Summary on Deep Learning Methods',
|
||||
2,
|
||||
None,
|
||||
'summary-on-deep-learning-methods'),
|
||||
('CNNs in brief', 2, None, 'cnns-in-brief'),
|
||||
('Recurrent neural networks: Overarching view',
|
||||
2,
|
||||
None,
|
||||
@@ -73,7 +78,7 @@ end of tocinfo -->
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Convolutional Neural Networks and Recurrent Neural Networks</a>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Deep Learning: Recurrent Neural Networks and other methods</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
@@ -82,15 +87,17 @@ end of tocinfo -->
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs001.html#plans-for-week-43" style="font-size: 80%;">Plans for week 43</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#summary-on-cnns" style="font-size: 80%;">Summary on CNNs</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#recurrent-neural-networks-overarching-view" style="font-size: 80%;">Recurrent neural networks: Overarching view</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#set-up-of-an-rnn" style="font-size: 80%;">Set up of an RNN</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#a-simple-example" style="font-size: 80%;">A simple example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#an-extrapolation-example" style="font-size: 80%;">An extrapolation example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#formatting-the-data" style="font-size: 80%;">Formatting the Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;">Predicting New Points With A Trained Recurrent Neural Network</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#other-things-to-try" style="font-size: 80%;">Other Things to Try</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;">Other Types of Recurrent Neural Networks</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#reading-recommendations" style="font-size: 80%;">Reading Recommendations</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#summary-on-deep-learning-methods" style="font-size: 80%;">Summary on Deep Learning Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#cnns-in-brief" style="font-size: 80%;">CNNs in brief</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#recurrent-neural-networks-overarching-view" style="font-size: 80%;">Recurrent neural networks: Overarching view</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#set-up-of-an-rnn" style="font-size: 80%;">Set up of an RNN</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#a-simple-example" style="font-size: 80%;">A simple example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#an-extrapolation-example" style="font-size: 80%;">An extrapolation example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#formatting-the-data" style="font-size: 80%;">Formatting the Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;">Predicting New Points With A Trained Recurrent Neural Network</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#other-things-to-try" style="font-size: 80%;">Other Things to Try</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;">Other Types of Recurrent Neural Networks</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -106,45 +113,8 @@ end of tocinfo -->
|
||||
<a name="part0006"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="an-extrapolation-example" class="anchor">An extrapolation example </h2>
|
||||
<h2 id="set-up-of-an-rnn" class="anchor">Set up of an RNN </h2>
|
||||
|
||||
<p>
|
||||
The following code provides an example of how recurrent neural
|
||||
networks can be used to extrapolate to unknown values of physics data
|
||||
sets. Specifically, the data sets used in this program come from
|
||||
a quantum mechanical many-body calculation of energies as functions of the number of particles.
|
||||
|
||||
<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"># For matrices and calculations</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: #408080; font-style: italic"># For machine learning (backend for keras)</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: #408080; font-style: italic"># User-friendly machine learning library</span>
|
||||
<span style="color: #408080; font-style: italic"># Front end for TensorFlow</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">tensorflow.keras</span>
|
||||
<span style="color: #408080; font-style: italic"># Different methods from Keras needed to create an RNN</span>
|
||||
<span style="color: #408080; font-style: italic"># This is not necessary but it shortened function calls </span>
|
||||
<span style="color: #408080; font-style: italic"># that need to be used in the code.</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">tensorflow.keras</span> <span style="color: #008000; font-weight: bold">import</span> datasets, layers, models
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">tensorflow.keras.layers</span> <span style="color: #008000; font-weight: bold">import</span> Input
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">tensorflow.keras</span> <span style="color: #008000; font-weight: bold">import</span> regularizers
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">tensorflow.keras.models</span> <span style="color: #008000; font-weight: bold">import</span> Model, Sequential
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">tensorflow.keras.layers</span> <span style="color: #008000; font-weight: bold">import</span> Dense, SimpleRNN, LSTM, GRU
|
||||
<span style="color: #408080; font-style: italic"># For timing the code</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">timeit</span> <span style="color: #008000; font-weight: bold">import</span> default_timer <span style="color: #008000; font-weight: bold">as</span> timer
|
||||
<span style="color: #408080; font-style: italic"># For plotting</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: #408080; font-style: italic"># The data set</span>
|
||||
datatype<span style="color: #666666">=</span><span style="color: #BA2121">'VaryDimension'</span>
|
||||
X_tot <span style="color: #666666">=</span> np<span style="color: #666666">.</span>arange(<span style="color: #666666">2</span>, <span style="color: #666666">42</span>, <span style="color: #666666">2</span>)
|
||||
y_tot <span style="color: #666666">=</span> np<span style="color: #666666">.</span>array([<span style="color: #666666">-0.03077640549</span>, <span style="color: #666666">-0.08336233266</span>, <span style="color: #666666">-0.1446729567</span>, <span style="color: #666666">-0.2116753732</span>, <span style="color: #666666">-0.2830637392</span>, <span style="color: #666666">-0.3581341341</span>, <span style="color: #666666">-0.436462435</span>, <span style="color: #666666">-0.5177783846</span>,
|
||||
<span style="color: #666666">-0.6019067271</span>, <span style="color: #666666">-0.6887363571</span>, <span style="color: #666666">-0.7782028952</span>, <span style="color: #666666">-0.8702784034</span>, <span style="color: #666666">-0.9649652536</span>, <span style="color: #666666">-1.062292565</span>, <span style="color: #666666">-1.16231451</span>,
|
||||
<span style="color: #666666">-1.265109911</span>, <span style="color: #666666">-1.370782966</span>, <span style="color: #666666">-1.479465113</span>, <span style="color: #666666">-1.591317992</span>, <span style="color: #666666">-1.70653767</span>])
|
||||
</pre></div>
|
||||
<p>
|
||||
<p>
|
||||
<!-- navigation buttons at the bottom of the page -->
|
||||
@@ -161,6 +131,8 @@ y_tot <span style="color: #666666">=</span> np<span style="color: #666666">.</sp
|
||||
<li><a href="._week43-bs008.html">9</a></li>
|
||||
<li><a href="._week43-bs009.html">10</a></li>
|
||||
<li><a href="._week43-bs010.html">11</a></li>
|
||||
<li><a href="._week43-bs011.html">12</a></li>
|
||||
<li><a href="._week43-bs012.html">13</a></li>
|
||||
<li><a href="._week43-bs007.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/doconce/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Convolutional Neural Networks and Recurrent Neural Networks">
|
||||
<meta name="description" content="Week 43: Deep Learning: Recurrent Neural Networks and other methods">
|
||||
|
||||
<title>Week 43: Convolutional Neural Networks and Recurrent Neural Networks</title>
|
||||
<title>Week 43: Deep Learning: Recurrent Neural Networks and other methods</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
@@ -42,7 +42,12 @@ Automatically generated HTML file from DocOnce source
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Plans for week 43', 2, None, 'plans-for-week-43'),
|
||||
('Summary on CNNs', 2, None, 'summary-on-cnns'),
|
||||
('Reading Recommendations', 2, None, 'reading-recommendations'),
|
||||
('Summary on Deep Learning Methods',
|
||||
2,
|
||||
None,
|
||||
'summary-on-deep-learning-methods'),
|
||||
('CNNs in brief', 2, None, 'cnns-in-brief'),
|
||||
('Recurrent neural networks: Overarching view',
|
||||
2,
|
||||
None,
|
||||
@@ -73,7 +78,7 @@ end of tocinfo -->
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Convolutional Neural Networks and Recurrent Neural Networks</a>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Deep Learning: Recurrent Neural Networks and other methods</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
@@ -82,15 +87,17 @@ end of tocinfo -->
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs001.html#plans-for-week-43" style="font-size: 80%;">Plans for week 43</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#summary-on-cnns" style="font-size: 80%;">Summary on CNNs</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#recurrent-neural-networks-overarching-view" style="font-size: 80%;">Recurrent neural networks: Overarching view</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#set-up-of-an-rnn" style="font-size: 80%;">Set up of an RNN</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#a-simple-example" style="font-size: 80%;">A simple example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#an-extrapolation-example" style="font-size: 80%;">An extrapolation example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#formatting-the-data" style="font-size: 80%;">Formatting the Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;">Predicting New Points With A Trained Recurrent Neural Network</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#other-things-to-try" style="font-size: 80%;">Other Things to Try</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;">Other Types of Recurrent Neural Networks</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#reading-recommendations" style="font-size: 80%;">Reading Recommendations</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#summary-on-deep-learning-methods" style="font-size: 80%;">Summary on Deep Learning Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#cnns-in-brief" style="font-size: 80%;">CNNs in brief</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#recurrent-neural-networks-overarching-view" style="font-size: 80%;">Recurrent neural networks: Overarching view</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#set-up-of-an-rnn" style="font-size: 80%;">Set up of an RNN</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#a-simple-example" style="font-size: 80%;">A simple example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#an-extrapolation-example" style="font-size: 80%;">An extrapolation example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#formatting-the-data" style="font-size: 80%;">Formatting the Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;">Predicting New Points With A Trained Recurrent Neural Network</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#other-things-to-try" style="font-size: 80%;">Other Things to Try</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;">Other Types of Recurrent Neural Networks</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -106,119 +113,79 @@ end of tocinfo -->
|
||||
<a name="part0007"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="formatting-the-data" class="anchor">Formatting the Data </h2>
|
||||
|
||||
<p>
|
||||
The way the recurrent neural networks are trained in this program
|
||||
differs from how machine learning algorithms are usually trained.
|
||||
Typically a machine learning algorithm is trained by learning the
|
||||
relationship between the x data and the y data. In this program, the
|
||||
recurrent neural network will be trained to recognize the relationship
|
||||
in a sequence of y values. This is type of data formatting is
|
||||
typically used time series forcasting, but it can also be used in any
|
||||
extrapolation (time series forecasting is just a specific type of
|
||||
extrapolation along the time axis). This method of data formatting
|
||||
does not use the x data and assumes that the y data are evenly spaced.
|
||||
|
||||
<p>
|
||||
For a standard machine learning algorithm, the training data has the
|
||||
form of (x,y) so the machine learning algorithm learns to assiciate a
|
||||
y value with a given x value. This is useful when the test data has x
|
||||
values within the same range as the training data. However, for this
|
||||
application, the x values of the test data are outside of the x values
|
||||
of the training data and the traditional method of training a machine
|
||||
learning algorithm does not work as well. For this reason, the
|
||||
recurrent neural network is trained on sequences of y values of the
|
||||
form ((y1, y2), y3), so that the network is concerned with learning
|
||||
the pattern of the y data and not the relation between the x and y
|
||||
data. As long as the pattern of y data outside of the training region
|
||||
stays relatively stable compared to what was inside the training
|
||||
region, this method of training can produce accurate extrapolations to
|
||||
y values far removed from the training data set.
|
||||
|
||||
<p>
|
||||
<!-- -->
|
||||
<!-- The idea behind formatting the data in this way comes from [this resource](https://machinelearningmastery.com/time-series-prediction-lstm-recurrent-neural-networks-python-keras/) and [this one](https://fairyonice.github.io/Understand-Keras%27s-RNN-behind-the-scenes-with-a-sin-wave-example.html). -->
|
||||
<!-- -->
|
||||
<!-- The following method takes in a y data set and formats it so the "x data" are of the form (y1, y2) and the "y data" are of the form y3, with extra brackets added in to make the resulting arrays compatable with both Keras and Tensorflow. -->
|
||||
<!-- -->
|
||||
<!-- Note: Using a sequence length of two is not required for time series forecasting so any lenght of sequence could be used (for example instead of ((y1, y2) y3) you could change the length of sequence to be 4 and the resulting data points would have the form ((y1, y2, y3, y4), y5)). While the following method can be used to create a data set of any sequence length, the remainder of the code expects the length of sequence to be 2. This is because the data sets are very small and the higher the lenght of the sequence the less resulting data points. -->
|
||||
<h2 id="a-simple-example" class="anchor">A simple example </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"># FORMAT_DATA</span>
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">format_data</span>(data, length_of_sequence <span style="color: #666666">=</span> <span style="color: #666666">2</span>):
|
||||
<span style="color: #BA2121; font-style: italic">"""</span>
|
||||
<span style="color: #BA2121; font-style: italic"> Inputs:</span>
|
||||
<span style="color: #BA2121; font-style: italic"> data(a numpy array): the data that will be the inputs to the recurrent neural</span>
|
||||
<span style="color: #BA2121; font-style: italic"> network</span>
|
||||
<span style="color: #BA2121; font-style: italic"> length_of_sequence (an int): the number of elements in one iteration of the</span>
|
||||
<span style="color: #BA2121; font-style: italic"> sequence patter. For a function approximator use length_of_sequence = 2.</span>
|
||||
<span style="color: #BA2121; font-style: italic"> Returns:</span>
|
||||
<span style="color: #BA2121; font-style: italic"> rnn_input (a 3D numpy array): the input data for the recurrent neural network. Its</span>
|
||||
<span style="color: #BA2121; font-style: italic"> dimensions are length of data - length of sequence, length of sequence, </span>
|
||||
<span style="color: #BA2121; font-style: italic"> dimnsion of data</span>
|
||||
<span style="color: #BA2121; font-style: italic"> rnn_output (a numpy array): the training data for the neural network</span>
|
||||
<span style="color: #BA2121; font-style: italic"> Formats data to be used in a recurrent neural network.</span>
|
||||
<span style="color: #BA2121; font-style: italic"> """</span>
|
||||
|
||||
X, Y <span style="color: #666666">=</span> [], []
|
||||
<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>(data)<span style="color: #666666">-</span>length_of_sequence):
|
||||
<span style="color: #408080; font-style: italic"># Get the next length_of_sequence elements</span>
|
||||
a <span style="color: #666666">=</span> data[i:i<span style="color: #666666">+</span>length_of_sequence]
|
||||
<span style="color: #408080; font-style: italic"># Get the element that immediately follows that</span>
|
||||
b <span style="color: #666666">=</span> data[i<span style="color: #666666">+</span>length_of_sequence]
|
||||
<span style="color: #408080; font-style: italic"># Reshape so that each data point is contained in its own array</span>
|
||||
a <span style="color: #666666">=</span> np<span style="color: #666666">.</span>reshape (a, (<span style="color: #008000">len</span>(a), <span style="color: #666666">1</span>))
|
||||
X<span style="color: #666666">.</span>append(a)
|
||||
Y<span style="color: #666666">.</span>append(b)
|
||||
rnn_input <span style="color: #666666">=</span> np<span style="color: #666666">.</span>array(X)
|
||||
rnn_output <span style="color: #666666">=</span> np<span style="color: #666666">.</span>array(Y)
|
||||
|
||||
<span style="color: #008000; font-weight: bold">return</span> rnn_input, rnn_output
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%;"><span></span><span style="color: #408080; font-style: italic"># Start importing packages</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">pandas</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">pd</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">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">from</span> <span style="color: #0000FF; font-weight: bold">tensorflow.keras</span> <span style="color: #008000; font-weight: bold">import</span> datasets, layers, models
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">tensorflow.keras.layers</span> <span style="color: #008000; font-weight: bold">import</span> Input
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">tensorflow.keras.models</span> <span style="color: #008000; font-weight: bold">import</span> Model, Sequential
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">tensorflow.keras.layers</span> <span style="color: #008000; font-weight: bold">import</span> Dense, SimpleRNN, LSTM, GRU
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">tensorflow.keras</span> <span style="color: #008000; font-weight: bold">import</span> optimizers
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">tensorflow.keras</span> <span style="color: #008000; font-weight: bold">import</span> regularizers
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">tensorflow.keras.utils</span> <span style="color: #008000; font-weight: bold">import</span> to_categorical
|
||||
|
||||
|
||||
<span style="color: #408080; font-style: italic"># ## Defining the Recurrent Neural Network Using Keras</span>
|
||||
<span style="color: #408080; font-style: italic"># </span>
|
||||
<span style="color: #408080; font-style: italic"># The following method defines a simple recurrent neural network in keras consisting of one input layer, one hidden layer, and one output layer.</span>
|
||||
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">rnn</span>(length_of_sequences, batch_size <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">None</span>, stateful <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">False</span>):
|
||||
<span style="color: #BA2121; font-style: italic">"""</span>
|
||||
<span style="color: #BA2121; font-style: italic"> Inputs:</span>
|
||||
<span style="color: #BA2121; font-style: italic"> length_of_sequences (an int): the number of y values in "x data". This is determined</span>
|
||||
<span style="color: #BA2121; font-style: italic"> when the data is formatted</span>
|
||||
<span style="color: #BA2121; font-style: italic"> batch_size (an int): Default value is None. See Keras documentation of SimpleRNN.</span>
|
||||
<span style="color: #BA2121; font-style: italic"> stateful (a boolean): Default value is False. See Keras documentation of SimpleRNN.</span>
|
||||
<span style="color: #BA2121; font-style: italic"> Returns:</span>
|
||||
<span style="color: #BA2121; font-style: italic"> model (a Keras model): The recurrent neural network that is built and compiled by this</span>
|
||||
<span style="color: #BA2121; font-style: italic"> method</span>
|
||||
<span style="color: #BA2121; font-style: italic"> Builds and compiles a recurrent neural network with one hidden layer and returns the model.</span>
|
||||
<span style="color: #BA2121; font-style: italic"> """</span>
|
||||
<span style="color: #408080; font-style: italic"># Number of neurons in the input and output layers</span>
|
||||
in_out_neurons <span style="color: #666666">=</span> <span style="color: #666666">1</span>
|
||||
<span style="color: #408080; font-style: italic"># Number of neurons in the hidden layer</span>
|
||||
hidden_neurons <span style="color: #666666">=</span> <span style="color: #666666">200</span>
|
||||
<span style="color: #408080; font-style: italic"># Define the input layer</span>
|
||||
inp <span style="color: #666666">=</span> Input(batch_shape<span style="color: #666666">=</span>(batch_size,
|
||||
length_of_sequences,
|
||||
in_out_neurons))
|
||||
<span style="color: #408080; font-style: italic"># Define the hidden layer as a simple RNN layer with a set number of neurons and add it to </span>
|
||||
<span style="color: #408080; font-style: italic"># the network immediately after the input layer</span>
|
||||
rnn <span style="color: #666666">=</span> SimpleRNN(hidden_neurons,
|
||||
return_sequences<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">False</span>,
|
||||
stateful <span style="color: #666666">=</span> stateful,
|
||||
name<span style="color: #666666">=</span><span style="color: #BA2121">"RNN"</span>)(inp)
|
||||
<span style="color: #408080; font-style: italic"># Define the output layer as a dense neural network layer (standard neural network layer)</span>
|
||||
<span style="color: #408080; font-style: italic">#and add it to the network immediately after the hidden layer.</span>
|
||||
dens <span style="color: #666666">=</span> Dense(in_out_neurons,name<span style="color: #666666">=</span><span style="color: #BA2121">"dense"</span>)(rnn)
|
||||
<span style="color: #408080; font-style: italic"># Create the machine learning model starting with the input layer and ending with the </span>
|
||||
<span style="color: #408080; font-style: italic"># output layer</span>
|
||||
model <span style="color: #666666">=</span> Model(inputs<span style="color: #666666">=</span>[inp],outputs<span style="color: #666666">=</span>[dens])
|
||||
<span style="color: #408080; font-style: italic"># Compile the machine learning model using the mean squared error function as the loss </span>
|
||||
<span style="color: #408080; font-style: italic"># function and an Adams optimizer.</span>
|
||||
model<span style="color: #666666">.</span>compile(loss<span style="color: #666666">=</span><span style="color: #BA2121">"mean_squared_error"</span>, optimizer<span style="color: #666666">=</span><span style="color: #BA2121">"adam"</span>)
|
||||
<span style="color: #008000; font-weight: bold">return</span> model
|
||||
<span style="color: #408080; font-style: italic"># convert into dataset matrix</span>
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">convertToMatrix</span>(data, step):
|
||||
X, Y <span style="color: #666666">=</span>[], []
|
||||
<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>(data)<span style="color: #666666">-</span>step):
|
||||
d<span style="color: #666666">=</span>i<span style="color: #666666">+</span>step
|
||||
X<span style="color: #666666">.</span>append(data[i:d,])
|
||||
Y<span style="color: #666666">.</span>append(data[d,])
|
||||
<span style="color: #008000; font-weight: bold">return</span> np<span style="color: #666666">.</span>array(X), np<span style="color: #666666">.</span>array(Y)
|
||||
|
||||
step <span style="color: #666666">=</span> <span style="color: #666666">4</span>
|
||||
N <span style="color: #666666">=</span> <span style="color: #666666">1000</span>
|
||||
Tp <span style="color: #666666">=</span> <span style="color: #666666">800</span>
|
||||
|
||||
t<span style="color: #666666">=</span>np<span style="color: #666666">.</span>arange(<span style="color: #666666">0</span>,N)
|
||||
x<span style="color: #666666">=</span>np<span style="color: #666666">.</span>sin(<span style="color: #666666">0.02*</span>t)<span style="color: #666666">+2*</span>np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>rand(N)
|
||||
df <span style="color: #666666">=</span> pd<span style="color: #666666">.</span>DataFrame(x)
|
||||
df<span style="color: #666666">.</span>head()
|
||||
|
||||
plt<span style="color: #666666">.</span>plot(df)
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
|
||||
values<span style="color: #666666">=</span>df<span style="color: #666666">.</span>values
|
||||
train,test <span style="color: #666666">=</span> values[<span style="color: #666666">0</span>:Tp,:], values[Tp:N,:]
|
||||
|
||||
<span style="color: #408080; font-style: italic"># add step elements into train and test</span>
|
||||
test <span style="color: #666666">=</span> np<span style="color: #666666">.</span>append(test,np<span style="color: #666666">.</span>repeat(test[<span style="color: #666666">-1</span>,],step))
|
||||
train <span style="color: #666666">=</span> np<span style="color: #666666">.</span>append(train,np<span style="color: #666666">.</span>repeat(train[<span style="color: #666666">-1</span>,],step))
|
||||
|
||||
trainX,trainY <span style="color: #666666">=</span>convertToMatrix(train,step)
|
||||
testX,testY <span style="color: #666666">=</span>convertToMatrix(test,step)
|
||||
trainX <span style="color: #666666">=</span> np<span style="color: #666666">.</span>reshape(trainX, (trainX<span style="color: #666666">.</span>shape[<span style="color: #666666">0</span>], <span style="color: #666666">1</span>, trainX<span style="color: #666666">.</span>shape[<span style="color: #666666">1</span>]))
|
||||
testX <span style="color: #666666">=</span> np<span style="color: #666666">.</span>reshape(testX, (testX<span style="color: #666666">.</span>shape[<span style="color: #666666">0</span>], <span style="color: #666666">1</span>, testX<span style="color: #666666">.</span>shape[<span style="color: #666666">1</span>]))
|
||||
|
||||
model <span style="color: #666666">=</span> Sequential()
|
||||
model<span style="color: #666666">.</span>add(SimpleRNN(units<span style="color: #666666">=32</span>, input_shape<span style="color: #666666">=</span>(<span style="color: #666666">1</span>,step), activation<span style="color: #666666">=</span><span style="color: #BA2121">"relu"</span>))
|
||||
model<span style="color: #666666">.</span>add(Dense(<span style="color: #666666">8</span>, activation<span style="color: #666666">=</span><span style="color: #BA2121">"relu"</span>))
|
||||
model<span style="color: #666666">.</span>add(Dense(<span style="color: #666666">1</span>))
|
||||
model<span style="color: #666666">.</span>compile(loss<span style="color: #666666">=</span><span style="color: #BA2121">'mean_squared_error'</span>, optimizer<span style="color: #666666">=</span><span style="color: #BA2121">'rmsprop'</span>)
|
||||
model<span style="color: #666666">.</span>summary()
|
||||
|
||||
model<span style="color: #666666">.</span>fit(trainX,trainY, epochs<span style="color: #666666">=100</span>, batch_size<span style="color: #666666">=16</span>, verbose<span style="color: #666666">=2</span>)
|
||||
trainPredict <span style="color: #666666">=</span> model<span style="color: #666666">.</span>predict(trainX)
|
||||
testPredict<span style="color: #666666">=</span> model<span style="color: #666666">.</span>predict(testX)
|
||||
predicted<span style="color: #666666">=</span>np<span style="color: #666666">.</span>concatenate((trainPredict,testPredict),axis<span style="color: #666666">=0</span>)
|
||||
|
||||
trainScore <span style="color: #666666">=</span> model<span style="color: #666666">.</span>evaluate(trainX, trainY, verbose<span style="color: #666666">=0</span>)
|
||||
<span style="color: #008000">print</span>(trainScore)
|
||||
|
||||
index <span style="color: #666666">=</span> df<span style="color: #666666">.</span>index<span style="color: #666666">.</span>values
|
||||
plt<span style="color: #666666">.</span>plot(index,df)
|
||||
plt<span style="color: #666666">.</span>plot(index,predicted)
|
||||
plt<span style="color: #666666">.</span>axvline(df<span style="color: #666666">.</span>index[Tp], c<span style="color: #666666">=</span><span style="color: #BA2121">"r"</span>)
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
</pre></div>
|
||||
<p>
|
||||
<p>
|
||||
@@ -236,6 +203,8 @@ y values far removed from the training data set.
|
||||
<li><a href="._week43-bs008.html">9</a></li>
|
||||
<li><a href="._week43-bs009.html">10</a></li>
|
||||
<li><a href="._week43-bs010.html">11</a></li>
|
||||
<li><a href="._week43-bs011.html">12</a></li>
|
||||
<li><a href="._week43-bs012.html">13</a></li>
|
||||
<li><a href="._week43-bs008.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/doconce/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Convolutional Neural Networks and Recurrent Neural Networks">
|
||||
<meta name="description" content="Week 43: Deep Learning: Recurrent Neural Networks and other methods">
|
||||
|
||||
<title>Week 43: Convolutional Neural Networks and Recurrent Neural Networks</title>
|
||||
<title>Week 43: Deep Learning: Recurrent Neural Networks and other methods</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
@@ -42,7 +42,12 @@ Automatically generated HTML file from DocOnce source
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Plans for week 43', 2, None, 'plans-for-week-43'),
|
||||
('Summary on CNNs', 2, None, 'summary-on-cnns'),
|
||||
('Reading Recommendations', 2, None, 'reading-recommendations'),
|
||||
('Summary on Deep Learning Methods',
|
||||
2,
|
||||
None,
|
||||
'summary-on-deep-learning-methods'),
|
||||
('CNNs in brief', 2, None, 'cnns-in-brief'),
|
||||
('Recurrent neural networks: Overarching view',
|
||||
2,
|
||||
None,
|
||||
@@ -73,7 +78,7 @@ end of tocinfo -->
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Convolutional Neural Networks and Recurrent Neural Networks</a>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Deep Learning: Recurrent Neural Networks and other methods</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
@@ -82,15 +87,17 @@ end of tocinfo -->
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs001.html#plans-for-week-43" style="font-size: 80%;">Plans for week 43</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#summary-on-cnns" style="font-size: 80%;">Summary on CNNs</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#recurrent-neural-networks-overarching-view" style="font-size: 80%;">Recurrent neural networks: Overarching view</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#set-up-of-an-rnn" style="font-size: 80%;">Set up of an RNN</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#a-simple-example" style="font-size: 80%;">A simple example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#an-extrapolation-example" style="font-size: 80%;">An extrapolation example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#formatting-the-data" style="font-size: 80%;">Formatting the Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;">Predicting New Points With A Trained Recurrent Neural Network</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#other-things-to-try" style="font-size: 80%;">Other Things to Try</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;">Other Types of Recurrent Neural Networks</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#reading-recommendations" style="font-size: 80%;">Reading Recommendations</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#summary-on-deep-learning-methods" style="font-size: 80%;">Summary on Deep Learning Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#cnns-in-brief" style="font-size: 80%;">CNNs in brief</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#recurrent-neural-networks-overarching-view" style="font-size: 80%;">Recurrent neural networks: Overarching view</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#set-up-of-an-rnn" style="font-size: 80%;">Set up of an RNN</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#a-simple-example" style="font-size: 80%;">A simple example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#an-extrapolation-example" style="font-size: 80%;">An extrapolation example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#formatting-the-data" style="font-size: 80%;">Formatting the Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;">Predicting New Points With A Trained Recurrent Neural Network</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#other-things-to-try" style="font-size: 80%;">Other Things to Try</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;">Other Types of Recurrent Neural Networks</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -106,102 +113,44 @@ end of tocinfo -->
|
||||
<a name="part0008"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="predicting-new-points-with-a-trained-recurrent-neural-network" class="anchor">Predicting New Points With A Trained Recurrent Neural Network </h2>
|
||||
<h2 id="an-extrapolation-example" class="anchor">An extrapolation example </h2>
|
||||
|
||||
<p>
|
||||
The following code provides an example of how recurrent neural
|
||||
networks can be used to extrapolate to unknown values of physics data
|
||||
sets. Specifically, the data sets used in this program come from
|
||||
a quantum mechanical many-body calculation of energies as functions of the number of particles.
|
||||
|
||||
<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">def</span> <span style="color: #0000FF">test_rnn</span> (x1, y_test, plot_min, plot_max):
|
||||
<span style="color: #BA2121; font-style: italic">"""</span>
|
||||
<span style="color: #BA2121; font-style: italic"> Inputs:</span>
|
||||
<span style="color: #BA2121; font-style: italic"> x1 (a list or numpy array): The complete x component of the data set</span>
|
||||
<span style="color: #BA2121; font-style: italic"> y_test (a list or numpy array): The complete y component of the data set</span>
|
||||
<span style="color: #BA2121; font-style: italic"> plot_min (an int or float): the smallest x value used in the training data</span>
|
||||
<span style="color: #BA2121; font-style: italic"> plot_max (an int or float): the largest x valye used in the training data</span>
|
||||
<span style="color: #BA2121; font-style: italic"> Returns:</span>
|
||||
<span style="color: #BA2121; font-style: italic"> None.</span>
|
||||
<span style="color: #BA2121; font-style: italic"> Uses a trained recurrent neural network model to predict future points in the </span>
|
||||
<span style="color: #BA2121; font-style: italic"> series. Computes the MSE of the predicted data set from the true data set, saves</span>
|
||||
<span style="color: #BA2121; font-style: italic"> the predicted data set to a csv file, and plots the predicted and true data sets w</span>
|
||||
<span style="color: #BA2121; font-style: italic"> while also displaying the data range used for training.</span>
|
||||
<span style="color: #BA2121; font-style: italic"> """</span>
|
||||
<span style="color: #408080; font-style: italic"># Add the training data as the first dim points in the predicted data array as these</span>
|
||||
<span style="color: #408080; font-style: italic"># are known values.</span>
|
||||
y_pred <span style="color: #666666">=</span> y_test[:dim]<span style="color: #666666">.</span>tolist()
|
||||
<span style="color: #408080; font-style: italic"># Generate the first input to the trained recurrent neural network using the last two </span>
|
||||
<span style="color: #408080; font-style: italic"># points of the training data. Based on how the network was trained this means that it</span>
|
||||
<span style="color: #408080; font-style: italic"># will predict the first point in the data set after the training data. All of the </span>
|
||||
<span style="color: #408080; font-style: italic"># brackets are necessary for Tensorflow.</span>
|
||||
next_input <span style="color: #666666">=</span> np<span style="color: #666666">.</span>array([[[y_test[dim<span style="color: #666666">-2</span>]], [y_test[dim<span style="color: #666666">-1</span>]]]])
|
||||
<span style="color: #408080; font-style: italic"># Save the very last point in the training data set. This will be used later.</span>
|
||||
last <span style="color: #666666">=</span> [y_test[dim<span style="color: #666666">-1</span>]]
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Iterate until the complete data set is created.</span>
|
||||
<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> (dim, <span style="color: #008000">len</span>(y_test)):
|
||||
<span style="color: #408080; font-style: italic"># Predict the next point in the data set using the previous two points.</span>
|
||||
<span style="color: #008000">next</span> <span style="color: #666666">=</span> model<span style="color: #666666">.</span>predict(next_input)
|
||||
<span style="color: #408080; font-style: italic"># Append just the number of the predicted data set</span>
|
||||
y_pred<span style="color: #666666">.</span>append(<span style="color: #008000">next</span>[<span style="color: #666666">0</span>][<span style="color: #666666">0</span>])
|
||||
<span style="color: #408080; font-style: italic"># Create the input that will be used to predict the next data point in the data set.</span>
|
||||
next_input <span style="color: #666666">=</span> np<span style="color: #666666">.</span>array([[last, <span style="color: #008000">next</span>[<span style="color: #666666">0</span>]]], dtype<span style="color: #666666">=</span>np<span style="color: #666666">.</span>float64)
|
||||
last <span style="color: #666666">=</span> <span style="color: #008000">next</span>
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Print the mean squared error between the known data set and the predicted data set.</span>
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">'MSE: '</span>, np<span style="color: #666666">.</span>square(np<span style="color: #666666">.</span>subtract(y_test, y_pred))<span style="color: #666666">.</span>mean())
|
||||
<span style="color: #408080; font-style: italic"># Save the predicted data set as a csv file for later use</span>
|
||||
name <span style="color: #666666">=</span> datatype <span style="color: #666666">+</span> <span style="color: #BA2121">'Predicted'</span><span style="color: #666666">+</span><span style="color: #008000">str</span>(dim)<span style="color: #666666">+</span><span style="color: #BA2121">'.csv'</span>
|
||||
np<span style="color: #666666">.</span>savetxt(name, y_pred, delimiter<span style="color: #666666">=</span><span style="color: #BA2121">','</span>)
|
||||
<span style="color: #408080; font-style: italic"># Plot the known data set and the predicted data set. The red box represents the region that was used</span>
|
||||
<span style="color: #408080; font-style: italic"># for the training data.</span>
|
||||
fig, ax <span style="color: #666666">=</span> plt<span style="color: #666666">.</span>subplots()
|
||||
ax<span style="color: #666666">.</span>plot(x1, y_test, label<span style="color: #666666">=</span><span style="color: #BA2121">"true"</span>, linewidth<span style="color: #666666">=3</span>)
|
||||
ax<span style="color: #666666">.</span>plot(x1, y_pred, <span style="color: #BA2121">'g-.'</span>,label<span style="color: #666666">=</span><span style="color: #BA2121">"predicted"</span>, linewidth<span style="color: #666666">=4</span>)
|
||||
ax<span style="color: #666666">.</span>legend()
|
||||
<span style="color: #408080; font-style: italic"># Created a red region to represent the points used in the training data.</span>
|
||||
ax<span style="color: #666666">.</span>axvspan(plot_min, plot_max, alpha<span style="color: #666666">=0.25</span>, color<span style="color: #666666">=</span><span style="color: #BA2121">'red'</span>)
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Check to make sure the data set is complete</span>
|
||||
<span style="color: #008000; font-weight: bold">assert</span> <span style="color: #008000">len</span>(X_tot) <span style="color: #666666">==</span> <span style="color: #008000">len</span>(y_tot)
|
||||
|
||||
<span style="color: #408080; font-style: italic"># This is the number of points that will be used in as the training data</span>
|
||||
dim<span style="color: #666666">=12</span>
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Separate the training data from the whole data set</span>
|
||||
X_train <span style="color: #666666">=</span> X_tot[:dim]
|
||||
y_train <span style="color: #666666">=</span> y_tot[:dim]
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%;"><span></span><span style="color: #408080; font-style: italic"># For matrices and calculations</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: #408080; font-style: italic"># For machine learning (backend for keras)</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: #408080; font-style: italic"># User-friendly machine learning library</span>
|
||||
<span style="color: #408080; font-style: italic"># Front end for TensorFlow</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">tensorflow.keras</span>
|
||||
<span style="color: #408080; font-style: italic"># Different methods from Keras needed to create an RNN</span>
|
||||
<span style="color: #408080; font-style: italic"># This is not necessary but it shortened function calls </span>
|
||||
<span style="color: #408080; font-style: italic"># that need to be used in the code.</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">tensorflow.keras</span> <span style="color: #008000; font-weight: bold">import</span> datasets, layers, models
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">tensorflow.keras.layers</span> <span style="color: #008000; font-weight: bold">import</span> Input
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">tensorflow.keras</span> <span style="color: #008000; font-weight: bold">import</span> regularizers
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">tensorflow.keras.models</span> <span style="color: #008000; font-weight: bold">import</span> Model, Sequential
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">tensorflow.keras.layers</span> <span style="color: #008000; font-weight: bold">import</span> Dense, SimpleRNN, LSTM, GRU
|
||||
<span style="color: #408080; font-style: italic"># For timing the code</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">timeit</span> <span style="color: #008000; font-weight: bold">import</span> default_timer <span style="color: #008000; font-weight: bold">as</span> timer
|
||||
<span style="color: #408080; font-style: italic"># For plotting</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: #408080; font-style: italic"># Generate the training data for the RNN, using a sequence of 2</span>
|
||||
rnn_input, rnn_training <span style="color: #666666">=</span> format_data(y_train, <span style="color: #666666">2</span>)
|
||||
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Create a recurrent neural network in Keras and produce a summary of the </span>
|
||||
<span style="color: #408080; font-style: italic"># machine learning model</span>
|
||||
model <span style="color: #666666">=</span> rnn(length_of_sequences <span style="color: #666666">=</span> rnn_input<span style="color: #666666">.</span>shape[<span style="color: #666666">1</span>])
|
||||
model<span style="color: #666666">.</span>summary()
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Start the timer. Want to time training+testing</span>
|
||||
start <span style="color: #666666">=</span> timer()
|
||||
<span style="color: #408080; font-style: italic"># Fit the model using the training data genenerated above using 150 training iterations and a 5%</span>
|
||||
<span style="color: #408080; font-style: italic"># validation split. Setting verbose to True prints information about each training iteration.</span>
|
||||
hist <span style="color: #666666">=</span> model<span style="color: #666666">.</span>fit(rnn_input, rnn_training, batch_size<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">None</span>, epochs<span style="color: #666666">=150</span>,
|
||||
verbose<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">True</span>,validation_split<span style="color: #666666">=0.05</span>)
|
||||
|
||||
<span style="color: #008000; font-weight: bold">for</span> label <span style="color: #AA22FF; font-weight: bold">in</span> [<span style="color: #BA2121">"loss"</span>,<span style="color: #BA2121">"val_loss"</span>]:
|
||||
plt<span style="color: #666666">.</span>plot(hist<span style="color: #666666">.</span>history[label],label<span style="color: #666666">=</span>label)
|
||||
|
||||
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">"loss"</span>)
|
||||
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">"epoch"</span>)
|
||||
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">"The final validation loss: </span><span style="color: #BB6688; font-weight: bold">{}</span><span style="color: #BA2121">"</span><span style="color: #666666">.</span>format(hist<span style="color: #666666">.</span>history[<span style="color: #BA2121">"val_loss"</span>][<span style="color: #666666">-1</span>]))
|
||||
plt<span style="color: #666666">.</span>legend()
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Use the trained neural network to predict more points of the data set</span>
|
||||
test_rnn(X_tot, y_tot, X_tot[<span style="color: #666666">0</span>], X_tot[dim<span style="color: #666666">-1</span>])
|
||||
<span style="color: #408080; font-style: italic"># Stop the timer and calculate the total time needed.</span>
|
||||
end <span style="color: #666666">=</span> timer()
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">'Time: '</span>, end<span style="color: #666666">-</span>start)
|
||||
<span style="color: #408080; font-style: italic"># The data set</span>
|
||||
datatype<span style="color: #666666">=</span><span style="color: #BA2121">'VaryDimension'</span>
|
||||
X_tot <span style="color: #666666">=</span> np<span style="color: #666666">.</span>arange(<span style="color: #666666">2</span>, <span style="color: #666666">42</span>, <span style="color: #666666">2</span>)
|
||||
y_tot <span style="color: #666666">=</span> np<span style="color: #666666">.</span>array([<span style="color: #666666">-0.03077640549</span>, <span style="color: #666666">-0.08336233266</span>, <span style="color: #666666">-0.1446729567</span>, <span style="color: #666666">-0.2116753732</span>, <span style="color: #666666">-0.2830637392</span>, <span style="color: #666666">-0.3581341341</span>, <span style="color: #666666">-0.436462435</span>, <span style="color: #666666">-0.5177783846</span>,
|
||||
<span style="color: #666666">-0.6019067271</span>, <span style="color: #666666">-0.6887363571</span>, <span style="color: #666666">-0.7782028952</span>, <span style="color: #666666">-0.8702784034</span>, <span style="color: #666666">-0.9649652536</span>, <span style="color: #666666">-1.062292565</span>, <span style="color: #666666">-1.16231451</span>,
|
||||
<span style="color: #666666">-1.265109911</span>, <span style="color: #666666">-1.370782966</span>, <span style="color: #666666">-1.479465113</span>, <span style="color: #666666">-1.591317992</span>, <span style="color: #666666">-1.70653767</span>])
|
||||
</pre></div>
|
||||
<p>
|
||||
<p>
|
||||
@@ -219,6 +168,8 @@ end <span style="color: #666666">=</span> timer()
|
||||
<li class="active"><a href="._week43-bs008.html">9</a></li>
|
||||
<li><a href="._week43-bs009.html">10</a></li>
|
||||
<li><a href="._week43-bs010.html">11</a></li>
|
||||
<li><a href="._week43-bs011.html">12</a></li>
|
||||
<li><a href="._week43-bs012.html">13</a></li>
|
||||
<li><a href="._week43-bs009.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/doconce/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Convolutional Neural Networks and Recurrent Neural Networks">
|
||||
<meta name="description" content="Week 43: Deep Learning: Recurrent Neural Networks and other methods">
|
||||
|
||||
<title>Week 43: Convolutional Neural Networks and Recurrent Neural Networks</title>
|
||||
<title>Week 43: Deep Learning: Recurrent Neural Networks and other methods</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
@@ -42,7 +42,12 @@ Automatically generated HTML file from DocOnce source
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Plans for week 43', 2, None, 'plans-for-week-43'),
|
||||
('Summary on CNNs', 2, None, 'summary-on-cnns'),
|
||||
('Reading Recommendations', 2, None, 'reading-recommendations'),
|
||||
('Summary on Deep Learning Methods',
|
||||
2,
|
||||
None,
|
||||
'summary-on-deep-learning-methods'),
|
||||
('CNNs in brief', 2, None, 'cnns-in-brief'),
|
||||
('Recurrent neural networks: Overarching view',
|
||||
2,
|
||||
None,
|
||||
@@ -73,7 +78,7 @@ end of tocinfo -->
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Convolutional Neural Networks and Recurrent Neural Networks</a>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Deep Learning: Recurrent Neural Networks and other methods</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
@@ -82,15 +87,17 @@ end of tocinfo -->
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs001.html#plans-for-week-43" style="font-size: 80%;">Plans for week 43</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#summary-on-cnns" style="font-size: 80%;">Summary on CNNs</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#recurrent-neural-networks-overarching-view" style="font-size: 80%;">Recurrent neural networks: Overarching view</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#set-up-of-an-rnn" style="font-size: 80%;">Set up of an RNN</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#a-simple-example" style="font-size: 80%;">A simple example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#an-extrapolation-example" style="font-size: 80%;">An extrapolation example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#formatting-the-data" style="font-size: 80%;">Formatting the Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;">Predicting New Points With A Trained Recurrent Neural Network</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#other-things-to-try" style="font-size: 80%;">Other Things to Try</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;">Other Types of Recurrent Neural Networks</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#reading-recommendations" style="font-size: 80%;">Reading Recommendations</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#summary-on-deep-learning-methods" style="font-size: 80%;">Summary on Deep Learning Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#cnns-in-brief" style="font-size: 80%;">CNNs in brief</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#recurrent-neural-networks-overarching-view" style="font-size: 80%;">Recurrent neural networks: Overarching view</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#set-up-of-an-rnn" style="font-size: 80%;">Set up of an RNN</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#a-simple-example" style="font-size: 80%;">A simple example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#an-extrapolation-example" style="font-size: 80%;">An extrapolation example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#formatting-the-data" style="font-size: 80%;">Formatting the Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;">Predicting New Points With A Trained Recurrent Neural Network</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#other-things-to-try" style="font-size: 80%;">Other Things to Try</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;">Other Types of Recurrent Neural Networks</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -106,24 +113,84 @@ end of tocinfo -->
|
||||
<a name="part0009"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="other-things-to-try" class="anchor">Other Things to Try </h2>
|
||||
<h2 id="formatting-the-data" class="anchor">Formatting the Data </h2>
|
||||
|
||||
<p>
|
||||
Changing the size of the recurrent neural network and its parameters
|
||||
can drastically change the results you get from the model. The below
|
||||
code takes the simple recurrent neural network from above and adds a
|
||||
second hidden layer, changes the number of neurons in the hidden
|
||||
layer, and explicitly declares the activation function of the hidden
|
||||
layers to be a sigmoid function. The loss function and optimizer can
|
||||
also be changed but are kept the same as the above network. These
|
||||
parameters can be tuned to provide the optimal result from the
|
||||
network. For some ideas on how to improve the performance of a
|
||||
<a href="https://danijar.com/tips-for-training-recurrent-neural-networks" target="_self">recurrent neural network</a>.
|
||||
The way the recurrent neural networks are trained in this program
|
||||
differs from how machine learning algorithms are usually trained.
|
||||
Typically a machine learning algorithm is trained by learning the
|
||||
relationship between the x data and the y data. In this program, the
|
||||
recurrent neural network will be trained to recognize the relationship
|
||||
in a sequence of y values. This is type of data formatting is
|
||||
typically used time series forcasting, but it can also be used in any
|
||||
extrapolation (time series forecasting is just a specific type of
|
||||
extrapolation along the time axis). This method of data formatting
|
||||
does not use the x data and assumes that the y data are evenly spaced.
|
||||
|
||||
<p>
|
||||
For a standard machine learning algorithm, the training data has the
|
||||
form of (x,y) so the machine learning algorithm learns to assiciate a
|
||||
y value with a given x value. This is useful when the test data has x
|
||||
values within the same range as the training data. However, for this
|
||||
application, the x values of the test data are outside of the x values
|
||||
of the training data and the traditional method of training a machine
|
||||
learning algorithm does not work as well. For this reason, the
|
||||
recurrent neural network is trained on sequences of y values of the
|
||||
form ((y1, y2), y3), so that the network is concerned with learning
|
||||
the pattern of the y data and not the relation between the x and y
|
||||
data. As long as the pattern of y data outside of the training region
|
||||
stays relatively stable compared to what was inside the training
|
||||
region, this method of training can produce accurate extrapolations to
|
||||
y values far removed from the training data set.
|
||||
|
||||
<p>
|
||||
<!-- -->
|
||||
<!-- The idea behind formatting the data in this way comes from [this resource](https://machinelearningmastery.com/time-series-prediction-lstm-recurrent-neural-networks-python-keras/) and [this one](https://fairyonice.github.io/Understand-Keras%27s-RNN-behind-the-scenes-with-a-sin-wave-example.html). -->
|
||||
<!-- -->
|
||||
<!-- The following method takes in a y data set and formats it so the "x data" are of the form (y1, y2) and the "y data" are of the form y3, with extra brackets added in to make the resulting arrays compatable with both Keras and Tensorflow. -->
|
||||
<!-- -->
|
||||
<!-- Note: Using a sequence length of two is not required for time series forecasting so any lenght of sequence could be used (for example instead of ((y1, y2) y3) you could change the length of sequence to be 4 and the resulting data points would have the form ((y1, y2, y3, y4), y5)). While the following method can be used to create a data set of any sequence length, the remainder of the code expects the length of sequence to be 2. This is because the data sets are very small and the higher the lenght of the sequence the less resulting data points. -->
|
||||
|
||||
<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">def</span> <span style="color: #0000FF">rnn_2layers</span>(length_of_sequences, batch_size <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">None</span>, stateful <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">False</span>):
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%;"><span></span><span style="color: #408080; font-style: italic"># FORMAT_DATA</span>
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">format_data</span>(data, length_of_sequence <span style="color: #666666">=</span> <span style="color: #666666">2</span>):
|
||||
<span style="color: #BA2121; font-style: italic">"""</span>
|
||||
<span style="color: #BA2121; font-style: italic"> Inputs:</span>
|
||||
<span style="color: #BA2121; font-style: italic"> data(a numpy array): the data that will be the inputs to the recurrent neural</span>
|
||||
<span style="color: #BA2121; font-style: italic"> network</span>
|
||||
<span style="color: #BA2121; font-style: italic"> length_of_sequence (an int): the number of elements in one iteration of the</span>
|
||||
<span style="color: #BA2121; font-style: italic"> sequence patter. For a function approximator use length_of_sequence = 2.</span>
|
||||
<span style="color: #BA2121; font-style: italic"> Returns:</span>
|
||||
<span style="color: #BA2121; font-style: italic"> rnn_input (a 3D numpy array): the input data for the recurrent neural network. Its</span>
|
||||
<span style="color: #BA2121; font-style: italic"> dimensions are length of data - length of sequence, length of sequence, </span>
|
||||
<span style="color: #BA2121; font-style: italic"> dimnsion of data</span>
|
||||
<span style="color: #BA2121; font-style: italic"> rnn_output (a numpy array): the training data for the neural network</span>
|
||||
<span style="color: #BA2121; font-style: italic"> Formats data to be used in a recurrent neural network.</span>
|
||||
<span style="color: #BA2121; font-style: italic"> """</span>
|
||||
|
||||
X, Y <span style="color: #666666">=</span> [], []
|
||||
<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>(data)<span style="color: #666666">-</span>length_of_sequence):
|
||||
<span style="color: #408080; font-style: italic"># Get the next length_of_sequence elements</span>
|
||||
a <span style="color: #666666">=</span> data[i:i<span style="color: #666666">+</span>length_of_sequence]
|
||||
<span style="color: #408080; font-style: italic"># Get the element that immediately follows that</span>
|
||||
b <span style="color: #666666">=</span> data[i<span style="color: #666666">+</span>length_of_sequence]
|
||||
<span style="color: #408080; font-style: italic"># Reshape so that each data point is contained in its own array</span>
|
||||
a <span style="color: #666666">=</span> np<span style="color: #666666">.</span>reshape (a, (<span style="color: #008000">len</span>(a), <span style="color: #666666">1</span>))
|
||||
X<span style="color: #666666">.</span>append(a)
|
||||
Y<span style="color: #666666">.</span>append(b)
|
||||
rnn_input <span style="color: #666666">=</span> np<span style="color: #666666">.</span>array(X)
|
||||
rnn_output <span style="color: #666666">=</span> np<span style="color: #666666">.</span>array(Y)
|
||||
|
||||
<span style="color: #008000; font-weight: bold">return</span> rnn_input, rnn_output
|
||||
|
||||
|
||||
<span style="color: #408080; font-style: italic"># ## Defining the Recurrent Neural Network Using Keras</span>
|
||||
<span style="color: #408080; font-style: italic"># </span>
|
||||
<span style="color: #408080; font-style: italic"># The following method defines a simple recurrent neural network in keras consisting of one input layer, one hidden layer, and one output layer.</span>
|
||||
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">rnn</span>(length_of_sequences, batch_size <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">None</span>, stateful <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">False</span>):
|
||||
<span style="color: #BA2121; font-style: italic">"""</span>
|
||||
<span style="color: #BA2121; font-style: italic"> Inputs:</span>
|
||||
<span style="color: #BA2121; font-style: italic"> length_of_sequences (an int): the number of y values in "x data". This is determined</span>
|
||||
@@ -133,29 +200,25 @@ network. For some ideas on how to improve the performance of a
|
||||
<span style="color: #BA2121; font-style: italic"> Returns:</span>
|
||||
<span style="color: #BA2121; font-style: italic"> model (a Keras model): The recurrent neural network that is built and compiled by this</span>
|
||||
<span style="color: #BA2121; font-style: italic"> method</span>
|
||||
<span style="color: #BA2121; font-style: italic"> Builds and compiles a recurrent neural network with two hidden layers and returns the model.</span>
|
||||
<span style="color: #BA2121; font-style: italic"> Builds and compiles a recurrent neural network with one hidden layer and returns the model.</span>
|
||||
<span style="color: #BA2121; font-style: italic"> """</span>
|
||||
<span style="color: #408080; font-style: italic"># Number of neurons in the input and output layers</span>
|
||||
in_out_neurons <span style="color: #666666">=</span> <span style="color: #666666">1</span>
|
||||
<span style="color: #408080; font-style: italic"># Number of neurons in the hidden layer, increased from the first network</span>
|
||||
hidden_neurons <span style="color: #666666">=</span> <span style="color: #666666">500</span>
|
||||
<span style="color: #408080; font-style: italic"># Number of neurons in the hidden layer</span>
|
||||
hidden_neurons <span style="color: #666666">=</span> <span style="color: #666666">200</span>
|
||||
<span style="color: #408080; font-style: italic"># Define the input layer</span>
|
||||
inp <span style="color: #666666">=</span> Input(batch_shape<span style="color: #666666">=</span>(batch_size,
|
||||
length_of_sequences,
|
||||
in_out_neurons))
|
||||
<span style="color: #408080; font-style: italic"># Create two hidden layers instead of one hidden layer. Explicitly set the activation</span>
|
||||
<span style="color: #408080; font-style: italic"># function to be the sigmoid function (the default value is hyperbolic tangent)</span>
|
||||
rnn1 <span style="color: #666666">=</span> SimpleRNN(hidden_neurons,
|
||||
return_sequences<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">True</span>, <span style="color: #408080; font-style: italic"># This needs to be True if another hidden layer is to follow</span>
|
||||
stateful <span style="color: #666666">=</span> stateful, activation <span style="color: #666666">=</span> <span style="color: #BA2121">'sigmoid'</span>,
|
||||
name<span style="color: #666666">=</span><span style="color: #BA2121">"RNN1"</span>)(inp)
|
||||
rnn2 <span style="color: #666666">=</span> SimpleRNN(hidden_neurons,
|
||||
return_sequences<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">False</span>, activation <span style="color: #666666">=</span> <span style="color: #BA2121">'sigmoid'</span>,
|
||||
<span style="color: #408080; font-style: italic"># Define the hidden layer as a simple RNN layer with a set number of neurons and add it to </span>
|
||||
<span style="color: #408080; font-style: italic"># the network immediately after the input layer</span>
|
||||
rnn <span style="color: #666666">=</span> SimpleRNN(hidden_neurons,
|
||||
return_sequences<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">False</span>,
|
||||
stateful <span style="color: #666666">=</span> stateful,
|
||||
name<span style="color: #666666">=</span><span style="color: #BA2121">"RNN2"</span>)(rnn1)
|
||||
name<span style="color: #666666">=</span><span style="color: #BA2121">"RNN"</span>)(inp)
|
||||
<span style="color: #408080; font-style: italic"># Define the output layer as a dense neural network layer (standard neural network layer)</span>
|
||||
<span style="color: #408080; font-style: italic">#and add it to the network immediately after the hidden layer.</span>
|
||||
dens <span style="color: #666666">=</span> Dense(in_out_neurons,name<span style="color: #666666">=</span><span style="color: #BA2121">"dense"</span>)(rnn2)
|
||||
dens <span style="color: #666666">=</span> Dense(in_out_neurons,name<span style="color: #666666">=</span><span style="color: #BA2121">"dense"</span>)(rnn)
|
||||
<span style="color: #408080; font-style: italic"># Create the machine learning model starting with the input layer and ending with the </span>
|
||||
<span style="color: #408080; font-style: italic"># output layer</span>
|
||||
model <span style="color: #666666">=</span> Model(inputs<span style="color: #666666">=</span>[inp],outputs<span style="color: #666666">=</span>[dens])
|
||||
@@ -163,52 +226,6 @@ network. For some ideas on how to improve the performance of a
|
||||
<span style="color: #408080; font-style: italic"># function and an Adams optimizer.</span>
|
||||
model<span style="color: #666666">.</span>compile(loss<span style="color: #666666">=</span><span style="color: #BA2121">"mean_squared_error"</span>, optimizer<span style="color: #666666">=</span><span style="color: #BA2121">"adam"</span>)
|
||||
<span style="color: #008000; font-weight: bold">return</span> model
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Check to make sure the data set is complete</span>
|
||||
<span style="color: #008000; font-weight: bold">assert</span> <span style="color: #008000">len</span>(X_tot) <span style="color: #666666">==</span> <span style="color: #008000">len</span>(y_tot)
|
||||
|
||||
<span style="color: #408080; font-style: italic"># This is the number of points that will be used in as the training data</span>
|
||||
dim<span style="color: #666666">=12</span>
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Separate the training data from the whole data set</span>
|
||||
X_train <span style="color: #666666">=</span> X_tot[:dim]
|
||||
y_train <span style="color: #666666">=</span> y_tot[:dim]
|
||||
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Generate the training data for the RNN, using a sequence of 2</span>
|
||||
rnn_input, rnn_training <span style="color: #666666">=</span> format_data(y_train, <span style="color: #666666">2</span>)
|
||||
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Create a recurrent neural network in Keras and produce a summary of the </span>
|
||||
<span style="color: #408080; font-style: italic"># machine learning model</span>
|
||||
model <span style="color: #666666">=</span> rnn_2layers(length_of_sequences <span style="color: #666666">=</span> <span style="color: #666666">2</span>)
|
||||
model<span style="color: #666666">.</span>summary()
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Start the timer. Want to time training+testing</span>
|
||||
start <span style="color: #666666">=</span> timer()
|
||||
<span style="color: #408080; font-style: italic"># Fit the model using the training data genenerated above using 150 training iterations and a 5%</span>
|
||||
<span style="color: #408080; font-style: italic"># validation split. Setting verbose to True prints information about each training iteration.</span>
|
||||
hist <span style="color: #666666">=</span> model<span style="color: #666666">.</span>fit(rnn_input, rnn_training, batch_size<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">None</span>, epochs<span style="color: #666666">=150</span>,
|
||||
verbose<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">True</span>,validation_split<span style="color: #666666">=0.05</span>)
|
||||
|
||||
|
||||
<span style="color: #408080; font-style: italic"># This section plots the training loss and the validation loss as a function of training iteration.</span>
|
||||
<span style="color: #408080; font-style: italic"># This is not required for analyzing the couple cluster data but can help determine if the network is</span>
|
||||
<span style="color: #408080; font-style: italic"># being overtrained.</span>
|
||||
<span style="color: #008000; font-weight: bold">for</span> label <span style="color: #AA22FF; font-weight: bold">in</span> [<span style="color: #BA2121">"loss"</span>,<span style="color: #BA2121">"val_loss"</span>]:
|
||||
plt<span style="color: #666666">.</span>plot(hist<span style="color: #666666">.</span>history[label],label<span style="color: #666666">=</span>label)
|
||||
|
||||
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">"loss"</span>)
|
||||
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">"epoch"</span>)
|
||||
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">"The final validation loss: </span><span style="color: #BB6688; font-weight: bold">{}</span><span style="color: #BA2121">"</span><span style="color: #666666">.</span>format(hist<span style="color: #666666">.</span>history[<span style="color: #BA2121">"val_loss"</span>][<span style="color: #666666">-1</span>]))
|
||||
plt<span style="color: #666666">.</span>legend()
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Use the trained neural network to predict more points of the data set</span>
|
||||
test_rnn(X_tot, y_tot, X_tot[<span style="color: #666666">0</span>], X_tot[dim<span style="color: #666666">-1</span>])
|
||||
<span style="color: #408080; font-style: italic"># Stop the timer and calculate the total time needed.</span>
|
||||
end <span style="color: #666666">=</span> timer()
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">'Time: '</span>, end<span style="color: #666666">-</span>start)
|
||||
</pre></div>
|
||||
<p>
|
||||
<p>
|
||||
@@ -226,6 +243,8 @@ end <span style="color: #666666">=</span> timer()
|
||||
<li><a href="._week43-bs008.html">9</a></li>
|
||||
<li class="active"><a href="._week43-bs009.html">10</a></li>
|
||||
<li><a href="._week43-bs010.html">11</a></li>
|
||||
<li><a href="._week43-bs011.html">12</a></li>
|
||||
<li><a href="._week43-bs012.html">13</a></li>
|
||||
<li><a href="._week43-bs010.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/doconce/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Convolutional Neural Networks and Recurrent Neural Networks">
|
||||
<meta name="description" content="Week 43: Deep Learning: Recurrent Neural Networks and other methods">
|
||||
|
||||
<title>Week 43: Convolutional Neural Networks and Recurrent Neural Networks</title>
|
||||
<title>Week 43: Deep Learning: Recurrent Neural Networks and other methods</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
@@ -42,7 +42,12 @@ Automatically generated HTML file from DocOnce source
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Plans for week 43', 2, None, 'plans-for-week-43'),
|
||||
('Summary on CNNs', 2, None, 'summary-on-cnns'),
|
||||
('Reading Recommendations', 2, None, 'reading-recommendations'),
|
||||
('Summary on Deep Learning Methods',
|
||||
2,
|
||||
None,
|
||||
'summary-on-deep-learning-methods'),
|
||||
('CNNs in brief', 2, None, 'cnns-in-brief'),
|
||||
('Recurrent neural networks: Overarching view',
|
||||
2,
|
||||
None,
|
||||
@@ -73,7 +78,7 @@ end of tocinfo -->
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Convolutional Neural Networks and Recurrent Neural Networks</a>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Deep Learning: Recurrent Neural Networks and other methods</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
@@ -82,15 +87,17 @@ end of tocinfo -->
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs001.html#plans-for-week-43" style="font-size: 80%;">Plans for week 43</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#summary-on-cnns" style="font-size: 80%;">Summary on CNNs</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#recurrent-neural-networks-overarching-view" style="font-size: 80%;">Recurrent neural networks: Overarching view</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#set-up-of-an-rnn" style="font-size: 80%;">Set up of an RNN</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#a-simple-example" style="font-size: 80%;">A simple example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#an-extrapolation-example" style="font-size: 80%;">An extrapolation example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#formatting-the-data" style="font-size: 80%;">Formatting the Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;">Predicting New Points With A Trained Recurrent Neural Network</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#other-things-to-try" style="font-size: 80%;">Other Things to Try</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#other-types-of-recurrent-neural-networks" style="font-size: 80%;">Other Types of Recurrent Neural Networks</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#reading-recommendations" style="font-size: 80%;">Reading Recommendations</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#summary-on-deep-learning-methods" style="font-size: 80%;">Summary on Deep Learning Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#cnns-in-brief" style="font-size: 80%;">CNNs in brief</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#recurrent-neural-networks-overarching-view" style="font-size: 80%;">Recurrent neural networks: Overarching view</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#set-up-of-an-rnn" style="font-size: 80%;">Set up of an RNN</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#a-simple-example" style="font-size: 80%;">A simple example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#an-extrapolation-example" style="font-size: 80%;">An extrapolation example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#formatting-the-data" style="font-size: 80%;">Formatting the Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;">Predicting New Points With A Trained Recurrent Neural Network</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#other-things-to-try" style="font-size: 80%;">Other Things to Try</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;">Other Types of Recurrent Neural Networks</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -106,105 +113,60 @@ end of tocinfo -->
|
||||
<a name="part0010"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="other-types-of-recurrent-neural-networks" class="anchor">Other Types of Recurrent Neural Networks </h2>
|
||||
|
||||
<p>
|
||||
Besides a simple recurrent neural network layer, there are two other
|
||||
commonly used types of recurrent neural network layers: Long Short
|
||||
Term Memory (LSTM) and Gated Recurrent Unit (GRU). For a short
|
||||
introduction to these layers see <a href="https://medium.com/mindboard/lstm-vs-gru-experimental-comparison-955820c21e8b" target="_self"><tt>https://medium.com/mindboard/lstm-vs-gru-experimental-comparison-955820c21e8b</tt></a>
|
||||
and <a href="https://medium.com/mindboard/lstm-vs-gru-experimental-comparison-955820c21e8b" target="_self"><tt>https://medium.com/mindboard/lstm-vs-gru-experimental-comparison-955820c21e8b</tt></a>.
|
||||
|
||||
<p>
|
||||
The first network created below is similar to the previous network,
|
||||
but it replaces the SimpleRNN layers with LSTM layers. The second
|
||||
network below has two hidden layers made up of GRUs, which are
|
||||
preceeded by two dense (feeddorward) neural network layers. These
|
||||
dense layers "preprocess" the data before it reaches the recurrent
|
||||
layers. This architecture has been shown to improve the performance
|
||||
of recurrent neural networks (see the link above and also
|
||||
<a href="https://arxiv.org/pdf/1807.02857.pdf" target="_self"><tt>https://arxiv.org/pdf/1807.02857.pdf</tt></a>.
|
||||
<h2 id="predicting-new-points-with-a-trained-recurrent-neural-network" class="anchor">Predicting New Points With A Trained Recurrent Neural Network </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: #008000; font-weight: bold">def</span> <span style="color: #0000FF">lstm_2layers</span>(length_of_sequences, batch_size <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">None</span>, stateful <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">False</span>):
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%;"><span></span><span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">test_rnn</span> (x1, y_test, plot_min, plot_max):
|
||||
<span style="color: #BA2121; font-style: italic">"""</span>
|
||||
<span style="color: #BA2121; font-style: italic"> Inputs:</span>
|
||||
<span style="color: #BA2121; font-style: italic"> length_of_sequences (an int): the number of y values in "x data". This is determined</span>
|
||||
<span style="color: #BA2121; font-style: italic"> when the data is formatted</span>
|
||||
<span style="color: #BA2121; font-style: italic"> batch_size (an int): Default value is None. See Keras documentation of SimpleRNN.</span>
|
||||
<span style="color: #BA2121; font-style: italic"> stateful (a boolean): Default value is False. See Keras documentation of SimpleRNN.</span>
|
||||
<span style="color: #BA2121; font-style: italic"> x1 (a list or numpy array): The complete x component of the data set</span>
|
||||
<span style="color: #BA2121; font-style: italic"> y_test (a list or numpy array): The complete y component of the data set</span>
|
||||
<span style="color: #BA2121; font-style: italic"> plot_min (an int or float): the smallest x value used in the training data</span>
|
||||
<span style="color: #BA2121; font-style: italic"> plot_max (an int or float): the largest x valye used in the training data</span>
|
||||
<span style="color: #BA2121; font-style: italic"> Returns:</span>
|
||||
<span style="color: #BA2121; font-style: italic"> model (a Keras model): The recurrent neural network that is built and compiled by this</span>
|
||||
<span style="color: #BA2121; font-style: italic"> method</span>
|
||||
<span style="color: #BA2121; font-style: italic"> Builds and compiles a recurrent neural network with two LSTM hidden layers and returns the model.</span>
|
||||
<span style="color: #BA2121; font-style: italic"> None.</span>
|
||||
<span style="color: #BA2121; font-style: italic"> Uses a trained recurrent neural network model to predict future points in the </span>
|
||||
<span style="color: #BA2121; font-style: italic"> series. Computes the MSE of the predicted data set from the true data set, saves</span>
|
||||
<span style="color: #BA2121; font-style: italic"> the predicted data set to a csv file, and plots the predicted and true data sets w</span>
|
||||
<span style="color: #BA2121; font-style: italic"> while also displaying the data range used for training.</span>
|
||||
<span style="color: #BA2121; font-style: italic"> """</span>
|
||||
<span style="color: #408080; font-style: italic"># Number of neurons on the input/output layer and the number of neurons in the hidden layer</span>
|
||||
in_out_neurons <span style="color: #666666">=</span> <span style="color: #666666">1</span>
|
||||
hidden_neurons <span style="color: #666666">=</span> <span style="color: #666666">250</span>
|
||||
<span style="color: #408080; font-style: italic"># Input Layer</span>
|
||||
inp <span style="color: #666666">=</span> Input(batch_shape<span style="color: #666666">=</span>(batch_size,
|
||||
length_of_sequences,
|
||||
in_out_neurons))
|
||||
<span style="color: #408080; font-style: italic"># Hidden layers (in this case they are LSTM layers instead if SimpleRNN layers)</span>
|
||||
rnn<span style="color: #666666">=</span> LSTM(hidden_neurons,
|
||||
return_sequences<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">True</span>,
|
||||
stateful <span style="color: #666666">=</span> stateful,
|
||||
name<span style="color: #666666">=</span><span style="color: #BA2121">"RNN"</span>, use_bias<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">True</span>, activation<span style="color: #666666">=</span><span style="color: #BA2121">'tanh'</span>)(inp)
|
||||
rnn1 <span style="color: #666666">=</span> LSTM(hidden_neurons,
|
||||
return_sequences<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">False</span>,
|
||||
stateful <span style="color: #666666">=</span> stateful,
|
||||
name<span style="color: #666666">=</span><span style="color: #BA2121">"RNN1"</span>, use_bias<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">True</span>, activation<span style="color: #666666">=</span><span style="color: #BA2121">'tanh'</span>)(rnn)
|
||||
<span style="color: #408080; font-style: italic"># Output layer</span>
|
||||
dens <span style="color: #666666">=</span> Dense(in_out_neurons,name<span style="color: #666666">=</span><span style="color: #BA2121">"dense"</span>)(rnn1)
|
||||
<span style="color: #408080; font-style: italic"># Define the midel</span>
|
||||
model <span style="color: #666666">=</span> Model(inputs<span style="color: #666666">=</span>[inp],outputs<span style="color: #666666">=</span>[dens])
|
||||
<span style="color: #408080; font-style: italic"># Compile the model</span>
|
||||
model<span style="color: #666666">.</span>compile(loss<span style="color: #666666">=</span><span style="color: #BA2121">'mean_squared_error'</span>, optimizer<span style="color: #666666">=</span><span style="color: #BA2121">'adam'</span>)
|
||||
<span style="color: #408080; font-style: italic"># Return the model</span>
|
||||
<span style="color: #008000; font-weight: bold">return</span> model
|
||||
<span style="color: #408080; font-style: italic"># Add the training data as the first dim points in the predicted data array as these</span>
|
||||
<span style="color: #408080; font-style: italic"># are known values.</span>
|
||||
y_pred <span style="color: #666666">=</span> y_test[:dim]<span style="color: #666666">.</span>tolist()
|
||||
<span style="color: #408080; font-style: italic"># Generate the first input to the trained recurrent neural network using the last two </span>
|
||||
<span style="color: #408080; font-style: italic"># points of the training data. Based on how the network was trained this means that it</span>
|
||||
<span style="color: #408080; font-style: italic"># will predict the first point in the data set after the training data. All of the </span>
|
||||
<span style="color: #408080; font-style: italic"># brackets are necessary for Tensorflow.</span>
|
||||
next_input <span style="color: #666666">=</span> np<span style="color: #666666">.</span>array([[[y_test[dim<span style="color: #666666">-2</span>]], [y_test[dim<span style="color: #666666">-1</span>]]]])
|
||||
<span style="color: #408080; font-style: italic"># Save the very last point in the training data set. This will be used later.</span>
|
||||
last <span style="color: #666666">=</span> [y_test[dim<span style="color: #666666">-1</span>]]
|
||||
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">dnn2_gru2</span>(length_of_sequences, batch_size <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">None</span>, stateful <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">False</span>):
|
||||
<span style="color: #BA2121; font-style: italic">"""</span>
|
||||
<span style="color: #BA2121; font-style: italic"> Inputs:</span>
|
||||
<span style="color: #BA2121; font-style: italic"> length_of_sequences (an int): the number of y values in "x data". This is determined</span>
|
||||
<span style="color: #BA2121; font-style: italic"> when the data is formatted</span>
|
||||
<span style="color: #BA2121; font-style: italic"> batch_size (an int): Default value is None. See Keras documentation of SimpleRNN.</span>
|
||||
<span style="color: #BA2121; font-style: italic"> stateful (a boolean): Default value is False. See Keras documentation of SimpleRNN.</span>
|
||||
<span style="color: #BA2121; font-style: italic"> Returns:</span>
|
||||
<span style="color: #BA2121; font-style: italic"> model (a Keras model): The recurrent neural network that is built and compiled by this</span>
|
||||
<span style="color: #BA2121; font-style: italic"> method</span>
|
||||
<span style="color: #BA2121; font-style: italic"> Builds and compiles a recurrent neural network with four hidden layers (two dense followed by</span>
|
||||
<span style="color: #BA2121; font-style: italic"> two GRU layers) and returns the model.</span>
|
||||
<span style="color: #BA2121; font-style: italic"> """</span>
|
||||
<span style="color: #408080; font-style: italic"># Number of neurons on the input/output layers and hidden layers</span>
|
||||
in_out_neurons <span style="color: #666666">=</span> <span style="color: #666666">1</span>
|
||||
hidden_neurons <span style="color: #666666">=</span> <span style="color: #666666">250</span>
|
||||
<span style="color: #408080; font-style: italic"># Input layer</span>
|
||||
inp <span style="color: #666666">=</span> Input(batch_shape<span style="color: #666666">=</span>(batch_size,
|
||||
length_of_sequences,
|
||||
in_out_neurons))
|
||||
<span style="color: #408080; font-style: italic"># Hidden Dense (feedforward) layers</span>
|
||||
dnn <span style="color: #666666">=</span> Dense(hidden_neurons<span style="color: #666666">/2</span>, activation<span style="color: #666666">=</span><span style="color: #BA2121">'relu'</span>, name<span style="color: #666666">=</span><span style="color: #BA2121">'dnn'</span>)(inp)
|
||||
dnn1 <span style="color: #666666">=</span> Dense(hidden_neurons<span style="color: #666666">/2</span>, activation<span style="color: #666666">=</span><span style="color: #BA2121">'relu'</span>, name<span style="color: #666666">=</span><span style="color: #BA2121">'dnn1'</span>)(dnn)
|
||||
<span style="color: #408080; font-style: italic"># Hidden GRU layers</span>
|
||||
rnn1 <span style="color: #666666">=</span> GRU(hidden_neurons,
|
||||
return_sequences<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">True</span>,
|
||||
stateful <span style="color: #666666">=</span> stateful,
|
||||
name<span style="color: #666666">=</span><span style="color: #BA2121">"RNN1"</span>, use_bias<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">True</span>)(dnn1)
|
||||
rnn <span style="color: #666666">=</span> GRU(hidden_neurons,
|
||||
return_sequences<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">False</span>,
|
||||
stateful <span style="color: #666666">=</span> stateful,
|
||||
name<span style="color: #666666">=</span><span style="color: #BA2121">"RNN"</span>, use_bias<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">True</span>)(rnn1)
|
||||
<span style="color: #408080; font-style: italic"># Output layer</span>
|
||||
dens <span style="color: #666666">=</span> Dense(in_out_neurons,name<span style="color: #666666">=</span><span style="color: #BA2121">"dense"</span>)(rnn)
|
||||
<span style="color: #408080; font-style: italic"># Define the model</span>
|
||||
model <span style="color: #666666">=</span> Model(inputs<span style="color: #666666">=</span>[inp],outputs<span style="color: #666666">=</span>[dens])
|
||||
<span style="color: #408080; font-style: italic"># Compile the mdoel</span>
|
||||
model<span style="color: #666666">.</span>compile(loss<span style="color: #666666">=</span><span style="color: #BA2121">'mean_squared_error'</span>, optimizer<span style="color: #666666">=</span><span style="color: #BA2121">'adam'</span>)
|
||||
<span style="color: #408080; font-style: italic"># Return the model</span>
|
||||
<span style="color: #008000; font-weight: bold">return</span> model
|
||||
<span style="color: #408080; font-style: italic"># Iterate until the complete data set is created.</span>
|
||||
<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> (dim, <span style="color: #008000">len</span>(y_test)):
|
||||
<span style="color: #408080; font-style: italic"># Predict the next point in the data set using the previous two points.</span>
|
||||
<span style="color: #008000">next</span> <span style="color: #666666">=</span> model<span style="color: #666666">.</span>predict(next_input)
|
||||
<span style="color: #408080; font-style: italic"># Append just the number of the predicted data set</span>
|
||||
y_pred<span style="color: #666666">.</span>append(<span style="color: #008000">next</span>[<span style="color: #666666">0</span>][<span style="color: #666666">0</span>])
|
||||
<span style="color: #408080; font-style: italic"># Create the input that will be used to predict the next data point in the data set.</span>
|
||||
next_input <span style="color: #666666">=</span> np<span style="color: #666666">.</span>array([[last, <span style="color: #008000">next</span>[<span style="color: #666666">0</span>]]], dtype<span style="color: #666666">=</span>np<span style="color: #666666">.</span>float64)
|
||||
last <span style="color: #666666">=</span> <span style="color: #008000">next</span>
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Print the mean squared error between the known data set and the predicted data set.</span>
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">'MSE: '</span>, np<span style="color: #666666">.</span>square(np<span style="color: #666666">.</span>subtract(y_test, y_pred))<span style="color: #666666">.</span>mean())
|
||||
<span style="color: #408080; font-style: italic"># Save the predicted data set as a csv file for later use</span>
|
||||
name <span style="color: #666666">=</span> datatype <span style="color: #666666">+</span> <span style="color: #BA2121">'Predicted'</span><span style="color: #666666">+</span><span style="color: #008000">str</span>(dim)<span style="color: #666666">+</span><span style="color: #BA2121">'.csv'</span>
|
||||
np<span style="color: #666666">.</span>savetxt(name, y_pred, delimiter<span style="color: #666666">=</span><span style="color: #BA2121">','</span>)
|
||||
<span style="color: #408080; font-style: italic"># Plot the known data set and the predicted data set. The red box represents the region that was used</span>
|
||||
<span style="color: #408080; font-style: italic"># for the training data.</span>
|
||||
fig, ax <span style="color: #666666">=</span> plt<span style="color: #666666">.</span>subplots()
|
||||
ax<span style="color: #666666">.</span>plot(x1, y_test, label<span style="color: #666666">=</span><span style="color: #BA2121">"true"</span>, linewidth<span style="color: #666666">=3</span>)
|
||||
ax<span style="color: #666666">.</span>plot(x1, y_pred, <span style="color: #BA2121">'g-.'</span>,label<span style="color: #666666">=</span><span style="color: #BA2121">"predicted"</span>, linewidth<span style="color: #666666">=4</span>)
|
||||
ax<span style="color: #666666">.</span>legend()
|
||||
<span style="color: #408080; font-style: italic"># Created a red region to represent the points used in the training data.</span>
|
||||
ax<span style="color: #666666">.</span>axvspan(plot_min, plot_max, alpha<span style="color: #666666">=0.25</span>, color<span style="color: #666666">=</span><span style="color: #BA2121">'red'</span>)
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Check to make sure the data set is complete</span>
|
||||
<span style="color: #008000; font-weight: bold">assert</span> <span style="color: #008000">len</span>(X_tot) <span style="color: #666666">==</span> <span style="color: #008000">len</span>(y_tot)
|
||||
@@ -223,8 +185,7 @@ rnn_input, rnn_training <span style="color: #666666">=</span> format_data(y_trai
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Create a recurrent neural network in Keras and produce a summary of the </span>
|
||||
<span style="color: #408080; font-style: italic"># machine learning model</span>
|
||||
<span style="color: #408080; font-style: italic"># Change the method name to reflect which network you want to use</span>
|
||||
model <span style="color: #666666">=</span> dnn2_gru2(length_of_sequences <span style="color: #666666">=</span> <span style="color: #666666">2</span>)
|
||||
model <span style="color: #666666">=</span> rnn(length_of_sequences <span style="color: #666666">=</span> rnn_input<span style="color: #666666">.</span>shape[<span style="color: #666666">1</span>])
|
||||
model<span style="color: #666666">.</span>summary()
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Start the timer. Want to time training+testing</span>
|
||||
@@ -234,10 +195,6 @@ start <span style="color: #666666">=</span> timer()
|
||||
hist <span style="color: #666666">=</span> model<span style="color: #666666">.</span>fit(rnn_input, rnn_training, batch_size<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">None</span>, epochs<span style="color: #666666">=150</span>,
|
||||
verbose<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">True</span>,validation_split<span style="color: #666666">=0.05</span>)
|
||||
|
||||
|
||||
<span style="color: #408080; font-style: italic"># This section plots the training loss and the validation loss as a function of training iteration.</span>
|
||||
<span style="color: #408080; font-style: italic"># This is not required for analyzing the couple cluster data but can help determine if the network is</span>
|
||||
<span style="color: #408080; font-style: italic"># being overtrained.</span>
|
||||
<span style="color: #008000; font-weight: bold">for</span> label <span style="color: #AA22FF; font-weight: bold">in</span> [<span style="color: #BA2121">"loss"</span>,<span style="color: #BA2121">"val_loss"</span>]:
|
||||
plt<span style="color: #666666">.</span>plot(hist<span style="color: #666666">.</span>history[label],label<span style="color: #666666">=</span>label)
|
||||
|
||||
@@ -252,75 +209,8 @@ test_rnn(X_tot, y_tot, X_tot[<span style="color: #666666">0</span>], X_tot[dim<s
|
||||
<span style="color: #408080; font-style: italic"># Stop the timer and calculate the total time needed.</span>
|
||||
end <span style="color: #666666">=</span> timer()
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">'Time: '</span>, end<span style="color: #666666">-</span>start)
|
||||
|
||||
|
||||
<span style="color: #408080; font-style: italic"># ### Training Recurrent Neural Networks in the Standard Way (i.e. learning the relationship between the X and Y data)</span>
|
||||
<span style="color: #408080; font-style: italic"># </span>
|
||||
<span style="color: #408080; font-style: italic"># Finally, comparing the performace of a recurrent neural network using the standard data formatting to the performance of the network with time sequence data formatting shows the benefit of this type of data formatting with extrapolation.</span>
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Check to make sure the data set is complete</span>
|
||||
<span style="color: #008000; font-weight: bold">assert</span> <span style="color: #008000">len</span>(X_tot) <span style="color: #666666">==</span> <span style="color: #008000">len</span>(y_tot)
|
||||
|
||||
<span style="color: #408080; font-style: italic"># This is the number of points that will be used in as the training data</span>
|
||||
dim<span style="color: #666666">=12</span>
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Separate the training data from the whole data set</span>
|
||||
X_train <span style="color: #666666">=</span> X_tot[:dim]
|
||||
y_train <span style="color: #666666">=</span> y_tot[:dim]
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Reshape the data for Keras specifications</span>
|
||||
X_train <span style="color: #666666">=</span> X_train<span style="color: #666666">.</span>reshape((dim, <span style="color: #666666">1</span>))
|
||||
y_train <span style="color: #666666">=</span> y_train<span style="color: #666666">.</span>reshape((dim, <span style="color: #666666">1</span>))
|
||||
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Create a recurrent neural network in Keras and produce a summary of the </span>
|
||||
<span style="color: #408080; font-style: italic"># machine learning model</span>
|
||||
<span style="color: #408080; font-style: italic"># Set the sequence length to 1 for regular data formatting </span>
|
||||
model <span style="color: #666666">=</span> rnn(length_of_sequences <span style="color: #666666">=</span> <span style="color: #666666">1</span>)
|
||||
model<span style="color: #666666">.</span>summary()
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Start the timer. Want to time training+testing</span>
|
||||
start <span style="color: #666666">=</span> timer()
|
||||
<span style="color: #408080; font-style: italic"># Fit the model using the training data genenerated above using 150 training iterations and a 5%</span>
|
||||
<span style="color: #408080; font-style: italic"># validation split. Setting verbose to True prints information about each training iteration.</span>
|
||||
hist <span style="color: #666666">=</span> model<span style="color: #666666">.</span>fit(X_train, y_train, batch_size<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">None</span>, epochs<span style="color: #666666">=150</span>,
|
||||
verbose<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">True</span>,validation_split<span style="color: #666666">=0.05</span>)
|
||||
|
||||
|
||||
<span style="color: #408080; font-style: italic"># This section plots the training loss and the validation loss as a function of training iteration.</span>
|
||||
<span style="color: #408080; font-style: italic"># This is not required for analyzing the couple cluster data but can help determine if the network is</span>
|
||||
<span style="color: #408080; font-style: italic"># being overtrained.</span>
|
||||
<span style="color: #008000; font-weight: bold">for</span> label <span style="color: #AA22FF; font-weight: bold">in</span> [<span style="color: #BA2121">"loss"</span>,<span style="color: #BA2121">"val_loss"</span>]:
|
||||
plt<span style="color: #666666">.</span>plot(hist<span style="color: #666666">.</span>history[label],label<span style="color: #666666">=</span>label)
|
||||
|
||||
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">"loss"</span>)
|
||||
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">"epoch"</span>)
|
||||
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">"The final validation loss: </span><span style="color: #BB6688; font-weight: bold">{}</span><span style="color: #BA2121">"</span><span style="color: #666666">.</span>format(hist<span style="color: #666666">.</span>history[<span style="color: #BA2121">"val_loss"</span>][<span style="color: #666666">-1</span>]))
|
||||
plt<span style="color: #666666">.</span>legend()
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Use the trained neural network to predict the remaining data points</span>
|
||||
X_pred <span style="color: #666666">=</span> X_tot[dim:]
|
||||
X_pred <span style="color: #666666">=</span> X_pred<span style="color: #666666">.</span>reshape((<span style="color: #008000">len</span>(X_pred), <span style="color: #666666">1</span>))
|
||||
y_model <span style="color: #666666">=</span> model<span style="color: #666666">.</span>predict(X_pred)
|
||||
y_pred <span style="color: #666666">=</span> np<span style="color: #666666">.</span>concatenate((y_tot[:dim], y_model<span style="color: #666666">.</span>flatten()))
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Plot the known data set and the predicted data set. The red box represents the region that was used</span>
|
||||
<span style="color: #408080; font-style: italic"># for the training data.</span>
|
||||
fig, ax <span style="color: #666666">=</span> plt<span style="color: #666666">.</span>subplots()
|
||||
ax<span style="color: #666666">.</span>plot(X_tot, y_tot, label<span style="color: #666666">=</span><span style="color: #BA2121">"true"</span>, linewidth<span style="color: #666666">=3</span>)
|
||||
ax<span style="color: #666666">.</span>plot(X_tot, y_pred, <span style="color: #BA2121">'g-.'</span>,label<span style="color: #666666">=</span><span style="color: #BA2121">"predicted"</span>, linewidth<span style="color: #666666">=4</span>)
|
||||
ax<span style="color: #666666">.</span>legend()
|
||||
<span style="color: #408080; font-style: italic"># Created a red region to represent the points used in the training data.</span>
|
||||
ax<span style="color: #666666">.</span>axvspan(X_tot[<span style="color: #666666">0</span>], X_tot[dim], alpha<span style="color: #666666">=0.25</span>, color<span style="color: #666666">=</span><span style="color: #BA2121">'red'</span>)
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Stop the timer and calculate the total time needed.</span>
|
||||
end <span style="color: #666666">=</span> timer()
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">'Time: '</span>, end<span style="color: #666666">-</span>start)
|
||||
</pre></div>
|
||||
<p>
|
||||
|
||||
<p>
|
||||
<!-- navigation buttons at the bottom of the page -->
|
||||
<ul class="pagination">
|
||||
@@ -336,6 +226,9 @@ end <span style="color: #666666">=</span> timer()
|
||||
<li><a href="._week43-bs008.html">9</a></li>
|
||||
<li><a href="._week43-bs009.html">10</a></li>
|
||||
<li class="active"><a href="._week43-bs010.html">11</a></li>
|
||||
<li><a href="._week43-bs011.html">12</a></li>
|
||||
<li><a href="._week43-bs012.html">13</a></li>
|
||||
<li><a href="._week43-bs011.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
|
||||
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/doconce/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Convolutional Neural Networks and Recurrent Neural Networks">
|
||||
<meta name="description" content="Week 43: Deep Learning: Recurrent Neural Networks and other methods">
|
||||
|
||||
<title>Week 43: Convolutional Neural Networks and Recurrent Neural Networks</title>
|
||||
<title>Week 43: Deep Learning: Recurrent Neural Networks and other methods</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
@@ -42,60 +42,12 @@ Automatically generated HTML file from DocOnce source
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Plans for week 43', 2, None, 'plans-for-week-43'),
|
||||
('Convolutional Neural Networks (recognizing images)',
|
||||
('Reading Recommendations', 2, None, 'reading-recommendations'),
|
||||
('Summary on Deep Learning Methods',
|
||||
2,
|
||||
None,
|
||||
'convolutional-neural-networks-recognizing-images'),
|
||||
('Neural Networks vs CNNs', 2, None, 'neural-networks-vs-cnns'),
|
||||
('Why CNNS for images, sound files, medical images from CT scans '
|
||||
'etc?',
|
||||
2,
|
||||
None,
|
||||
'why-cnns-for-images-sound-files-medical-images-from-ct-scans-etc'),
|
||||
('Regular NNs don’t scale well to full images',
|
||||
2,
|
||||
None,
|
||||
'regular-nns-don-t-scale-well-to-full-images'),
|
||||
('3D volumes of neurons', 2, None, '3d-volumes-of-neurons'),
|
||||
('Layers used to build CNNs',
|
||||
2,
|
||||
None,
|
||||
'layers-used-to-build-cnns'),
|
||||
('Transforming images', 2, None, 'transforming-images'),
|
||||
'summary-on-deep-learning-methods'),
|
||||
('CNNs in brief', 2, None, 'cnns-in-brief'),
|
||||
('CNNs in more detail, building convolutional neural networks in '
|
||||
'Tensorflow and Keras',
|
||||
2,
|
||||
None,
|
||||
'cnns-in-more-detail-building-convolutional-neural-networks-in-tensorflow-and-keras'),
|
||||
('Setting it up', 2, None, 'setting-it-up'),
|
||||
('The MNIST dataset again', 2, None, 'the-mnist-dataset-again'),
|
||||
('Strong correlations', 2, None, 'strong-correlations'),
|
||||
('Layers of a CNN', 2, None, 'layers-of-a-cnn'),
|
||||
('Systematic reduction', 2, None, 'systematic-reduction'),
|
||||
('Prerequisites: Collect and pre-process data',
|
||||
2,
|
||||
None,
|
||||
'prerequisites-collect-and-pre-process-data'),
|
||||
('Importing Keras and Tensorflow',
|
||||
2,
|
||||
None,
|
||||
'importing-keras-and-tensorflow'),
|
||||
('Running with Keras', 2, None, 'running-with-keras'),
|
||||
('Final part', 2, None, 'final-part'),
|
||||
('Final visualization', 2, None, 'final-visualization'),
|
||||
('The CIFAR01 data set', 2, None, 'the-cifar01-data-set'),
|
||||
('Verifying the data set', 2, None, 'verifying-the-data-set'),
|
||||
('Set up the model', 2, None, 'set-up-the-model'),
|
||||
('Add Dense layers on top', 2, None, 'add-dense-layers-on-top'),
|
||||
('Compile and train the model',
|
||||
2,
|
||||
None,
|
||||
'compile-and-train-the-model'),
|
||||
('Finally, evaluate the model',
|
||||
2,
|
||||
None,
|
||||
'finally-evaluate-the-model'),
|
||||
('Recurrent neural networks: Overarching view',
|
||||
2,
|
||||
None,
|
||||
@@ -117,22 +69,6 @@ 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">
|
||||
@@ -142,7 +78,7 @@ MathJax.Hub.Config({
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Convolutional Neural Networks and Recurrent Neural Networks</a>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Deep Learning: Recurrent Neural Networks and other methods</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
@@ -151,39 +87,17 @@ MathJax.Hub.Config({
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs001.html#plans-for-week-43" style="font-size: 80%;">Plans for week 43</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#convolutional-neural-networks-recognizing-images" style="font-size: 80%;">Convolutional Neural Networks (recognizing images)</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#neural-networks-vs-cnns" style="font-size: 80%;">Neural Networks vs CNNs</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#why-cnns-for-images-sound-files-medical-images-from-ct-scans-etc" style="font-size: 80%;">Why CNNS for images, sound files, medical images from CT scans etc?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#regular-nns-don-t-scale-well-to-full-images" style="font-size: 80%;">Regular NNs don’t scale well to full images</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#3d-volumes-of-neurons" style="font-size: 80%;">3D volumes of neurons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#layers-used-to-build-cnns" style="font-size: 80%;">Layers used to build CNNs</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#transforming-images" style="font-size: 80%;">Transforming images</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#cnns-in-brief" style="font-size: 80%;">CNNs in brief</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#cnns-in-more-detail-building-convolutional-neural-networks-in-tensorflow-and-keras" style="font-size: 80%;">CNNs in more detail, building convolutional neural networks in Tensorflow and Keras</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#setting-it-up" style="font-size: 80%;">Setting it up</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#the-mnist-dataset-again" style="font-size: 80%;">The MNIST dataset again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#strong-correlations" style="font-size: 80%;">Strong correlations</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#layers-of-a-cnn" style="font-size: 80%;">Layers of a CNN</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#systematic-reduction" style="font-size: 80%;">Systematic reduction</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#prerequisites-collect-and-pre-process-data" style="font-size: 80%;">Prerequisites: Collect and pre-process data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#importing-keras-and-tensorflow" style="font-size: 80%;">Importing Keras and Tensorflow</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#running-with-keras" style="font-size: 80%;">Running with Keras</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#final-part" style="font-size: 80%;">Final part</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#final-visualization" style="font-size: 80%;">Final visualization</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#the-cifar01-data-set" style="font-size: 80%;">The CIFAR01 data set</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#verifying-the-data-set" style="font-size: 80%;">Verifying the data set</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#set-up-the-model" style="font-size: 80%;">Set up the model</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#add-dense-layers-on-top" style="font-size: 80%;">Add Dense layers on top</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#compile-and-train-the-model" style="font-size: 80%;">Compile and train the model</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#finally-evaluate-the-model" style="font-size: 80%;">Finally, evaluate the model</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs027.html#recurrent-neural-networks-overarching-view" style="font-size: 80%;">Recurrent neural networks: Overarching view</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs028.html#set-up-of-an-rnn" style="font-size: 80%;">Set up of an RNN</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs029.html#a-simple-example" style="font-size: 80%;">A simple example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs030.html#an-extrapolation-example" style="font-size: 80%;">An extrapolation example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs031.html#formatting-the-data" style="font-size: 80%;">Formatting the Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs032.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;">Predicting New Points With A Trained Recurrent Neural Network</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs033.html#other-things-to-try" style="font-size: 80%;">Other Things to Try</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs034.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;">Other Types of Recurrent Neural Networks</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#reading-recommendations" style="font-size: 80%;">Reading Recommendations</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#summary-on-deep-learning-methods" style="font-size: 80%;">Summary on Deep Learning Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#cnns-in-brief" style="font-size: 80%;">CNNs in brief</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#recurrent-neural-networks-overarching-view" style="font-size: 80%;">Recurrent neural networks: Overarching view</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#set-up-of-an-rnn" style="font-size: 80%;">Set up of an RNN</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#a-simple-example" style="font-size: 80%;">A simple example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#an-extrapolation-example" style="font-size: 80%;">An extrapolation example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#formatting-the-data" style="font-size: 80%;">Formatting the Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;">Predicting New Points With A Trained Recurrent Neural Network</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#other-things-to-try" style="font-size: 80%;">Other Things to Try</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;">Other Types of Recurrent Neural Networks</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -199,15 +113,110 @@ MathJax.Hub.Config({
|
||||
<a name="part0011"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="setting-it-up" class="anchor">Setting it up </h2>
|
||||
<h2 id="other-things-to-try" class="anchor">Other Things to Try </h2>
|
||||
|
||||
<p>
|
||||
It means that to represent the entire
|
||||
dataset of images, we require a 4D matrix or <b>tensor</b>. This tensor has the dimensions:
|
||||
$$
|
||||
(n_{inputs},\, n_{pixels, width},\, n_{pixels, height},\, depth) .
|
||||
$$
|
||||
Changing the size of the recurrent neural network and its parameters
|
||||
can drastically change the results you get from the model. The below
|
||||
code takes the simple recurrent neural network from above and adds a
|
||||
second hidden layer, changes the number of neurons in the hidden
|
||||
layer, and explicitly declares the activation function of the hidden
|
||||
layers to be a sigmoid function. The loss function and optimizer can
|
||||
also be changed but are kept the same as the above network. These
|
||||
parameters can be tuned to provide the optimal result from the
|
||||
network. For some ideas on how to improve the performance of a
|
||||
<a href="https://danijar.com/tips-for-training-recurrent-neural-networks" target="_self">recurrent neural network</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">def</span> <span style="color: #0000FF">rnn_2layers</span>(length_of_sequences, batch_size <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">None</span>, stateful <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">False</span>):
|
||||
<span style="color: #BA2121; font-style: italic">"""</span>
|
||||
<span style="color: #BA2121; font-style: italic"> Inputs:</span>
|
||||
<span style="color: #BA2121; font-style: italic"> length_of_sequences (an int): the number of y values in "x data". This is determined</span>
|
||||
<span style="color: #BA2121; font-style: italic"> when the data is formatted</span>
|
||||
<span style="color: #BA2121; font-style: italic"> batch_size (an int): Default value is None. See Keras documentation of SimpleRNN.</span>
|
||||
<span style="color: #BA2121; font-style: italic"> stateful (a boolean): Default value is False. See Keras documentation of SimpleRNN.</span>
|
||||
<span style="color: #BA2121; font-style: italic"> Returns:</span>
|
||||
<span style="color: #BA2121; font-style: italic"> model (a Keras model): The recurrent neural network that is built and compiled by this</span>
|
||||
<span style="color: #BA2121; font-style: italic"> method</span>
|
||||
<span style="color: #BA2121; font-style: italic"> Builds and compiles a recurrent neural network with two hidden layers and returns the model.</span>
|
||||
<span style="color: #BA2121; font-style: italic"> """</span>
|
||||
<span style="color: #408080; font-style: italic"># Number of neurons in the input and output layers</span>
|
||||
in_out_neurons <span style="color: #666666">=</span> <span style="color: #666666">1</span>
|
||||
<span style="color: #408080; font-style: italic"># Number of neurons in the hidden layer, increased from the first network</span>
|
||||
hidden_neurons <span style="color: #666666">=</span> <span style="color: #666666">500</span>
|
||||
<span style="color: #408080; font-style: italic"># Define the input layer</span>
|
||||
inp <span style="color: #666666">=</span> Input(batch_shape<span style="color: #666666">=</span>(batch_size,
|
||||
length_of_sequences,
|
||||
in_out_neurons))
|
||||
<span style="color: #408080; font-style: italic"># Create two hidden layers instead of one hidden layer. Explicitly set the activation</span>
|
||||
<span style="color: #408080; font-style: italic"># function to be the sigmoid function (the default value is hyperbolic tangent)</span>
|
||||
rnn1 <span style="color: #666666">=</span> SimpleRNN(hidden_neurons,
|
||||
return_sequences<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">True</span>, <span style="color: #408080; font-style: italic"># This needs to be True if another hidden layer is to follow</span>
|
||||
stateful <span style="color: #666666">=</span> stateful, activation <span style="color: #666666">=</span> <span style="color: #BA2121">'sigmoid'</span>,
|
||||
name<span style="color: #666666">=</span><span style="color: #BA2121">"RNN1"</span>)(inp)
|
||||
rnn2 <span style="color: #666666">=</span> SimpleRNN(hidden_neurons,
|
||||
return_sequences<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">False</span>, activation <span style="color: #666666">=</span> <span style="color: #BA2121">'sigmoid'</span>,
|
||||
stateful <span style="color: #666666">=</span> stateful,
|
||||
name<span style="color: #666666">=</span><span style="color: #BA2121">"RNN2"</span>)(rnn1)
|
||||
<span style="color: #408080; font-style: italic"># Define the output layer as a dense neural network layer (standard neural network layer)</span>
|
||||
<span style="color: #408080; font-style: italic">#and add it to the network immediately after the hidden layer.</span>
|
||||
dens <span style="color: #666666">=</span> Dense(in_out_neurons,name<span style="color: #666666">=</span><span style="color: #BA2121">"dense"</span>)(rnn2)
|
||||
<span style="color: #408080; font-style: italic"># Create the machine learning model starting with the input layer and ending with the </span>
|
||||
<span style="color: #408080; font-style: italic"># output layer</span>
|
||||
model <span style="color: #666666">=</span> Model(inputs<span style="color: #666666">=</span>[inp],outputs<span style="color: #666666">=</span>[dens])
|
||||
<span style="color: #408080; font-style: italic"># Compile the machine learning model using the mean squared error function as the loss </span>
|
||||
<span style="color: #408080; font-style: italic"># function and an Adams optimizer.</span>
|
||||
model<span style="color: #666666">.</span>compile(loss<span style="color: #666666">=</span><span style="color: #BA2121">"mean_squared_error"</span>, optimizer<span style="color: #666666">=</span><span style="color: #BA2121">"adam"</span>)
|
||||
<span style="color: #008000; font-weight: bold">return</span> model
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Check to make sure the data set is complete</span>
|
||||
<span style="color: #008000; font-weight: bold">assert</span> <span style="color: #008000">len</span>(X_tot) <span style="color: #666666">==</span> <span style="color: #008000">len</span>(y_tot)
|
||||
|
||||
<span style="color: #408080; font-style: italic"># This is the number of points that will be used in as the training data</span>
|
||||
dim<span style="color: #666666">=12</span>
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Separate the training data from the whole data set</span>
|
||||
X_train <span style="color: #666666">=</span> X_tot[:dim]
|
||||
y_train <span style="color: #666666">=</span> y_tot[:dim]
|
||||
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Generate the training data for the RNN, using a sequence of 2</span>
|
||||
rnn_input, rnn_training <span style="color: #666666">=</span> format_data(y_train, <span style="color: #666666">2</span>)
|
||||
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Create a recurrent neural network in Keras and produce a summary of the </span>
|
||||
<span style="color: #408080; font-style: italic"># machine learning model</span>
|
||||
model <span style="color: #666666">=</span> rnn_2layers(length_of_sequences <span style="color: #666666">=</span> <span style="color: #666666">2</span>)
|
||||
model<span style="color: #666666">.</span>summary()
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Start the timer. Want to time training+testing</span>
|
||||
start <span style="color: #666666">=</span> timer()
|
||||
<span style="color: #408080; font-style: italic"># Fit the model using the training data genenerated above using 150 training iterations and a 5%</span>
|
||||
<span style="color: #408080; font-style: italic"># validation split. Setting verbose to True prints information about each training iteration.</span>
|
||||
hist <span style="color: #666666">=</span> model<span style="color: #666666">.</span>fit(rnn_input, rnn_training, batch_size<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">None</span>, epochs<span style="color: #666666">=150</span>,
|
||||
verbose<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">True</span>,validation_split<span style="color: #666666">=0.05</span>)
|
||||
|
||||
|
||||
<span style="color: #408080; font-style: italic"># This section plots the training loss and the validation loss as a function of training iteration.</span>
|
||||
<span style="color: #408080; font-style: italic"># This is not required for analyzing the couple cluster data but can help determine if the network is</span>
|
||||
<span style="color: #408080; font-style: italic"># being overtrained.</span>
|
||||
<span style="color: #008000; font-weight: bold">for</span> label <span style="color: #AA22FF; font-weight: bold">in</span> [<span style="color: #BA2121">"loss"</span>,<span style="color: #BA2121">"val_loss"</span>]:
|
||||
plt<span style="color: #666666">.</span>plot(hist<span style="color: #666666">.</span>history[label],label<span style="color: #666666">=</span>label)
|
||||
|
||||
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">"loss"</span>)
|
||||
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">"epoch"</span>)
|
||||
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">"The final validation loss: </span><span style="color: #BB6688; font-weight: bold">{}</span><span style="color: #BA2121">"</span><span style="color: #666666">.</span>format(hist<span style="color: #666666">.</span>history[<span style="color: #BA2121">"val_loss"</span>][<span style="color: #666666">-1</span>]))
|
||||
plt<span style="color: #666666">.</span>legend()
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Use the trained neural network to predict more points of the data set</span>
|
||||
test_rnn(X_tot, y_tot, X_tot[<span style="color: #666666">0</span>], X_tot[dim<span style="color: #666666">-1</span>])
|
||||
<span style="color: #408080; font-style: italic"># Stop the timer and calculate the total time needed.</span>
|
||||
end <span style="color: #666666">=</span> timer()
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">'Time: '</span>, end<span style="color: #666666">-</span>start)
|
||||
</pre></div>
|
||||
<p>
|
||||
<p>
|
||||
<!-- navigation buttons at the bottom of the page -->
|
||||
@@ -225,16 +234,6 @@ $$
|
||||
<li><a href="._week43-bs010.html">11</a></li>
|
||||
<li class="active"><a href="._week43-bs011.html">12</a></li>
|
||||
<li><a href="._week43-bs012.html">13</a></li>
|
||||
<li><a href="._week43-bs013.html">14</a></li>
|
||||
<li><a href="._week43-bs014.html">15</a></li>
|
||||
<li><a href="._week43-bs015.html">16</a></li>
|
||||
<li><a href="._week43-bs016.html">17</a></li>
|
||||
<li><a href="._week43-bs017.html">18</a></li>
|
||||
<li><a href="._week43-bs018.html">19</a></li>
|
||||
<li><a href="._week43-bs019.html">20</a></li>
|
||||
<li><a href="._week43-bs020.html">21</a></li>
|
||||
<li><a href="">...</a></li>
|
||||
<li><a href="._week43-bs034.html">35</a></li>
|
||||
<li><a href="._week43-bs012.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/doconce/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Convolutional Neural Networks and Recurrent Neural Networks">
|
||||
<meta name="description" content="Week 43: Deep Learning: Recurrent Neural Networks and other methods">
|
||||
|
||||
<title>Week 43: Convolutional Neural Networks and Recurrent Neural Networks</title>
|
||||
<title>Week 43: Deep Learning: Recurrent Neural Networks and other methods</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
@@ -42,60 +42,12 @@ Automatically generated HTML file from DocOnce source
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Plans for week 43', 2, None, 'plans-for-week-43'),
|
||||
('Convolutional Neural Networks (recognizing images)',
|
||||
('Reading Recommendations', 2, None, 'reading-recommendations'),
|
||||
('Summary on Deep Learning Methods',
|
||||
2,
|
||||
None,
|
||||
'convolutional-neural-networks-recognizing-images'),
|
||||
('Neural Networks vs CNNs', 2, None, 'neural-networks-vs-cnns'),
|
||||
('Why CNNS for images, sound files, medical images from CT scans '
|
||||
'etc?',
|
||||
2,
|
||||
None,
|
||||
'why-cnns-for-images-sound-files-medical-images-from-ct-scans-etc'),
|
||||
('Regular NNs don’t scale well to full images',
|
||||
2,
|
||||
None,
|
||||
'regular-nns-don-t-scale-well-to-full-images'),
|
||||
('3D volumes of neurons', 2, None, '3d-volumes-of-neurons'),
|
||||
('Layers used to build CNNs',
|
||||
2,
|
||||
None,
|
||||
'layers-used-to-build-cnns'),
|
||||
('Transforming images', 2, None, 'transforming-images'),
|
||||
'summary-on-deep-learning-methods'),
|
||||
('CNNs in brief', 2, None, 'cnns-in-brief'),
|
||||
('CNNs in more detail, building convolutional neural networks in '
|
||||
'Tensorflow and Keras',
|
||||
2,
|
||||
None,
|
||||
'cnns-in-more-detail-building-convolutional-neural-networks-in-tensorflow-and-keras'),
|
||||
('Setting it up', 2, None, 'setting-it-up'),
|
||||
('The MNIST dataset again', 2, None, 'the-mnist-dataset-again'),
|
||||
('Strong correlations', 2, None, 'strong-correlations'),
|
||||
('Layers of a CNN', 2, None, 'layers-of-a-cnn'),
|
||||
('Systematic reduction', 2, None, 'systematic-reduction'),
|
||||
('Prerequisites: Collect and pre-process data',
|
||||
2,
|
||||
None,
|
||||
'prerequisites-collect-and-pre-process-data'),
|
||||
('Importing Keras and Tensorflow',
|
||||
2,
|
||||
None,
|
||||
'importing-keras-and-tensorflow'),
|
||||
('Running with Keras', 2, None, 'running-with-keras'),
|
||||
('Final part', 2, None, 'final-part'),
|
||||
('Final visualization', 2, None, 'final-visualization'),
|
||||
('The CIFAR01 data set', 2, None, 'the-cifar01-data-set'),
|
||||
('Verifying the data set', 2, None, 'verifying-the-data-set'),
|
||||
('Set up the model', 2, None, 'set-up-the-model'),
|
||||
('Add Dense layers on top', 2, None, 'add-dense-layers-on-top'),
|
||||
('Compile and train the model',
|
||||
2,
|
||||
None,
|
||||
'compile-and-train-the-model'),
|
||||
('Finally, evaluate the model',
|
||||
2,
|
||||
None,
|
||||
'finally-evaluate-the-model'),
|
||||
('Recurrent neural networks: Overarching view',
|
||||
2,
|
||||
None,
|
||||
@@ -117,22 +69,6 @@ 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">
|
||||
@@ -142,7 +78,7 @@ MathJax.Hub.Config({
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Convolutional Neural Networks and Recurrent Neural Networks</a>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Deep Learning: Recurrent Neural Networks and other methods</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
@@ -151,39 +87,17 @@ MathJax.Hub.Config({
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs001.html#plans-for-week-43" style="font-size: 80%;">Plans for week 43</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#convolutional-neural-networks-recognizing-images" style="font-size: 80%;">Convolutional Neural Networks (recognizing images)</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#neural-networks-vs-cnns" style="font-size: 80%;">Neural Networks vs CNNs</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#why-cnns-for-images-sound-files-medical-images-from-ct-scans-etc" style="font-size: 80%;">Why CNNS for images, sound files, medical images from CT scans etc?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#regular-nns-don-t-scale-well-to-full-images" style="font-size: 80%;">Regular NNs don’t scale well to full images</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#3d-volumes-of-neurons" style="font-size: 80%;">3D volumes of neurons</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#layers-used-to-build-cnns" style="font-size: 80%;">Layers used to build CNNs</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#transforming-images" style="font-size: 80%;">Transforming images</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#cnns-in-brief" style="font-size: 80%;">CNNs in brief</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#cnns-in-more-detail-building-convolutional-neural-networks-in-tensorflow-and-keras" style="font-size: 80%;">CNNs in more detail, building convolutional neural networks in Tensorflow and Keras</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#setting-it-up" style="font-size: 80%;">Setting it up</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#the-mnist-dataset-again" style="font-size: 80%;">The MNIST dataset again</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#strong-correlations" style="font-size: 80%;">Strong correlations</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#layers-of-a-cnn" style="font-size: 80%;">Layers of a CNN</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#systematic-reduction" style="font-size: 80%;">Systematic reduction</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#prerequisites-collect-and-pre-process-data" style="font-size: 80%;">Prerequisites: Collect and pre-process data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#importing-keras-and-tensorflow" style="font-size: 80%;">Importing Keras and Tensorflow</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#running-with-keras" style="font-size: 80%;">Running with Keras</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#final-part" style="font-size: 80%;">Final part</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#final-visualization" style="font-size: 80%;">Final visualization</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#the-cifar01-data-set" style="font-size: 80%;">The CIFAR01 data set</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#verifying-the-data-set" style="font-size: 80%;">Verifying the data set</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#set-up-the-model" style="font-size: 80%;">Set up the model</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#add-dense-layers-on-top" style="font-size: 80%;">Add Dense layers on top</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#compile-and-train-the-model" style="font-size: 80%;">Compile and train the model</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#finally-evaluate-the-model" style="font-size: 80%;">Finally, evaluate the model</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs027.html#recurrent-neural-networks-overarching-view" style="font-size: 80%;">Recurrent neural networks: Overarching view</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs028.html#set-up-of-an-rnn" style="font-size: 80%;">Set up of an RNN</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs029.html#a-simple-example" style="font-size: 80%;">A simple example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs030.html#an-extrapolation-example" style="font-size: 80%;">An extrapolation example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs031.html#formatting-the-data" style="font-size: 80%;">Formatting the Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs032.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;">Predicting New Points With A Trained Recurrent Neural Network</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs033.html#other-things-to-try" style="font-size: 80%;">Other Things to Try</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs034.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;">Other Types of Recurrent Neural Networks</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#reading-recommendations" style="font-size: 80%;">Reading Recommendations</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#summary-on-deep-learning-methods" style="font-size: 80%;">Summary on Deep Learning Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#cnns-in-brief" style="font-size: 80%;">CNNs in brief</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#recurrent-neural-networks-overarching-view" style="font-size: 80%;">Recurrent neural networks: Overarching view</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#set-up-of-an-rnn" style="font-size: 80%;">Set up of an RNN</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#a-simple-example" style="font-size: 80%;">A simple example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#an-extrapolation-example" style="font-size: 80%;">An extrapolation example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#formatting-the-data" style="font-size: 80%;">Formatting the Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;">Predicting New Points With A Trained Recurrent Neural Network</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#other-things-to-try" style="font-size: 80%;">Other Things to Try</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#other-types-of-recurrent-neural-networks" style="font-size: 80%;">Other Types of Recurrent Neural Networks</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -199,22 +113,221 @@ MathJax.Hub.Config({
|
||||
<a name="part0012"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="the-mnist-dataset-again" class="anchor">The MNIST dataset again </h2>
|
||||
<h2 id="other-types-of-recurrent-neural-networks" class="anchor">Other Types of Recurrent Neural Networks </h2>
|
||||
|
||||
<p>
|
||||
The MNIST dataset consists of grayscale images with a pixel size of
|
||||
\( 28\times 28 \), meaning we require \( 28 \times 28 = 724 \) weights to each
|
||||
neuron in the first hidden layer.
|
||||
Besides a simple recurrent neural network layer, there are two other
|
||||
commonly used types of recurrent neural network layers: Long Short
|
||||
Term Memory (LSTM) and Gated Recurrent Unit (GRU). For a short
|
||||
introduction to these layers see <a href="https://medium.com/mindboard/lstm-vs-gru-experimental-comparison-955820c21e8b" target="_self"><tt>https://medium.com/mindboard/lstm-vs-gru-experimental-comparison-955820c21e8b</tt></a>
|
||||
and <a href="https://medium.com/mindboard/lstm-vs-gru-experimental-comparison-955820c21e8b" target="_self"><tt>https://medium.com/mindboard/lstm-vs-gru-experimental-comparison-955820c21e8b</tt></a>.
|
||||
|
||||
<p>
|
||||
If we were to analyze images of size \( 128\times 128 \) we would require
|
||||
\( 128 \times 128 = 16384 \) weights to each neuron. Even worse if we were
|
||||
dealing with color images, as most images are, we have an image matrix
|
||||
of size \( 128\times 128 \) for each color dimension (Red, Green, Blue),
|
||||
meaning 3 times the number of weights \( = 49152 \) are required for every
|
||||
single neuron in the first hidden layer.
|
||||
The first network created below is similar to the previous network,
|
||||
but it replaces the SimpleRNN layers with LSTM layers. The second
|
||||
network below has two hidden layers made up of GRUs, which are
|
||||
preceeded by two dense (feeddorward) neural network layers. These
|
||||
dense layers "preprocess" the data before it reaches the recurrent
|
||||
layers. This architecture has been shown to improve the performance
|
||||
of recurrent neural networks (see the link above and also
|
||||
<a href="https://arxiv.org/pdf/1807.02857.pdf" target="_self"><tt>https://arxiv.org/pdf/1807.02857.pdf</tt></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">def</span> <span style="color: #0000FF">lstm_2layers</span>(length_of_sequences, batch_size <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">None</span>, stateful <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">False</span>):
|
||||
<span style="color: #BA2121; font-style: italic">"""</span>
|
||||
<span style="color: #BA2121; font-style: italic"> Inputs:</span>
|
||||
<span style="color: #BA2121; font-style: italic"> length_of_sequences (an int): the number of y values in "x data". This is determined</span>
|
||||
<span style="color: #BA2121; font-style: italic"> when the data is formatted</span>
|
||||
<span style="color: #BA2121; font-style: italic"> batch_size (an int): Default value is None. See Keras documentation of SimpleRNN.</span>
|
||||
<span style="color: #BA2121; font-style: italic"> stateful (a boolean): Default value is False. See Keras documentation of SimpleRNN.</span>
|
||||
<span style="color: #BA2121; font-style: italic"> Returns:</span>
|
||||
<span style="color: #BA2121; font-style: italic"> model (a Keras model): The recurrent neural network that is built and compiled by this</span>
|
||||
<span style="color: #BA2121; font-style: italic"> method</span>
|
||||
<span style="color: #BA2121; font-style: italic"> Builds and compiles a recurrent neural network with two LSTM hidden layers and returns the model.</span>
|
||||
<span style="color: #BA2121; font-style: italic"> """</span>
|
||||
<span style="color: #408080; font-style: italic"># Number of neurons on the input/output layer and the number of neurons in the hidden layer</span>
|
||||
in_out_neurons <span style="color: #666666">=</span> <span style="color: #666666">1</span>
|
||||
hidden_neurons <span style="color: #666666">=</span> <span style="color: #666666">250</span>
|
||||
<span style="color: #408080; font-style: italic"># Input Layer</span>
|
||||
inp <span style="color: #666666">=</span> Input(batch_shape<span style="color: #666666">=</span>(batch_size,
|
||||
length_of_sequences,
|
||||
in_out_neurons))
|
||||
<span style="color: #408080; font-style: italic"># Hidden layers (in this case they are LSTM layers instead if SimpleRNN layers)</span>
|
||||
rnn<span style="color: #666666">=</span> LSTM(hidden_neurons,
|
||||
return_sequences<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">True</span>,
|
||||
stateful <span style="color: #666666">=</span> stateful,
|
||||
name<span style="color: #666666">=</span><span style="color: #BA2121">"RNN"</span>, use_bias<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">True</span>, activation<span style="color: #666666">=</span><span style="color: #BA2121">'tanh'</span>)(inp)
|
||||
rnn1 <span style="color: #666666">=</span> LSTM(hidden_neurons,
|
||||
return_sequences<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">False</span>,
|
||||
stateful <span style="color: #666666">=</span> stateful,
|
||||
name<span style="color: #666666">=</span><span style="color: #BA2121">"RNN1"</span>, use_bias<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">True</span>, activation<span style="color: #666666">=</span><span style="color: #BA2121">'tanh'</span>)(rnn)
|
||||
<span style="color: #408080; font-style: italic"># Output layer</span>
|
||||
dens <span style="color: #666666">=</span> Dense(in_out_neurons,name<span style="color: #666666">=</span><span style="color: #BA2121">"dense"</span>)(rnn1)
|
||||
<span style="color: #408080; font-style: italic"># Define the midel</span>
|
||||
model <span style="color: #666666">=</span> Model(inputs<span style="color: #666666">=</span>[inp],outputs<span style="color: #666666">=</span>[dens])
|
||||
<span style="color: #408080; font-style: italic"># Compile the model</span>
|
||||
model<span style="color: #666666">.</span>compile(loss<span style="color: #666666">=</span><span style="color: #BA2121">'mean_squared_error'</span>, optimizer<span style="color: #666666">=</span><span style="color: #BA2121">'adam'</span>)
|
||||
<span style="color: #408080; font-style: italic"># Return the model</span>
|
||||
<span style="color: #008000; font-weight: bold">return</span> model
|
||||
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">dnn2_gru2</span>(length_of_sequences, batch_size <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">None</span>, stateful <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">False</span>):
|
||||
<span style="color: #BA2121; font-style: italic">"""</span>
|
||||
<span style="color: #BA2121; font-style: italic"> Inputs:</span>
|
||||
<span style="color: #BA2121; font-style: italic"> length_of_sequences (an int): the number of y values in "x data". This is determined</span>
|
||||
<span style="color: #BA2121; font-style: italic"> when the data is formatted</span>
|
||||
<span style="color: #BA2121; font-style: italic"> batch_size (an int): Default value is None. See Keras documentation of SimpleRNN.</span>
|
||||
<span style="color: #BA2121; font-style: italic"> stateful (a boolean): Default value is False. See Keras documentation of SimpleRNN.</span>
|
||||
<span style="color: #BA2121; font-style: italic"> Returns:</span>
|
||||
<span style="color: #BA2121; font-style: italic"> model (a Keras model): The recurrent neural network that is built and compiled by this</span>
|
||||
<span style="color: #BA2121; font-style: italic"> method</span>
|
||||
<span style="color: #BA2121; font-style: italic"> Builds and compiles a recurrent neural network with four hidden layers (two dense followed by</span>
|
||||
<span style="color: #BA2121; font-style: italic"> two GRU layers) and returns the model.</span>
|
||||
<span style="color: #BA2121; font-style: italic"> """</span>
|
||||
<span style="color: #408080; font-style: italic"># Number of neurons on the input/output layers and hidden layers</span>
|
||||
in_out_neurons <span style="color: #666666">=</span> <span style="color: #666666">1</span>
|
||||
hidden_neurons <span style="color: #666666">=</span> <span style="color: #666666">250</span>
|
||||
<span style="color: #408080; font-style: italic"># Input layer</span>
|
||||
inp <span style="color: #666666">=</span> Input(batch_shape<span style="color: #666666">=</span>(batch_size,
|
||||
length_of_sequences,
|
||||
in_out_neurons))
|
||||
<span style="color: #408080; font-style: italic"># Hidden Dense (feedforward) layers</span>
|
||||
dnn <span style="color: #666666">=</span> Dense(hidden_neurons<span style="color: #666666">/2</span>, activation<span style="color: #666666">=</span><span style="color: #BA2121">'relu'</span>, name<span style="color: #666666">=</span><span style="color: #BA2121">'dnn'</span>)(inp)
|
||||
dnn1 <span style="color: #666666">=</span> Dense(hidden_neurons<span style="color: #666666">/2</span>, activation<span style="color: #666666">=</span><span style="color: #BA2121">'relu'</span>, name<span style="color: #666666">=</span><span style="color: #BA2121">'dnn1'</span>)(dnn)
|
||||
<span style="color: #408080; font-style: italic"># Hidden GRU layers</span>
|
||||
rnn1 <span style="color: #666666">=</span> GRU(hidden_neurons,
|
||||
return_sequences<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">True</span>,
|
||||
stateful <span style="color: #666666">=</span> stateful,
|
||||
name<span style="color: #666666">=</span><span style="color: #BA2121">"RNN1"</span>, use_bias<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">True</span>)(dnn1)
|
||||
rnn <span style="color: #666666">=</span> GRU(hidden_neurons,
|
||||
return_sequences<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">False</span>,
|
||||
stateful <span style="color: #666666">=</span> stateful,
|
||||
name<span style="color: #666666">=</span><span style="color: #BA2121">"RNN"</span>, use_bias<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">True</span>)(rnn1)
|
||||
<span style="color: #408080; font-style: italic"># Output layer</span>
|
||||
dens <span style="color: #666666">=</span> Dense(in_out_neurons,name<span style="color: #666666">=</span><span style="color: #BA2121">"dense"</span>)(rnn)
|
||||
<span style="color: #408080; font-style: italic"># Define the model</span>
|
||||
model <span style="color: #666666">=</span> Model(inputs<span style="color: #666666">=</span>[inp],outputs<span style="color: #666666">=</span>[dens])
|
||||
<span style="color: #408080; font-style: italic"># Compile the mdoel</span>
|
||||
model<span style="color: #666666">.</span>compile(loss<span style="color: #666666">=</span><span style="color: #BA2121">'mean_squared_error'</span>, optimizer<span style="color: #666666">=</span><span style="color: #BA2121">'adam'</span>)
|
||||
<span style="color: #408080; font-style: italic"># Return the model</span>
|
||||
<span style="color: #008000; font-weight: bold">return</span> model
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Check to make sure the data set is complete</span>
|
||||
<span style="color: #008000; font-weight: bold">assert</span> <span style="color: #008000">len</span>(X_tot) <span style="color: #666666">==</span> <span style="color: #008000">len</span>(y_tot)
|
||||
|
||||
<span style="color: #408080; font-style: italic"># This is the number of points that will be used in as the training data</span>
|
||||
dim<span style="color: #666666">=12</span>
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Separate the training data from the whole data set</span>
|
||||
X_train <span style="color: #666666">=</span> X_tot[:dim]
|
||||
y_train <span style="color: #666666">=</span> y_tot[:dim]
|
||||
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Generate the training data for the RNN, using a sequence of 2</span>
|
||||
rnn_input, rnn_training <span style="color: #666666">=</span> format_data(y_train, <span style="color: #666666">2</span>)
|
||||
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Create a recurrent neural network in Keras and produce a summary of the </span>
|
||||
<span style="color: #408080; font-style: italic"># machine learning model</span>
|
||||
<span style="color: #408080; font-style: italic"># Change the method name to reflect which network you want to use</span>
|
||||
model <span style="color: #666666">=</span> dnn2_gru2(length_of_sequences <span style="color: #666666">=</span> <span style="color: #666666">2</span>)
|
||||
model<span style="color: #666666">.</span>summary()
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Start the timer. Want to time training+testing</span>
|
||||
start <span style="color: #666666">=</span> timer()
|
||||
<span style="color: #408080; font-style: italic"># Fit the model using the training data genenerated above using 150 training iterations and a 5%</span>
|
||||
<span style="color: #408080; font-style: italic"># validation split. Setting verbose to True prints information about each training iteration.</span>
|
||||
hist <span style="color: #666666">=</span> model<span style="color: #666666">.</span>fit(rnn_input, rnn_training, batch_size<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">None</span>, epochs<span style="color: #666666">=150</span>,
|
||||
verbose<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">True</span>,validation_split<span style="color: #666666">=0.05</span>)
|
||||
|
||||
|
||||
<span style="color: #408080; font-style: italic"># This section plots the training loss and the validation loss as a function of training iteration.</span>
|
||||
<span style="color: #408080; font-style: italic"># This is not required for analyzing the couple cluster data but can help determine if the network is</span>
|
||||
<span style="color: #408080; font-style: italic"># being overtrained.</span>
|
||||
<span style="color: #008000; font-weight: bold">for</span> label <span style="color: #AA22FF; font-weight: bold">in</span> [<span style="color: #BA2121">"loss"</span>,<span style="color: #BA2121">"val_loss"</span>]:
|
||||
plt<span style="color: #666666">.</span>plot(hist<span style="color: #666666">.</span>history[label],label<span style="color: #666666">=</span>label)
|
||||
|
||||
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">"loss"</span>)
|
||||
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">"epoch"</span>)
|
||||
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">"The final validation loss: </span><span style="color: #BB6688; font-weight: bold">{}</span><span style="color: #BA2121">"</span><span style="color: #666666">.</span>format(hist<span style="color: #666666">.</span>history[<span style="color: #BA2121">"val_loss"</span>][<span style="color: #666666">-1</span>]))
|
||||
plt<span style="color: #666666">.</span>legend()
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Use the trained neural network to predict more points of the data set</span>
|
||||
test_rnn(X_tot, y_tot, X_tot[<span style="color: #666666">0</span>], X_tot[dim<span style="color: #666666">-1</span>])
|
||||
<span style="color: #408080; font-style: italic"># Stop the timer and calculate the total time needed.</span>
|
||||
end <span style="color: #666666">=</span> timer()
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">'Time: '</span>, end<span style="color: #666666">-</span>start)
|
||||
|
||||
|
||||
<span style="color: #408080; font-style: italic"># ### Training Recurrent Neural Networks in the Standard Way (i.e. learning the relationship between the X and Y data)</span>
|
||||
<span style="color: #408080; font-style: italic"># </span>
|
||||
<span style="color: #408080; font-style: italic"># Finally, comparing the performace of a recurrent neural network using the standard data formatting to the performance of the network with time sequence data formatting shows the benefit of this type of data formatting with extrapolation.</span>
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Check to make sure the data set is complete</span>
|
||||
<span style="color: #008000; font-weight: bold">assert</span> <span style="color: #008000">len</span>(X_tot) <span style="color: #666666">==</span> <span style="color: #008000">len</span>(y_tot)
|
||||
|
||||
<span style="color: #408080; font-style: italic"># This is the number of points that will be used in as the training data</span>
|
||||
dim<span style="color: #666666">=12</span>
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Separate the training data from the whole data set</span>
|
||||
X_train <span style="color: #666666">=</span> X_tot[:dim]
|
||||
y_train <span style="color: #666666">=</span> y_tot[:dim]
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Reshape the data for Keras specifications</span>
|
||||
X_train <span style="color: #666666">=</span> X_train<span style="color: #666666">.</span>reshape((dim, <span style="color: #666666">1</span>))
|
||||
y_train <span style="color: #666666">=</span> y_train<span style="color: #666666">.</span>reshape((dim, <span style="color: #666666">1</span>))
|
||||
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Create a recurrent neural network in Keras and produce a summary of the </span>
|
||||
<span style="color: #408080; font-style: italic"># machine learning model</span>
|
||||
<span style="color: #408080; font-style: italic"># Set the sequence length to 1 for regular data formatting </span>
|
||||
model <span style="color: #666666">=</span> rnn(length_of_sequences <span style="color: #666666">=</span> <span style="color: #666666">1</span>)
|
||||
model<span style="color: #666666">.</span>summary()
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Start the timer. Want to time training+testing</span>
|
||||
start <span style="color: #666666">=</span> timer()
|
||||
<span style="color: #408080; font-style: italic"># Fit the model using the training data genenerated above using 150 training iterations and a 5%</span>
|
||||
<span style="color: #408080; font-style: italic"># validation split. Setting verbose to True prints information about each training iteration.</span>
|
||||
hist <span style="color: #666666">=</span> model<span style="color: #666666">.</span>fit(X_train, y_train, batch_size<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">None</span>, epochs<span style="color: #666666">=150</span>,
|
||||
verbose<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">True</span>,validation_split<span style="color: #666666">=0.05</span>)
|
||||
|
||||
|
||||
<span style="color: #408080; font-style: italic"># This section plots the training loss and the validation loss as a function of training iteration.</span>
|
||||
<span style="color: #408080; font-style: italic"># This is not required for analyzing the couple cluster data but can help determine if the network is</span>
|
||||
<span style="color: #408080; font-style: italic"># being overtrained.</span>
|
||||
<span style="color: #008000; font-weight: bold">for</span> label <span style="color: #AA22FF; font-weight: bold">in</span> [<span style="color: #BA2121">"loss"</span>,<span style="color: #BA2121">"val_loss"</span>]:
|
||||
plt<span style="color: #666666">.</span>plot(hist<span style="color: #666666">.</span>history[label],label<span style="color: #666666">=</span>label)
|
||||
|
||||
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">"loss"</span>)
|
||||
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">"epoch"</span>)
|
||||
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">"The final validation loss: </span><span style="color: #BB6688; font-weight: bold">{}</span><span style="color: #BA2121">"</span><span style="color: #666666">.</span>format(hist<span style="color: #666666">.</span>history[<span style="color: #BA2121">"val_loss"</span>][<span style="color: #666666">-1</span>]))
|
||||
plt<span style="color: #666666">.</span>legend()
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Use the trained neural network to predict the remaining data points</span>
|
||||
X_pred <span style="color: #666666">=</span> X_tot[dim:]
|
||||
X_pred <span style="color: #666666">=</span> X_pred<span style="color: #666666">.</span>reshape((<span style="color: #008000">len</span>(X_pred), <span style="color: #666666">1</span>))
|
||||
y_model <span style="color: #666666">=</span> model<span style="color: #666666">.</span>predict(X_pred)
|
||||
y_pred <span style="color: #666666">=</span> np<span style="color: #666666">.</span>concatenate((y_tot[:dim], y_model<span style="color: #666666">.</span>flatten()))
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Plot the known data set and the predicted data set. The red box represents the region that was used</span>
|
||||
<span style="color: #408080; font-style: italic"># for the training data.</span>
|
||||
fig, ax <span style="color: #666666">=</span> plt<span style="color: #666666">.</span>subplots()
|
||||
ax<span style="color: #666666">.</span>plot(X_tot, y_tot, label<span style="color: #666666">=</span><span style="color: #BA2121">"true"</span>, linewidth<span style="color: #666666">=3</span>)
|
||||
ax<span style="color: #666666">.</span>plot(X_tot, y_pred, <span style="color: #BA2121">'g-.'</span>,label<span style="color: #666666">=</span><span style="color: #BA2121">"predicted"</span>, linewidth<span style="color: #666666">=4</span>)
|
||||
ax<span style="color: #666666">.</span>legend()
|
||||
<span style="color: #408080; font-style: italic"># Created a red region to represent the points used in the training data.</span>
|
||||
ax<span style="color: #666666">.</span>axvspan(X_tot[<span style="color: #666666">0</span>], X_tot[dim], alpha<span style="color: #666666">=0.25</span>, color<span style="color: #666666">=</span><span style="color: #BA2121">'red'</span>)
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Stop the timer and calculate the total time needed.</span>
|
||||
end <span style="color: #666666">=</span> timer()
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">'Time: '</span>, end<span style="color: #666666">-</span>start)
|
||||
</pre></div>
|
||||
<p>
|
||||
|
||||
<p>
|
||||
<!-- navigation buttons at the bottom of the page -->
|
||||
<ul class="pagination">
|
||||
@@ -230,18 +343,6 @@ single neuron in the first hidden layer.
|
||||
<li><a href="._week43-bs010.html">11</a></li>
|
||||
<li><a href="._week43-bs011.html">12</a></li>
|
||||
<li class="active"><a href="._week43-bs012.html">13</a></li>
|
||||
<li><a href="._week43-bs013.html">14</a></li>
|
||||
<li><a href="._week43-bs014.html">15</a></li>
|
||||
<li><a href="._week43-bs015.html">16</a></li>
|
||||
<li><a href="._week43-bs016.html">17</a></li>
|
||||
<li><a href="._week43-bs017.html">18</a></li>
|
||||
<li><a href="._week43-bs018.html">19</a></li>
|
||||
<li><a href="._week43-bs019.html">20</a></li>
|
||||
<li><a href="._week43-bs020.html">21</a></li>
|
||||
<li><a href="._week43-bs021.html">22</a></li>
|
||||
<li><a href="">...</a></li>
|
||||
<li><a href="._week43-bs034.html">35</a></li>
|
||||
<li><a href="._week43-bs013.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
|
||||
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/doconce/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Convolutional Neural Networks and Recurrent Neural Networks">
|
||||
<meta name="description" content="Week 43: Deep Learning: Recurrent Neural Networks and other methods">
|
||||
|
||||
<title>Week 43: Convolutional Neural Networks and Recurrent Neural Networks</title>
|
||||
<title>Week 43: Deep Learning: Recurrent Neural Networks and other methods</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
@@ -42,7 +42,12 @@ Automatically generated HTML file from DocOnce source
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Plans for week 43', 2, None, 'plans-for-week-43'),
|
||||
('Summary on CNNs', 2, None, 'summary-on-cnns'),
|
||||
('Reading Recommendations', 2, None, 'reading-recommendations'),
|
||||
('Summary on Deep Learning Methods',
|
||||
2,
|
||||
None,
|
||||
'summary-on-deep-learning-methods'),
|
||||
('CNNs in brief', 2, None, 'cnns-in-brief'),
|
||||
('Recurrent neural networks: Overarching view',
|
||||
2,
|
||||
None,
|
||||
@@ -73,7 +78,7 @@ end of tocinfo -->
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Convolutional Neural Networks and Recurrent Neural Networks</a>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Deep Learning: Recurrent Neural Networks and other methods</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
@@ -82,15 +87,17 @@ end of tocinfo -->
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs001.html#plans-for-week-43" style="font-size: 80%;">Plans for week 43</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#summary-on-cnns" style="font-size: 80%;">Summary on CNNs</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#recurrent-neural-networks-overarching-view" style="font-size: 80%;">Recurrent neural networks: Overarching view</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#set-up-of-an-rnn" style="font-size: 80%;">Set up of an RNN</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#a-simple-example" style="font-size: 80%;">A simple example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#an-extrapolation-example" style="font-size: 80%;">An extrapolation example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#formatting-the-data" style="font-size: 80%;">Formatting the Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;">Predicting New Points With A Trained Recurrent Neural Network</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#other-things-to-try" style="font-size: 80%;">Other Things to Try</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;">Other Types of Recurrent Neural Networks</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#reading-recommendations" style="font-size: 80%;">Reading Recommendations</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#summary-on-deep-learning-methods" style="font-size: 80%;">Summary on Deep Learning Methods</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#cnns-in-brief" style="font-size: 80%;">CNNs in brief</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#recurrent-neural-networks-overarching-view" style="font-size: 80%;">Recurrent neural networks: Overarching view</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#set-up-of-an-rnn" style="font-size: 80%;">Set up of an RNN</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#a-simple-example" style="font-size: 80%;">A simple example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#an-extrapolation-example" style="font-size: 80%;">An extrapolation example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#formatting-the-data" style="font-size: 80%;">Formatting the Data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;">Predicting New Points With A Trained Recurrent Neural Network</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#other-things-to-try" style="font-size: 80%;">Other Things to Try</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;">Other Types of Recurrent Neural Networks</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -109,7 +116,7 @@ end of tocinfo -->
|
||||
|
||||
|
||||
<div class="jumbotron">
|
||||
<center><h1>Week 43: Convolutional Neural Networks and Recurrent Neural Networks</h1></center> <!-- document title -->
|
||||
<center><h1>Week 43: Deep Learning: Recurrent Neural Networks and other methods</h1></center> <!-- document title -->
|
||||
|
||||
<p>
|
||||
<!-- author(s): Morten Hjorth-Jensen -->
|
||||
@@ -125,7 +132,7 @@ end of tocinfo -->
|
||||
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
|
||||
<br>
|
||||
<p>
|
||||
<center><h4>Oct 24, 2021</h4></center> <!-- date -->
|
||||
<center><h4>Oct 26, 2021</h4></center> <!-- date -->
|
||||
<br>
|
||||
<p>
|
||||
|
||||
@@ -149,7 +156,7 @@ end of tocinfo -->
|
||||
<li><a href="._week43-bs008.html">9</a></li>
|
||||
<li><a href="._week43-bs009.html">10</a></li>
|
||||
<li><a href="">...</a></li>
|
||||
<li><a href="._week43-bs010.html">11</a></li>
|
||||
<li><a href="._week43-bs012.html">13</a></li>
|
||||
<li><a href="._week43-bs001.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/doconce/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Convolutional Neural Networks and Recurrent Neural Networks">
|
||||
<meta name="description" content="Week 43: Deep Learning: Recurrent Neural Networks and other methods">
|
||||
|
||||
<title>Week 43: Convolutional Neural Networks and Recurrent Neural Networks</title>
|
||||
<title>Week 43: Deep Learning: Recurrent Neural Networks and other methods</title>
|
||||
|
||||
|
||||
|
||||
@@ -116,7 +116,7 @@ td.padding {
|
||||
|
||||
|
||||
|
||||
<center><h1 style="text-align: center;">Week 43: Convolutional Neural Networks and Recurrent Neural Networks</h1></center> <!-- document title -->
|
||||
<center><h1 style="text-align: center;">Week 43: Deep Learning: Recurrent Neural Networks and other methods</h1></center> <!-- document title -->
|
||||
|
||||
<p>
|
||||
<!-- author(s): Morten Hjorth-Jensen -->
|
||||
@@ -132,7 +132,7 @@ td.padding {
|
||||
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
|
||||
<br>
|
||||
<p> <br>
|
||||
<center><h4>Oct 24, 2021</h4></center> <!-- date -->
|
||||
<center><h4>Oct 26, 2021</h4></center> <!-- date -->
|
||||
<br>
|
||||
<p>
|
||||
|
||||
@@ -147,7 +147,7 @@ td.padding {
|
||||
|
||||
<ul>
|
||||
<p><li> Thursday: Summary of Convolutional Neural Networks from week 42 and Recurrent Neural Networks</li>
|
||||
<p><li> Friday: Recurrent Neural Networks and other Deep Learning methods</li>
|
||||
<p><li> Friday: Recurrent Neural Networks and other Deep Learning methods such as Generalized Adversarial Neural Networks</li>
|
||||
</ul>
|
||||
<p>
|
||||
|
||||
@@ -164,18 +164,59 @@ td.padding {
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
<b>More resources</b>
|
||||
<ul>
|
||||
<p><li> <a href="https://www.uio.no/studier/emner/matnat/ifi/IN5400/v20/material/week10/in5400_2020_week10_recurrent_neural_network.pdf" target="_blank">IN5400 at UiO Lecture</a></li>
|
||||
<p><li> <a href="https://www.uio.no/studier/emner/matnat/ifi/IN5400/v20/material/week10/in5400_2020_week10_recurrent_neural_network.pdf" target="_blank">IN5400 at UiO Lecture</a></li>
|
||||
<p><li> <a href="https://www.youtube.com/watch?v=6niqTuYFZLQ&list=PLzUTmXVwsnXod6WNdg57Yc3zFx_f-RYsq&index=10&ab_channel=StanfordUniversitySchoolofEngineering" target="_blank">CS231 at Stanford Lecture</a></li>
|
||||
</ul>
|
||||
<p><li> <a href="https://www.youtube.com/watch?v=6niqTuYFZLQ&list=PLzUTmXVwsnXod6WNdg57Yc3zFx_f-RYsq&index=10&ab_channel=StanfordUniversitySchoolofEngineering" target="_blank">CS231 at Stanford Lecture</a>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="summary-on-cnns">Summary on CNNs </h2>
|
||||
<h2 id="reading-recommendations">Reading Recommendations </h2>
|
||||
|
||||
<ul>
|
||||
<p><li> Goodfellow et al, chapter 10 on Recurrent NNs, chapters 11 and 12 on various practicalities around deep learning are also recommended.</li>
|
||||
<p><li> Aurelien Geron, chapter 14 on RNNs.</li>
|
||||
</ul>
|
||||
</section>
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="summary-on-deep-learning-methods">Summary on Deep Learning Methods </h2>
|
||||
|
||||
<p>
|
||||
Material to be added
|
||||
We have studied fully connected neural networks (also called artifical nueral networks) and convolutional neural networks (CNNs).
|
||||
|
||||
<p>
|
||||
The first type of deep learning networks work very well on homogeneous and structured input data while CCNs are normally tailored to recognizing images.
|
||||
</section>
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="cnns-in-brief">CNNs in brief </h2>
|
||||
|
||||
<p>
|
||||
In summary:
|
||||
|
||||
<ul>
|
||||
<p><li> A CNN architecture is in the simplest case a list of Layers that transform the image volume into an output volume (e.g. holding the class scores)</li>
|
||||
<p><li> There are a few distinct types of Layers (e.g. CONV/FC/RELU/POOL are by far the most popular)</li>
|
||||
<p><li> Each Layer accepts an input 3D volume and transforms it to an output 3D volume through a differentiable function</li>
|
||||
<p><li> Each Layer may or may not have parameters (e.g. CONV/FC do, RELU/POOL don’t)</li>
|
||||
<p><li> Each Layer may or may not have additional hyperparameters (e.g. CONV/FC/POOL do, RELU doesn’t)</li>
|
||||
</ul>
|
||||
<p>
|
||||
|
||||
For more material on convolutional networks, we strongly recommend
|
||||
the course
|
||||
<a href="https://www.uio.no/studier/emner/matnat/ifi/IN5400/index-eng.html" target="_blank">IN5400 – Machine Learning for Image Analysis</a>
|
||||
and the slides of <a href="http://cs231n.github.io/convolutional-networks/" target="_blank">CS231</a> which is taught at Stanford University (consistently ranked as one of the top computer science programs in the world). <a href="http://neuralnetworksanddeeplearning.com/chap6.html" target="_blank">Michael Nielsen's book is a must read, in particular chapter 6 which deals with CNNs</a>.
|
||||
|
||||
<p>
|
||||
However, both standard feed forwards networks and CNNs perform well on data with unknown length.
|
||||
|
||||
<p>
|
||||
This is where recurrent nueral networks (RNNs) come to our rescue.
|
||||
</section>
|
||||
|
||||
|
||||
@@ -201,14 +242,14 @@ fixed-sized inputs like all the nets we have discussed so far. For
|
||||
example, they can take sentences, documents, or audio samples as
|
||||
input, making them extremely useful for natural language processing
|
||||
systems such as automatic translation and speech-to-text.
|
||||
|
||||
<p>
|
||||
RNNs are also used in "text classification":"Text Classification Algorithms: A Survey"
|
||||
</section>
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="set-up-of-an-rnn">Set up of an RNN </h2>
|
||||
|
||||
<p>
|
||||
Text to come.
|
||||
</section>
|
||||
|
||||
|
||||
|
||||
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/doconce/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Convolutional Neural Networks and Recurrent Neural Networks">
|
||||
<meta name="description" content="Week 43: Deep Learning: Recurrent Neural Networks and other methods">
|
||||
|
||||
<title>Week 43: Convolutional Neural Networks and Recurrent Neural Networks</title>
|
||||
<title>Week 43: Deep Learning: Recurrent Neural Networks and other methods</title>
|
||||
|
||||
|
||||
<link href="https://cdn.rawgit.com/doconce/doconce/master/bundled/html_styles/style_solarized_box/css/solarized_light_code.css" rel="stylesheet" type="text/css" title="light"/>
|
||||
@@ -62,7 +62,12 @@ div { text-align: justify; text-justify: inter-word; }
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Plans for week 43', 2, None, 'plans-for-week-43'),
|
||||
('Summary on CNNs', 2, None, 'summary-on-cnns'),
|
||||
('Reading Recommendations', 2, None, 'reading-recommendations'),
|
||||
('Summary on Deep Learning Methods',
|
||||
2,
|
||||
None,
|
||||
'summary-on-deep-learning-methods'),
|
||||
('CNNs in brief', 2, None, 'cnns-in-brief'),
|
||||
('Recurrent neural networks: Overarching view',
|
||||
2,
|
||||
None,
|
||||
@@ -89,7 +94,7 @@ end of tocinfo -->
|
||||
|
||||
|
||||
|
||||
<center><h1>Week 43: Convolutional Neural Networks and Recurrent Neural Networks</h1></center> <!-- document title -->
|
||||
<center><h1>Week 43: Deep Learning: Recurrent Neural Networks and other methods</h1></center> <!-- document title -->
|
||||
|
||||
<p>
|
||||
<!-- author(s): Morten Hjorth-Jensen -->
|
||||
@@ -105,7 +110,7 @@ end of tocinfo -->
|
||||
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
|
||||
<br>
|
||||
<p>
|
||||
<center><h4>Oct 24, 2021</h4></center> <!-- date -->
|
||||
<center><h4>Oct 26, 2021</h4></center> <!-- date -->
|
||||
<br>
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
@@ -114,7 +119,7 @@ end of tocinfo -->
|
||||
|
||||
<ul>
|
||||
<li> Thursday: Summary of Convolutional Neural Networks from week 42 and Recurrent Neural Networks</li>
|
||||
<li> Friday: Recurrent Neural Networks and other Deep Learning methods</li>
|
||||
<li> Friday: Recurrent Neural Networks and other Deep Learning methods such as Generalized Adversarial Neural Networks</li>
|
||||
</ul>
|
||||
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
@@ -135,20 +140,58 @@ end of tocinfo -->
|
||||
<p>
|
||||
|
||||
<ul>
|
||||
<li> <a href="https://www.uio.no/studier/emner/matnat/ifi/IN5400/v20/material/week10/in5400_2020_week10_recurrent_neural_network.pdf" target="_blank">IN5400 at UiO Lecture</a></li>
|
||||
<li> <a href="https://www.uio.no/studier/emner/matnat/ifi/IN5400/v20/material/week10/in5400_2020_week10_recurrent_neural_network.pdf" target="_blank">IN5400 at UiO Lecture</a></li>
|
||||
<li> <a href="https://www.youtube.com/watch?v=6niqTuYFZLQ&list=PLzUTmXVwsnXod6WNdg57Yc3zFx_f-RYsq&index=10&ab_channel=StanfordUniversitySchoolofEngineering" target="_blank">CS231 at Stanford Lecture</a></li>
|
||||
</ul>
|
||||
|
||||
<li> <a href="https://www.youtube.com/watch?v=6niqTuYFZLQ&list=PLzUTmXVwsnXod6WNdg57Yc3zFx_f-RYsq&index=10&ab_channel=StanfordUniversitySchoolofEngineering" target="_blank">CS231 at Stanford Lecture</a>
|
||||
</div>
|
||||
|
||||
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="summary-on-cnns">Summary on CNNs </h2>
|
||||
<h2 id="reading-recommendations">Reading Recommendations </h2>
|
||||
|
||||
<ul>
|
||||
<li> Goodfellow et al, chapter 10 on Recurrent NNs, chapters 11 and 12 on various practicalities around deep learning are also recommended.</li>
|
||||
<li> Aurelien Geron, chapter 14 on RNNs.</li>
|
||||
</ul>
|
||||
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="summary-on-deep-learning-methods">Summary on Deep Learning Methods </h2>
|
||||
|
||||
<p>
|
||||
Material to be added
|
||||
We have studied fully connected neural networks (also called artifical nueral networks) and convolutional neural networks (CNNs).
|
||||
|
||||
<p>
|
||||
The first type of deep learning networks work very well on homogeneous and structured input data while CCNs are normally tailored to recognizing images.
|
||||
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="cnns-in-brief">CNNs in brief </h2>
|
||||
|
||||
<p>
|
||||
In summary:
|
||||
|
||||
<ul>
|
||||
<li> A CNN architecture is in the simplest case a list of Layers that transform the image volume into an output volume (e.g. holding the class scores)</li>
|
||||
<li> There are a few distinct types of Layers (e.g. CONV/FC/RELU/POOL are by far the most popular)</li>
|
||||
<li> Each Layer accepts an input 3D volume and transforms it to an output 3D volume through a differentiable function</li>
|
||||
<li> Each Layer may or may not have parameters (e.g. CONV/FC do, RELU/POOL don’t)</li>
|
||||
<li> Each Layer may or may not have additional hyperparameters (e.g. CONV/FC/POOL do, RELU doesn’t)</li>
|
||||
</ul>
|
||||
|
||||
For more material on convolutional networks, we strongly recommend
|
||||
the course
|
||||
<a href="https://www.uio.no/studier/emner/matnat/ifi/IN5400/index-eng.html" target="_blank">IN5400 – Machine Learning for Image Analysis</a>
|
||||
and the slides of <a href="http://cs231n.github.io/convolutional-networks/" target="_blank">CS231</a> which is taught at Stanford University (consistently ranked as one of the top computer science programs in the world). <a href="http://neuralnetworksanddeeplearning.com/chap6.html" target="_blank">Michael Nielsen's book is a must read, in particular chapter 6 which deals with CNNs</a>.
|
||||
|
||||
<p>
|
||||
However, both standard feed forwards networks and CNNs perform well on data with unknown length.
|
||||
|
||||
<p>
|
||||
This is where recurrent nueral networks (RNNs) come to our rescue.
|
||||
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
@@ -175,14 +218,14 @@ example, they can take sentences, documents, or audio samples as
|
||||
input, making them extremely useful for natural language processing
|
||||
systems such as automatic translation and speech-to-text.
|
||||
|
||||
<p>
|
||||
RNNs are also used in "text classification":"Text Classification Algorithms: A Survey"
|
||||
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="set-up-of-an-rnn">Set up of an RNN </h2>
|
||||
|
||||
<p>
|
||||
Text to come.
|
||||
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
|
||||
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/doconce/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Convolutional Neural Networks and Recurrent Neural Networks">
|
||||
<meta name="description" content="Week 43: Deep Learning: Recurrent Neural Networks and other methods">
|
||||
|
||||
<title>Week 43: Convolutional Neural Networks and Recurrent Neural Networks</title>
|
||||
<title>Week 43: Deep Learning: Recurrent Neural Networks and other methods</title>
|
||||
|
||||
|
||||
<style type="text/css">
|
||||
@@ -67,7 +67,12 @@ div { text-align: justify; text-justify: inter-word; }
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Plans for week 43', 2, None, 'plans-for-week-43'),
|
||||
('Summary on CNNs', 2, None, 'summary-on-cnns'),
|
||||
('Reading Recommendations', 2, None, 'reading-recommendations'),
|
||||
('Summary on Deep Learning Methods',
|
||||
2,
|
||||
None,
|
||||
'summary-on-deep-learning-methods'),
|
||||
('CNNs in brief', 2, None, 'cnns-in-brief'),
|
||||
('Recurrent neural networks: Overarching view',
|
||||
2,
|
||||
None,
|
||||
@@ -94,7 +99,7 @@ end of tocinfo -->
|
||||
|
||||
|
||||
|
||||
<center><h1>Week 43: Convolutional Neural Networks and Recurrent Neural Networks</h1></center> <!-- document title -->
|
||||
<center><h1>Week 43: Deep Learning: Recurrent Neural Networks and other methods</h1></center> <!-- document title -->
|
||||
|
||||
<p>
|
||||
<!-- author(s): Morten Hjorth-Jensen -->
|
||||
@@ -110,7 +115,7 @@ end of tocinfo -->
|
||||
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
|
||||
<br>
|
||||
<p>
|
||||
<center><h4>Oct 24, 2021</h4></center> <!-- date -->
|
||||
<center><h4>Oct 26, 2021</h4></center> <!-- date -->
|
||||
<br>
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
@@ -119,7 +124,7 @@ end of tocinfo -->
|
||||
|
||||
<ul>
|
||||
<li> Thursday: Summary of Convolutional Neural Networks from week 42 and Recurrent Neural Networks</li>
|
||||
<li> Friday: Recurrent Neural Networks and other Deep Learning methods</li>
|
||||
<li> Friday: Recurrent Neural Networks and other Deep Learning methods such as Generalized Adversarial Neural Networks</li>
|
||||
</ul>
|
||||
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
@@ -140,20 +145,58 @@ end of tocinfo -->
|
||||
<p>
|
||||
|
||||
<ul>
|
||||
<li> <a href="https://www.uio.no/studier/emner/matnat/ifi/IN5400/v20/material/week10/in5400_2020_week10_recurrent_neural_network.pdf" target="_blank">IN5400 at UiO Lecture</a></li>
|
||||
<li> <a href="https://www.uio.no/studier/emner/matnat/ifi/IN5400/v20/material/week10/in5400_2020_week10_recurrent_neural_network.pdf" target="_blank">IN5400 at UiO Lecture</a></li>
|
||||
<li> <a href="https://www.youtube.com/watch?v=6niqTuYFZLQ&list=PLzUTmXVwsnXod6WNdg57Yc3zFx_f-RYsq&index=10&ab_channel=StanfordUniversitySchoolofEngineering" target="_blank">CS231 at Stanford Lecture</a></li>
|
||||
</ul>
|
||||
|
||||
<li> <a href="https://www.youtube.com/watch?v=6niqTuYFZLQ&list=PLzUTmXVwsnXod6WNdg57Yc3zFx_f-RYsq&index=10&ab_channel=StanfordUniversitySchoolofEngineering" target="_blank">CS231 at Stanford Lecture</a>
|
||||
</div>
|
||||
|
||||
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="summary-on-cnns">Summary on CNNs </h2>
|
||||
<h2 id="reading-recommendations">Reading Recommendations </h2>
|
||||
|
||||
<ul>
|
||||
<li> Goodfellow et al, chapter 10 on Recurrent NNs, chapters 11 and 12 on various practicalities around deep learning are also recommended.</li>
|
||||
<li> Aurelien Geron, chapter 14 on RNNs.</li>
|
||||
</ul>
|
||||
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="summary-on-deep-learning-methods">Summary on Deep Learning Methods </h2>
|
||||
|
||||
<p>
|
||||
Material to be added
|
||||
We have studied fully connected neural networks (also called artifical nueral networks) and convolutional neural networks (CNNs).
|
||||
|
||||
<p>
|
||||
The first type of deep learning networks work very well on homogeneous and structured input data while CCNs are normally tailored to recognizing images.
|
||||
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="cnns-in-brief">CNNs in brief </h2>
|
||||
|
||||
<p>
|
||||
In summary:
|
||||
|
||||
<ul>
|
||||
<li> A CNN architecture is in the simplest case a list of Layers that transform the image volume into an output volume (e.g. holding the class scores)</li>
|
||||
<li> There are a few distinct types of Layers (e.g. CONV/FC/RELU/POOL are by far the most popular)</li>
|
||||
<li> Each Layer accepts an input 3D volume and transforms it to an output 3D volume through a differentiable function</li>
|
||||
<li> Each Layer may or may not have parameters (e.g. CONV/FC do, RELU/POOL don’t)</li>
|
||||
<li> Each Layer may or may not have additional hyperparameters (e.g. CONV/FC/POOL do, RELU doesn’t)</li>
|
||||
</ul>
|
||||
|
||||
For more material on convolutional networks, we strongly recommend
|
||||
the course
|
||||
<a href="https://www.uio.no/studier/emner/matnat/ifi/IN5400/index-eng.html" target="_blank">IN5400 – Machine Learning for Image Analysis</a>
|
||||
and the slides of <a href="http://cs231n.github.io/convolutional-networks/" target="_blank">CS231</a> which is taught at Stanford University (consistently ranked as one of the top computer science programs in the world). <a href="http://neuralnetworksanddeeplearning.com/chap6.html" target="_blank">Michael Nielsen's book is a must read, in particular chapter 6 which deals with CNNs</a>.
|
||||
|
||||
<p>
|
||||
However, both standard feed forwards networks and CNNs perform well on data with unknown length.
|
||||
|
||||
<p>
|
||||
This is where recurrent nueral networks (RNNs) come to our rescue.
|
||||
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
@@ -180,14 +223,14 @@ example, they can take sentences, documents, or audio samples as
|
||||
input, making them extremely useful for natural language processing
|
||||
systems such as automatic translation and speech-to-text.
|
||||
|
||||
<p>
|
||||
RNNs are also used in "text classification":"Text Classification Algorithms: A Survey"
|
||||
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="set-up-of-an-rnn">Set up of an RNN </h2>
|
||||
|
||||
<p>
|
||||
Text to come.
|
||||
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
|
||||
Binary file not shown.
@@ -4,13 +4,13 @@
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"<!-- dom:TITLE: Week 43: Convolutional Neural Networks and Recurrent Neural Networks -->\n",
|
||||
"# Week 43: Convolutional Neural Networks and Recurrent Neural Networks\n",
|
||||
"<!-- dom:TITLE: Week 43: Deep Learning: Recurrent Neural Networks and other methods -->\n",
|
||||
"# Week 43: Deep Learning: Recurrent Neural Networks and other methods\n",
|
||||
"<!-- dom:AUTHOR: Morten Hjorth-Jensen at Department of Physics, University of Oslo & Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University -->\n",
|
||||
"<!-- Author: --> \n",
|
||||
"**Morten Hjorth-Jensen**, Department of Physics, University of Oslo and Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University\n",
|
||||
"\n",
|
||||
"Date: **Oct 24, 2021**\n",
|
||||
"Date: **Oct 26, 2021**\n",
|
||||
"\n",
|
||||
"Copyright 1999-2021, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license\n",
|
||||
"\n",
|
||||
@@ -20,7 +20,7 @@
|
||||
"\n",
|
||||
"* Thursday: Summary of Convolutional Neural Networks from week 42 and Recurrent Neural Networks\n",
|
||||
"\n",
|
||||
"* Friday: Recurrent Neural Networks and other Deep Learning methods\n",
|
||||
"* Friday: Recurrent Neural Networks and other Deep Learning methods such as Generalized Adversarial Neural Networks\n",
|
||||
"\n",
|
||||
"**Excellent lectures on CNNs and RNNs.**\n",
|
||||
"\n",
|
||||
@@ -34,18 +34,48 @@
|
||||
"\n",
|
||||
"**More resources.**\n",
|
||||
"\n",
|
||||
" * [IN5400 at UiO Lecture](https://www.uio.no/studier/emner/matnat/ifi/IN5400/v20/material/week10/in5400_2020_week10_recurrent_neural_network.pdf)\n",
|
||||
"\n",
|
||||
"* [IN5400 at UiO Lecture](https://www.uio.no/studier/emner/matnat/ifi/IN5400/v20/material/week10/in5400_2020_week10_recurrent_neural_network.pdf)\n",
|
||||
"\n",
|
||||
"* [CS231 at Stanford Lecture](https://www.youtube.com/watch?v=6niqTuYFZLQ&list=PLzUTmXVwsnXod6WNdg57Yc3zFx_f-RYsq&index=10&ab_channel=StanfordUniversitySchoolofEngineering)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"## Summary on CNNs\n",
|
||||
"## Reading Recommendations\n",
|
||||
"\n",
|
||||
"Material to be added\n",
|
||||
"* Goodfellow et al, chapter 10 on Recurrent NNs, chapters 11 and 12 on various practicalities around deep learning are also recommended.\n",
|
||||
"\n",
|
||||
"* Aurelien Geron, chapter 14 on RNNs.\n",
|
||||
"\n",
|
||||
"## Summary on Deep Learning Methods\n",
|
||||
"\n",
|
||||
"We have studied fully connected neural networks (also called artifical nueral networks) and convolutional neural networks (CNNs).\n",
|
||||
"\n",
|
||||
"The first type of deep learning networks work very well on homogeneous and structured input data while CCNs are normally tailored to recognizing images.\n",
|
||||
"\n",
|
||||
"## CNNs in brief\n",
|
||||
"\n",
|
||||
"In summary:\n",
|
||||
"\n",
|
||||
"* A CNN architecture is in the simplest case a list of Layers that transform the image volume into an output volume (e.g. holding the class scores)\n",
|
||||
"\n",
|
||||
"* There are a few distinct types of Layers (e.g. CONV/FC/RELU/POOL are by far the most popular)\n",
|
||||
"\n",
|
||||
"* Each Layer accepts an input 3D volume and transforms it to an output 3D volume through a differentiable function\n",
|
||||
"\n",
|
||||
"* Each Layer may or may not have parameters (e.g. CONV/FC do, RELU/POOL don’t)\n",
|
||||
"\n",
|
||||
"* Each Layer may or may not have additional hyperparameters (e.g. CONV/FC/POOL do, RELU doesn’t)\n",
|
||||
"\n",
|
||||
"For more material on convolutional networks, we strongly recommend\n",
|
||||
"the course\n",
|
||||
"[IN5400 – Machine Learning for Image Analysis](https://www.uio.no/studier/emner/matnat/ifi/IN5400/index-eng.html)\n",
|
||||
"and the slides of [CS231](http://cs231n.github.io/convolutional-networks/) which is taught at Stanford University (consistently ranked as one of the top computer science programs in the world). [Michael Nielsen's book is a must read, in particular chapter 6 which deals with CNNs](http://neuralnetworksanddeeplearning.com/chap6.html).\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"However, both standard feed forwards networks and CNNs perform well on data with unknown length.\n",
|
||||
"\n",
|
||||
"This is where recurrent nueral networks (RNNs) come to our rescue.\n",
|
||||
"\n",
|
||||
"## Recurrent neural networks: Overarching view\n",
|
||||
"\n",
|
||||
@@ -66,14 +96,12 @@
|
||||
"input, making them extremely useful for natural language processing\n",
|
||||
"systems such as automatic translation and speech-to-text.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"RNNs are also used in \"text classification\":\"Text Classification Algorithms: A Survey\"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"## Set up of an RNN\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Text to come.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"## A simple example"
|
||||
]
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
TITLE: Week 43: Convolutional Neural Networks and Recurrent Neural Networks
|
||||
TITLE: Week 43: Deep Learning: Recurrent Neural Networks and other methods
|
||||
AUTHOR: Morten Hjorth-Jensen {copyright, 1999-present|CC BY-NC} at Department of Physics, University of Oslo & Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University
|
||||
DATE: today
|
||||
|
||||
@@ -6,7 +6,7 @@ DATE: today
|
||||
===== Plans for week 43 =====
|
||||
|
||||
* Thursday: Summary of Convolutional Neural Networks from week 42 and Recurrent Neural Networks
|
||||
* Friday: Recurrent Neural Networks and other Deep Learning methods
|
||||
* Friday: Recurrent Neural Networks and other Deep Learning methods such as Generalized Adversarial Neural Networks
|
||||
|
||||
|
||||
|
||||
@@ -17,18 +17,46 @@ DATE: today
|
||||
!eblock
|
||||
|
||||
!bblock More resources
|
||||
* "IN5400 at UiO Lecture":"https://www.uio.no/studier/emner/matnat/ifi/IN5400/v20/material/week10/in5400_2020_week10_recurrent_neural_network.pdf"
|
||||
|
||||
* "IN5400 at UiO Lecture":"https://www.uio.no/studier/emner/matnat/ifi/IN5400/v20/material/week10/in5400_2020_week10_recurrent_neural_network.pdf"
|
||||
* "CS231 at Stanford Lecture":"https://www.youtube.com/watch?v=6niqTuYFZLQ&list=PLzUTmXVwsnXod6WNdg57Yc3zFx_f-RYsq&index=10&ab_channel=StanfordUniversitySchoolofEngineering"
|
||||
!eblock
|
||||
|
||||
|
||||
!split
|
||||
===== Summary on CNNs =====
|
||||
===== Reading Recommendations =====
|
||||
|
||||
Material to be added
|
||||
* Goodfellow et al, chapter 10 on Recurrent NNs, chapters 11 and 12 on various practicalities around deep learning are also recommended.
|
||||
* Aurelien Geron, chapter 14 on RNNs.
|
||||
|
||||
|
||||
!split
|
||||
===== Summary on Deep Learning Methods =====
|
||||
|
||||
We have studied fully connected neural networks (also called artifical nueral networks) and convolutional neural networks (CNNs).
|
||||
|
||||
The first type of deep learning networks work very well on homogeneous and structured input data while CCNs are normally tailored to recognizing images.
|
||||
|
||||
!split
|
||||
===== CNNs in brief =====
|
||||
|
||||
In summary:
|
||||
|
||||
* A CNN architecture is in the simplest case a list of Layers that transform the image volume into an output volume (e.g. holding the class scores)
|
||||
* There are a few distinct types of Layers (e.g. CONV/FC/RELU/POOL are by far the most popular)
|
||||
* Each Layer accepts an input 3D volume and transforms it to an output 3D volume through a differentiable function
|
||||
* Each Layer may or may not have parameters (e.g. CONV/FC do, RELU/POOL don’t)
|
||||
* Each Layer may or may not have additional hyperparameters (e.g. CONV/FC/POOL do, RELU doesn’t)
|
||||
|
||||
For more material on convolutional networks, we strongly recommend
|
||||
the course
|
||||
"IN5400 – Machine Learning for Image Analysis":"https://www.uio.no/studier/emner/matnat/ifi/IN5400/index-eng.html"
|
||||
and the slides of "CS231":"http://cs231n.github.io/convolutional-networks/" which is taught at Stanford University (consistently ranked as one of the top computer science programs in the world). "Michael Nielsen's book is a must read, in particular chapter 6 which deals with CNNs":"http://neuralnetworksanddeeplearning.com/chap6.html".
|
||||
|
||||
|
||||
However, both standard feed forwards networks and CNNs perform well on data with unknown length.
|
||||
|
||||
This is where recurrent nueral networks (RNNs) come to our rescue.
|
||||
|
||||
!split
|
||||
===== Recurrent neural networks: Overarching view =====
|
||||
|
||||
@@ -49,15 +77,13 @@ example, they can take sentences, documents, or audio samples as
|
||||
input, making them extremely useful for natural language processing
|
||||
systems such as automatic translation and speech-to-text.
|
||||
|
||||
|
||||
RNNs are also used in "text classification":"Text Classification Algorithms: A Survey"
|
||||
|
||||
|
||||
!split
|
||||
===== Set up of an RNN =====
|
||||
|
||||
|
||||
Text to come.
|
||||
|
||||
|
||||
!split
|
||||
===== A simple example =====
|
||||
@@ -702,3 +728,7 @@ print('Time: ', end-start)
|
||||
!ec
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user