update week45

This commit is contained in:
Morten Hjorth-Jensen
2023-11-06 06:15:10 +01:00
parent 4cc3d3c20b
commit 64486c146d
50 changed files with 11164 additions and 10188 deletions
+113 -127
View File
@@ -8,8 +8,8 @@ doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=d
<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 45: Decisions Trees, Random Forests, Bagging and Boosting">
<title>Week 45: Decisions Trees, Random Forests, Bagging and Boosting</title>
<meta name="description" content="Week 45, Recurrent Neural Networks">
<title>Week 45, Recurrent Neural Networks</title>
<!-- Bootstrap style: bootstrap -->
<!-- doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=default --html_admon=bootstrap_panel --html_output=week45-bs --no_mako -->
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
@@ -36,109 +36,101 @@ doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=d
<!-- tocinfo
{'highest level': 2,
'sections': [('Overview of week 45', 2, None, 'overview-of-week-45'),
('Brief code reminder from last wekk',
'sections': [('Plan for week 45', 2, None, 'plan-for-week-45'),
('Material for the lab sessions, additional ways to present '
'classification results and other practicalities',
2,
None,
'brief-code-reminder-from-last-wekk'),
("Boosting, a Bird's Eye View",
'material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities'),
('Searching for Optimal Regularization Parameters $\\lambda$',
2,
None,
'boosting-a-bird-s-eye-view'),
('What is boosting? Additive Modelling/Iterative Fitting',
'searching-for-optimal-regularization-parameters-lambda'),
('Grid Search', 2, None, 'grid-search'),
('Randomized Grid Search', 2, None, 'randomized-grid-search'),
('Wisconsin Cancer Data', 2, None, 'wisconsin-cancer-data'),
('Using the correlation matrix',
2,
None,
'what-is-boosting-additive-modelling-iterative-fitting'),
('Iterative Fitting, Regression and Squared-error Cost Function',
'using-the-correlation-matrix'),
('Discussing the correlation data',
2,
None,
'iterative-fitting-regression-and-squared-error-cost-function'),
('Squared-Error Example and Iterative Fitting',
'discussing-the-correlation-data'),
('Other measures in classification studies: Cancer Data again',
2,
None,
'squared-error-example-and-iterative-fitting'),
('Iterative Fitting, Classification and AdaBoost',
'other-measures-in-classification-studies-cancer-data-again'),
('Material for Lecture Thursday November 9',
2,
None,
'iterative-fitting-classification-and-adaboost'),
('Adaptive Boosting, AdaBoost',
'material-for-lecture-thursday-november-9'),
('Recurrent neural networks (RNNs): Overarching view',
2,
None,
'adaptive-boosting-adaboost'),
('Building up AdaBoost', 2, None, 'building-up-adaboost'),
('Adaptive boosting: AdaBoost, Basic Algorithm',
'recurrent-neural-networks-rnns-overarching-view'),
('A simple example', 2, None, 'a-simple-example'),
('Memoryless models', 3, None, 'memoryless-models'),
('Linear dynamical model', 3, None, 'linear-dynamical-model'),
('Hidden Markov Models', 3, None, 'hidden-markov-models'),
('RNNs', 3, None, 'rnns'),
('Do generative models need to be stochastic?',
3,
None,
'do-generative-models-need-to-be-stochastic'),
('What kinds of behaviour can RNNs exhibit?',
3,
None,
'what-kinds-of-behaviour-can-rnns-exhibit'),
('Basic layout', 2, None, 'basic-layout'),
('We need to specify the initial activity state of all the '
'hidden and output units',
3,
None,
'we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units'),
('We can specify inputs in several ways',
3,
None,
'we-can-specify-inputs-in-several-ways'),
('We can specify targets in several ways',
3,
None,
'we-can-specify-targets-in-several-ways'),
('Backpropagation through time',
3,
None,
'backpropagation-through-time'),
('The backward pass is linear',
3,
None,
'the-backward-pass-is-linear'),
('The problem of exploding or vanishing gradients',
2,
None,
'adaptive-boosting-adaboost-basic-algorithm'),
('Basic Steps of AdaBoost', 2, None, 'basic-steps-of-adaboost'),
('AdaBoost Examples', 2, None, 'adaboost-examples'),
('Gradient boosting: Basics with Steepest Descent/Functional '
'Gradient Descent',
'the-problem-of-exploding-or-vanishing-gradients'),
('Four effective ways to learn an RNN',
2,
None,
'gradient-boosting-basics-with-steepest-descent-functional-gradient-descent'),
('The Squared-Error again! Steepest Descent',
'four-effective-ways-to-learn-an-rnn'),
('Long Short Term Memory (LSTM)',
3,
None,
'long-short-term-memory-lstm'),
('Implementing a memory cell in a neural network',
3,
None,
'implementing-a-memory-cell-in-a-neural-network'),
('An extrapolation example', 2, None, 'an-extrapolation-example'),
('Formatting the Data', 2, None, 'formatting-the-data'),
('Predicting New Points With A Trained Recurrent Neural Network',
2,
None,
'the-squared-error-again-steepest-descent'),
('Steepest Descent Example', 2, None, 'steepest-descent-example'),
('Gradient Boosting, algorithm',
'predicting-new-points-with-a-trained-recurrent-neural-network'),
('Other Things to Try', 2, None, 'other-things-to-try'),
('Other Types of Recurrent Neural Networks',
2,
None,
'gradient-boosting-algorithm'),
('Gradient Boosting, Examples of Regression',
2,
None,
'gradient-boosting-examples-of-regression'),
('Gradient Boosting, Classification Example',
2,
None,
'gradient-boosting-classification-example'),
('XGBoost: Extreme Gradient Boosting',
2,
None,
'xgboost-extreme-gradient-boosting'),
('Regression Case', 2, None, 'regression-case'),
('Xgboost on the Cancer Data',
2,
None,
'xgboost-on-the-cancer-data'),
('Support Vector Machines, overarching aims',
2,
None,
'support-vector-machines-overarching-aims'),
('Hyperplanes and all that', 2, None, 'hyperplanes-and-all-that'),
('What is a hyperplane?', 2, None, 'what-is-a-hyperplane'),
('A $p$-dimensional space of features',
2,
None,
'a-p-dimensional-space-of-features'),
('The two-dimensional case', 2, None, 'the-two-dimensional-case'),
('Getting into the details', 2, None, 'getting-into-the-details'),
('First attempt at a minimization approach',
2,
None,
'first-attempt-at-a-minimization-approach'),
('Solving the equations', 2, None, 'solving-the-equations'),
('Code Example', 2, None, 'code-example'),
('Problems with the Simpler Approach',
2,
None,
'problems-with-the-simpler-approach'),
('A better approach', 2, None, 'a-better-approach'),
('A quick Reminder on Lagrangian Multipliers',
2,
None,
'a-quick-reminder-on-lagrangian-multipliers'),
('Adding the Multiplier', 2, None, 'adding-the-multiplier'),
('Setting up the Problem', 2, None, 'setting-up-the-problem'),
('The problem to solve', 2, None, 'the-problem-to-solve'),
('The last steps', 2, None, 'the-last-steps'),
('A soft classifier', 2, None, 'a-soft-classifier'),
('Soft optmization problem',
2,
None,
'soft-optmization-problem')]}
'other-types-of-recurrent-neural-networks')]}
end of tocinfo -->
<body>
@@ -166,52 +158,46 @@ MathJax.Hub.Config({
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="week45-bs.html">Week 45: Decisions Trees, Random Forests, Bagging and Boosting</a>
<a class="navbar-brand" href="week45-bs.html">Week 45, Recurrent Neural Networks</a>
</div>
<div class="navbar-collapse collapse navbar-responsive-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
<ul class="dropdown-menu">
<!-- navigation toc: --> <li><a href="._week45-bs001.html#overview-of-week-45" style="font-size: 80%;">Overview of week 45</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs002.html#brief-code-reminder-from-last-wekk" style="font-size: 80%;">Brief code reminder from last wekk</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs003.html#boosting-a-bird-s-eye-view" style="font-size: 80%;">Boosting, a Bird's Eye View</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs004.html#what-is-boosting-additive-modelling-iterative-fitting" style="font-size: 80%;">What is boosting? Additive Modelling/Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs005.html#iterative-fitting-regression-and-squared-error-cost-function" style="font-size: 80%;">Iterative Fitting, Regression and Squared-error Cost Function</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs006.html#squared-error-example-and-iterative-fitting" style="font-size: 80%;">Squared-Error Example and Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs007.html#iterative-fitting-classification-and-adaboost" style="font-size: 80%;">Iterative Fitting, Classification and AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs008.html#adaptive-boosting-adaboost" style="font-size: 80%;">Adaptive Boosting, AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs009.html#building-up-adaboost" style="font-size: 80%;">Building up AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs010.html#adaptive-boosting-adaboost-basic-algorithm" style="font-size: 80%;">Adaptive boosting: AdaBoost, Basic Algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs011.html#basic-steps-of-adaboost" style="font-size: 80%;">Basic Steps of AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#adaboost-examples" style="font-size: 80%;">AdaBoost Examples</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#gradient-boosting-basics-with-steepest-descent-functional-gradient-descent" style="font-size: 80%;">Gradient boosting: Basics with Steepest Descent/Functional Gradient Descent</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs014.html#the-squared-error-again-steepest-descent" style="font-size: 80%;">The Squared-Error again! Steepest Descent</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#steepest-descent-example" style="font-size: 80%;">Steepest Descent Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs016.html#gradient-boosting-algorithm" style="font-size: 80%;">Gradient Boosting, algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs017.html#gradient-boosting-examples-of-regression" style="font-size: 80%;">Gradient Boosting, Examples of Regression</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs018.html#gradient-boosting-classification-example" style="font-size: 80%;">Gradient Boosting, Classification Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs019.html#xgboost-extreme-gradient-boosting" style="font-size: 80%;">XGBoost: Extreme Gradient Boosting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs020.html#regression-case" style="font-size: 80%;">Regression Case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs021.html#xgboost-on-the-cancer-data" style="font-size: 80%;">Xgboost on the Cancer Data</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs022.html#support-vector-machines-overarching-aims" style="font-size: 80%;">Support Vector Machines, overarching aims</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs023.html#hyperplanes-and-all-that" style="font-size: 80%;">Hyperplanes and all that</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs024.html#what-is-a-hyperplane" style="font-size: 80%;">What is a hyperplane?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs025.html#a-p-dimensional-space-of-features" style="font-size: 80%;">A \( p \)-dimensional space of features</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs026.html#the-two-dimensional-case" style="font-size: 80%;">The two-dimensional case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs027.html#getting-into-the-details" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs028.html#first-attempt-at-a-minimization-approach" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs029.html#solving-the-equations" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs030.html#code-example" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs031.html#problems-with-the-simpler-approach" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs032.html#a-better-approach" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs033.html#a-quick-reminder-on-lagrangian-multipliers" style="font-size: 80%;">A quick Reminder on Lagrangian Multipliers</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs034.html#adding-the-multiplier" style="font-size: 80%;">Adding the Multiplier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs035.html#setting-up-the-problem" style="font-size: 80%;">Setting up the Problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs036.html#the-problem-to-solve" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs037.html#the-last-steps" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs038.html#a-soft-classifier" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs039.html#soft-optmization-problem" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs001.html#plan-for-week-45" style="font-size: 80%;"><b>Plan for week 45</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs002.html#material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities" style="font-size: 80%;"><b>Material for the lab sessions, additional ways to present classification results and other practicalities</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs003.html#searching-for-optimal-regularization-parameters-lambda" style="font-size: 80%;"><b>Searching for Optimal Regularization Parameters \( \lambda \)</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs004.html#grid-search" style="font-size: 80%;"><b>Grid Search</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs005.html#randomized-grid-search" style="font-size: 80%;"><b>Randomized Grid Search</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs006.html#wisconsin-cancer-data" style="font-size: 80%;"><b>Wisconsin Cancer Data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs007.html#using-the-correlation-matrix" style="font-size: 80%;"><b>Using the correlation matrix</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs008.html#discussing-the-correlation-data" style="font-size: 80%;"><b>Discussing the correlation data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs009.html#other-measures-in-classification-studies-cancer-data-again" style="font-size: 80%;"><b>Other measures in classification studies: Cancer Data again</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs010.html#material-for-lecture-thursday-november-9" style="font-size: 80%;"><b>Material for Lecture Thursday November 9</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs011.html#recurrent-neural-networks-rnns-overarching-view" style="font-size: 80%;"><b>Recurrent neural networks (RNNs): Overarching view</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#a-simple-example" style="font-size: 80%;"><b>A simple example</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#memoryless-models" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Memoryless models</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#linear-dynamical-model" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Linear dynamical model</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#hidden-markov-models" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Hidden Markov Models</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#rnns" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;RNNs</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#do-generative-models-need-to-be-stochastic" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Do generative models need to be stochastic?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#what-kinds-of-behaviour-can-rnns-exhibit" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;What kinds of behaviour can RNNs exhibit?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#basic-layout" style="font-size: 80%;"><b>Basic layout</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We need to specify the initial activity state of all the hidden and output units</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-can-specify-inputs-in-several-ways" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We can specify inputs in several ways</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-can-specify-targets-in-several-ways" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We can specify targets in several ways</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#backpropagation-through-time" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Backpropagation through time</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#the-backward-pass-is-linear" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;The backward pass is linear</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs014.html#the-problem-of-exploding-or-vanishing-gradients" style="font-size: 80%;"><b>The problem of exploding or vanishing gradients</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#four-effective-ways-to-learn-an-rnn" style="font-size: 80%;"><b>Four effective ways to learn an RNN</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#long-short-term-memory-lstm" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Long Short Term Memory (LSTM)</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#implementing-a-memory-cell-in-a-neural-network" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Implementing a memory cell in a neural network</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs016.html#an-extrapolation-example" style="font-size: 80%;"><b>An extrapolation example</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs017.html#formatting-the-data" style="font-size: 80%;"><b>Formatting the Data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs018.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;"><b>Predicting New Points With A Trained Recurrent Neural Network</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs019.html#other-things-to-try" style="font-size: 80%;"><b>Other Things to Try</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs020.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;"><b>Other Types of Recurrent Neural Networks</b></a></li>
</ul>
</li>
@@ -225,7 +211,7 @@ MathJax.Hub.Config({
<!-- ------------------- main content ---------------------- -->
<div class="jumbotron">
<center>
<h1>Week 45: Decisions Trees, Random Forests, Bagging and Boosting</h1>
<h1>Week 45, Recurrent Neural Networks</h1>
</center> <!-- document title -->
<!-- author(s): Morten Hjorth-Jensen -->
@@ -241,7 +227,7 @@ MathJax.Hub.Config({
</center>
<br>
<center>
<h4>Nov 11, 2022</h4>
<h4>November 6-10</h4>
</center> <!-- date -->
<br>
@@ -266,7 +252,7 @@ MathJax.Hub.Config({
<li><a href="._week45-bs008.html">9</a></li>
<li><a href="._week45-bs009.html">10</a></li>
<li><a href="">...</a></li>
<li><a href="._week45-bs039.html">40</a></li>
<li><a href="._week45-bs020.html">21</a></li>
<li><a href="._week45-bs001.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
@@ -280,7 +266,7 @@ MathJax.Hub.Config({
</footer>
-->
<center style="font-size:80%">
<!-- copyright --> &copy; 1999-2022, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license
<!-- copyright --> &copy; 1999-2023, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license
</center>
</body>
</html>
+138 -151
View File
@@ -8,8 +8,8 @@ doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=d
<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 45: Decisions Trees, Random Forests, Bagging and Boosting">
<title>Week 45: Decisions Trees, Random Forests, Bagging and Boosting</title>
<meta name="description" content="Week 45, Recurrent Neural Networks">
<title>Week 45, Recurrent Neural Networks</title>
<!-- Bootstrap style: bootstrap -->
<!-- doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=default --html_admon=bootstrap_panel --html_output=week45-bs --no_mako -->
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
@@ -36,109 +36,101 @@ doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=d
<!-- tocinfo
{'highest level': 2,
'sections': [('Overview of week 45', 2, None, 'overview-of-week-45'),
('Brief code reminder from last wekk',
'sections': [('Plan for week 45', 2, None, 'plan-for-week-45'),
('Material for the lab sessions, additional ways to present '
'classification results and other practicalities',
2,
None,
'brief-code-reminder-from-last-wekk'),
("Boosting, a Bird's Eye View",
'material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities'),
('Searching for Optimal Regularization Parameters $\\lambda$',
2,
None,
'boosting-a-bird-s-eye-view'),
('What is boosting? Additive Modelling/Iterative Fitting',
'searching-for-optimal-regularization-parameters-lambda'),
('Grid Search', 2, None, 'grid-search'),
('Randomized Grid Search', 2, None, 'randomized-grid-search'),
('Wisconsin Cancer Data', 2, None, 'wisconsin-cancer-data'),
('Using the correlation matrix',
2,
None,
'what-is-boosting-additive-modelling-iterative-fitting'),
('Iterative Fitting, Regression and Squared-error Cost Function',
'using-the-correlation-matrix'),
('Discussing the correlation data',
2,
None,
'iterative-fitting-regression-and-squared-error-cost-function'),
('Squared-Error Example and Iterative Fitting',
'discussing-the-correlation-data'),
('Other measures in classification studies: Cancer Data again',
2,
None,
'squared-error-example-and-iterative-fitting'),
('Iterative Fitting, Classification and AdaBoost',
'other-measures-in-classification-studies-cancer-data-again'),
('Material for Lecture Thursday November 9',
2,
None,
'iterative-fitting-classification-and-adaboost'),
('Adaptive Boosting, AdaBoost',
'material-for-lecture-thursday-november-9'),
('Recurrent neural networks (RNNs): Overarching view',
2,
None,
'adaptive-boosting-adaboost'),
('Building up AdaBoost', 2, None, 'building-up-adaboost'),
('Adaptive boosting: AdaBoost, Basic Algorithm',
'recurrent-neural-networks-rnns-overarching-view'),
('A simple example', 2, None, 'a-simple-example'),
('Memoryless models', 3, None, 'memoryless-models'),
('Linear dynamical model', 3, None, 'linear-dynamical-model'),
('Hidden Markov Models', 3, None, 'hidden-markov-models'),
('RNNs', 3, None, 'rnns'),
('Do generative models need to be stochastic?',
3,
None,
'do-generative-models-need-to-be-stochastic'),
('What kinds of behaviour can RNNs exhibit?',
3,
None,
'what-kinds-of-behaviour-can-rnns-exhibit'),
('Basic layout', 2, None, 'basic-layout'),
('We need to specify the initial activity state of all the '
'hidden and output units',
3,
None,
'we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units'),
('We can specify inputs in several ways',
3,
None,
'we-can-specify-inputs-in-several-ways'),
('We can specify targets in several ways',
3,
None,
'we-can-specify-targets-in-several-ways'),
('Backpropagation through time',
3,
None,
'backpropagation-through-time'),
('The backward pass is linear',
3,
None,
'the-backward-pass-is-linear'),
('The problem of exploding or vanishing gradients',
2,
None,
'adaptive-boosting-adaboost-basic-algorithm'),
('Basic Steps of AdaBoost', 2, None, 'basic-steps-of-adaboost'),
('AdaBoost Examples', 2, None, 'adaboost-examples'),
('Gradient boosting: Basics with Steepest Descent/Functional '
'Gradient Descent',
'the-problem-of-exploding-or-vanishing-gradients'),
('Four effective ways to learn an RNN',
2,
None,
'gradient-boosting-basics-with-steepest-descent-functional-gradient-descent'),
('The Squared-Error again! Steepest Descent',
'four-effective-ways-to-learn-an-rnn'),
('Long Short Term Memory (LSTM)',
3,
None,
'long-short-term-memory-lstm'),
('Implementing a memory cell in a neural network',
3,
None,
'implementing-a-memory-cell-in-a-neural-network'),
('An extrapolation example', 2, None, 'an-extrapolation-example'),
('Formatting the Data', 2, None, 'formatting-the-data'),
('Predicting New Points With A Trained Recurrent Neural Network',
2,
None,
'the-squared-error-again-steepest-descent'),
('Steepest Descent Example', 2, None, 'steepest-descent-example'),
('Gradient Boosting, algorithm',
'predicting-new-points-with-a-trained-recurrent-neural-network'),
('Other Things to Try', 2, None, 'other-things-to-try'),
('Other Types of Recurrent Neural Networks',
2,
None,
'gradient-boosting-algorithm'),
('Gradient Boosting, Examples of Regression',
2,
None,
'gradient-boosting-examples-of-regression'),
('Gradient Boosting, Classification Example',
2,
None,
'gradient-boosting-classification-example'),
('XGBoost: Extreme Gradient Boosting',
2,
None,
'xgboost-extreme-gradient-boosting'),
('Regression Case', 2, None, 'regression-case'),
('Xgboost on the Cancer Data',
2,
None,
'xgboost-on-the-cancer-data'),
('Support Vector Machines, overarching aims',
2,
None,
'support-vector-machines-overarching-aims'),
('Hyperplanes and all that', 2, None, 'hyperplanes-and-all-that'),
('What is a hyperplane?', 2, None, 'what-is-a-hyperplane'),
('A $p$-dimensional space of features',
2,
None,
'a-p-dimensional-space-of-features'),
('The two-dimensional case', 2, None, 'the-two-dimensional-case'),
('Getting into the details', 2, None, 'getting-into-the-details'),
('First attempt at a minimization approach',
2,
None,
'first-attempt-at-a-minimization-approach'),
('Solving the equations', 2, None, 'solving-the-equations'),
('Code Example', 2, None, 'code-example'),
('Problems with the Simpler Approach',
2,
None,
'problems-with-the-simpler-approach'),
('A better approach', 2, None, 'a-better-approach'),
('A quick Reminder on Lagrangian Multipliers',
2,
None,
'a-quick-reminder-on-lagrangian-multipliers'),
('Adding the Multiplier', 2, None, 'adding-the-multiplier'),
('Setting up the Problem', 2, None, 'setting-up-the-problem'),
('The problem to solve', 2, None, 'the-problem-to-solve'),
('The last steps', 2, None, 'the-last-steps'),
('A soft classifier', 2, None, 'a-soft-classifier'),
('Soft optmization problem',
2,
None,
'soft-optmization-problem')]}
'other-types-of-recurrent-neural-networks')]}
end of tocinfo -->
<body>
@@ -166,52 +158,46 @@ MathJax.Hub.Config({
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="week45-bs.html">Week 45: Decisions Trees, Random Forests, Bagging and Boosting</a>
<a class="navbar-brand" href="week45-bs.html">Week 45, Recurrent Neural Networks</a>
</div>
<div class="navbar-collapse collapse navbar-responsive-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
<ul class="dropdown-menu">
<!-- navigation toc: --> <li><a href="#overview-of-week-45" style="font-size: 80%;">Overview of week 45</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs002.html#brief-code-reminder-from-last-wekk" style="font-size: 80%;">Brief code reminder from last wekk</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs003.html#boosting-a-bird-s-eye-view" style="font-size: 80%;">Boosting, a Bird's Eye View</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs004.html#what-is-boosting-additive-modelling-iterative-fitting" style="font-size: 80%;">What is boosting? Additive Modelling/Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs005.html#iterative-fitting-regression-and-squared-error-cost-function" style="font-size: 80%;">Iterative Fitting, Regression and Squared-error Cost Function</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs006.html#squared-error-example-and-iterative-fitting" style="font-size: 80%;">Squared-Error Example and Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs007.html#iterative-fitting-classification-and-adaboost" style="font-size: 80%;">Iterative Fitting, Classification and AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs008.html#adaptive-boosting-adaboost" style="font-size: 80%;">Adaptive Boosting, AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs009.html#building-up-adaboost" style="font-size: 80%;">Building up AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs010.html#adaptive-boosting-adaboost-basic-algorithm" style="font-size: 80%;">Adaptive boosting: AdaBoost, Basic Algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs011.html#basic-steps-of-adaboost" style="font-size: 80%;">Basic Steps of AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#adaboost-examples" style="font-size: 80%;">AdaBoost Examples</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#gradient-boosting-basics-with-steepest-descent-functional-gradient-descent" style="font-size: 80%;">Gradient boosting: Basics with Steepest Descent/Functional Gradient Descent</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs014.html#the-squared-error-again-steepest-descent" style="font-size: 80%;">The Squared-Error again! Steepest Descent</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#steepest-descent-example" style="font-size: 80%;">Steepest Descent Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs016.html#gradient-boosting-algorithm" style="font-size: 80%;">Gradient Boosting, algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs017.html#gradient-boosting-examples-of-regression" style="font-size: 80%;">Gradient Boosting, Examples of Regression</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs018.html#gradient-boosting-classification-example" style="font-size: 80%;">Gradient Boosting, Classification Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs019.html#xgboost-extreme-gradient-boosting" style="font-size: 80%;">XGBoost: Extreme Gradient Boosting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs020.html#regression-case" style="font-size: 80%;">Regression Case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs021.html#xgboost-on-the-cancer-data" style="font-size: 80%;">Xgboost on the Cancer Data</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs022.html#support-vector-machines-overarching-aims" style="font-size: 80%;">Support Vector Machines, overarching aims</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs023.html#hyperplanes-and-all-that" style="font-size: 80%;">Hyperplanes and all that</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs024.html#what-is-a-hyperplane" style="font-size: 80%;">What is a hyperplane?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs025.html#a-p-dimensional-space-of-features" style="font-size: 80%;">A \( p \)-dimensional space of features</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs026.html#the-two-dimensional-case" style="font-size: 80%;">The two-dimensional case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs027.html#getting-into-the-details" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs028.html#first-attempt-at-a-minimization-approach" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs029.html#solving-the-equations" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs030.html#code-example" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs031.html#problems-with-the-simpler-approach" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs032.html#a-better-approach" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs033.html#a-quick-reminder-on-lagrangian-multipliers" style="font-size: 80%;">A quick Reminder on Lagrangian Multipliers</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs034.html#adding-the-multiplier" style="font-size: 80%;">Adding the Multiplier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs035.html#setting-up-the-problem" style="font-size: 80%;">Setting up the Problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs036.html#the-problem-to-solve" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs037.html#the-last-steps" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs038.html#a-soft-classifier" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs039.html#soft-optmization-problem" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="#plan-for-week-45" style="font-size: 80%;"><b>Plan for week 45</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs002.html#material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities" style="font-size: 80%;"><b>Material for the lab sessions, additional ways to present classification results and other practicalities</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs003.html#searching-for-optimal-regularization-parameters-lambda" style="font-size: 80%;"><b>Searching for Optimal Regularization Parameters \( \lambda \)</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs004.html#grid-search" style="font-size: 80%;"><b>Grid Search</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs005.html#randomized-grid-search" style="font-size: 80%;"><b>Randomized Grid Search</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs006.html#wisconsin-cancer-data" style="font-size: 80%;"><b>Wisconsin Cancer Data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs007.html#using-the-correlation-matrix" style="font-size: 80%;"><b>Using the correlation matrix</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs008.html#discussing-the-correlation-data" style="font-size: 80%;"><b>Discussing the correlation data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs009.html#other-measures-in-classification-studies-cancer-data-again" style="font-size: 80%;"><b>Other measures in classification studies: Cancer Data again</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs010.html#material-for-lecture-thursday-november-9" style="font-size: 80%;"><b>Material for Lecture Thursday November 9</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs011.html#recurrent-neural-networks-rnns-overarching-view" style="font-size: 80%;"><b>Recurrent neural networks (RNNs): Overarching view</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#a-simple-example" style="font-size: 80%;"><b>A simple example</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#memoryless-models" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Memoryless models</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#linear-dynamical-model" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Linear dynamical model</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#hidden-markov-models" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Hidden Markov Models</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#rnns" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;RNNs</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#do-generative-models-need-to-be-stochastic" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Do generative models need to be stochastic?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#what-kinds-of-behaviour-can-rnns-exhibit" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;What kinds of behaviour can RNNs exhibit?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#basic-layout" style="font-size: 80%;"><b>Basic layout</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We need to specify the initial activity state of all the hidden and output units</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-can-specify-inputs-in-several-ways" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We can specify inputs in several ways</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-can-specify-targets-in-several-ways" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We can specify targets in several ways</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#backpropagation-through-time" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Backpropagation through time</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#the-backward-pass-is-linear" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;The backward pass is linear</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs014.html#the-problem-of-exploding-or-vanishing-gradients" style="font-size: 80%;"><b>The problem of exploding or vanishing gradients</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#four-effective-ways-to-learn-an-rnn" style="font-size: 80%;"><b>Four effective ways to learn an RNN</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#long-short-term-memory-lstm" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Long Short Term Memory (LSTM)</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#implementing-a-memory-cell-in-a-neural-network" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Implementing a memory cell in a neural network</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs016.html#an-extrapolation-example" style="font-size: 80%;"><b>An extrapolation example</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs017.html#formatting-the-data" style="font-size: 80%;"><b>Formatting the Data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs018.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;"><b>Predicting New Points With A Trained Recurrent Neural Network</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs019.html#other-things-to-try" style="font-size: 80%;"><b>Other Things to Try</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs020.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;"><b>Other Types of Recurrent Neural Networks</b></a></li>
</ul>
</li>
@@ -223,37 +209,38 @@ MathJax.Hub.Config({
<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p> <!-- add vertical space -->
<a name="part0001"></a>
<!-- !split -->
<h2 id="overview-of-week-45" class="anchor">Overview of week 45 </h2>
<ul>
<li> Thursday: Boosting methods, froma AdaBoost to Gradient boosting</li>
<ul>
<li> <a href="https://youtu.be/mK48PfCxgYk" target="_self">Video of lecture</a></li>
</ul>
<li> Friday: Gradient boosting and discussion of Decision trees and ensemble methods. Wrapping up trees and start discussing Support Vector Machines</li>
<ul>
<li> <a href="https://youtu.be/v8eJBFeZKuI" target="_self">Video of lecture</a></li>
</ul>
</ul>
<div class="panel panel-default">
<div class="panel-body">
<!-- subsequent paragraphs come in larger fonts, so start with a paragraph -->
<ol>
<li> <a href="https://www.youtube.com/watch?v=RmajweUFKvM&ab_channel=Simplilearn" target="_self">Video on Decision trees</a></li>
<li> <a href="https://www.youtube.com/watch?v=wPqtzj5VZus&ab_channel=H2O.ai" target="_self">Video on boosting methods by Hastie</a>.</li>
<li> <a href="https://www.youtube.com/watch?v=LsK-xG1cLYA" target="_self">Video on AdaBoost</a></li>
<li> <a href="https://www.youtube.com/watch?v=3CC4N4z3GJc" target="_self">Video on Gradient boost, part 1, parts 2-4 follows</a></li>
</ol>
</div>
</div>
<h2 id="plan-for-week-45" class="anchor">Plan for week 45 </h2>
<div class="panel panel-default">
<div class="panel-body">
<!-- subsequent paragraphs come in larger fonts, so start with a paragraph -->
<ol>
<li> <a href="https://github.com/CompPhysics/MachineLearning/blob/master/doc/Textbooks/elementsstat.pdf" target="_self">Hastie et al, chapter 10.1-10.10</a>. Geron's chapters 6 and 7 are also useful.</li>
</ol>
<ul>
<li> Discussion of project 2</li>
<li> <a href="https://youtu.be/Ia6wwDLxqtM" target="_self">Video of lab session from week 43</a></li>
<li> <a href="https://youtu.be/EajWMW__k0I" target="_self">Video of lab session from week 44</a></li>
<li> <a href="https://github.com/CompPhysics/MachineLearning/blob/master/doc/HandWrittenNotes/2023/Exercisesweek44.pdf" target="_self">See also whiteboard notes from lab session week 44</a></li>
</ul>
</div>
</div>
<div class="panel panel-default">
<div class="panel-body">
<!-- subsequent paragraphs come in larger fonts, so start with a paragraph -->
<ul>
<li> Short repetition on Convolutional Neural Networks</li>
<li> Recurrent Neural Networks (RNNs)</li>
<li> Readings and Videos:</li>
<ul>
<li> These lecture notes</li>
<li> For a more in depth discussion on neural networks we recommend Goodfellow et al chapter 10. See also chapter 11 and 12 on practicalities and applications</li>
<ul>
<li> Reading suggestions for implementation of RNNs: <a href="https://github.com/CompPhysics/MachineLearning/blob/master/doc/Textbooks/TensorflowML.pdf" target="_self">Aurelien Geron's chapter 14</a>.</li>
</ul>
<li> <a href="https://www.youtube.com/watch?v=SEnXr6v2ifU&ab_channel=AlexanderAmini" target="_self">Video on Recurrent Neural Networks from MIT</a></li>
<li> <a href="https://www.youtube.com/playlist?list=PLZHQObOWTQDNU6R1_67000Dx_ZCJB-3pi" target="_self">Video on Deep Learning</a></li>
</ul>
</ul>
</div>
</div>
@@ -274,7 +261,7 @@ MathJax.Hub.Config({
<li><a href="._week45-bs009.html">10</a></li>
<li><a href="._week45-bs010.html">11</a></li>
<li><a href="">...</a></li>
<li><a href="._week45-bs039.html">40</a></li>
<li><a href="._week45-bs020.html">21</a></li>
<li><a href="._week45-bs002.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+111 -234
View File
@@ -8,8 +8,8 @@ doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=d
<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 45: Decisions Trees, Random Forests, Bagging and Boosting">
<title>Week 45: Decisions Trees, Random Forests, Bagging and Boosting</title>
<meta name="description" content="Week 45, Recurrent Neural Networks">
<title>Week 45, Recurrent Neural Networks</title>
<!-- Bootstrap style: bootstrap -->
<!-- doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=default --html_admon=bootstrap_panel --html_output=week45-bs --no_mako -->
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
@@ -36,109 +36,101 @@ doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=d
<!-- tocinfo
{'highest level': 2,
'sections': [('Overview of week 45', 2, None, 'overview-of-week-45'),
('Brief code reminder from last wekk',
'sections': [('Plan for week 45', 2, None, 'plan-for-week-45'),
('Material for the lab sessions, additional ways to present '
'classification results and other practicalities',
2,
None,
'brief-code-reminder-from-last-wekk'),
("Boosting, a Bird's Eye View",
'material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities'),
('Searching for Optimal Regularization Parameters $\\lambda$',
2,
None,
'boosting-a-bird-s-eye-view'),
('What is boosting? Additive Modelling/Iterative Fitting',
'searching-for-optimal-regularization-parameters-lambda'),
('Grid Search', 2, None, 'grid-search'),
('Randomized Grid Search', 2, None, 'randomized-grid-search'),
('Wisconsin Cancer Data', 2, None, 'wisconsin-cancer-data'),
('Using the correlation matrix',
2,
None,
'what-is-boosting-additive-modelling-iterative-fitting'),
('Iterative Fitting, Regression and Squared-error Cost Function',
'using-the-correlation-matrix'),
('Discussing the correlation data',
2,
None,
'iterative-fitting-regression-and-squared-error-cost-function'),
('Squared-Error Example and Iterative Fitting',
'discussing-the-correlation-data'),
('Other measures in classification studies: Cancer Data again',
2,
None,
'squared-error-example-and-iterative-fitting'),
('Iterative Fitting, Classification and AdaBoost',
'other-measures-in-classification-studies-cancer-data-again'),
('Material for Lecture Thursday November 9',
2,
None,
'iterative-fitting-classification-and-adaboost'),
('Adaptive Boosting, AdaBoost',
'material-for-lecture-thursday-november-9'),
('Recurrent neural networks (RNNs): Overarching view',
2,
None,
'adaptive-boosting-adaboost'),
('Building up AdaBoost', 2, None, 'building-up-adaboost'),
('Adaptive boosting: AdaBoost, Basic Algorithm',
'recurrent-neural-networks-rnns-overarching-view'),
('A simple example', 2, None, 'a-simple-example'),
('Memoryless models', 3, None, 'memoryless-models'),
('Linear dynamical model', 3, None, 'linear-dynamical-model'),
('Hidden Markov Models', 3, None, 'hidden-markov-models'),
('RNNs', 3, None, 'rnns'),
('Do generative models need to be stochastic?',
3,
None,
'do-generative-models-need-to-be-stochastic'),
('What kinds of behaviour can RNNs exhibit?',
3,
None,
'what-kinds-of-behaviour-can-rnns-exhibit'),
('Basic layout', 2, None, 'basic-layout'),
('We need to specify the initial activity state of all the '
'hidden and output units',
3,
None,
'we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units'),
('We can specify inputs in several ways',
3,
None,
'we-can-specify-inputs-in-several-ways'),
('We can specify targets in several ways',
3,
None,
'we-can-specify-targets-in-several-ways'),
('Backpropagation through time',
3,
None,
'backpropagation-through-time'),
('The backward pass is linear',
3,
None,
'the-backward-pass-is-linear'),
('The problem of exploding or vanishing gradients',
2,
None,
'adaptive-boosting-adaboost-basic-algorithm'),
('Basic Steps of AdaBoost', 2, None, 'basic-steps-of-adaboost'),
('AdaBoost Examples', 2, None, 'adaboost-examples'),
('Gradient boosting: Basics with Steepest Descent/Functional '
'Gradient Descent',
'the-problem-of-exploding-or-vanishing-gradients'),
('Four effective ways to learn an RNN',
2,
None,
'gradient-boosting-basics-with-steepest-descent-functional-gradient-descent'),
('The Squared-Error again! Steepest Descent',
'four-effective-ways-to-learn-an-rnn'),
('Long Short Term Memory (LSTM)',
3,
None,
'long-short-term-memory-lstm'),
('Implementing a memory cell in a neural network',
3,
None,
'implementing-a-memory-cell-in-a-neural-network'),
('An extrapolation example', 2, None, 'an-extrapolation-example'),
('Formatting the Data', 2, None, 'formatting-the-data'),
('Predicting New Points With A Trained Recurrent Neural Network',
2,
None,
'the-squared-error-again-steepest-descent'),
('Steepest Descent Example', 2, None, 'steepest-descent-example'),
('Gradient Boosting, algorithm',
'predicting-new-points-with-a-trained-recurrent-neural-network'),
('Other Things to Try', 2, None, 'other-things-to-try'),
('Other Types of Recurrent Neural Networks',
2,
None,
'gradient-boosting-algorithm'),
('Gradient Boosting, Examples of Regression',
2,
None,
'gradient-boosting-examples-of-regression'),
('Gradient Boosting, Classification Example',
2,
None,
'gradient-boosting-classification-example'),
('XGBoost: Extreme Gradient Boosting',
2,
None,
'xgboost-extreme-gradient-boosting'),
('Regression Case', 2, None, 'regression-case'),
('Xgboost on the Cancer Data',
2,
None,
'xgboost-on-the-cancer-data'),
('Support Vector Machines, overarching aims',
2,
None,
'support-vector-machines-overarching-aims'),
('Hyperplanes and all that', 2, None, 'hyperplanes-and-all-that'),
('What is a hyperplane?', 2, None, 'what-is-a-hyperplane'),
('A $p$-dimensional space of features',
2,
None,
'a-p-dimensional-space-of-features'),
('The two-dimensional case', 2, None, 'the-two-dimensional-case'),
('Getting into the details', 2, None, 'getting-into-the-details'),
('First attempt at a minimization approach',
2,
None,
'first-attempt-at-a-minimization-approach'),
('Solving the equations', 2, None, 'solving-the-equations'),
('Code Example', 2, None, 'code-example'),
('Problems with the Simpler Approach',
2,
None,
'problems-with-the-simpler-approach'),
('A better approach', 2, None, 'a-better-approach'),
('A quick Reminder on Lagrangian Multipliers',
2,
None,
'a-quick-reminder-on-lagrangian-multipliers'),
('Adding the Multiplier', 2, None, 'adding-the-multiplier'),
('Setting up the Problem', 2, None, 'setting-up-the-problem'),
('The problem to solve', 2, None, 'the-problem-to-solve'),
('The last steps', 2, None, 'the-last-steps'),
('A soft classifier', 2, None, 'a-soft-classifier'),
('Soft optmization problem',
2,
None,
'soft-optmization-problem')]}
'other-types-of-recurrent-neural-networks')]}
end of tocinfo -->
<body>
@@ -166,52 +158,46 @@ MathJax.Hub.Config({
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="week45-bs.html">Week 45: Decisions Trees, Random Forests, Bagging and Boosting</a>
<a class="navbar-brand" href="week45-bs.html">Week 45, Recurrent Neural Networks</a>
</div>
<div class="navbar-collapse collapse navbar-responsive-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
<ul class="dropdown-menu">
<!-- navigation toc: --> <li><a href="._week45-bs001.html#overview-of-week-45" style="font-size: 80%;">Overview of week 45</a></li>
<!-- navigation toc: --> <li><a href="#brief-code-reminder-from-last-wekk" style="font-size: 80%;">Brief code reminder from last wekk</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs003.html#boosting-a-bird-s-eye-view" style="font-size: 80%;">Boosting, a Bird's Eye View</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs004.html#what-is-boosting-additive-modelling-iterative-fitting" style="font-size: 80%;">What is boosting? Additive Modelling/Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs005.html#iterative-fitting-regression-and-squared-error-cost-function" style="font-size: 80%;">Iterative Fitting, Regression and Squared-error Cost Function</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs006.html#squared-error-example-and-iterative-fitting" style="font-size: 80%;">Squared-Error Example and Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs007.html#iterative-fitting-classification-and-adaboost" style="font-size: 80%;">Iterative Fitting, Classification and AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs008.html#adaptive-boosting-adaboost" style="font-size: 80%;">Adaptive Boosting, AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs009.html#building-up-adaboost" style="font-size: 80%;">Building up AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs010.html#adaptive-boosting-adaboost-basic-algorithm" style="font-size: 80%;">Adaptive boosting: AdaBoost, Basic Algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs011.html#basic-steps-of-adaboost" style="font-size: 80%;">Basic Steps of AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#adaboost-examples" style="font-size: 80%;">AdaBoost Examples</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#gradient-boosting-basics-with-steepest-descent-functional-gradient-descent" style="font-size: 80%;">Gradient boosting: Basics with Steepest Descent/Functional Gradient Descent</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs014.html#the-squared-error-again-steepest-descent" style="font-size: 80%;">The Squared-Error again! Steepest Descent</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#steepest-descent-example" style="font-size: 80%;">Steepest Descent Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs016.html#gradient-boosting-algorithm" style="font-size: 80%;">Gradient Boosting, algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs017.html#gradient-boosting-examples-of-regression" style="font-size: 80%;">Gradient Boosting, Examples of Regression</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs018.html#gradient-boosting-classification-example" style="font-size: 80%;">Gradient Boosting, Classification Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs019.html#xgboost-extreme-gradient-boosting" style="font-size: 80%;">XGBoost: Extreme Gradient Boosting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs020.html#regression-case" style="font-size: 80%;">Regression Case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs021.html#xgboost-on-the-cancer-data" style="font-size: 80%;">Xgboost on the Cancer Data</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs022.html#support-vector-machines-overarching-aims" style="font-size: 80%;">Support Vector Machines, overarching aims</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs023.html#hyperplanes-and-all-that" style="font-size: 80%;">Hyperplanes and all that</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs024.html#what-is-a-hyperplane" style="font-size: 80%;">What is a hyperplane?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs025.html#a-p-dimensional-space-of-features" style="font-size: 80%;">A \( p \)-dimensional space of features</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs026.html#the-two-dimensional-case" style="font-size: 80%;">The two-dimensional case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs027.html#getting-into-the-details" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs028.html#first-attempt-at-a-minimization-approach" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs029.html#solving-the-equations" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs030.html#code-example" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs031.html#problems-with-the-simpler-approach" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs032.html#a-better-approach" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs033.html#a-quick-reminder-on-lagrangian-multipliers" style="font-size: 80%;">A quick Reminder on Lagrangian Multipliers</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs034.html#adding-the-multiplier" style="font-size: 80%;">Adding the Multiplier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs035.html#setting-up-the-problem" style="font-size: 80%;">Setting up the Problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs036.html#the-problem-to-solve" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs037.html#the-last-steps" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs038.html#a-soft-classifier" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs039.html#soft-optmization-problem" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs001.html#plan-for-week-45" style="font-size: 80%;"><b>Plan for week 45</b></a></li>
<!-- navigation toc: --> <li><a href="#material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities" style="font-size: 80%;"><b>Material for the lab sessions, additional ways to present classification results and other practicalities</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs003.html#searching-for-optimal-regularization-parameters-lambda" style="font-size: 80%;"><b>Searching for Optimal Regularization Parameters \( \lambda \)</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs004.html#grid-search" style="font-size: 80%;"><b>Grid Search</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs005.html#randomized-grid-search" style="font-size: 80%;"><b>Randomized Grid Search</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs006.html#wisconsin-cancer-data" style="font-size: 80%;"><b>Wisconsin Cancer Data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs007.html#using-the-correlation-matrix" style="font-size: 80%;"><b>Using the correlation matrix</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs008.html#discussing-the-correlation-data" style="font-size: 80%;"><b>Discussing the correlation data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs009.html#other-measures-in-classification-studies-cancer-data-again" style="font-size: 80%;"><b>Other measures in classification studies: Cancer Data again</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs010.html#material-for-lecture-thursday-november-9" style="font-size: 80%;"><b>Material for Lecture Thursday November 9</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs011.html#recurrent-neural-networks-rnns-overarching-view" style="font-size: 80%;"><b>Recurrent neural networks (RNNs): Overarching view</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#a-simple-example" style="font-size: 80%;"><b>A simple example</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#memoryless-models" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Memoryless models</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#linear-dynamical-model" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Linear dynamical model</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#hidden-markov-models" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Hidden Markov Models</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#rnns" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;RNNs</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#do-generative-models-need-to-be-stochastic" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Do generative models need to be stochastic?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#what-kinds-of-behaviour-can-rnns-exhibit" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;What kinds of behaviour can RNNs exhibit?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#basic-layout" style="font-size: 80%;"><b>Basic layout</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We need to specify the initial activity state of all the hidden and output units</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-can-specify-inputs-in-several-ways" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We can specify inputs in several ways</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-can-specify-targets-in-several-ways" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We can specify targets in several ways</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#backpropagation-through-time" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Backpropagation through time</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#the-backward-pass-is-linear" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;The backward pass is linear</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs014.html#the-problem-of-exploding-or-vanishing-gradients" style="font-size: 80%;"><b>The problem of exploding or vanishing gradients</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#four-effective-ways-to-learn-an-rnn" style="font-size: 80%;"><b>Four effective ways to learn an RNN</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#long-short-term-memory-lstm" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Long Short Term Memory (LSTM)</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#implementing-a-memory-cell-in-a-neural-network" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Implementing a memory cell in a neural network</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs016.html#an-extrapolation-example" style="font-size: 80%;"><b>An extrapolation example</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs017.html#formatting-the-data" style="font-size: 80%;"><b>Formatting the Data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs018.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;"><b>Predicting New Points With A Trained Recurrent Neural Network</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs019.html#other-things-to-try" style="font-size: 80%;"><b>Other Things to Try</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs020.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;"><b>Other Types of Recurrent Neural Networks</b></a></li>
</ul>
</li>
@@ -223,116 +209,7 @@ MathJax.Hub.Config({
<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p> <!-- add vertical space -->
<a name="part0002"></a>
<!-- !split -->
<h2 id="brief-code-reminder-from-last-wekk" class="anchor">Brief code reminder from last wekk </h2>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight" style="background: #f8f8f8">
<pre style="line-height: 125%;"><span style="color: #666666">%</span>matplotlib inline
<span style="color: #408080; font-style: italic"># Common imports</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">IPython.display</span> <span style="color: #008000; font-weight: bold">import</span> Image
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">pydot</span> <span style="color: #008000; font-weight: bold">import</span> graph_from_dot_data
<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">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> train_test_split
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.tree</span> <span style="color: #008000; font-weight: bold">import</span> export_graphviz
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> StandardScaler, OneHotEncoder
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.compose</span> <span style="color: #008000; font-weight: bold">import</span> ColumnTransformer
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">pydot</span> <span style="color: #008000; font-weight: bold">import</span> graph_from_dot_data
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.datasets</span> <span style="color: #008000; font-weight: bold">import</span> load_breast_cancer
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.svm</span> <span style="color: #008000; font-weight: bold">import</span> SVC
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.linear_model</span> <span style="color: #008000; font-weight: bold">import</span> LogisticRegression
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.tree</span> <span style="color: #008000; font-weight: bold">import</span> DecisionTreeClassifier
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.ensemble</span> <span style="color: #008000; font-weight: bold">import</span> RandomForestClassifier
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> LabelEncoder
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> cross_validate
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">scikitplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">skplt</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> StandardScaler
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">os</span>
<span style="color: #408080; font-style: italic"># Where to save the figures and data files</span>
PROJECT_ROOT_DIR <span style="color: #666666">=</span> <span style="color: #BA2121">&quot;Results&quot;</span>
FIGURE_ID <span style="color: #666666">=</span> <span style="color: #BA2121">&quot;Results/FigureFiles&quot;</span>
DATA_ID <span style="color: #666666">=</span> <span style="color: #BA2121">&quot;DataFiles/&quot;</span>
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #AA22FF; font-weight: bold">not</span> os<span style="color: #666666">.</span>path<span style="color: #666666">.</span>exists(PROJECT_ROOT_DIR):
os<span style="color: #666666">.</span>mkdir(PROJECT_ROOT_DIR)
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #AA22FF; font-weight: bold">not</span> os<span style="color: #666666">.</span>path<span style="color: #666666">.</span>exists(FIGURE_ID):
os<span style="color: #666666">.</span>makedirs(FIGURE_ID)
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #AA22FF; font-weight: bold">not</span> os<span style="color: #666666">.</span>path<span style="color: #666666">.</span>exists(DATA_ID):
os<span style="color: #666666">.</span>makedirs(DATA_ID)
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">image_path</span>(fig_id):
<span style="color: #008000; font-weight: bold">return</span> os<span style="color: #666666">.</span>path<span style="color: #666666">.</span>join(FIGURE_ID, fig_id)
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">data_path</span>(dat_id):
<span style="color: #008000; font-weight: bold">return</span> os<span style="color: #666666">.</span>path<span style="color: #666666">.</span>join(DATA_ID, dat_id)
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">save_fig</span>(fig_id):
plt<span style="color: #666666">.</span>savefig(image_path(fig_id) <span style="color: #666666">+</span> <span style="color: #BA2121">&quot;.png&quot;</span>, <span style="color: #008000">format</span><span style="color: #666666">=</span><span style="color: #BA2121">&#39;png&#39;</span>)
<span style="color: #408080; font-style: italic"># Load the cancer data</span>
cancer <span style="color: #666666">=</span> load_breast_cancer()
X_train, X_test, y_train, y_test <span style="color: #666666">=</span> train_test_split(cancer<span style="color: #666666">.</span>data,cancer<span style="color: #666666">.</span>target,random_state<span style="color: #666666">=0</span>)
<span style="color: #008000">print</span>(X_train<span style="color: #666666">.</span>shape)
<span style="color: #008000">print</span>(X_test<span style="color: #666666">.</span>shape)
<span style="color: #408080; font-style: italic">#Scale the data</span>
scaler <span style="color: #666666">=</span> StandardScaler()
scaler<span style="color: #666666">.</span>fit(X_train)
X_train_scaled <span style="color: #666666">=</span> scaler<span style="color: #666666">.</span>transform(X_train)
X_test_scaled <span style="color: #666666">=</span> scaler<span style="color: #666666">.</span>transform(X_test)
<span style="color: #408080; font-style: italic">#define methods</span>
<span style="color: #408080; font-style: italic"># Logistic Regression</span>
logreg <span style="color: #666666">=</span> LogisticRegression(solver<span style="color: #666666">=</span><span style="color: #BA2121">&#39;lbfgs&#39;</span>)
logreg<span style="color: #666666">.</span>fit(X_train_scaled, y_train)
<span style="color: #008000">print</span>(<span style="color: #BA2121">&quot;Test set accuracy Logistic Regression with scaled data: </span><span style="color: #BB6688; font-weight: bold">{:.2f}</span><span style="color: #BA2121">&quot;</span><span style="color: #666666">.</span>format(logreg<span style="color: #666666">.</span>score(X_test_scaled,y_test)))
<span style="color: #408080; font-style: italic"># Decision Trees</span>
deep_tree_clf <span style="color: #666666">=</span> DecisionTreeClassifier(max_depth<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">None</span>)
deep_tree_clf<span style="color: #666666">.</span>fit(X_train_scaled, y_train)
<span style="color: #008000">print</span>(<span style="color: #BA2121">&quot;Test set accuracy with Decision Trees and scaled data: </span><span style="color: #BB6688; font-weight: bold">{:.2f}</span><span style="color: #BA2121">&quot;</span><span style="color: #666666">.</span>format(deep_tree_clf<span style="color: #666666">.</span>score(X_test_scaled,y_test)))
<span style="color: #408080; font-style: italic"># Support Vector Machine</span>
svm <span style="color: #666666">=</span> SVC(gamma<span style="color: #666666">=</span><span style="color: #BA2121">&#39;auto&#39;</span>, C<span style="color: #666666">=100</span>)
svm<span style="color: #666666">.</span>fit(X_train_scaled, y_train)
<span style="color: #008000">print</span>(<span style="color: #BA2121">&quot;Test set accuracy SVM with scaled data: </span><span style="color: #BB6688; font-weight: bold">{:.2f}</span><span style="color: #BA2121">&quot;</span><span style="color: #666666">.</span>format(logreg<span style="color: #666666">.</span>score(X_test_scaled,y_test)))
<span style="color: #408080; font-style: italic"># Random forests</span>
<span style="color: #408080; font-style: italic">#Instantiate the model with 500 trees and entropy as splitting criteria</span>
Random_Forest_model <span style="color: #666666">=</span> RandomForestClassifier(n_estimators<span style="color: #666666">=500</span>,criterion<span style="color: #666666">=</span><span style="color: #BA2121">&quot;entropy&quot;</span>)
Random_Forest_model<span style="color: #666666">.</span>fit(X_train_scaled, y_train)
<span style="color: #008000">print</span>(<span style="color: #BA2121">&quot;Test set accuracy with Random Forests and scaled data: </span><span style="color: #BB6688; font-weight: bold">{:.2f}</span><span style="color: #BA2121">&quot;</span><span style="color: #666666">.</span>format(Random_Forest_model<span style="color: #666666">.</span>score(X_test_scaled,y_test)))
y_pred <span style="color: #666666">=</span> Random_Forest_model<span style="color: #666666">.</span>predict(X_test_scaled)
skplt<span style="color: #666666">.</span>metrics<span style="color: #666666">.</span>plot_confusion_matrix(y_test, y_pred, normalize<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">True</span>)
plt<span style="color: #666666">.</span>show()
y_probas <span style="color: #666666">=</span> Random_Forest_model<span style="color: #666666">.</span>predict_proba(X_test_scaled)
skplt<span style="color: #666666">.</span>metrics<span style="color: #666666">.</span>plot_roc(y_test, y_probas)
plt<span style="color: #666666">.</span>show()
skplt<span style="color: #666666">.</span>metrics<span style="color: #666666">.</span>plot_cumulative_gain(y_test, y_probas)
plt<span style="color: #666666">.</span>show()
</pre>
</div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_subarea output_stream output_stdout output_text">
</div>
</div>
</div>
</div>
</div>
<h2 id="material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities" class="anchor">Material for the lab sessions, additional ways to present classification results and other practicalities </h2>
<p>
<!-- navigation buttons at the bottom of the page -->
@@ -351,7 +228,7 @@ plt<span style="color: #666666">.</span>show()
<li><a href="._week45-bs010.html">11</a></li>
<li><a href="._week45-bs011.html">12</a></li>
<li><a href="">...</a></li>
<li><a href="._week45-bs039.html">40</a></li>
<li><a href="._week45-bs020.html">21</a></li>
<li><a href="._week45-bs003.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+186 -133
View File
@@ -8,8 +8,8 @@ doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=d
<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 45: Decisions Trees, Random Forests, Bagging and Boosting">
<title>Week 45: Decisions Trees, Random Forests, Bagging and Boosting</title>
<meta name="description" content="Week 45, Recurrent Neural Networks">
<title>Week 45, Recurrent Neural Networks</title>
<!-- Bootstrap style: bootstrap -->
<!-- doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=default --html_admon=bootstrap_panel --html_output=week45-bs --no_mako -->
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
@@ -36,109 +36,101 @@ doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=d
<!-- tocinfo
{'highest level': 2,
'sections': [('Overview of week 45', 2, None, 'overview-of-week-45'),
('Brief code reminder from last wekk',
'sections': [('Plan for week 45', 2, None, 'plan-for-week-45'),
('Material for the lab sessions, additional ways to present '
'classification results and other practicalities',
2,
None,
'brief-code-reminder-from-last-wekk'),
("Boosting, a Bird's Eye View",
'material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities'),
('Searching for Optimal Regularization Parameters $\\lambda$',
2,
None,
'boosting-a-bird-s-eye-view'),
('What is boosting? Additive Modelling/Iterative Fitting',
'searching-for-optimal-regularization-parameters-lambda'),
('Grid Search', 2, None, 'grid-search'),
('Randomized Grid Search', 2, None, 'randomized-grid-search'),
('Wisconsin Cancer Data', 2, None, 'wisconsin-cancer-data'),
('Using the correlation matrix',
2,
None,
'what-is-boosting-additive-modelling-iterative-fitting'),
('Iterative Fitting, Regression and Squared-error Cost Function',
'using-the-correlation-matrix'),
('Discussing the correlation data',
2,
None,
'iterative-fitting-regression-and-squared-error-cost-function'),
('Squared-Error Example and Iterative Fitting',
'discussing-the-correlation-data'),
('Other measures in classification studies: Cancer Data again',
2,
None,
'squared-error-example-and-iterative-fitting'),
('Iterative Fitting, Classification and AdaBoost',
'other-measures-in-classification-studies-cancer-data-again'),
('Material for Lecture Thursday November 9',
2,
None,
'iterative-fitting-classification-and-adaboost'),
('Adaptive Boosting, AdaBoost',
'material-for-lecture-thursday-november-9'),
('Recurrent neural networks (RNNs): Overarching view',
2,
None,
'adaptive-boosting-adaboost'),
('Building up AdaBoost', 2, None, 'building-up-adaboost'),
('Adaptive boosting: AdaBoost, Basic Algorithm',
'recurrent-neural-networks-rnns-overarching-view'),
('A simple example', 2, None, 'a-simple-example'),
('Memoryless models', 3, None, 'memoryless-models'),
('Linear dynamical model', 3, None, 'linear-dynamical-model'),
('Hidden Markov Models', 3, None, 'hidden-markov-models'),
('RNNs', 3, None, 'rnns'),
('Do generative models need to be stochastic?',
3,
None,
'do-generative-models-need-to-be-stochastic'),
('What kinds of behaviour can RNNs exhibit?',
3,
None,
'what-kinds-of-behaviour-can-rnns-exhibit'),
('Basic layout', 2, None, 'basic-layout'),
('We need to specify the initial activity state of all the '
'hidden and output units',
3,
None,
'we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units'),
('We can specify inputs in several ways',
3,
None,
'we-can-specify-inputs-in-several-ways'),
('We can specify targets in several ways',
3,
None,
'we-can-specify-targets-in-several-ways'),
('Backpropagation through time',
3,
None,
'backpropagation-through-time'),
('The backward pass is linear',
3,
None,
'the-backward-pass-is-linear'),
('The problem of exploding or vanishing gradients',
2,
None,
'adaptive-boosting-adaboost-basic-algorithm'),
('Basic Steps of AdaBoost', 2, None, 'basic-steps-of-adaboost'),
('AdaBoost Examples', 2, None, 'adaboost-examples'),
('Gradient boosting: Basics with Steepest Descent/Functional '
'Gradient Descent',
'the-problem-of-exploding-or-vanishing-gradients'),
('Four effective ways to learn an RNN',
2,
None,
'gradient-boosting-basics-with-steepest-descent-functional-gradient-descent'),
('The Squared-Error again! Steepest Descent',
'four-effective-ways-to-learn-an-rnn'),
('Long Short Term Memory (LSTM)',
3,
None,
'long-short-term-memory-lstm'),
('Implementing a memory cell in a neural network',
3,
None,
'implementing-a-memory-cell-in-a-neural-network'),
('An extrapolation example', 2, None, 'an-extrapolation-example'),
('Formatting the Data', 2, None, 'formatting-the-data'),
('Predicting New Points With A Trained Recurrent Neural Network',
2,
None,
'the-squared-error-again-steepest-descent'),
('Steepest Descent Example', 2, None, 'steepest-descent-example'),
('Gradient Boosting, algorithm',
'predicting-new-points-with-a-trained-recurrent-neural-network'),
('Other Things to Try', 2, None, 'other-things-to-try'),
('Other Types of Recurrent Neural Networks',
2,
None,
'gradient-boosting-algorithm'),
('Gradient Boosting, Examples of Regression',
2,
None,
'gradient-boosting-examples-of-regression'),
('Gradient Boosting, Classification Example',
2,
None,
'gradient-boosting-classification-example'),
('XGBoost: Extreme Gradient Boosting',
2,
None,
'xgboost-extreme-gradient-boosting'),
('Regression Case', 2, None, 'regression-case'),
('Xgboost on the Cancer Data',
2,
None,
'xgboost-on-the-cancer-data'),
('Support Vector Machines, overarching aims',
2,
None,
'support-vector-machines-overarching-aims'),
('Hyperplanes and all that', 2, None, 'hyperplanes-and-all-that'),
('What is a hyperplane?', 2, None, 'what-is-a-hyperplane'),
('A $p$-dimensional space of features',
2,
None,
'a-p-dimensional-space-of-features'),
('The two-dimensional case', 2, None, 'the-two-dimensional-case'),
('Getting into the details', 2, None, 'getting-into-the-details'),
('First attempt at a minimization approach',
2,
None,
'first-attempt-at-a-minimization-approach'),
('Solving the equations', 2, None, 'solving-the-equations'),
('Code Example', 2, None, 'code-example'),
('Problems with the Simpler Approach',
2,
None,
'problems-with-the-simpler-approach'),
('A better approach', 2, None, 'a-better-approach'),
('A quick Reminder on Lagrangian Multipliers',
2,
None,
'a-quick-reminder-on-lagrangian-multipliers'),
('Adding the Multiplier', 2, None, 'adding-the-multiplier'),
('Setting up the Problem', 2, None, 'setting-up-the-problem'),
('The problem to solve', 2, None, 'the-problem-to-solve'),
('The last steps', 2, None, 'the-last-steps'),
('A soft classifier', 2, None, 'a-soft-classifier'),
('Soft optmization problem',
2,
None,
'soft-optmization-problem')]}
'other-types-of-recurrent-neural-networks')]}
end of tocinfo -->
<body>
@@ -166,52 +158,46 @@ MathJax.Hub.Config({
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="week45-bs.html">Week 45: Decisions Trees, Random Forests, Bagging and Boosting</a>
<a class="navbar-brand" href="week45-bs.html">Week 45, Recurrent Neural Networks</a>
</div>
<div class="navbar-collapse collapse navbar-responsive-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
<ul class="dropdown-menu">
<!-- navigation toc: --> <li><a href="._week45-bs001.html#overview-of-week-45" style="font-size: 80%;">Overview of week 45</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs002.html#brief-code-reminder-from-last-wekk" style="font-size: 80%;">Brief code reminder from last wekk</a></li>
<!-- navigation toc: --> <li><a href="#boosting-a-bird-s-eye-view" style="font-size: 80%;">Boosting, a Bird's Eye View</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs004.html#what-is-boosting-additive-modelling-iterative-fitting" style="font-size: 80%;">What is boosting? Additive Modelling/Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs005.html#iterative-fitting-regression-and-squared-error-cost-function" style="font-size: 80%;">Iterative Fitting, Regression and Squared-error Cost Function</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs006.html#squared-error-example-and-iterative-fitting" style="font-size: 80%;">Squared-Error Example and Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs007.html#iterative-fitting-classification-and-adaboost" style="font-size: 80%;">Iterative Fitting, Classification and AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs008.html#adaptive-boosting-adaboost" style="font-size: 80%;">Adaptive Boosting, AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs009.html#building-up-adaboost" style="font-size: 80%;">Building up AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs010.html#adaptive-boosting-adaboost-basic-algorithm" style="font-size: 80%;">Adaptive boosting: AdaBoost, Basic Algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs011.html#basic-steps-of-adaboost" style="font-size: 80%;">Basic Steps of AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#adaboost-examples" style="font-size: 80%;">AdaBoost Examples</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#gradient-boosting-basics-with-steepest-descent-functional-gradient-descent" style="font-size: 80%;">Gradient boosting: Basics with Steepest Descent/Functional Gradient Descent</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs014.html#the-squared-error-again-steepest-descent" style="font-size: 80%;">The Squared-Error again! Steepest Descent</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#steepest-descent-example" style="font-size: 80%;">Steepest Descent Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs016.html#gradient-boosting-algorithm" style="font-size: 80%;">Gradient Boosting, algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs017.html#gradient-boosting-examples-of-regression" style="font-size: 80%;">Gradient Boosting, Examples of Regression</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs018.html#gradient-boosting-classification-example" style="font-size: 80%;">Gradient Boosting, Classification Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs019.html#xgboost-extreme-gradient-boosting" style="font-size: 80%;">XGBoost: Extreme Gradient Boosting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs020.html#regression-case" style="font-size: 80%;">Regression Case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs021.html#xgboost-on-the-cancer-data" style="font-size: 80%;">Xgboost on the Cancer Data</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs022.html#support-vector-machines-overarching-aims" style="font-size: 80%;">Support Vector Machines, overarching aims</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs023.html#hyperplanes-and-all-that" style="font-size: 80%;">Hyperplanes and all that</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs024.html#what-is-a-hyperplane" style="font-size: 80%;">What is a hyperplane?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs025.html#a-p-dimensional-space-of-features" style="font-size: 80%;">A \( p \)-dimensional space of features</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs026.html#the-two-dimensional-case" style="font-size: 80%;">The two-dimensional case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs027.html#getting-into-the-details" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs028.html#first-attempt-at-a-minimization-approach" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs029.html#solving-the-equations" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs030.html#code-example" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs031.html#problems-with-the-simpler-approach" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs032.html#a-better-approach" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs033.html#a-quick-reminder-on-lagrangian-multipliers" style="font-size: 80%;">A quick Reminder on Lagrangian Multipliers</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs034.html#adding-the-multiplier" style="font-size: 80%;">Adding the Multiplier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs035.html#setting-up-the-problem" style="font-size: 80%;">Setting up the Problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs036.html#the-problem-to-solve" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs037.html#the-last-steps" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs038.html#a-soft-classifier" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs039.html#soft-optmization-problem" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs001.html#plan-for-week-45" style="font-size: 80%;"><b>Plan for week 45</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs002.html#material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities" style="font-size: 80%;"><b>Material for the lab sessions, additional ways to present classification results and other practicalities</b></a></li>
<!-- navigation toc: --> <li><a href="#searching-for-optimal-regularization-parameters-lambda" style="font-size: 80%;"><b>Searching for Optimal Regularization Parameters \( \lambda \)</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs004.html#grid-search" style="font-size: 80%;"><b>Grid Search</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs005.html#randomized-grid-search" style="font-size: 80%;"><b>Randomized Grid Search</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs006.html#wisconsin-cancer-data" style="font-size: 80%;"><b>Wisconsin Cancer Data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs007.html#using-the-correlation-matrix" style="font-size: 80%;"><b>Using the correlation matrix</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs008.html#discussing-the-correlation-data" style="font-size: 80%;"><b>Discussing the correlation data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs009.html#other-measures-in-classification-studies-cancer-data-again" style="font-size: 80%;"><b>Other measures in classification studies: Cancer Data again</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs010.html#material-for-lecture-thursday-november-9" style="font-size: 80%;"><b>Material for Lecture Thursday November 9</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs011.html#recurrent-neural-networks-rnns-overarching-view" style="font-size: 80%;"><b>Recurrent neural networks (RNNs): Overarching view</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#a-simple-example" style="font-size: 80%;"><b>A simple example</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#memoryless-models" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Memoryless models</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#linear-dynamical-model" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Linear dynamical model</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#hidden-markov-models" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Hidden Markov Models</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#rnns" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;RNNs</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#do-generative-models-need-to-be-stochastic" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Do generative models need to be stochastic?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#what-kinds-of-behaviour-can-rnns-exhibit" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;What kinds of behaviour can RNNs exhibit?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#basic-layout" style="font-size: 80%;"><b>Basic layout</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We need to specify the initial activity state of all the hidden and output units</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-can-specify-inputs-in-several-ways" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We can specify inputs in several ways</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-can-specify-targets-in-several-ways" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We can specify targets in several ways</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#backpropagation-through-time" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Backpropagation through time</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#the-backward-pass-is-linear" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;The backward pass is linear</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs014.html#the-problem-of-exploding-or-vanishing-gradients" style="font-size: 80%;"><b>The problem of exploding or vanishing gradients</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#four-effective-ways-to-learn-an-rnn" style="font-size: 80%;"><b>Four effective ways to learn an RNN</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#long-short-term-memory-lstm" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Long Short Term Memory (LSTM)</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#implementing-a-memory-cell-in-a-neural-network" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Implementing a memory cell in a neural network</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs016.html#an-extrapolation-example" style="font-size: 80%;"><b>An extrapolation example</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs017.html#formatting-the-data" style="font-size: 80%;"><b>Formatting the Data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs018.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;"><b>Predicting New Points With A Trained Recurrent Neural Network</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs019.html#other-things-to-try" style="font-size: 80%;"><b>Other Things to Try</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs020.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;"><b>Other Types of Recurrent Neural Networks</b></a></li>
</ul>
</li>
@@ -223,18 +209,85 @@ MathJax.Hub.Config({
<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p> <!-- add vertical space -->
<a name="part0003"></a>
<!-- !split -->
<h2 id="boosting-a-bird-s-eye-view" class="anchor">Boosting, a Bird's Eye View </h2>
<h2 id="searching-for-optimal-regularization-parameters-lambda" class="anchor">Searching for Optimal Regularization Parameters \( \lambda \) </h2>
<p>The basic idea is to combine weak classifiers in order to create a good
classifier. With a weak classifier we often intend a classifier which
produces results which are only slightly better than we would get by
random guesses.
<p>In project 1, when using Ridge and Lasso regression, we end up
searching for the optimal parameter \( \lambda \) which minimizes our
selected scores (MSE or \( R2 \) values for example). The brute force
approach, as discussed in the code here for Ridge regression, consists
in evaluating the MSE as function of different \( \lambda \) values.
Based on these calculations, one tries then to determine the value of the hyperparameter \( \lambda \)
which results in optimal scores (for example the smallest MSE or an \( R2=1 \)).
</p>
<p>This is done by applying in an iterative way a weak (or a standard
classifier like decision trees) to modify the data. In each iteration
we emphasize those observations which are misclassified by weighting
them with a factor.
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight" style="background: #f8f8f8">
<pre style="line-height: 125%;"><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">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">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> train_test_split
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn</span> <span style="color: #008000; font-weight: bold">import</span> linear_model
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">MSE</span>(y_data,y_model):
n <span style="color: #666666">=</span> np<span style="color: #666666">.</span>size(y_model)
<span style="color: #008000; font-weight: bold">return</span> np<span style="color: #666666">.</span>sum((y_data<span style="color: #666666">-</span>y_model)<span style="color: #666666">**2</span>)<span style="color: #666666">/</span>n
<span style="color: #408080; font-style: italic"># A seed just to ensure that the random numbers are the same for every run.</span>
<span style="color: #408080; font-style: italic"># Useful for eventual debugging.</span>
np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>seed(<span style="color: #666666">2021</span>)
n <span style="color: #666666">=</span> <span style="color: #666666">100</span>
x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>rand(n)
y <span style="color: #666666">=</span> np<span style="color: #666666">.</span>exp(<span style="color: #666666">-</span>x<span style="color: #666666">**2</span>) <span style="color: #666666">+</span> <span style="color: #666666">1.5</span> <span style="color: #666666">*</span> np<span style="color: #666666">.</span>exp(<span style="color: #666666">-</span>(x<span style="color: #666666">-2</span>)<span style="color: #666666">**2</span>)<span style="color: #666666">+</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>randn(n)
Maxpolydegree <span style="color: #666666">=</span> <span style="color: #666666">5</span>
X <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros((n,Maxpolydegree<span style="color: #666666">-1</span>))
<span style="color: #008000; font-weight: bold">for</span> degree <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #666666">1</span>,Maxpolydegree): <span style="color: #408080; font-style: italic">#No intercept column</span>
X[:,degree<span style="color: #666666">-1</span>] <span style="color: #666666">=</span> x<span style="color: #666666">**</span>(degree)
<span style="color: #408080; font-style: italic"># We split the data in test and training data</span>
X_train, X_test, y_train, y_test <span style="color: #666666">=</span> train_test_split(X, y, test_size<span style="color: #666666">=0.2</span>)
<span style="color: #408080; font-style: italic"># Decide which values of lambda to use</span>
nlambdas <span style="color: #666666">=</span> <span style="color: #666666">500</span>
MSERidgePredict <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros(nlambdas)
lambdas <span style="color: #666666">=</span> np<span style="color: #666666">.</span>logspace(<span style="color: #666666">-4</span>, <span style="color: #666666">2</span>, nlambdas)
<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>(nlambdas):
lmb <span style="color: #666666">=</span> lambdas[i]
RegRidge <span style="color: #666666">=</span> linear_model<span style="color: #666666">.</span>Ridge(lmb)
RegRidge<span style="color: #666666">.</span>fit(X_train,y_train)
ypredictRidge <span style="color: #666666">=</span> RegRidge<span style="color: #666666">.</span>predict(X_test)
MSERidgePredict[i] <span style="color: #666666">=</span> MSE(y_test,ypredictRidge)
<span style="color: #408080; font-style: italic"># Now plot the results</span>
plt<span style="color: #666666">.</span>figure()
plt<span style="color: #666666">.</span>plot(np<span style="color: #666666">.</span>log10(lambdas), MSERidgePredict, <span style="color: #BA2121">&#39;g--&#39;</span>, label <span style="color: #666666">=</span> <span style="color: #BA2121">&#39;MSE SL Ridge Test&#39;</span>)
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">&#39;log10(lambda)&#39;</span>)
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">&#39;MSE&#39;</span>)
plt<span style="color: #666666">.</span>legend()
plt<span style="color: #666666">.</span>show()
</pre>
</div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_subarea output_stream output_stdout output_text">
</div>
</div>
</div>
</div>
</div>
<p>Here we have performed a rather data greedy calculation as function of the regularization parameter \( \lambda \). There is no resampling here. The latter can easily be added by employing the function <b>RidgeCV</b> instead of just calling the <b>Ridge</b> function. For <b>RidgeCV</b> we need to pass the array of \( \lambda \) values.
By inspecting the figure we can in turn determine which is the optimal regularization parameter.
This becomes however less functional in the long run.
</p>
<p>
@@ -255,7 +308,7 @@ them with a factor.
<li><a href="._week45-bs011.html">12</a></li>
<li><a href="._week45-bs012.html">13</a></li>
<li><a href="">...</a></li>
<li><a href="._week45-bs039.html">40</a></li>
<li><a href="._week45-bs020.html">21</a></li>
<li><a href="._week45-bs004.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+183 -165
View File
@@ -8,8 +8,8 @@ doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=d
<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 45: Decisions Trees, Random Forests, Bagging and Boosting">
<title>Week 45: Decisions Trees, Random Forests, Bagging and Boosting</title>
<meta name="description" content="Week 45, Recurrent Neural Networks">
<title>Week 45, Recurrent Neural Networks</title>
<!-- Bootstrap style: bootstrap -->
<!-- doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=default --html_admon=bootstrap_panel --html_output=week45-bs --no_mako -->
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
@@ -36,109 +36,101 @@ doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=d
<!-- tocinfo
{'highest level': 2,
'sections': [('Overview of week 45', 2, None, 'overview-of-week-45'),
('Brief code reminder from last wekk',
'sections': [('Plan for week 45', 2, None, 'plan-for-week-45'),
('Material for the lab sessions, additional ways to present '
'classification results and other practicalities',
2,
None,
'brief-code-reminder-from-last-wekk'),
("Boosting, a Bird's Eye View",
'material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities'),
('Searching for Optimal Regularization Parameters $\\lambda$',
2,
None,
'boosting-a-bird-s-eye-view'),
('What is boosting? Additive Modelling/Iterative Fitting',
'searching-for-optimal-regularization-parameters-lambda'),
('Grid Search', 2, None, 'grid-search'),
('Randomized Grid Search', 2, None, 'randomized-grid-search'),
('Wisconsin Cancer Data', 2, None, 'wisconsin-cancer-data'),
('Using the correlation matrix',
2,
None,
'what-is-boosting-additive-modelling-iterative-fitting'),
('Iterative Fitting, Regression and Squared-error Cost Function',
'using-the-correlation-matrix'),
('Discussing the correlation data',
2,
None,
'iterative-fitting-regression-and-squared-error-cost-function'),
('Squared-Error Example and Iterative Fitting',
'discussing-the-correlation-data'),
('Other measures in classification studies: Cancer Data again',
2,
None,
'squared-error-example-and-iterative-fitting'),
('Iterative Fitting, Classification and AdaBoost',
'other-measures-in-classification-studies-cancer-data-again'),
('Material for Lecture Thursday November 9',
2,
None,
'iterative-fitting-classification-and-adaboost'),
('Adaptive Boosting, AdaBoost',
'material-for-lecture-thursday-november-9'),
('Recurrent neural networks (RNNs): Overarching view',
2,
None,
'adaptive-boosting-adaboost'),
('Building up AdaBoost', 2, None, 'building-up-adaboost'),
('Adaptive boosting: AdaBoost, Basic Algorithm',
'recurrent-neural-networks-rnns-overarching-view'),
('A simple example', 2, None, 'a-simple-example'),
('Memoryless models', 3, None, 'memoryless-models'),
('Linear dynamical model', 3, None, 'linear-dynamical-model'),
('Hidden Markov Models', 3, None, 'hidden-markov-models'),
('RNNs', 3, None, 'rnns'),
('Do generative models need to be stochastic?',
3,
None,
'do-generative-models-need-to-be-stochastic'),
('What kinds of behaviour can RNNs exhibit?',
3,
None,
'what-kinds-of-behaviour-can-rnns-exhibit'),
('Basic layout', 2, None, 'basic-layout'),
('We need to specify the initial activity state of all the '
'hidden and output units',
3,
None,
'we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units'),
('We can specify inputs in several ways',
3,
None,
'we-can-specify-inputs-in-several-ways'),
('We can specify targets in several ways',
3,
None,
'we-can-specify-targets-in-several-ways'),
('Backpropagation through time',
3,
None,
'backpropagation-through-time'),
('The backward pass is linear',
3,
None,
'the-backward-pass-is-linear'),
('The problem of exploding or vanishing gradients',
2,
None,
'adaptive-boosting-adaboost-basic-algorithm'),
('Basic Steps of AdaBoost', 2, None, 'basic-steps-of-adaboost'),
('AdaBoost Examples', 2, None, 'adaboost-examples'),
('Gradient boosting: Basics with Steepest Descent/Functional '
'Gradient Descent',
'the-problem-of-exploding-or-vanishing-gradients'),
('Four effective ways to learn an RNN',
2,
None,
'gradient-boosting-basics-with-steepest-descent-functional-gradient-descent'),
('The Squared-Error again! Steepest Descent',
'four-effective-ways-to-learn-an-rnn'),
('Long Short Term Memory (LSTM)',
3,
None,
'long-short-term-memory-lstm'),
('Implementing a memory cell in a neural network',
3,
None,
'implementing-a-memory-cell-in-a-neural-network'),
('An extrapolation example', 2, None, 'an-extrapolation-example'),
('Formatting the Data', 2, None, 'formatting-the-data'),
('Predicting New Points With A Trained Recurrent Neural Network',
2,
None,
'the-squared-error-again-steepest-descent'),
('Steepest Descent Example', 2, None, 'steepest-descent-example'),
('Gradient Boosting, algorithm',
'predicting-new-points-with-a-trained-recurrent-neural-network'),
('Other Things to Try', 2, None, 'other-things-to-try'),
('Other Types of Recurrent Neural Networks',
2,
None,
'gradient-boosting-algorithm'),
('Gradient Boosting, Examples of Regression',
2,
None,
'gradient-boosting-examples-of-regression'),
('Gradient Boosting, Classification Example',
2,
None,
'gradient-boosting-classification-example'),
('XGBoost: Extreme Gradient Boosting',
2,
None,
'xgboost-extreme-gradient-boosting'),
('Regression Case', 2, None, 'regression-case'),
('Xgboost on the Cancer Data',
2,
None,
'xgboost-on-the-cancer-data'),
('Support Vector Machines, overarching aims',
2,
None,
'support-vector-machines-overarching-aims'),
('Hyperplanes and all that', 2, None, 'hyperplanes-and-all-that'),
('What is a hyperplane?', 2, None, 'what-is-a-hyperplane'),
('A $p$-dimensional space of features',
2,
None,
'a-p-dimensional-space-of-features'),
('The two-dimensional case', 2, None, 'the-two-dimensional-case'),
('Getting into the details', 2, None, 'getting-into-the-details'),
('First attempt at a minimization approach',
2,
None,
'first-attempt-at-a-minimization-approach'),
('Solving the equations', 2, None, 'solving-the-equations'),
('Code Example', 2, None, 'code-example'),
('Problems with the Simpler Approach',
2,
None,
'problems-with-the-simpler-approach'),
('A better approach', 2, None, 'a-better-approach'),
('A quick Reminder on Lagrangian Multipliers',
2,
None,
'a-quick-reminder-on-lagrangian-multipliers'),
('Adding the Multiplier', 2, None, 'adding-the-multiplier'),
('Setting up the Problem', 2, None, 'setting-up-the-problem'),
('The problem to solve', 2, None, 'the-problem-to-solve'),
('The last steps', 2, None, 'the-last-steps'),
('A soft classifier', 2, None, 'a-soft-classifier'),
('Soft optmization problem',
2,
None,
'soft-optmization-problem')]}
'other-types-of-recurrent-neural-networks')]}
end of tocinfo -->
<body>
@@ -166,52 +158,46 @@ MathJax.Hub.Config({
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="week45-bs.html">Week 45: Decisions Trees, Random Forests, Bagging and Boosting</a>
<a class="navbar-brand" href="week45-bs.html">Week 45, Recurrent Neural Networks</a>
</div>
<div class="navbar-collapse collapse navbar-responsive-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
<ul class="dropdown-menu">
<!-- navigation toc: --> <li><a href="._week45-bs001.html#overview-of-week-45" style="font-size: 80%;">Overview of week 45</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs002.html#brief-code-reminder-from-last-wekk" style="font-size: 80%;">Brief code reminder from last wekk</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs003.html#boosting-a-bird-s-eye-view" style="font-size: 80%;">Boosting, a Bird's Eye View</a></li>
<!-- navigation toc: --> <li><a href="#what-is-boosting-additive-modelling-iterative-fitting" style="font-size: 80%;">What is boosting? Additive Modelling/Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs005.html#iterative-fitting-regression-and-squared-error-cost-function" style="font-size: 80%;">Iterative Fitting, Regression and Squared-error Cost Function</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs006.html#squared-error-example-and-iterative-fitting" style="font-size: 80%;">Squared-Error Example and Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs007.html#iterative-fitting-classification-and-adaboost" style="font-size: 80%;">Iterative Fitting, Classification and AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs008.html#adaptive-boosting-adaboost" style="font-size: 80%;">Adaptive Boosting, AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs009.html#building-up-adaboost" style="font-size: 80%;">Building up AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs010.html#adaptive-boosting-adaboost-basic-algorithm" style="font-size: 80%;">Adaptive boosting: AdaBoost, Basic Algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs011.html#basic-steps-of-adaboost" style="font-size: 80%;">Basic Steps of AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#adaboost-examples" style="font-size: 80%;">AdaBoost Examples</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#gradient-boosting-basics-with-steepest-descent-functional-gradient-descent" style="font-size: 80%;">Gradient boosting: Basics with Steepest Descent/Functional Gradient Descent</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs014.html#the-squared-error-again-steepest-descent" style="font-size: 80%;">The Squared-Error again! Steepest Descent</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#steepest-descent-example" style="font-size: 80%;">Steepest Descent Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs016.html#gradient-boosting-algorithm" style="font-size: 80%;">Gradient Boosting, algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs017.html#gradient-boosting-examples-of-regression" style="font-size: 80%;">Gradient Boosting, Examples of Regression</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs018.html#gradient-boosting-classification-example" style="font-size: 80%;">Gradient Boosting, Classification Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs019.html#xgboost-extreme-gradient-boosting" style="font-size: 80%;">XGBoost: Extreme Gradient Boosting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs020.html#regression-case" style="font-size: 80%;">Regression Case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs021.html#xgboost-on-the-cancer-data" style="font-size: 80%;">Xgboost on the Cancer Data</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs022.html#support-vector-machines-overarching-aims" style="font-size: 80%;">Support Vector Machines, overarching aims</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs023.html#hyperplanes-and-all-that" style="font-size: 80%;">Hyperplanes and all that</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs024.html#what-is-a-hyperplane" style="font-size: 80%;">What is a hyperplane?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs025.html#a-p-dimensional-space-of-features" style="font-size: 80%;">A \( p \)-dimensional space of features</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs026.html#the-two-dimensional-case" style="font-size: 80%;">The two-dimensional case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs027.html#getting-into-the-details" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs028.html#first-attempt-at-a-minimization-approach" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs029.html#solving-the-equations" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs030.html#code-example" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs031.html#problems-with-the-simpler-approach" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs032.html#a-better-approach" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs033.html#a-quick-reminder-on-lagrangian-multipliers" style="font-size: 80%;">A quick Reminder on Lagrangian Multipliers</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs034.html#adding-the-multiplier" style="font-size: 80%;">Adding the Multiplier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs035.html#setting-up-the-problem" style="font-size: 80%;">Setting up the Problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs036.html#the-problem-to-solve" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs037.html#the-last-steps" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs038.html#a-soft-classifier" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs039.html#soft-optmization-problem" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs001.html#plan-for-week-45" style="font-size: 80%;"><b>Plan for week 45</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs002.html#material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities" style="font-size: 80%;"><b>Material for the lab sessions, additional ways to present classification results and other practicalities</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs003.html#searching-for-optimal-regularization-parameters-lambda" style="font-size: 80%;"><b>Searching for Optimal Regularization Parameters \( \lambda \)</b></a></li>
<!-- navigation toc: --> <li><a href="#grid-search" style="font-size: 80%;"><b>Grid Search</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs005.html#randomized-grid-search" style="font-size: 80%;"><b>Randomized Grid Search</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs006.html#wisconsin-cancer-data" style="font-size: 80%;"><b>Wisconsin Cancer Data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs007.html#using-the-correlation-matrix" style="font-size: 80%;"><b>Using the correlation matrix</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs008.html#discussing-the-correlation-data" style="font-size: 80%;"><b>Discussing the correlation data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs009.html#other-measures-in-classification-studies-cancer-data-again" style="font-size: 80%;"><b>Other measures in classification studies: Cancer Data again</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs010.html#material-for-lecture-thursday-november-9" style="font-size: 80%;"><b>Material for Lecture Thursday November 9</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs011.html#recurrent-neural-networks-rnns-overarching-view" style="font-size: 80%;"><b>Recurrent neural networks (RNNs): Overarching view</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#a-simple-example" style="font-size: 80%;"><b>A simple example</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#memoryless-models" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Memoryless models</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#linear-dynamical-model" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Linear dynamical model</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#hidden-markov-models" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Hidden Markov Models</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#rnns" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;RNNs</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#do-generative-models-need-to-be-stochastic" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Do generative models need to be stochastic?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#what-kinds-of-behaviour-can-rnns-exhibit" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;What kinds of behaviour can RNNs exhibit?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#basic-layout" style="font-size: 80%;"><b>Basic layout</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We need to specify the initial activity state of all the hidden and output units</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-can-specify-inputs-in-several-ways" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We can specify inputs in several ways</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-can-specify-targets-in-several-ways" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We can specify targets in several ways</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#backpropagation-through-time" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Backpropagation through time</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#the-backward-pass-is-linear" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;The backward pass is linear</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs014.html#the-problem-of-exploding-or-vanishing-gradients" style="font-size: 80%;"><b>The problem of exploding or vanishing gradients</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#four-effective-ways-to-learn-an-rnn" style="font-size: 80%;"><b>Four effective ways to learn an RNN</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#long-short-term-memory-lstm" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Long Short Term Memory (LSTM)</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#implementing-a-memory-cell-in-a-neural-network" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Implementing a memory cell in a neural network</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs016.html#an-extrapolation-example" style="font-size: 80%;"><b>An extrapolation example</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs017.html#formatting-the-data" style="font-size: 80%;"><b>Formatting the Data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs018.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;"><b>Predicting New Points With A Trained Recurrent Neural Network</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs019.html#other-things-to-try" style="font-size: 80%;"><b>Other Things to Try</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs020.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;"><b>Other Types of Recurrent Neural Networks</b></a></li>
</ul>
</li>
@@ -223,52 +209,84 @@ MathJax.Hub.Config({
<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p> <!-- add vertical space -->
<a name="part0004"></a>
<!-- !split -->
<h2 id="what-is-boosting-additive-modelling-iterative-fitting" class="anchor">What is boosting? Additive Modelling/Iterative Fitting </h2>
<h2 id="grid-search" class="anchor">Grid Search </h2>
<p>Boosting is a way of fitting an additive expansion in a set of
elementary basis functions like for example some simple polynomials.
Assume for example that we have a function
</p>
$$
f_M(x) = \sum_{i=1}^M \beta_m b(x;\gamma_m),
$$
<p>where \( \beta_m \) are the expansion parameters to be determined in a
minimization process and \( b(x;\gamma_m) \) are some simple functions of
the multivariable parameter \( x \) which is characterized by the
parameters \( \gamma_m \).
<p>An alternative is to use the so-called grid search functionality
included with the library <b>Scikit-Learn</b>, as demonstrated for the same
example here.
</p>
<p>As an example, consider the Sigmoid function we used in logistic
regression. In that case, we can translate the function
\( b(x;\gamma_m) \) into the Sigmoid function
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight" style="background: #f8f8f8">
<pre style="line-height: 125%;"><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">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> train_test_split
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.linear_model</span> <span style="color: #008000; font-weight: bold">import</span> Ridge
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> GridSearchCV
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">R2</span>(y_data, y_model):
<span style="color: #008000; font-weight: bold">return</span> <span style="color: #666666">1</span> <span style="color: #666666">-</span> np<span style="color: #666666">.</span>sum((y_data <span style="color: #666666">-</span> y_model) <span style="color: #666666">**</span> <span style="color: #666666">2</span>) <span style="color: #666666">/</span> np<span style="color: #666666">.</span>sum((y_data <span style="color: #666666">-</span> np<span style="color: #666666">.</span>mean(y_data)) <span style="color: #666666">**</span> <span style="color: #666666">2</span>)
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">MSE</span>(y_data,y_model):
n <span style="color: #666666">=</span> np<span style="color: #666666">.</span>size(y_model)
<span style="color: #008000; font-weight: bold">return</span> np<span style="color: #666666">.</span>sum((y_data<span style="color: #666666">-</span>y_model)<span style="color: #666666">**2</span>)<span style="color: #666666">/</span>n
<span style="color: #408080; font-style: italic"># A seed just to ensure that the random numbers are the same for every run.</span>
<span style="color: #408080; font-style: italic"># Useful for eventual debugging.</span>
np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>seed(<span style="color: #666666">2021</span>)
n <span style="color: #666666">=</span> <span style="color: #666666">100</span>
x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>rand(n)
y <span style="color: #666666">=</span> np<span style="color: #666666">.</span>exp(<span style="color: #666666">-</span>x<span style="color: #666666">**2</span>) <span style="color: #666666">+</span> <span style="color: #666666">1.5</span> <span style="color: #666666">*</span> np<span style="color: #666666">.</span>exp(<span style="color: #666666">-</span>(x<span style="color: #666666">-2</span>)<span style="color: #666666">**2</span>)<span style="color: #666666">+</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>randn(n)
Maxpolydegree <span style="color: #666666">=</span> <span style="color: #666666">5</span>
X <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros((n,Maxpolydegree<span style="color: #666666">-1</span>))
<span style="color: #008000; font-weight: bold">for</span> degree <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #666666">1</span>,Maxpolydegree): <span style="color: #408080; font-style: italic">#No intercept column</span>
X[:,degree<span style="color: #666666">-1</span>] <span style="color: #666666">=</span> x<span style="color: #666666">**</span>(degree)
<span style="color: #408080; font-style: italic"># We split the data in test and training data</span>
X_train, X_test, y_train, y_test <span style="color: #666666">=</span> train_test_split(X, y, test_size<span style="color: #666666">=0.2</span>)
<span style="color: #408080; font-style: italic"># Decide which values of lambda to use</span>
nlambdas <span style="color: #666666">=</span> <span style="color: #666666">10</span>
lambdas <span style="color: #666666">=</span> np<span style="color: #666666">.</span>logspace(<span style="color: #666666">-4</span>, <span style="color: #666666">2</span>, nlambdas)
<span style="color: #408080; font-style: italic"># create and fit a ridge regression model, testing each alpha</span>
model <span style="color: #666666">=</span> Ridge()
gridsearch <span style="color: #666666">=</span> GridSearchCV(estimator<span style="color: #666666">=</span>model, param_grid<span style="color: #666666">=</span><span style="color: #008000">dict</span>(alpha<span style="color: #666666">=</span>lambdas))
gridsearch<span style="color: #666666">.</span>fit(X_train, y_train)
<span style="color: #008000">print</span>(gridsearch)
ypredictRidge <span style="color: #666666">=</span> gridsearch<span style="color: #666666">.</span>predict(X_test)
<span style="color: #408080; font-style: italic"># summarize the results of the grid search</span>
<span style="color: #008000">print</span>(<span style="color: #BA2121">f&quot;Best estimated lambda-value: </span><span style="color: #BB6688; font-weight: bold">{</span>gridsearch<span style="color: #666666">.</span>best_estimator_<span style="color: #666666">.</span>alpha<span style="color: #BB6688; font-weight: bold">}</span><span style="color: #BA2121">&quot;</span>)
<span style="color: #008000">print</span>(<span style="color: #BA2121">f&quot;MSE score: </span><span style="color: #BB6688; font-weight: bold">{</span>MSE(y_test,ypredictRidge)<span style="color: #BB6688; font-weight: bold">}</span><span style="color: #BA2121">&quot;</span>)
<span style="color: #008000">print</span>(<span style="color: #BA2121">f&quot;R2 score: </span><span style="color: #BB6688; font-weight: bold">{</span>R2(y_test,ypredictRidge)<span style="color: #BB6688; font-weight: bold">}</span><span style="color: #BA2121">&quot;</span>)
</pre>
</div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_subarea output_stream output_stdout output_text">
</div>
</div>
</div>
</div>
</div>
<p>By default the grid search function includes cross validation with
five folds. The <a href="https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.GridSearchCV.html#sklearn.model_selection.GridSearchCV" target="_self">Scikit-Learn
documentation</a>
contains more information on how to set the different parameters.
</p>
$$
\sigma(t) = \frac{1}{1+\exp{(-t)}},
$$
<p>where \( t=\gamma_0+\gamma_1 x \) and the parameters \( \gamma_0 \) and
\( \gamma_1 \) were determined by the Logistic Regression fitting
algorithm.
</p>
<p>As another example, consider the cost function we defined for linear regression</p>
$$
C(\boldsymbol{y},\boldsymbol{f}) = \frac{1}{n} \sum_{i=0}^{n-1}(y_i-f(x_i))^2.
$$
<p>In this case the function \( f(x) \) was replaced by the design matrix
\( \boldsymbol{X} \) and the unknown linear regression parameters \( \boldsymbol{\beta} \),
that is \( \boldsymbol{f}=\boldsymbol{X}\boldsymbol{\beta} \). In linear regression we can
simply invert a matrix and obtain the parameters \( \beta \) by
</p>
$$
\boldsymbol{\beta}=\left(\boldsymbol{X}^T\boldsymbol{X}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y}.
$$
<p>In iterative fitting or additive modeling, we minimize the cost function with respect to the parameters \( \beta_m \) and \( \gamma_m \).</p>
<p>If we take out the random noise, running the above codes results in \( \lambda=0 \) yielding the best fit. </p>
<p>
<!-- navigation buttons at the bottom of the page -->
@@ -289,7 +307,7 @@ $$
<li><a href="._week45-bs012.html">13</a></li>
<li><a href="._week45-bs013.html">14</a></li>
<li><a href="">...</a></li>
<li><a href="._week45-bs039.html">40</a></li>
<li><a href="._week45-bs020.html">21</a></li>
<li><a href="._week45-bs005.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+185 -140
View File
@@ -8,8 +8,8 @@ doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=d
<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 45: Decisions Trees, Random Forests, Bagging and Boosting">
<title>Week 45: Decisions Trees, Random Forests, Bagging and Boosting</title>
<meta name="description" content="Week 45, Recurrent Neural Networks">
<title>Week 45, Recurrent Neural Networks</title>
<!-- Bootstrap style: bootstrap -->
<!-- doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=default --html_admon=bootstrap_panel --html_output=week45-bs --no_mako -->
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
@@ -36,109 +36,101 @@ doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=d
<!-- tocinfo
{'highest level': 2,
'sections': [('Overview of week 45', 2, None, 'overview-of-week-45'),
('Brief code reminder from last wekk',
'sections': [('Plan for week 45', 2, None, 'plan-for-week-45'),
('Material for the lab sessions, additional ways to present '
'classification results and other practicalities',
2,
None,
'brief-code-reminder-from-last-wekk'),
("Boosting, a Bird's Eye View",
'material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities'),
('Searching for Optimal Regularization Parameters $\\lambda$',
2,
None,
'boosting-a-bird-s-eye-view'),
('What is boosting? Additive Modelling/Iterative Fitting',
'searching-for-optimal-regularization-parameters-lambda'),
('Grid Search', 2, None, 'grid-search'),
('Randomized Grid Search', 2, None, 'randomized-grid-search'),
('Wisconsin Cancer Data', 2, None, 'wisconsin-cancer-data'),
('Using the correlation matrix',
2,
None,
'what-is-boosting-additive-modelling-iterative-fitting'),
('Iterative Fitting, Regression and Squared-error Cost Function',
'using-the-correlation-matrix'),
('Discussing the correlation data',
2,
None,
'iterative-fitting-regression-and-squared-error-cost-function'),
('Squared-Error Example and Iterative Fitting',
'discussing-the-correlation-data'),
('Other measures in classification studies: Cancer Data again',
2,
None,
'squared-error-example-and-iterative-fitting'),
('Iterative Fitting, Classification and AdaBoost',
'other-measures-in-classification-studies-cancer-data-again'),
('Material for Lecture Thursday November 9',
2,
None,
'iterative-fitting-classification-and-adaboost'),
('Adaptive Boosting, AdaBoost',
'material-for-lecture-thursday-november-9'),
('Recurrent neural networks (RNNs): Overarching view',
2,
None,
'adaptive-boosting-adaboost'),
('Building up AdaBoost', 2, None, 'building-up-adaboost'),
('Adaptive boosting: AdaBoost, Basic Algorithm',
'recurrent-neural-networks-rnns-overarching-view'),
('A simple example', 2, None, 'a-simple-example'),
('Memoryless models', 3, None, 'memoryless-models'),
('Linear dynamical model', 3, None, 'linear-dynamical-model'),
('Hidden Markov Models', 3, None, 'hidden-markov-models'),
('RNNs', 3, None, 'rnns'),
('Do generative models need to be stochastic?',
3,
None,
'do-generative-models-need-to-be-stochastic'),
('What kinds of behaviour can RNNs exhibit?',
3,
None,
'what-kinds-of-behaviour-can-rnns-exhibit'),
('Basic layout', 2, None, 'basic-layout'),
('We need to specify the initial activity state of all the '
'hidden and output units',
3,
None,
'we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units'),
('We can specify inputs in several ways',
3,
None,
'we-can-specify-inputs-in-several-ways'),
('We can specify targets in several ways',
3,
None,
'we-can-specify-targets-in-several-ways'),
('Backpropagation through time',
3,
None,
'backpropagation-through-time'),
('The backward pass is linear',
3,
None,
'the-backward-pass-is-linear'),
('The problem of exploding or vanishing gradients',
2,
None,
'adaptive-boosting-adaboost-basic-algorithm'),
('Basic Steps of AdaBoost', 2, None, 'basic-steps-of-adaboost'),
('AdaBoost Examples', 2, None, 'adaboost-examples'),
('Gradient boosting: Basics with Steepest Descent/Functional '
'Gradient Descent',
'the-problem-of-exploding-or-vanishing-gradients'),
('Four effective ways to learn an RNN',
2,
None,
'gradient-boosting-basics-with-steepest-descent-functional-gradient-descent'),
('The Squared-Error again! Steepest Descent',
'four-effective-ways-to-learn-an-rnn'),
('Long Short Term Memory (LSTM)',
3,
None,
'long-short-term-memory-lstm'),
('Implementing a memory cell in a neural network',
3,
None,
'implementing-a-memory-cell-in-a-neural-network'),
('An extrapolation example', 2, None, 'an-extrapolation-example'),
('Formatting the Data', 2, None, 'formatting-the-data'),
('Predicting New Points With A Trained Recurrent Neural Network',
2,
None,
'the-squared-error-again-steepest-descent'),
('Steepest Descent Example', 2, None, 'steepest-descent-example'),
('Gradient Boosting, algorithm',
'predicting-new-points-with-a-trained-recurrent-neural-network'),
('Other Things to Try', 2, None, 'other-things-to-try'),
('Other Types of Recurrent Neural Networks',
2,
None,
'gradient-boosting-algorithm'),
('Gradient Boosting, Examples of Regression',
2,
None,
'gradient-boosting-examples-of-regression'),
('Gradient Boosting, Classification Example',
2,
None,
'gradient-boosting-classification-example'),
('XGBoost: Extreme Gradient Boosting',
2,
None,
'xgboost-extreme-gradient-boosting'),
('Regression Case', 2, None, 'regression-case'),
('Xgboost on the Cancer Data',
2,
None,
'xgboost-on-the-cancer-data'),
('Support Vector Machines, overarching aims',
2,
None,
'support-vector-machines-overarching-aims'),
('Hyperplanes and all that', 2, None, 'hyperplanes-and-all-that'),
('What is a hyperplane?', 2, None, 'what-is-a-hyperplane'),
('A $p$-dimensional space of features',
2,
None,
'a-p-dimensional-space-of-features'),
('The two-dimensional case', 2, None, 'the-two-dimensional-case'),
('Getting into the details', 2, None, 'getting-into-the-details'),
('First attempt at a minimization approach',
2,
None,
'first-attempt-at-a-minimization-approach'),
('Solving the equations', 2, None, 'solving-the-equations'),
('Code Example', 2, None, 'code-example'),
('Problems with the Simpler Approach',
2,
None,
'problems-with-the-simpler-approach'),
('A better approach', 2, None, 'a-better-approach'),
('A quick Reminder on Lagrangian Multipliers',
2,
None,
'a-quick-reminder-on-lagrangian-multipliers'),
('Adding the Multiplier', 2, None, 'adding-the-multiplier'),
('Setting up the Problem', 2, None, 'setting-up-the-problem'),
('The problem to solve', 2, None, 'the-problem-to-solve'),
('The last steps', 2, None, 'the-last-steps'),
('A soft classifier', 2, None, 'a-soft-classifier'),
('Soft optmization problem',
2,
None,
'soft-optmization-problem')]}
'other-types-of-recurrent-neural-networks')]}
end of tocinfo -->
<body>
@@ -166,52 +158,46 @@ MathJax.Hub.Config({
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="week45-bs.html">Week 45: Decisions Trees, Random Forests, Bagging and Boosting</a>
<a class="navbar-brand" href="week45-bs.html">Week 45, Recurrent Neural Networks</a>
</div>
<div class="navbar-collapse collapse navbar-responsive-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
<ul class="dropdown-menu">
<!-- navigation toc: --> <li><a href="._week45-bs001.html#overview-of-week-45" style="font-size: 80%;">Overview of week 45</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs002.html#brief-code-reminder-from-last-wekk" style="font-size: 80%;">Brief code reminder from last wekk</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs003.html#boosting-a-bird-s-eye-view" style="font-size: 80%;">Boosting, a Bird's Eye View</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs004.html#what-is-boosting-additive-modelling-iterative-fitting" style="font-size: 80%;">What is boosting? Additive Modelling/Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="#iterative-fitting-regression-and-squared-error-cost-function" style="font-size: 80%;">Iterative Fitting, Regression and Squared-error Cost Function</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs006.html#squared-error-example-and-iterative-fitting" style="font-size: 80%;">Squared-Error Example and Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs007.html#iterative-fitting-classification-and-adaboost" style="font-size: 80%;">Iterative Fitting, Classification and AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs008.html#adaptive-boosting-adaboost" style="font-size: 80%;">Adaptive Boosting, AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs009.html#building-up-adaboost" style="font-size: 80%;">Building up AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs010.html#adaptive-boosting-adaboost-basic-algorithm" style="font-size: 80%;">Adaptive boosting: AdaBoost, Basic Algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs011.html#basic-steps-of-adaboost" style="font-size: 80%;">Basic Steps of AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#adaboost-examples" style="font-size: 80%;">AdaBoost Examples</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#gradient-boosting-basics-with-steepest-descent-functional-gradient-descent" style="font-size: 80%;">Gradient boosting: Basics with Steepest Descent/Functional Gradient Descent</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs014.html#the-squared-error-again-steepest-descent" style="font-size: 80%;">The Squared-Error again! Steepest Descent</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#steepest-descent-example" style="font-size: 80%;">Steepest Descent Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs016.html#gradient-boosting-algorithm" style="font-size: 80%;">Gradient Boosting, algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs017.html#gradient-boosting-examples-of-regression" style="font-size: 80%;">Gradient Boosting, Examples of Regression</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs018.html#gradient-boosting-classification-example" style="font-size: 80%;">Gradient Boosting, Classification Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs019.html#xgboost-extreme-gradient-boosting" style="font-size: 80%;">XGBoost: Extreme Gradient Boosting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs020.html#regression-case" style="font-size: 80%;">Regression Case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs021.html#xgboost-on-the-cancer-data" style="font-size: 80%;">Xgboost on the Cancer Data</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs022.html#support-vector-machines-overarching-aims" style="font-size: 80%;">Support Vector Machines, overarching aims</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs023.html#hyperplanes-and-all-that" style="font-size: 80%;">Hyperplanes and all that</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs024.html#what-is-a-hyperplane" style="font-size: 80%;">What is a hyperplane?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs025.html#a-p-dimensional-space-of-features" style="font-size: 80%;">A \( p \)-dimensional space of features</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs026.html#the-two-dimensional-case" style="font-size: 80%;">The two-dimensional case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs027.html#getting-into-the-details" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs028.html#first-attempt-at-a-minimization-approach" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs029.html#solving-the-equations" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs030.html#code-example" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs031.html#problems-with-the-simpler-approach" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs032.html#a-better-approach" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs033.html#a-quick-reminder-on-lagrangian-multipliers" style="font-size: 80%;">A quick Reminder on Lagrangian Multipliers</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs034.html#adding-the-multiplier" style="font-size: 80%;">Adding the Multiplier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs035.html#setting-up-the-problem" style="font-size: 80%;">Setting up the Problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs036.html#the-problem-to-solve" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs037.html#the-last-steps" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs038.html#a-soft-classifier" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs039.html#soft-optmization-problem" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs001.html#plan-for-week-45" style="font-size: 80%;"><b>Plan for week 45</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs002.html#material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities" style="font-size: 80%;"><b>Material for the lab sessions, additional ways to present classification results and other practicalities</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs003.html#searching-for-optimal-regularization-parameters-lambda" style="font-size: 80%;"><b>Searching for Optimal Regularization Parameters \( \lambda \)</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs004.html#grid-search" style="font-size: 80%;"><b>Grid Search</b></a></li>
<!-- navigation toc: --> <li><a href="#randomized-grid-search" style="font-size: 80%;"><b>Randomized Grid Search</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs006.html#wisconsin-cancer-data" style="font-size: 80%;"><b>Wisconsin Cancer Data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs007.html#using-the-correlation-matrix" style="font-size: 80%;"><b>Using the correlation matrix</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs008.html#discussing-the-correlation-data" style="font-size: 80%;"><b>Discussing the correlation data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs009.html#other-measures-in-classification-studies-cancer-data-again" style="font-size: 80%;"><b>Other measures in classification studies: Cancer Data again</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs010.html#material-for-lecture-thursday-november-9" style="font-size: 80%;"><b>Material for Lecture Thursday November 9</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs011.html#recurrent-neural-networks-rnns-overarching-view" style="font-size: 80%;"><b>Recurrent neural networks (RNNs): Overarching view</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#a-simple-example" style="font-size: 80%;"><b>A simple example</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#memoryless-models" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Memoryless models</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#linear-dynamical-model" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Linear dynamical model</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#hidden-markov-models" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Hidden Markov Models</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#rnns" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;RNNs</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#do-generative-models-need-to-be-stochastic" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Do generative models need to be stochastic?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#what-kinds-of-behaviour-can-rnns-exhibit" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;What kinds of behaviour can RNNs exhibit?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#basic-layout" style="font-size: 80%;"><b>Basic layout</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We need to specify the initial activity state of all the hidden and output units</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-can-specify-inputs-in-several-ways" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We can specify inputs in several ways</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-can-specify-targets-in-several-ways" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We can specify targets in several ways</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#backpropagation-through-time" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Backpropagation through time</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#the-backward-pass-is-linear" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;The backward pass is linear</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs014.html#the-problem-of-exploding-or-vanishing-gradients" style="font-size: 80%;"><b>The problem of exploding or vanishing gradients</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#four-effective-ways-to-learn-an-rnn" style="font-size: 80%;"><b>Four effective ways to learn an RNN</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#long-short-term-memory-lstm" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Long Short Term Memory (LSTM)</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#implementing-a-memory-cell-in-a-neural-network" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Implementing a memory cell in a neural network</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs016.html#an-extrapolation-example" style="font-size: 80%;"><b>An extrapolation example</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs017.html#formatting-the-data" style="font-size: 80%;"><b>Formatting the Data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs018.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;"><b>Predicting New Points With A Trained Recurrent Neural Network</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs019.html#other-things-to-try" style="font-size: 80%;"><b>Other Things to Try</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs020.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;"><b>Other Types of Recurrent Neural Networks</b></a></li>
</ul>
</li>
@@ -223,25 +209,84 @@ MathJax.Hub.Config({
<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p> <!-- add vertical space -->
<a name="part0005"></a>
<!-- !split -->
<h2 id="iterative-fitting-regression-and-squared-error-cost-function" class="anchor">Iterative Fitting, Regression and Squared-error Cost Function </h2>
<h2 id="randomized-grid-search" class="anchor">Randomized Grid Search </h2>
<p>The way we proceed is as follows (here we specialize to the squared-error cost function)</p>
<ol>
<li> Establish a cost function, here \( {\cal C}(\boldsymbol{y},\boldsymbol{f}) = \frac{1}{n} \sum_{i=0}^{n-1}(y_i-f_M(x_i))^2 \) with \( f_M(x) = \sum_{i=1}^M \beta_m b(x;\gamma_m) \).</li>
<li> Initialize with a guess \( f_0(x) \). It could be one or even zero or some random numbers.</li>
<li> For \( m=1:M \)
<ol type="a"></li>
<li> minimize \( \sum_{i=0}^{n-1}(y_i-f_{m-1}(x_i)-\beta b(x;\gamma))^2 \) wrt \( \gamma \) and \( \beta \)</li>
<li> This gives the optimal values \( \beta_m \) and \( \gamma_m \)</li>
<li> Determine then the new values \( f_m(x)=f_{m-1}(x) +\beta_m b(x;\gamma_m) \)</li>
</ol>
</ol>
<p>We could use any of the algorithms we have discussed till now. If we
use trees, \( \gamma \) parameterizes the split variables and split points
at the internal nodes, and the predictions at the terminal nodes.
<p>An alternative to the above manual grid set up, is to use a random
search where the parameters are tuned from a random distribution
(uniform below) for a fixed number of iterations. A model is
constructed and evaluated for each combination of chosen parameters.
We repeat the previous example but now with a random search. Note
that values of \( \lambda \) are now limited to be within \( x\in
[0,1] \). This domain may not be the most relevant one for the specific
case under study.
</p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight" style="background: #f8f8f8">
<pre style="line-height: 125%;"><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">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> train_test_split
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.linear_model</span> <span style="color: #008000; font-weight: bold">import</span> Ridge
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> GridSearchCV
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">scipy.stats</span> <span style="color: #008000; font-weight: bold">import</span> uniform <span style="color: #008000; font-weight: bold">as</span> randuniform
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> RandomizedSearchCV
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">R2</span>(y_data, y_model):
<span style="color: #008000; font-weight: bold">return</span> <span style="color: #666666">1</span> <span style="color: #666666">-</span> np<span style="color: #666666">.</span>sum((y_data <span style="color: #666666">-</span> y_model) <span style="color: #666666">**</span> <span style="color: #666666">2</span>) <span style="color: #666666">/</span> np<span style="color: #666666">.</span>sum((y_data <span style="color: #666666">-</span> np<span style="color: #666666">.</span>mean(y_data)) <span style="color: #666666">**</span> <span style="color: #666666">2</span>)
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">MSE</span>(y_data,y_model):
n <span style="color: #666666">=</span> np<span style="color: #666666">.</span>size(y_model)
<span style="color: #008000; font-weight: bold">return</span> np<span style="color: #666666">.</span>sum((y_data<span style="color: #666666">-</span>y_model)<span style="color: #666666">**2</span>)<span style="color: #666666">/</span>n
<span style="color: #408080; font-style: italic"># A seed just to ensure that the random numbers are the same for every run.</span>
<span style="color: #408080; font-style: italic"># Useful for eventual debugging.</span>
np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>seed(<span style="color: #666666">2021</span>)
n <span style="color: #666666">=</span> <span style="color: #666666">100</span>
x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>rand(n)
y <span style="color: #666666">=</span> np<span style="color: #666666">.</span>exp(<span style="color: #666666">-</span>x<span style="color: #666666">**2</span>) <span style="color: #666666">+</span> <span style="color: #666666">1.5</span> <span style="color: #666666">*</span> np<span style="color: #666666">.</span>exp(<span style="color: #666666">-</span>(x<span style="color: #666666">-2</span>)<span style="color: #666666">**2</span>)<span style="color: #666666">+</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>randn(n)
Maxpolydegree <span style="color: #666666">=</span> <span style="color: #666666">5</span>
X <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros((n,Maxpolydegree<span style="color: #666666">-1</span>))
<span style="color: #008000; font-weight: bold">for</span> degree <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #666666">1</span>,Maxpolydegree): <span style="color: #408080; font-style: italic">#No intercept column</span>
X[:,degree<span style="color: #666666">-1</span>] <span style="color: #666666">=</span> x<span style="color: #666666">**</span>(degree)
<span style="color: #408080; font-style: italic"># We split the data in test and training data</span>
X_train, X_test, y_train, y_test <span style="color: #666666">=</span> train_test_split(X, y, test_size<span style="color: #666666">=0.2</span>)
param_grid <span style="color: #666666">=</span> {<span style="color: #BA2121">&#39;alpha&#39;</span>: randuniform()}
<span style="color: #408080; font-style: italic"># create and fit a ridge regression model, testing each alpha</span>
model <span style="color: #666666">=</span> Ridge()
gridsearch <span style="color: #666666">=</span> RandomizedSearchCV(estimator<span style="color: #666666">=</span>model, param_distributions<span style="color: #666666">=</span>param_grid, n_iter<span style="color: #666666">=100</span>)
gridsearch<span style="color: #666666">.</span>fit(X_train, y_train)
<span style="color: #008000">print</span>(gridsearch)
ypredictRidge <span style="color: #666666">=</span> gridsearch<span style="color: #666666">.</span>predict(X_test)
<span style="color: #408080; font-style: italic"># summarize the results of the grid search</span>
<span style="color: #008000">print</span>(<span style="color: #BA2121">f&quot;Best estimated lambda-value: </span><span style="color: #BB6688; font-weight: bold">{</span>gridsearch<span style="color: #666666">.</span>best_estimator_<span style="color: #666666">.</span>alpha<span style="color: #BB6688; font-weight: bold">}</span><span style="color: #BA2121">&quot;</span>)
<span style="color: #008000">print</span>(<span style="color: #BA2121">f&quot;MSE score: </span><span style="color: #BB6688; font-weight: bold">{</span>MSE(y_test,ypredictRidge)<span style="color: #BB6688; font-weight: bold">}</span><span style="color: #BA2121">&quot;</span>)
<span style="color: #008000">print</span>(<span style="color: #BA2121">f&quot;R2 score: </span><span style="color: #BB6688; font-weight: bold">{</span>R2(y_test,ypredictRidge)<span style="color: #BB6688; font-weight: bold">}</span><span style="color: #BA2121">&quot;</span>)
</pre>
</div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_subarea output_stream output_stdout output_text">
</div>
</div>
</div>
</div>
</div>
<p>
<!-- navigation buttons at the bottom of the page -->
<ul class="pagination">
@@ -262,7 +307,7 @@ at the internal nodes, and the predictions at the terminal nodes.
<li><a href="._week45-bs013.html">14</a></li>
<li><a href="._week45-bs014.html">15</a></li>
<li><a href="">...</a></li>
<li><a href="._week45-bs039.html">40</a></li>
<li><a href="._week45-bs020.html">21</a></li>
<li><a href="._week45-bs006.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+152 -162
View File
@@ -8,8 +8,8 @@ doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=d
<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 45: Decisions Trees, Random Forests, Bagging and Boosting">
<title>Week 45: Decisions Trees, Random Forests, Bagging and Boosting</title>
<meta name="description" content="Week 45, Recurrent Neural Networks">
<title>Week 45, Recurrent Neural Networks</title>
<!-- Bootstrap style: bootstrap -->
<!-- doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=default --html_admon=bootstrap_panel --html_output=week45-bs --no_mako -->
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
@@ -36,109 +36,101 @@ doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=d
<!-- tocinfo
{'highest level': 2,
'sections': [('Overview of week 45', 2, None, 'overview-of-week-45'),
('Brief code reminder from last wekk',
'sections': [('Plan for week 45', 2, None, 'plan-for-week-45'),
('Material for the lab sessions, additional ways to present '
'classification results and other practicalities',
2,
None,
'brief-code-reminder-from-last-wekk'),
("Boosting, a Bird's Eye View",
'material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities'),
('Searching for Optimal Regularization Parameters $\\lambda$',
2,
None,
'boosting-a-bird-s-eye-view'),
('What is boosting? Additive Modelling/Iterative Fitting',
'searching-for-optimal-regularization-parameters-lambda'),
('Grid Search', 2, None, 'grid-search'),
('Randomized Grid Search', 2, None, 'randomized-grid-search'),
('Wisconsin Cancer Data', 2, None, 'wisconsin-cancer-data'),
('Using the correlation matrix',
2,
None,
'what-is-boosting-additive-modelling-iterative-fitting'),
('Iterative Fitting, Regression and Squared-error Cost Function',
'using-the-correlation-matrix'),
('Discussing the correlation data',
2,
None,
'iterative-fitting-regression-and-squared-error-cost-function'),
('Squared-Error Example and Iterative Fitting',
'discussing-the-correlation-data'),
('Other measures in classification studies: Cancer Data again',
2,
None,
'squared-error-example-and-iterative-fitting'),
('Iterative Fitting, Classification and AdaBoost',
'other-measures-in-classification-studies-cancer-data-again'),
('Material for Lecture Thursday November 9',
2,
None,
'iterative-fitting-classification-and-adaboost'),
('Adaptive Boosting, AdaBoost',
'material-for-lecture-thursday-november-9'),
('Recurrent neural networks (RNNs): Overarching view',
2,
None,
'adaptive-boosting-adaboost'),
('Building up AdaBoost', 2, None, 'building-up-adaboost'),
('Adaptive boosting: AdaBoost, Basic Algorithm',
'recurrent-neural-networks-rnns-overarching-view'),
('A simple example', 2, None, 'a-simple-example'),
('Memoryless models', 3, None, 'memoryless-models'),
('Linear dynamical model', 3, None, 'linear-dynamical-model'),
('Hidden Markov Models', 3, None, 'hidden-markov-models'),
('RNNs', 3, None, 'rnns'),
('Do generative models need to be stochastic?',
3,
None,
'do-generative-models-need-to-be-stochastic'),
('What kinds of behaviour can RNNs exhibit?',
3,
None,
'what-kinds-of-behaviour-can-rnns-exhibit'),
('Basic layout', 2, None, 'basic-layout'),
('We need to specify the initial activity state of all the '
'hidden and output units',
3,
None,
'we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units'),
('We can specify inputs in several ways',
3,
None,
'we-can-specify-inputs-in-several-ways'),
('We can specify targets in several ways',
3,
None,
'we-can-specify-targets-in-several-ways'),
('Backpropagation through time',
3,
None,
'backpropagation-through-time'),
('The backward pass is linear',
3,
None,
'the-backward-pass-is-linear'),
('The problem of exploding or vanishing gradients',
2,
None,
'adaptive-boosting-adaboost-basic-algorithm'),
('Basic Steps of AdaBoost', 2, None, 'basic-steps-of-adaboost'),
('AdaBoost Examples', 2, None, 'adaboost-examples'),
('Gradient boosting: Basics with Steepest Descent/Functional '
'Gradient Descent',
'the-problem-of-exploding-or-vanishing-gradients'),
('Four effective ways to learn an RNN',
2,
None,
'gradient-boosting-basics-with-steepest-descent-functional-gradient-descent'),
('The Squared-Error again! Steepest Descent',
'four-effective-ways-to-learn-an-rnn'),
('Long Short Term Memory (LSTM)',
3,
None,
'long-short-term-memory-lstm'),
('Implementing a memory cell in a neural network',
3,
None,
'implementing-a-memory-cell-in-a-neural-network'),
('An extrapolation example', 2, None, 'an-extrapolation-example'),
('Formatting the Data', 2, None, 'formatting-the-data'),
('Predicting New Points With A Trained Recurrent Neural Network',
2,
None,
'the-squared-error-again-steepest-descent'),
('Steepest Descent Example', 2, None, 'steepest-descent-example'),
('Gradient Boosting, algorithm',
'predicting-new-points-with-a-trained-recurrent-neural-network'),
('Other Things to Try', 2, None, 'other-things-to-try'),
('Other Types of Recurrent Neural Networks',
2,
None,
'gradient-boosting-algorithm'),
('Gradient Boosting, Examples of Regression',
2,
None,
'gradient-boosting-examples-of-regression'),
('Gradient Boosting, Classification Example',
2,
None,
'gradient-boosting-classification-example'),
('XGBoost: Extreme Gradient Boosting',
2,
None,
'xgboost-extreme-gradient-boosting'),
('Regression Case', 2, None, 'regression-case'),
('Xgboost on the Cancer Data',
2,
None,
'xgboost-on-the-cancer-data'),
('Support Vector Machines, overarching aims',
2,
None,
'support-vector-machines-overarching-aims'),
('Hyperplanes and all that', 2, None, 'hyperplanes-and-all-that'),
('What is a hyperplane?', 2, None, 'what-is-a-hyperplane'),
('A $p$-dimensional space of features',
2,
None,
'a-p-dimensional-space-of-features'),
('The two-dimensional case', 2, None, 'the-two-dimensional-case'),
('Getting into the details', 2, None, 'getting-into-the-details'),
('First attempt at a minimization approach',
2,
None,
'first-attempt-at-a-minimization-approach'),
('Solving the equations', 2, None, 'solving-the-equations'),
('Code Example', 2, None, 'code-example'),
('Problems with the Simpler Approach',
2,
None,
'problems-with-the-simpler-approach'),
('A better approach', 2, None, 'a-better-approach'),
('A quick Reminder on Lagrangian Multipliers',
2,
None,
'a-quick-reminder-on-lagrangian-multipliers'),
('Adding the Multiplier', 2, None, 'adding-the-multiplier'),
('Setting up the Problem', 2, None, 'setting-up-the-problem'),
('The problem to solve', 2, None, 'the-problem-to-solve'),
('The last steps', 2, None, 'the-last-steps'),
('A soft classifier', 2, None, 'a-soft-classifier'),
('Soft optmization problem',
2,
None,
'soft-optmization-problem')]}
'other-types-of-recurrent-neural-networks')]}
end of tocinfo -->
<body>
@@ -166,52 +158,46 @@ MathJax.Hub.Config({
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="week45-bs.html">Week 45: Decisions Trees, Random Forests, Bagging and Boosting</a>
<a class="navbar-brand" href="week45-bs.html">Week 45, Recurrent Neural Networks</a>
</div>
<div class="navbar-collapse collapse navbar-responsive-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
<ul class="dropdown-menu">
<!-- navigation toc: --> <li><a href="._week45-bs001.html#overview-of-week-45" style="font-size: 80%;">Overview of week 45</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs002.html#brief-code-reminder-from-last-wekk" style="font-size: 80%;">Brief code reminder from last wekk</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs003.html#boosting-a-bird-s-eye-view" style="font-size: 80%;">Boosting, a Bird's Eye View</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs004.html#what-is-boosting-additive-modelling-iterative-fitting" style="font-size: 80%;">What is boosting? Additive Modelling/Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs005.html#iterative-fitting-regression-and-squared-error-cost-function" style="font-size: 80%;">Iterative Fitting, Regression and Squared-error Cost Function</a></li>
<!-- navigation toc: --> <li><a href="#squared-error-example-and-iterative-fitting" style="font-size: 80%;">Squared-Error Example and Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs007.html#iterative-fitting-classification-and-adaboost" style="font-size: 80%;">Iterative Fitting, Classification and AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs008.html#adaptive-boosting-adaboost" style="font-size: 80%;">Adaptive Boosting, AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs009.html#building-up-adaboost" style="font-size: 80%;">Building up AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs010.html#adaptive-boosting-adaboost-basic-algorithm" style="font-size: 80%;">Adaptive boosting: AdaBoost, Basic Algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs011.html#basic-steps-of-adaboost" style="font-size: 80%;">Basic Steps of AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#adaboost-examples" style="font-size: 80%;">AdaBoost Examples</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#gradient-boosting-basics-with-steepest-descent-functional-gradient-descent" style="font-size: 80%;">Gradient boosting: Basics with Steepest Descent/Functional Gradient Descent</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs014.html#the-squared-error-again-steepest-descent" style="font-size: 80%;">The Squared-Error again! Steepest Descent</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#steepest-descent-example" style="font-size: 80%;">Steepest Descent Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs016.html#gradient-boosting-algorithm" style="font-size: 80%;">Gradient Boosting, algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs017.html#gradient-boosting-examples-of-regression" style="font-size: 80%;">Gradient Boosting, Examples of Regression</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs018.html#gradient-boosting-classification-example" style="font-size: 80%;">Gradient Boosting, Classification Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs019.html#xgboost-extreme-gradient-boosting" style="font-size: 80%;">XGBoost: Extreme Gradient Boosting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs020.html#regression-case" style="font-size: 80%;">Regression Case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs021.html#xgboost-on-the-cancer-data" style="font-size: 80%;">Xgboost on the Cancer Data</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs022.html#support-vector-machines-overarching-aims" style="font-size: 80%;">Support Vector Machines, overarching aims</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs023.html#hyperplanes-and-all-that" style="font-size: 80%;">Hyperplanes and all that</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs024.html#what-is-a-hyperplane" style="font-size: 80%;">What is a hyperplane?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs025.html#a-p-dimensional-space-of-features" style="font-size: 80%;">A \( p \)-dimensional space of features</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs026.html#the-two-dimensional-case" style="font-size: 80%;">The two-dimensional case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs027.html#getting-into-the-details" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs028.html#first-attempt-at-a-minimization-approach" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs029.html#solving-the-equations" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs030.html#code-example" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs031.html#problems-with-the-simpler-approach" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs032.html#a-better-approach" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs033.html#a-quick-reminder-on-lagrangian-multipliers" style="font-size: 80%;">A quick Reminder on Lagrangian Multipliers</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs034.html#adding-the-multiplier" style="font-size: 80%;">Adding the Multiplier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs035.html#setting-up-the-problem" style="font-size: 80%;">Setting up the Problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs036.html#the-problem-to-solve" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs037.html#the-last-steps" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs038.html#a-soft-classifier" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs039.html#soft-optmization-problem" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs001.html#plan-for-week-45" style="font-size: 80%;"><b>Plan for week 45</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs002.html#material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities" style="font-size: 80%;"><b>Material for the lab sessions, additional ways to present classification results and other practicalities</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs003.html#searching-for-optimal-regularization-parameters-lambda" style="font-size: 80%;"><b>Searching for Optimal Regularization Parameters \( \lambda \)</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs004.html#grid-search" style="font-size: 80%;"><b>Grid Search</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs005.html#randomized-grid-search" style="font-size: 80%;"><b>Randomized Grid Search</b></a></li>
<!-- navigation toc: --> <li><a href="#wisconsin-cancer-data" style="font-size: 80%;"><b>Wisconsin Cancer Data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs007.html#using-the-correlation-matrix" style="font-size: 80%;"><b>Using the correlation matrix</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs008.html#discussing-the-correlation-data" style="font-size: 80%;"><b>Discussing the correlation data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs009.html#other-measures-in-classification-studies-cancer-data-again" style="font-size: 80%;"><b>Other measures in classification studies: Cancer Data again</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs010.html#material-for-lecture-thursday-november-9" style="font-size: 80%;"><b>Material for Lecture Thursday November 9</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs011.html#recurrent-neural-networks-rnns-overarching-view" style="font-size: 80%;"><b>Recurrent neural networks (RNNs): Overarching view</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#a-simple-example" style="font-size: 80%;"><b>A simple example</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#memoryless-models" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Memoryless models</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#linear-dynamical-model" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Linear dynamical model</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#hidden-markov-models" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Hidden Markov Models</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#rnns" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;RNNs</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#do-generative-models-need-to-be-stochastic" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Do generative models need to be stochastic?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#what-kinds-of-behaviour-can-rnns-exhibit" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;What kinds of behaviour can RNNs exhibit?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#basic-layout" style="font-size: 80%;"><b>Basic layout</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We need to specify the initial activity state of all the hidden and output units</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-can-specify-inputs-in-several-ways" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We can specify inputs in several ways</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-can-specify-targets-in-several-ways" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We can specify targets in several ways</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#backpropagation-through-time" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Backpropagation through time</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#the-backward-pass-is-linear" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;The backward pass is linear</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs014.html#the-problem-of-exploding-or-vanishing-gradients" style="font-size: 80%;"><b>The problem of exploding or vanishing gradients</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#four-effective-ways-to-learn-an-rnn" style="font-size: 80%;"><b>Four effective ways to learn an RNN</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#long-short-term-memory-lstm" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Long Short Term Memory (LSTM)</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#implementing-a-memory-cell-in-a-neural-network" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Implementing a memory cell in a neural network</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs016.html#an-extrapolation-example" style="font-size: 80%;"><b>An extrapolation example</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs017.html#formatting-the-data" style="font-size: 80%;"><b>Formatting the Data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs018.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;"><b>Predicting New Points With A Trained Recurrent Neural Network</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs019.html#other-things-to-try" style="font-size: 80%;"><b>Other Things to Try</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs020.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;"><b>Other Types of Recurrent Neural Networks</b></a></li>
</ul>
</li>
@@ -223,47 +209,51 @@ MathJax.Hub.Config({
<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p> <!-- add vertical space -->
<a name="part0006"></a>
<!-- !split -->
<h2 id="squared-error-example-and-iterative-fitting" class="anchor">Squared-Error Example and Iterative Fitting </h2>
<h2 id="wisconsin-cancer-data" class="anchor">Wisconsin Cancer Data </h2>
<p>To better understand what happens, let us develop the steps for the iterative fitting using the above squared error function.</p>
<p>For simplicity we assume also that our functions \( b(x;\gamma)=1+\gamma x \). </p>
<p>This means that for every iteration \( m \), we need to optimize</p>
$$
(\beta_m,\gamma_m) = \mathrm{argmin}_{\beta,\lambda}\hspace{0.1cm} \sum_{i=0}^{n-1}(y_i-f_{m-1}(x_i)-\beta b(x;\gamma))^2=\sum_{i=0}^{n-1}(y_i-f_{m-1}(x_i)-\beta(1+\gamma x_i))^2.
$$
<p>We start our iteration by simply setting \( f_0(x)=0 \).
Taking the derivatives with respect to \( \beta \) and \( \gamma \) we obtain
</p>
$$
\frac{\partial {\cal C}}{\partial \beta} = -2\sum_{i}(1+\gamma x_i)(y_i-\beta(1+\gamma x_i))=0,
$$
<p>and</p>
$$
\frac{\partial {\cal C}}{\partial \gamma} =-2\sum_{i}\beta x_i(y_i-\beta(1+\gamma x_i))=0.
$$
<p>We can then rewrite these equations as (defining \( \boldsymbol{w}=\boldsymbol{e}+\gamma \boldsymbol{x}) \) with \( \boldsymbol{e} \) being the unit vector)</p>
$$
\gamma \boldsymbol{w}^T(\boldsymbol{y}-\beta\gamma \boldsymbol{w})=0,
$$
<p>which gives us \( \beta = \boldsymbol{w}^T\boldsymbol{y}/(\boldsymbol{w}^T\boldsymbol{w}) \). Similarly we have </p>
$$
\beta\gamma \boldsymbol{x}^T(\boldsymbol{y}-\beta(1+\gamma \boldsymbol{x}))=0,
$$
<p>which leads to \( \gamma =(\boldsymbol{x}^T\boldsymbol{y}-\beta\boldsymbol{x}^T\boldsymbol{e})/(\beta\boldsymbol{x}^T\boldsymbol{x}) \). Inserting
for \( \beta \) gives us an equation for \( \gamma \). This is a non-linear equation in the unknown \( \gamma \) and has to be solved numerically.
<p>We show here how we can use a simple regression case on the breast
cancer data using Logistic regression as our algorithm for
classification.
</p>
<p>The solution to these two equations gives us in turn \( \beta_1 \) and \( \gamma_1 \) leading to the new expression for \( f_1(x) \) as
\( f_1(x) = \beta_1(1+\gamma_1x) \). Doing this \( M \) times results in our final estimate for the function \( f \).
</p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight" style="background: #f8f8f8">
<pre style="line-height: 125%;"><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">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">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> train_test_split
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.datasets</span> <span style="color: #008000; font-weight: bold">import</span> load_breast_cancer
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.linear_model</span> <span style="color: #008000; font-weight: bold">import</span> LogisticRegression
<span style="color: #408080; font-style: italic"># Load the data</span>
cancer <span style="color: #666666">=</span> load_breast_cancer()
X_train, X_test, y_train, y_test <span style="color: #666666">=</span> train_test_split(cancer<span style="color: #666666">.</span>data,cancer<span style="color: #666666">.</span>target,random_state<span style="color: #666666">=0</span>)
<span style="color: #008000">print</span>(X_train<span style="color: #666666">.</span>shape)
<span style="color: #008000">print</span>(X_test<span style="color: #666666">.</span>shape)
<span style="color: #408080; font-style: italic"># Logistic Regression</span>
logreg <span style="color: #666666">=</span> LogisticRegression(solver<span style="color: #666666">=</span><span style="color: #BA2121">&#39;lbfgs&#39;</span>)
logreg<span style="color: #666666">.</span>fit(X_train, y_train)
<span style="color: #008000">print</span>(<span style="color: #BA2121">&quot;Test set accuracy with Logistic Regression: </span><span style="color: #BB6688; font-weight: bold">{:.2f}</span><span style="color: #BA2121">&quot;</span><span style="color: #666666">.</span>format(logreg<span style="color: #666666">.</span>score(X_test,y_test)))
</pre>
</div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_subarea output_stream output_stdout output_text">
</div>
</div>
</div>
</div>
</div>
<p>
<!-- navigation buttons at the bottom of the page -->
@@ -286,7 +276,7 @@ for \( \beta \) gives us an equation for \( \gamma \). This is a non-linear equa
<li><a href="._week45-bs014.html">15</a></li>
<li><a href="._week45-bs015.html">16</a></li>
<li><a href="">...</a></li>
<li><a href="._week45-bs039.html">40</a></li>
<li><a href="._week45-bs020.html">21</a></li>
<li><a href="._week45-bs007.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+164 -147
View File
@@ -8,8 +8,8 @@ doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=d
<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 45: Decisions Trees, Random Forests, Bagging and Boosting">
<title>Week 45: Decisions Trees, Random Forests, Bagging and Boosting</title>
<meta name="description" content="Week 45, Recurrent Neural Networks">
<title>Week 45, Recurrent Neural Networks</title>
<!-- Bootstrap style: bootstrap -->
<!-- doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=default --html_admon=bootstrap_panel --html_output=week45-bs --no_mako -->
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
@@ -36,109 +36,101 @@ doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=d
<!-- tocinfo
{'highest level': 2,
'sections': [('Overview of week 45', 2, None, 'overview-of-week-45'),
('Brief code reminder from last wekk',
'sections': [('Plan for week 45', 2, None, 'plan-for-week-45'),
('Material for the lab sessions, additional ways to present '
'classification results and other practicalities',
2,
None,
'brief-code-reminder-from-last-wekk'),
("Boosting, a Bird's Eye View",
'material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities'),
('Searching for Optimal Regularization Parameters $\\lambda$',
2,
None,
'boosting-a-bird-s-eye-view'),
('What is boosting? Additive Modelling/Iterative Fitting',
'searching-for-optimal-regularization-parameters-lambda'),
('Grid Search', 2, None, 'grid-search'),
('Randomized Grid Search', 2, None, 'randomized-grid-search'),
('Wisconsin Cancer Data', 2, None, 'wisconsin-cancer-data'),
('Using the correlation matrix',
2,
None,
'what-is-boosting-additive-modelling-iterative-fitting'),
('Iterative Fitting, Regression and Squared-error Cost Function',
'using-the-correlation-matrix'),
('Discussing the correlation data',
2,
None,
'iterative-fitting-regression-and-squared-error-cost-function'),
('Squared-Error Example and Iterative Fitting',
'discussing-the-correlation-data'),
('Other measures in classification studies: Cancer Data again',
2,
None,
'squared-error-example-and-iterative-fitting'),
('Iterative Fitting, Classification and AdaBoost',
'other-measures-in-classification-studies-cancer-data-again'),
('Material for Lecture Thursday November 9',
2,
None,
'iterative-fitting-classification-and-adaboost'),
('Adaptive Boosting, AdaBoost',
'material-for-lecture-thursday-november-9'),
('Recurrent neural networks (RNNs): Overarching view',
2,
None,
'adaptive-boosting-adaboost'),
('Building up AdaBoost', 2, None, 'building-up-adaboost'),
('Adaptive boosting: AdaBoost, Basic Algorithm',
'recurrent-neural-networks-rnns-overarching-view'),
('A simple example', 2, None, 'a-simple-example'),
('Memoryless models', 3, None, 'memoryless-models'),
('Linear dynamical model', 3, None, 'linear-dynamical-model'),
('Hidden Markov Models', 3, None, 'hidden-markov-models'),
('RNNs', 3, None, 'rnns'),
('Do generative models need to be stochastic?',
3,
None,
'do-generative-models-need-to-be-stochastic'),
('What kinds of behaviour can RNNs exhibit?',
3,
None,
'what-kinds-of-behaviour-can-rnns-exhibit'),
('Basic layout', 2, None, 'basic-layout'),
('We need to specify the initial activity state of all the '
'hidden and output units',
3,
None,
'we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units'),
('We can specify inputs in several ways',
3,
None,
'we-can-specify-inputs-in-several-ways'),
('We can specify targets in several ways',
3,
None,
'we-can-specify-targets-in-several-ways'),
('Backpropagation through time',
3,
None,
'backpropagation-through-time'),
('The backward pass is linear',
3,
None,
'the-backward-pass-is-linear'),
('The problem of exploding or vanishing gradients',
2,
None,
'adaptive-boosting-adaboost-basic-algorithm'),
('Basic Steps of AdaBoost', 2, None, 'basic-steps-of-adaboost'),
('AdaBoost Examples', 2, None, 'adaboost-examples'),
('Gradient boosting: Basics with Steepest Descent/Functional '
'Gradient Descent',
'the-problem-of-exploding-or-vanishing-gradients'),
('Four effective ways to learn an RNN',
2,
None,
'gradient-boosting-basics-with-steepest-descent-functional-gradient-descent'),
('The Squared-Error again! Steepest Descent',
'four-effective-ways-to-learn-an-rnn'),
('Long Short Term Memory (LSTM)',
3,
None,
'long-short-term-memory-lstm'),
('Implementing a memory cell in a neural network',
3,
None,
'implementing-a-memory-cell-in-a-neural-network'),
('An extrapolation example', 2, None, 'an-extrapolation-example'),
('Formatting the Data', 2, None, 'formatting-the-data'),
('Predicting New Points With A Trained Recurrent Neural Network',
2,
None,
'the-squared-error-again-steepest-descent'),
('Steepest Descent Example', 2, None, 'steepest-descent-example'),
('Gradient Boosting, algorithm',
'predicting-new-points-with-a-trained-recurrent-neural-network'),
('Other Things to Try', 2, None, 'other-things-to-try'),
('Other Types of Recurrent Neural Networks',
2,
None,
'gradient-boosting-algorithm'),
('Gradient Boosting, Examples of Regression',
2,
None,
'gradient-boosting-examples-of-regression'),
('Gradient Boosting, Classification Example',
2,
None,
'gradient-boosting-classification-example'),
('XGBoost: Extreme Gradient Boosting',
2,
None,
'xgboost-extreme-gradient-boosting'),
('Regression Case', 2, None, 'regression-case'),
('Xgboost on the Cancer Data',
2,
None,
'xgboost-on-the-cancer-data'),
('Support Vector Machines, overarching aims',
2,
None,
'support-vector-machines-overarching-aims'),
('Hyperplanes and all that', 2, None, 'hyperplanes-and-all-that'),
('What is a hyperplane?', 2, None, 'what-is-a-hyperplane'),
('A $p$-dimensional space of features',
2,
None,
'a-p-dimensional-space-of-features'),
('The two-dimensional case', 2, None, 'the-two-dimensional-case'),
('Getting into the details', 2, None, 'getting-into-the-details'),
('First attempt at a minimization approach',
2,
None,
'first-attempt-at-a-minimization-approach'),
('Solving the equations', 2, None, 'solving-the-equations'),
('Code Example', 2, None, 'code-example'),
('Problems with the Simpler Approach',
2,
None,
'problems-with-the-simpler-approach'),
('A better approach', 2, None, 'a-better-approach'),
('A quick Reminder on Lagrangian Multipliers',
2,
None,
'a-quick-reminder-on-lagrangian-multipliers'),
('Adding the Multiplier', 2, None, 'adding-the-multiplier'),
('Setting up the Problem', 2, None, 'setting-up-the-problem'),
('The problem to solve', 2, None, 'the-problem-to-solve'),
('The last steps', 2, None, 'the-last-steps'),
('A soft classifier', 2, None, 'a-soft-classifier'),
('Soft optmization problem',
2,
None,
'soft-optmization-problem')]}
'other-types-of-recurrent-neural-networks')]}
end of tocinfo -->
<body>
@@ -166,52 +158,46 @@ MathJax.Hub.Config({
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="week45-bs.html">Week 45: Decisions Trees, Random Forests, Bagging and Boosting</a>
<a class="navbar-brand" href="week45-bs.html">Week 45, Recurrent Neural Networks</a>
</div>
<div class="navbar-collapse collapse navbar-responsive-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
<ul class="dropdown-menu">
<!-- navigation toc: --> <li><a href="._week45-bs001.html#overview-of-week-45" style="font-size: 80%;">Overview of week 45</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs002.html#brief-code-reminder-from-last-wekk" style="font-size: 80%;">Brief code reminder from last wekk</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs003.html#boosting-a-bird-s-eye-view" style="font-size: 80%;">Boosting, a Bird's Eye View</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs004.html#what-is-boosting-additive-modelling-iterative-fitting" style="font-size: 80%;">What is boosting? Additive Modelling/Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs005.html#iterative-fitting-regression-and-squared-error-cost-function" style="font-size: 80%;">Iterative Fitting, Regression and Squared-error Cost Function</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs006.html#squared-error-example-and-iterative-fitting" style="font-size: 80%;">Squared-Error Example and Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="#iterative-fitting-classification-and-adaboost" style="font-size: 80%;">Iterative Fitting, Classification and AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs008.html#adaptive-boosting-adaboost" style="font-size: 80%;">Adaptive Boosting, AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs009.html#building-up-adaboost" style="font-size: 80%;">Building up AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs010.html#adaptive-boosting-adaboost-basic-algorithm" style="font-size: 80%;">Adaptive boosting: AdaBoost, Basic Algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs011.html#basic-steps-of-adaboost" style="font-size: 80%;">Basic Steps of AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#adaboost-examples" style="font-size: 80%;">AdaBoost Examples</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#gradient-boosting-basics-with-steepest-descent-functional-gradient-descent" style="font-size: 80%;">Gradient boosting: Basics with Steepest Descent/Functional Gradient Descent</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs014.html#the-squared-error-again-steepest-descent" style="font-size: 80%;">The Squared-Error again! Steepest Descent</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#steepest-descent-example" style="font-size: 80%;">Steepest Descent Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs016.html#gradient-boosting-algorithm" style="font-size: 80%;">Gradient Boosting, algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs017.html#gradient-boosting-examples-of-regression" style="font-size: 80%;">Gradient Boosting, Examples of Regression</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs018.html#gradient-boosting-classification-example" style="font-size: 80%;">Gradient Boosting, Classification Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs019.html#xgboost-extreme-gradient-boosting" style="font-size: 80%;">XGBoost: Extreme Gradient Boosting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs020.html#regression-case" style="font-size: 80%;">Regression Case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs021.html#xgboost-on-the-cancer-data" style="font-size: 80%;">Xgboost on the Cancer Data</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs022.html#support-vector-machines-overarching-aims" style="font-size: 80%;">Support Vector Machines, overarching aims</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs023.html#hyperplanes-and-all-that" style="font-size: 80%;">Hyperplanes and all that</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs024.html#what-is-a-hyperplane" style="font-size: 80%;">What is a hyperplane?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs025.html#a-p-dimensional-space-of-features" style="font-size: 80%;">A \( p \)-dimensional space of features</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs026.html#the-two-dimensional-case" style="font-size: 80%;">The two-dimensional case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs027.html#getting-into-the-details" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs028.html#first-attempt-at-a-minimization-approach" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs029.html#solving-the-equations" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs030.html#code-example" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs031.html#problems-with-the-simpler-approach" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs032.html#a-better-approach" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs033.html#a-quick-reminder-on-lagrangian-multipliers" style="font-size: 80%;">A quick Reminder on Lagrangian Multipliers</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs034.html#adding-the-multiplier" style="font-size: 80%;">Adding the Multiplier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs035.html#setting-up-the-problem" style="font-size: 80%;">Setting up the Problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs036.html#the-problem-to-solve" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs037.html#the-last-steps" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs038.html#a-soft-classifier" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs039.html#soft-optmization-problem" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs001.html#plan-for-week-45" style="font-size: 80%;"><b>Plan for week 45</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs002.html#material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities" style="font-size: 80%;"><b>Material for the lab sessions, additional ways to present classification results and other practicalities</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs003.html#searching-for-optimal-regularization-parameters-lambda" style="font-size: 80%;"><b>Searching for Optimal Regularization Parameters \( \lambda \)</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs004.html#grid-search" style="font-size: 80%;"><b>Grid Search</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs005.html#randomized-grid-search" style="font-size: 80%;"><b>Randomized Grid Search</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs006.html#wisconsin-cancer-data" style="font-size: 80%;"><b>Wisconsin Cancer Data</b></a></li>
<!-- navigation toc: --> <li><a href="#using-the-correlation-matrix" style="font-size: 80%;"><b>Using the correlation matrix</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs008.html#discussing-the-correlation-data" style="font-size: 80%;"><b>Discussing the correlation data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs009.html#other-measures-in-classification-studies-cancer-data-again" style="font-size: 80%;"><b>Other measures in classification studies: Cancer Data again</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs010.html#material-for-lecture-thursday-november-9" style="font-size: 80%;"><b>Material for Lecture Thursday November 9</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs011.html#recurrent-neural-networks-rnns-overarching-view" style="font-size: 80%;"><b>Recurrent neural networks (RNNs): Overarching view</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#a-simple-example" style="font-size: 80%;"><b>A simple example</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#memoryless-models" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Memoryless models</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#linear-dynamical-model" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Linear dynamical model</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#hidden-markov-models" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Hidden Markov Models</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#rnns" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;RNNs</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#do-generative-models-need-to-be-stochastic" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Do generative models need to be stochastic?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#what-kinds-of-behaviour-can-rnns-exhibit" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;What kinds of behaviour can RNNs exhibit?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#basic-layout" style="font-size: 80%;"><b>Basic layout</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We need to specify the initial activity state of all the hidden and output units</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-can-specify-inputs-in-several-ways" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We can specify inputs in several ways</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-can-specify-targets-in-several-ways" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We can specify targets in several ways</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#backpropagation-through-time" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Backpropagation through time</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#the-backward-pass-is-linear" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;The backward pass is linear</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs014.html#the-problem-of-exploding-or-vanishing-gradients" style="font-size: 80%;"><b>The problem of exploding or vanishing gradients</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#four-effective-ways-to-learn-an-rnn" style="font-size: 80%;"><b>Four effective ways to learn an RNN</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#long-short-term-memory-lstm" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Long Short Term Memory (LSTM)</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#implementing-a-memory-cell-in-a-neural-network" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Implementing a memory cell in a neural network</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs016.html#an-extrapolation-example" style="font-size: 80%;"><b>An extrapolation example</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs017.html#formatting-the-data" style="font-size: 80%;"><b>Formatting the Data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs018.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;"><b>Predicting New Points With A Trained Recurrent Neural Network</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs019.html#other-things-to-try" style="font-size: 80%;"><b>Other Things to Try</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs020.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;"><b>Other Types of Recurrent Neural Networks</b></a></li>
</ul>
</li>
@@ -223,35 +209,66 @@ MathJax.Hub.Config({
<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p> <!-- add vertical space -->
<a name="part0007"></a>
<!-- !split -->
<h2 id="iterative-fitting-classification-and-adaboost" class="anchor">Iterative Fitting, Classification and AdaBoost </h2>
<h2 id="using-the-correlation-matrix" class="anchor">Using the correlation matrix </h2>
<p>Let us consider a binary classification problem with two outcomes \( y_i \in \{-1,1\} \) and \( i=0,1,2,\dots,n-1 \) as our set of
observations. We define a classification function \( G(x) \) which produces a prediction taking one or the other of the two values
\( \{-1,1\} \).
<p>In addition to the above scores, we could also study the covariance (and the correlation matrix).
We use <b>Pandas</b> to compute the correlation matrix.
</p>
<p>The error rate of the training sample is then</p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight" style="background: #f8f8f8">
<pre style="line-height: 125%;"><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">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">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> train_test_split
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.datasets</span> <span style="color: #008000; font-weight: bold">import</span> load_breast_cancer
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.linear_model</span> <span style="color: #008000; font-weight: bold">import</span> LogisticRegression
cancer <span style="color: #666666">=</span> load_breast_cancer()
<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: #408080; font-style: italic"># Making a data frame</span>
cancerpd <span style="color: #666666">=</span> pd<span style="color: #666666">.</span>DataFrame(cancer<span style="color: #666666">.</span>data, columns<span style="color: #666666">=</span>cancer<span style="color: #666666">.</span>feature_names)
$$
\mathrm{\overline{err}}=\frac{1}{n} \sum_{i=0}^{n-1} I(y_i\ne G(x_i)).
$$
fig, axes <span style="color: #666666">=</span> plt<span style="color: #666666">.</span>subplots(<span style="color: #666666">15</span>,<span style="color: #666666">2</span>,figsize<span style="color: #666666">=</span>(<span style="color: #666666">10</span>,<span style="color: #666666">20</span>))
malignant <span style="color: #666666">=</span> cancer<span style="color: #666666">.</span>data[cancer<span style="color: #666666">.</span>target <span style="color: #666666">==</span> <span style="color: #666666">0</span>]
benign <span style="color: #666666">=</span> cancer<span style="color: #666666">.</span>data[cancer<span style="color: #666666">.</span>target <span style="color: #666666">==</span> <span style="color: #666666">1</span>]
ax <span style="color: #666666">=</span> axes<span style="color: #666666">.</span>ravel()
<p>The iterative procedure starts with defining a weak classifier whose
error rate is barely better than random guessing. The iterative
procedure in boosting is to sequentially apply a weak
classification algorithm to repeatedly modified versions of the data
producing a sequence of weak classifiers \( G_m(x) \).
</p>
<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: #666666">30</span>):
_, bins <span style="color: #666666">=</span> np<span style="color: #666666">.</span>histogram(cancer<span style="color: #666666">.</span>data[:,i], bins <span style="color: #666666">=50</span>)
ax[i]<span style="color: #666666">.</span>hist(malignant[:,i], bins <span style="color: #666666">=</span> bins, alpha <span style="color: #666666">=</span> <span style="color: #666666">0.5</span>)
ax[i]<span style="color: #666666">.</span>hist(benign[:,i], bins <span style="color: #666666">=</span> bins, alpha <span style="color: #666666">=</span> <span style="color: #666666">0.5</span>)
ax[i]<span style="color: #666666">.</span>set_title(cancer<span style="color: #666666">.</span>feature_names[i])
ax[i]<span style="color: #666666">.</span>set_yticks(())
ax[<span style="color: #666666">0</span>]<span style="color: #666666">.</span>set_xlabel(<span style="color: #BA2121">&quot;Feature magnitude&quot;</span>)
ax[<span style="color: #666666">0</span>]<span style="color: #666666">.</span>set_ylabel(<span style="color: #BA2121">&quot;Frequency&quot;</span>)
ax[<span style="color: #666666">0</span>]<span style="color: #666666">.</span>legend([<span style="color: #BA2121">&quot;Malignant&quot;</span>, <span style="color: #BA2121">&quot;Benign&quot;</span>], loc <span style="color: #666666">=</span><span style="color: #BA2121">&quot;best&quot;</span>)
fig<span style="color: #666666">.</span>tight_layout()
plt<span style="color: #666666">.</span>show()
<p>Here we will express our function \( f(x) \) in terms of \( G(x) \). That is</p>
$$
f_M(x) = \sum_{i=1}^M \beta_m b(x;\gamma_m),
$$
<p>will be a function of </p>
$$
G_M(x) = \mathrm{sign} \sum_{i=1}^M \alpha_m G_m(x).
$$
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">seaborn</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">sns</span>
correlation_matrix <span style="color: #666666">=</span> cancerpd<span style="color: #666666">.</span>corr()<span style="color: #666666">.</span>round(<span style="color: #666666">1</span>)
<span style="color: #408080; font-style: italic"># use the heatmap function from seaborn to plot the correlation matrix</span>
<span style="color: #408080; font-style: italic"># annot = True to print the values inside the square</span>
plt<span style="color: #666666">.</span>figure(figsize<span style="color: #666666">=</span>(<span style="color: #666666">15</span>,<span style="color: #666666">8</span>))
sns<span style="color: #666666">.</span>heatmap(data<span style="color: #666666">=</span>correlation_matrix, annot<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">True</span>)
plt<span style="color: #666666">.</span>show()
</pre>
</div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_subarea output_stream output_stdout output_text">
</div>
</div>
</div>
</div>
</div>
<p>
@@ -276,7 +293,7 @@ $$
<li><a href="._week45-bs015.html">16</a></li>
<li><a href="._week45-bs016.html">17</a></li>
<li><a href="">...</a></li>
<li><a href="._week45-bs039.html">40</a></li>
<li><a href="._week45-bs020.html">21</a></li>
<li><a href="._week45-bs008.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+175 -143
View File
@@ -8,8 +8,8 @@ doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=d
<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 45: Decisions Trees, Random Forests, Bagging and Boosting">
<title>Week 45: Decisions Trees, Random Forests, Bagging and Boosting</title>
<meta name="description" content="Week 45, Recurrent Neural Networks">
<title>Week 45, Recurrent Neural Networks</title>
<!-- Bootstrap style: bootstrap -->
<!-- doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=default --html_admon=bootstrap_panel --html_output=week45-bs --no_mako -->
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
@@ -36,109 +36,101 @@ doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=d
<!-- tocinfo
{'highest level': 2,
'sections': [('Overview of week 45', 2, None, 'overview-of-week-45'),
('Brief code reminder from last wekk',
'sections': [('Plan for week 45', 2, None, 'plan-for-week-45'),
('Material for the lab sessions, additional ways to present '
'classification results and other practicalities',
2,
None,
'brief-code-reminder-from-last-wekk'),
("Boosting, a Bird's Eye View",
'material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities'),
('Searching for Optimal Regularization Parameters $\\lambda$',
2,
None,
'boosting-a-bird-s-eye-view'),
('What is boosting? Additive Modelling/Iterative Fitting',
'searching-for-optimal-regularization-parameters-lambda'),
('Grid Search', 2, None, 'grid-search'),
('Randomized Grid Search', 2, None, 'randomized-grid-search'),
('Wisconsin Cancer Data', 2, None, 'wisconsin-cancer-data'),
('Using the correlation matrix',
2,
None,
'what-is-boosting-additive-modelling-iterative-fitting'),
('Iterative Fitting, Regression and Squared-error Cost Function',
'using-the-correlation-matrix'),
('Discussing the correlation data',
2,
None,
'iterative-fitting-regression-and-squared-error-cost-function'),
('Squared-Error Example and Iterative Fitting',
'discussing-the-correlation-data'),
('Other measures in classification studies: Cancer Data again',
2,
None,
'squared-error-example-and-iterative-fitting'),
('Iterative Fitting, Classification and AdaBoost',
'other-measures-in-classification-studies-cancer-data-again'),
('Material for Lecture Thursday November 9',
2,
None,
'iterative-fitting-classification-and-adaboost'),
('Adaptive Boosting, AdaBoost',
'material-for-lecture-thursday-november-9'),
('Recurrent neural networks (RNNs): Overarching view',
2,
None,
'adaptive-boosting-adaboost'),
('Building up AdaBoost', 2, None, 'building-up-adaboost'),
('Adaptive boosting: AdaBoost, Basic Algorithm',
'recurrent-neural-networks-rnns-overarching-view'),
('A simple example', 2, None, 'a-simple-example'),
('Memoryless models', 3, None, 'memoryless-models'),
('Linear dynamical model', 3, None, 'linear-dynamical-model'),
('Hidden Markov Models', 3, None, 'hidden-markov-models'),
('RNNs', 3, None, 'rnns'),
('Do generative models need to be stochastic?',
3,
None,
'do-generative-models-need-to-be-stochastic'),
('What kinds of behaviour can RNNs exhibit?',
3,
None,
'what-kinds-of-behaviour-can-rnns-exhibit'),
('Basic layout', 2, None, 'basic-layout'),
('We need to specify the initial activity state of all the '
'hidden and output units',
3,
None,
'we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units'),
('We can specify inputs in several ways',
3,
None,
'we-can-specify-inputs-in-several-ways'),
('We can specify targets in several ways',
3,
None,
'we-can-specify-targets-in-several-ways'),
('Backpropagation through time',
3,
None,
'backpropagation-through-time'),
('The backward pass is linear',
3,
None,
'the-backward-pass-is-linear'),
('The problem of exploding or vanishing gradients',
2,
None,
'adaptive-boosting-adaboost-basic-algorithm'),
('Basic Steps of AdaBoost', 2, None, 'basic-steps-of-adaboost'),
('AdaBoost Examples', 2, None, 'adaboost-examples'),
('Gradient boosting: Basics with Steepest Descent/Functional '
'Gradient Descent',
'the-problem-of-exploding-or-vanishing-gradients'),
('Four effective ways to learn an RNN',
2,
None,
'gradient-boosting-basics-with-steepest-descent-functional-gradient-descent'),
('The Squared-Error again! Steepest Descent',
'four-effective-ways-to-learn-an-rnn'),
('Long Short Term Memory (LSTM)',
3,
None,
'long-short-term-memory-lstm'),
('Implementing a memory cell in a neural network',
3,
None,
'implementing-a-memory-cell-in-a-neural-network'),
('An extrapolation example', 2, None, 'an-extrapolation-example'),
('Formatting the Data', 2, None, 'formatting-the-data'),
('Predicting New Points With A Trained Recurrent Neural Network',
2,
None,
'the-squared-error-again-steepest-descent'),
('Steepest Descent Example', 2, None, 'steepest-descent-example'),
('Gradient Boosting, algorithm',
'predicting-new-points-with-a-trained-recurrent-neural-network'),
('Other Things to Try', 2, None, 'other-things-to-try'),
('Other Types of Recurrent Neural Networks',
2,
None,
'gradient-boosting-algorithm'),
('Gradient Boosting, Examples of Regression',
2,
None,
'gradient-boosting-examples-of-regression'),
('Gradient Boosting, Classification Example',
2,
None,
'gradient-boosting-classification-example'),
('XGBoost: Extreme Gradient Boosting',
2,
None,
'xgboost-extreme-gradient-boosting'),
('Regression Case', 2, None, 'regression-case'),
('Xgboost on the Cancer Data',
2,
None,
'xgboost-on-the-cancer-data'),
('Support Vector Machines, overarching aims',
2,
None,
'support-vector-machines-overarching-aims'),
('Hyperplanes and all that', 2, None, 'hyperplanes-and-all-that'),
('What is a hyperplane?', 2, None, 'what-is-a-hyperplane'),
('A $p$-dimensional space of features',
2,
None,
'a-p-dimensional-space-of-features'),
('The two-dimensional case', 2, None, 'the-two-dimensional-case'),
('Getting into the details', 2, None, 'getting-into-the-details'),
('First attempt at a minimization approach',
2,
None,
'first-attempt-at-a-minimization-approach'),
('Solving the equations', 2, None, 'solving-the-equations'),
('Code Example', 2, None, 'code-example'),
('Problems with the Simpler Approach',
2,
None,
'problems-with-the-simpler-approach'),
('A better approach', 2, None, 'a-better-approach'),
('A quick Reminder on Lagrangian Multipliers',
2,
None,
'a-quick-reminder-on-lagrangian-multipliers'),
('Adding the Multiplier', 2, None, 'adding-the-multiplier'),
('Setting up the Problem', 2, None, 'setting-up-the-problem'),
('The problem to solve', 2, None, 'the-problem-to-solve'),
('The last steps', 2, None, 'the-last-steps'),
('A soft classifier', 2, None, 'a-soft-classifier'),
('Soft optmization problem',
2,
None,
'soft-optmization-problem')]}
'other-types-of-recurrent-neural-networks')]}
end of tocinfo -->
<body>
@@ -166,52 +158,46 @@ MathJax.Hub.Config({
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="week45-bs.html">Week 45: Decisions Trees, Random Forests, Bagging and Boosting</a>
<a class="navbar-brand" href="week45-bs.html">Week 45, Recurrent Neural Networks</a>
</div>
<div class="navbar-collapse collapse navbar-responsive-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
<ul class="dropdown-menu">
<!-- navigation toc: --> <li><a href="._week45-bs001.html#overview-of-week-45" style="font-size: 80%;">Overview of week 45</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs002.html#brief-code-reminder-from-last-wekk" style="font-size: 80%;">Brief code reminder from last wekk</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs003.html#boosting-a-bird-s-eye-view" style="font-size: 80%;">Boosting, a Bird's Eye View</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs004.html#what-is-boosting-additive-modelling-iterative-fitting" style="font-size: 80%;">What is boosting? Additive Modelling/Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs005.html#iterative-fitting-regression-and-squared-error-cost-function" style="font-size: 80%;">Iterative Fitting, Regression and Squared-error Cost Function</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs006.html#squared-error-example-and-iterative-fitting" style="font-size: 80%;">Squared-Error Example and Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs007.html#iterative-fitting-classification-and-adaboost" style="font-size: 80%;">Iterative Fitting, Classification and AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="#adaptive-boosting-adaboost" style="font-size: 80%;">Adaptive Boosting, AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs009.html#building-up-adaboost" style="font-size: 80%;">Building up AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs010.html#adaptive-boosting-adaboost-basic-algorithm" style="font-size: 80%;">Adaptive boosting: AdaBoost, Basic Algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs011.html#basic-steps-of-adaboost" style="font-size: 80%;">Basic Steps of AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#adaboost-examples" style="font-size: 80%;">AdaBoost Examples</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#gradient-boosting-basics-with-steepest-descent-functional-gradient-descent" style="font-size: 80%;">Gradient boosting: Basics with Steepest Descent/Functional Gradient Descent</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs014.html#the-squared-error-again-steepest-descent" style="font-size: 80%;">The Squared-Error again! Steepest Descent</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#steepest-descent-example" style="font-size: 80%;">Steepest Descent Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs016.html#gradient-boosting-algorithm" style="font-size: 80%;">Gradient Boosting, algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs017.html#gradient-boosting-examples-of-regression" style="font-size: 80%;">Gradient Boosting, Examples of Regression</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs018.html#gradient-boosting-classification-example" style="font-size: 80%;">Gradient Boosting, Classification Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs019.html#xgboost-extreme-gradient-boosting" style="font-size: 80%;">XGBoost: Extreme Gradient Boosting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs020.html#regression-case" style="font-size: 80%;">Regression Case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs021.html#xgboost-on-the-cancer-data" style="font-size: 80%;">Xgboost on the Cancer Data</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs022.html#support-vector-machines-overarching-aims" style="font-size: 80%;">Support Vector Machines, overarching aims</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs023.html#hyperplanes-and-all-that" style="font-size: 80%;">Hyperplanes and all that</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs024.html#what-is-a-hyperplane" style="font-size: 80%;">What is a hyperplane?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs025.html#a-p-dimensional-space-of-features" style="font-size: 80%;">A \( p \)-dimensional space of features</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs026.html#the-two-dimensional-case" style="font-size: 80%;">The two-dimensional case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs027.html#getting-into-the-details" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs028.html#first-attempt-at-a-minimization-approach" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs029.html#solving-the-equations" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs030.html#code-example" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs031.html#problems-with-the-simpler-approach" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs032.html#a-better-approach" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs033.html#a-quick-reminder-on-lagrangian-multipliers" style="font-size: 80%;">A quick Reminder on Lagrangian Multipliers</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs034.html#adding-the-multiplier" style="font-size: 80%;">Adding the Multiplier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs035.html#setting-up-the-problem" style="font-size: 80%;">Setting up the Problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs036.html#the-problem-to-solve" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs037.html#the-last-steps" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs038.html#a-soft-classifier" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs039.html#soft-optmization-problem" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs001.html#plan-for-week-45" style="font-size: 80%;"><b>Plan for week 45</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs002.html#material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities" style="font-size: 80%;"><b>Material for the lab sessions, additional ways to present classification results and other practicalities</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs003.html#searching-for-optimal-regularization-parameters-lambda" style="font-size: 80%;"><b>Searching for Optimal Regularization Parameters \( \lambda \)</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs004.html#grid-search" style="font-size: 80%;"><b>Grid Search</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs005.html#randomized-grid-search" style="font-size: 80%;"><b>Randomized Grid Search</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs006.html#wisconsin-cancer-data" style="font-size: 80%;"><b>Wisconsin Cancer Data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs007.html#using-the-correlation-matrix" style="font-size: 80%;"><b>Using the correlation matrix</b></a></li>
<!-- navigation toc: --> <li><a href="#discussing-the-correlation-data" style="font-size: 80%;"><b>Discussing the correlation data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs009.html#other-measures-in-classification-studies-cancer-data-again" style="font-size: 80%;"><b>Other measures in classification studies: Cancer Data again</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs010.html#material-for-lecture-thursday-november-9" style="font-size: 80%;"><b>Material for Lecture Thursday November 9</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs011.html#recurrent-neural-networks-rnns-overarching-view" style="font-size: 80%;"><b>Recurrent neural networks (RNNs): Overarching view</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#a-simple-example" style="font-size: 80%;"><b>A simple example</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#memoryless-models" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Memoryless models</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#linear-dynamical-model" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Linear dynamical model</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#hidden-markov-models" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Hidden Markov Models</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#rnns" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;RNNs</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#do-generative-models-need-to-be-stochastic" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Do generative models need to be stochastic?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#what-kinds-of-behaviour-can-rnns-exhibit" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;What kinds of behaviour can RNNs exhibit?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#basic-layout" style="font-size: 80%;"><b>Basic layout</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We need to specify the initial activity state of all the hidden and output units</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-can-specify-inputs-in-several-ways" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We can specify inputs in several ways</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-can-specify-targets-in-several-ways" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We can specify targets in several ways</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#backpropagation-through-time" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Backpropagation through time</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#the-backward-pass-is-linear" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;The backward pass is linear</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs014.html#the-problem-of-exploding-or-vanishing-gradients" style="font-size: 80%;"><b>The problem of exploding or vanishing gradients</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#four-effective-ways-to-learn-an-rnn" style="font-size: 80%;"><b>Four effective ways to learn an RNN</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#long-short-term-memory-lstm" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Long Short Term Memory (LSTM)</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#implementing-a-memory-cell-in-a-neural-network" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Implementing a memory cell in a neural network</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs016.html#an-extrapolation-example" style="font-size: 80%;"><b>An extrapolation example</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs017.html#formatting-the-data" style="font-size: 80%;"><b>Formatting the Data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs018.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;"><b>Predicting New Points With A Trained Recurrent Neural Network</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs019.html#other-things-to-try" style="font-size: 80%;"><b>Other Things to Try</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs020.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;"><b>Other Types of Recurrent Neural Networks</b></a></li>
</ul>
</li>
@@ -223,29 +209,75 @@ MathJax.Hub.Config({
<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p> <!-- add vertical space -->
<a name="part0008"></a>
<!-- !split -->
<h2 id="adaptive-boosting-adaboost" class="anchor">Adaptive Boosting, AdaBoost </h2>
<h2 id="discussing-the-correlation-data" class="anchor">Discussing the correlation data </h2>
<p>In our iterative procedure we define thus</p>
$$
f_m(x) = f_{m-1}(x)+\beta_mG_m(x).
$$
<p>The simplest possible cost function which leads (also simple from a computational point of view) to the AdaBoost algorithm is the
exponential cost/loss function defined as
</p>
$$
C(\boldsymbol{y},\boldsymbol{f}) = \sum_{i=0}^{n-1}\exp{(-y_i(f_{m-1}(x_i)+\beta G(x_i))}.
$$
<p>We optimize \( \beta \) and \( G \) for each value of \( m=1:M \) as we did in the regression case.
This is normally done in two steps. Let us however first rewrite the cost function as
<p>In the above example we note two things. In the first plot we display
the overlap of benign and malignant tumors as functions of the various
features in the Wisconsing breast cancer data set. We see that for
some of the features we can distinguish clearly the benign and
malignant cases while for other features we cannot. This can point to
us which features may be of greater interest when we wish to classify
a benign or not benign tumour.
</p>
$$
C(\boldsymbol{y},\boldsymbol{f}) = \sum_{i=0}^{n-1}w_i^{m}\exp{(-y_i\beta G(x_i))},
$$
<p>In the second figure we have computed the so-called correlation
matrix, which in our case with thirty features becomes a \( 30\times 30 \)
matrix.
</p>
<p>where we have defined \( w_i^m= \exp{(-y_if_{m-1}(x_i))} \).</p>
<p>We constructed this matrix using <b>pandas</b> via the statements</p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight" style="background: #f8f8f8">
<pre style="line-height: 125%;">cancerpd <span style="color: #666666">=</span> pd<span style="color: #666666">.</span>DataFrame(cancer<span style="color: #666666">.</span>data, columns<span style="color: #666666">=</span>cancer<span style="color: #666666">.</span>feature_names)
</pre>
</div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_subarea output_stream output_stdout output_text">
</div>
</div>
</div>
</div>
</div>
<p>and then</p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight" style="background: #f8f8f8">
<pre style="line-height: 125%;">correlation_matrix <span style="color: #666666">=</span> cancerpd<span style="color: #666666">.</span>corr()<span style="color: #666666">.</span>round(<span style="color: #666666">1</span>)
</pre>
</div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_subarea output_stream output_stdout output_text">
</div>
</div>
</div>
</div>
</div>
<p>Diagonalizing this matrix we can in turn say something about which
features are of relevance and which are not. This leads us to
the classical Principal Component Analysis (PCA) theorem with
applications. This will be discussed later this semester (<a href="https://compphysics.github.io/MachineLearning/doc/pub/week43/html/week43-bs.html" target="_self">week 43</a>).
</p>
<p>
<!-- navigation buttons at the bottom of the page -->
@@ -270,7 +302,7 @@ $$
<li><a href="._week45-bs016.html">17</a></li>
<li><a href="._week45-bs017.html">18</a></li>
<li><a href="">...</a></li>
<li><a href="._week45-bs039.html">40</a></li>
<li><a href="._week45-bs020.html">21</a></li>
<li><a href="._week45-bs009.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+159 -157
View File
@@ -8,8 +8,8 @@ doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=d
<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 45: Decisions Trees, Random Forests, Bagging and Boosting">
<title>Week 45: Decisions Trees, Random Forests, Bagging and Boosting</title>
<meta name="description" content="Week 45, Recurrent Neural Networks">
<title>Week 45, Recurrent Neural Networks</title>
<!-- Bootstrap style: bootstrap -->
<!-- doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=default --html_admon=bootstrap_panel --html_output=week45-bs --no_mako -->
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
@@ -36,109 +36,101 @@ doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=d
<!-- tocinfo
{'highest level': 2,
'sections': [('Overview of week 45', 2, None, 'overview-of-week-45'),
('Brief code reminder from last wekk',
'sections': [('Plan for week 45', 2, None, 'plan-for-week-45'),
('Material for the lab sessions, additional ways to present '
'classification results and other practicalities',
2,
None,
'brief-code-reminder-from-last-wekk'),
("Boosting, a Bird's Eye View",
'material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities'),
('Searching for Optimal Regularization Parameters $\\lambda$',
2,
None,
'boosting-a-bird-s-eye-view'),
('What is boosting? Additive Modelling/Iterative Fitting',
'searching-for-optimal-regularization-parameters-lambda'),
('Grid Search', 2, None, 'grid-search'),
('Randomized Grid Search', 2, None, 'randomized-grid-search'),
('Wisconsin Cancer Data', 2, None, 'wisconsin-cancer-data'),
('Using the correlation matrix',
2,
None,
'what-is-boosting-additive-modelling-iterative-fitting'),
('Iterative Fitting, Regression and Squared-error Cost Function',
'using-the-correlation-matrix'),
('Discussing the correlation data',
2,
None,
'iterative-fitting-regression-and-squared-error-cost-function'),
('Squared-Error Example and Iterative Fitting',
'discussing-the-correlation-data'),
('Other measures in classification studies: Cancer Data again',
2,
None,
'squared-error-example-and-iterative-fitting'),
('Iterative Fitting, Classification and AdaBoost',
'other-measures-in-classification-studies-cancer-data-again'),
('Material for Lecture Thursday November 9',
2,
None,
'iterative-fitting-classification-and-adaboost'),
('Adaptive Boosting, AdaBoost',
'material-for-lecture-thursday-november-9'),
('Recurrent neural networks (RNNs): Overarching view',
2,
None,
'adaptive-boosting-adaboost'),
('Building up AdaBoost', 2, None, 'building-up-adaboost'),
('Adaptive boosting: AdaBoost, Basic Algorithm',
'recurrent-neural-networks-rnns-overarching-view'),
('A simple example', 2, None, 'a-simple-example'),
('Memoryless models', 3, None, 'memoryless-models'),
('Linear dynamical model', 3, None, 'linear-dynamical-model'),
('Hidden Markov Models', 3, None, 'hidden-markov-models'),
('RNNs', 3, None, 'rnns'),
('Do generative models need to be stochastic?',
3,
None,
'do-generative-models-need-to-be-stochastic'),
('What kinds of behaviour can RNNs exhibit?',
3,
None,
'what-kinds-of-behaviour-can-rnns-exhibit'),
('Basic layout', 2, None, 'basic-layout'),
('We need to specify the initial activity state of all the '
'hidden and output units',
3,
None,
'we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units'),
('We can specify inputs in several ways',
3,
None,
'we-can-specify-inputs-in-several-ways'),
('We can specify targets in several ways',
3,
None,
'we-can-specify-targets-in-several-ways'),
('Backpropagation through time',
3,
None,
'backpropagation-through-time'),
('The backward pass is linear',
3,
None,
'the-backward-pass-is-linear'),
('The problem of exploding or vanishing gradients',
2,
None,
'adaptive-boosting-adaboost-basic-algorithm'),
('Basic Steps of AdaBoost', 2, None, 'basic-steps-of-adaboost'),
('AdaBoost Examples', 2, None, 'adaboost-examples'),
('Gradient boosting: Basics with Steepest Descent/Functional '
'Gradient Descent',
'the-problem-of-exploding-or-vanishing-gradients'),
('Four effective ways to learn an RNN',
2,
None,
'gradient-boosting-basics-with-steepest-descent-functional-gradient-descent'),
('The Squared-Error again! Steepest Descent',
'four-effective-ways-to-learn-an-rnn'),
('Long Short Term Memory (LSTM)',
3,
None,
'long-short-term-memory-lstm'),
('Implementing a memory cell in a neural network',
3,
None,
'implementing-a-memory-cell-in-a-neural-network'),
('An extrapolation example', 2, None, 'an-extrapolation-example'),
('Formatting the Data', 2, None, 'formatting-the-data'),
('Predicting New Points With A Trained Recurrent Neural Network',
2,
None,
'the-squared-error-again-steepest-descent'),
('Steepest Descent Example', 2, None, 'steepest-descent-example'),
('Gradient Boosting, algorithm',
'predicting-new-points-with-a-trained-recurrent-neural-network'),
('Other Things to Try', 2, None, 'other-things-to-try'),
('Other Types of Recurrent Neural Networks',
2,
None,
'gradient-boosting-algorithm'),
('Gradient Boosting, Examples of Regression',
2,
None,
'gradient-boosting-examples-of-regression'),
('Gradient Boosting, Classification Example',
2,
None,
'gradient-boosting-classification-example'),
('XGBoost: Extreme Gradient Boosting',
2,
None,
'xgboost-extreme-gradient-boosting'),
('Regression Case', 2, None, 'regression-case'),
('Xgboost on the Cancer Data',
2,
None,
'xgboost-on-the-cancer-data'),
('Support Vector Machines, overarching aims',
2,
None,
'support-vector-machines-overarching-aims'),
('Hyperplanes and all that', 2, None, 'hyperplanes-and-all-that'),
('What is a hyperplane?', 2, None, 'what-is-a-hyperplane'),
('A $p$-dimensional space of features',
2,
None,
'a-p-dimensional-space-of-features'),
('The two-dimensional case', 2, None, 'the-two-dimensional-case'),
('Getting into the details', 2, None, 'getting-into-the-details'),
('First attempt at a minimization approach',
2,
None,
'first-attempt-at-a-minimization-approach'),
('Solving the equations', 2, None, 'solving-the-equations'),
('Code Example', 2, None, 'code-example'),
('Problems with the Simpler Approach',
2,
None,
'problems-with-the-simpler-approach'),
('A better approach', 2, None, 'a-better-approach'),
('A quick Reminder on Lagrangian Multipliers',
2,
None,
'a-quick-reminder-on-lagrangian-multipliers'),
('Adding the Multiplier', 2, None, 'adding-the-multiplier'),
('Setting up the Problem', 2, None, 'setting-up-the-problem'),
('The problem to solve', 2, None, 'the-problem-to-solve'),
('The last steps', 2, None, 'the-last-steps'),
('A soft classifier', 2, None, 'a-soft-classifier'),
('Soft optmization problem',
2,
None,
'soft-optmization-problem')]}
'other-types-of-recurrent-neural-networks')]}
end of tocinfo -->
<body>
@@ -166,52 +158,46 @@ MathJax.Hub.Config({
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="week45-bs.html">Week 45: Decisions Trees, Random Forests, Bagging and Boosting</a>
<a class="navbar-brand" href="week45-bs.html">Week 45, Recurrent Neural Networks</a>
</div>
<div class="navbar-collapse collapse navbar-responsive-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
<ul class="dropdown-menu">
<!-- navigation toc: --> <li><a href="._week45-bs001.html#overview-of-week-45" style="font-size: 80%;">Overview of week 45</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs002.html#brief-code-reminder-from-last-wekk" style="font-size: 80%;">Brief code reminder from last wekk</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs003.html#boosting-a-bird-s-eye-view" style="font-size: 80%;">Boosting, a Bird's Eye View</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs004.html#what-is-boosting-additive-modelling-iterative-fitting" style="font-size: 80%;">What is boosting? Additive Modelling/Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs005.html#iterative-fitting-regression-and-squared-error-cost-function" style="font-size: 80%;">Iterative Fitting, Regression and Squared-error Cost Function</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs006.html#squared-error-example-and-iterative-fitting" style="font-size: 80%;">Squared-Error Example and Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs007.html#iterative-fitting-classification-and-adaboost" style="font-size: 80%;">Iterative Fitting, Classification and AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs008.html#adaptive-boosting-adaboost" style="font-size: 80%;">Adaptive Boosting, AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="#building-up-adaboost" style="font-size: 80%;">Building up AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs010.html#adaptive-boosting-adaboost-basic-algorithm" style="font-size: 80%;">Adaptive boosting: AdaBoost, Basic Algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs011.html#basic-steps-of-adaboost" style="font-size: 80%;">Basic Steps of AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#adaboost-examples" style="font-size: 80%;">AdaBoost Examples</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#gradient-boosting-basics-with-steepest-descent-functional-gradient-descent" style="font-size: 80%;">Gradient boosting: Basics with Steepest Descent/Functional Gradient Descent</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs014.html#the-squared-error-again-steepest-descent" style="font-size: 80%;">The Squared-Error again! Steepest Descent</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#steepest-descent-example" style="font-size: 80%;">Steepest Descent Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs016.html#gradient-boosting-algorithm" style="font-size: 80%;">Gradient Boosting, algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs017.html#gradient-boosting-examples-of-regression" style="font-size: 80%;">Gradient Boosting, Examples of Regression</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs018.html#gradient-boosting-classification-example" style="font-size: 80%;">Gradient Boosting, Classification Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs019.html#xgboost-extreme-gradient-boosting" style="font-size: 80%;">XGBoost: Extreme Gradient Boosting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs020.html#regression-case" style="font-size: 80%;">Regression Case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs021.html#xgboost-on-the-cancer-data" style="font-size: 80%;">Xgboost on the Cancer Data</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs022.html#support-vector-machines-overarching-aims" style="font-size: 80%;">Support Vector Machines, overarching aims</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs023.html#hyperplanes-and-all-that" style="font-size: 80%;">Hyperplanes and all that</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs024.html#what-is-a-hyperplane" style="font-size: 80%;">What is a hyperplane?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs025.html#a-p-dimensional-space-of-features" style="font-size: 80%;">A \( p \)-dimensional space of features</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs026.html#the-two-dimensional-case" style="font-size: 80%;">The two-dimensional case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs027.html#getting-into-the-details" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs028.html#first-attempt-at-a-minimization-approach" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs029.html#solving-the-equations" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs030.html#code-example" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs031.html#problems-with-the-simpler-approach" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs032.html#a-better-approach" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs033.html#a-quick-reminder-on-lagrangian-multipliers" style="font-size: 80%;">A quick Reminder on Lagrangian Multipliers</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs034.html#adding-the-multiplier" style="font-size: 80%;">Adding the Multiplier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs035.html#setting-up-the-problem" style="font-size: 80%;">Setting up the Problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs036.html#the-problem-to-solve" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs037.html#the-last-steps" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs038.html#a-soft-classifier" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs039.html#soft-optmization-problem" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs001.html#plan-for-week-45" style="font-size: 80%;"><b>Plan for week 45</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs002.html#material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities" style="font-size: 80%;"><b>Material for the lab sessions, additional ways to present classification results and other practicalities</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs003.html#searching-for-optimal-regularization-parameters-lambda" style="font-size: 80%;"><b>Searching for Optimal Regularization Parameters \( \lambda \)</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs004.html#grid-search" style="font-size: 80%;"><b>Grid Search</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs005.html#randomized-grid-search" style="font-size: 80%;"><b>Randomized Grid Search</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs006.html#wisconsin-cancer-data" style="font-size: 80%;"><b>Wisconsin Cancer Data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs007.html#using-the-correlation-matrix" style="font-size: 80%;"><b>Using the correlation matrix</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs008.html#discussing-the-correlation-data" style="font-size: 80%;"><b>Discussing the correlation data</b></a></li>
<!-- navigation toc: --> <li><a href="#other-measures-in-classification-studies-cancer-data-again" style="font-size: 80%;"><b>Other measures in classification studies: Cancer Data again</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs010.html#material-for-lecture-thursday-november-9" style="font-size: 80%;"><b>Material for Lecture Thursday November 9</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs011.html#recurrent-neural-networks-rnns-overarching-view" style="font-size: 80%;"><b>Recurrent neural networks (RNNs): Overarching view</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#a-simple-example" style="font-size: 80%;"><b>A simple example</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#memoryless-models" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Memoryless models</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#linear-dynamical-model" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Linear dynamical model</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#hidden-markov-models" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Hidden Markov Models</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#rnns" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;RNNs</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#do-generative-models-need-to-be-stochastic" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Do generative models need to be stochastic?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#what-kinds-of-behaviour-can-rnns-exhibit" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;What kinds of behaviour can RNNs exhibit?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#basic-layout" style="font-size: 80%;"><b>Basic layout</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We need to specify the initial activity state of all the hidden and output units</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-can-specify-inputs-in-several-ways" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We can specify inputs in several ways</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-can-specify-targets-in-several-ways" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We can specify targets in several ways</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#backpropagation-through-time" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Backpropagation through time</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#the-backward-pass-is-linear" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;The backward pass is linear</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs014.html#the-problem-of-exploding-or-vanishing-gradients" style="font-size: 80%;"><b>The problem of exploding or vanishing gradients</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#four-effective-ways-to-learn-an-rnn" style="font-size: 80%;"><b>Four effective ways to learn an RNN</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#long-short-term-memory-lstm" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Long Short Term Memory (LSTM)</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#implementing-a-memory-cell-in-a-neural-network" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Implementing a memory cell in a neural network</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs016.html#an-extrapolation-example" style="font-size: 80%;"><b>An extrapolation example</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs017.html#formatting-the-data" style="font-size: 80%;"><b>Formatting the Data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs018.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;"><b>Predicting New Points With A Trained Recurrent Neural Network</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs019.html#other-things-to-try" style="font-size: 80%;"><b>Other Things to Try</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs020.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;"><b>Other Types of Recurrent Neural Networks</b></a></li>
</ul>
</li>
@@ -223,44 +209,60 @@ MathJax.Hub.Config({
<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p> <!-- add vertical space -->
<a name="part0009"></a>
<!-- !split -->
<h2 id="building-up-adaboost" class="anchor">Building up AdaBoost </h2>
<h2 id="other-measures-in-classification-studies-cancer-data-again" class="anchor">Other measures in classification studies: Cancer Data again </h2>
<p>First, for any \( \beta > 0 \), we optimize \( G \) by setting</p>
$$
G_m(x) = \mathrm{sign} \sum_{i=0}^{n-1} w_i^m I(y_i \ne G_(x_i)),
$$
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight" style="background: #f8f8f8">
<pre style="line-height: 125%;"><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">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">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> train_test_split
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.datasets</span> <span style="color: #008000; font-weight: bold">import</span> load_breast_cancer
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.linear_model</span> <span style="color: #008000; font-weight: bold">import</span> LogisticRegression
<p>which is the classifier that minimizes the weighted error rate in predicting \( y \).</p>
<span style="color: #408080; font-style: italic"># Load the data</span>
cancer <span style="color: #666666">=</span> load_breast_cancer()
<p>We can do this by rewriting</p>
$$
\exp{-(\beta)}\sum_{y_i=G(x_i)}w_i^m+\exp{(\beta)}\sum_{y_i\ne G(x_i)}w_i^m,
$$
X_train, X_test, y_train, y_test <span style="color: #666666">=</span> train_test_split(cancer<span style="color: #666666">.</span>data,cancer<span style="color: #666666">.</span>target,random_state<span style="color: #666666">=0</span>)
<span style="color: #008000">print</span>(X_train<span style="color: #666666">.</span>shape)
<span style="color: #008000">print</span>(X_test<span style="color: #666666">.</span>shape)
<span style="color: #408080; font-style: italic"># Logistic Regression</span>
logreg <span style="color: #666666">=</span> LogisticRegression(solver<span style="color: #666666">=</span><span style="color: #BA2121">&#39;lbfgs&#39;</span>)
logreg<span style="color: #666666">.</span>fit(X_train, y_train)
<p>which can be rewritten as</p>
$$
(\exp{(\beta)}-\exp{-(\beta)})\sum_{i=0}^{n-1}w_i^mI(y_i\ne G(x_i))+\exp{(-\beta)}\sum_{i=0}^{n-1}w_i^m=0,
$$
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> LabelEncoder
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> cross_validate
<span style="color: #408080; font-style: italic">#Cross validation</span>
accuracy <span style="color: #666666">=</span> cross_validate(logreg,X_test,y_test,cv<span style="color: #666666">=10</span>)[<span style="color: #BA2121">&#39;test_score&#39;</span>]
<span style="color: #008000">print</span>(accuracy)
<span style="color: #008000">print</span>(<span style="color: #BA2121">&quot;Test set accuracy with Logistic Regression: </span><span style="color: #BB6688; font-weight: bold">{:.2f}</span><span style="color: #BA2121">&quot;</span><span style="color: #666666">.</span>format(logreg<span style="color: #666666">.</span>score(X_test,y_test)))
<p>which leads to</p>
$$
\beta_m = \frac{1}{2}\log{\frac{1-\mathrm{\overline{err}}}{\mathrm{\overline{err}}}},
$$
<p>where we have redefined the error as </p>
$$
\mathrm{\overline{err}}_m=\frac{1}{n}\frac{\sum_{i=0}^{n-1}w_i^mI(y_i\ne G(x_i)}{\sum_{i=0}^{n-1}w_i^m},
$$
<p>which leads to an update of</p>
$$
f_m(x) = f_{m-1}(x) +\beta_m G_m(x).
$$
<p>This leads to the new weights</p>
$$
w_i^{m+1} = w_i^m \exp{(-y_i\beta_m G_m(x_i))}
$$
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">scikitplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">skplt</span>
y_pred <span style="color: #666666">=</span> logreg<span style="color: #666666">.</span>predict(X_test)
skplt<span style="color: #666666">.</span>metrics<span style="color: #666666">.</span>plot_confusion_matrix(y_test, y_pred, normalize<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">True</span>)
plt<span style="color: #666666">.</span>show()
y_probas <span style="color: #666666">=</span> logreg<span style="color: #666666">.</span>predict_proba(X_test)
skplt<span style="color: #666666">.</span>metrics<span style="color: #666666">.</span>plot_roc(y_test, y_probas)
plt<span style="color: #666666">.</span>show()
skplt<span style="color: #666666">.</span>metrics<span style="color: #666666">.</span>plot_cumulative_gain(y_test, y_probas)
plt<span style="color: #666666">.</span>show()
</pre>
</div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_subarea output_stream output_stdout output_text">
</div>
</div>
</div>
</div>
</div>
<p>
@@ -287,7 +289,7 @@ $$
<li><a href="._week45-bs017.html">18</a></li>
<li><a href="._week45-bs018.html">19</a></li>
<li><a href="">...</a></li>
<li><a href="._week45-bs039.html">40</a></li>
<li><a href="._week45-bs020.html">21</a></li>
<li><a href="._week45-bs010.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+111 -141
View File
@@ -8,8 +8,8 @@ doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=d
<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 45: Decisions Trees, Random Forests, Bagging and Boosting">
<title>Week 45: Decisions Trees, Random Forests, Bagging and Boosting</title>
<meta name="description" content="Week 45, Recurrent Neural Networks">
<title>Week 45, Recurrent Neural Networks</title>
<!-- Bootstrap style: bootstrap -->
<!-- doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=default --html_admon=bootstrap_panel --html_output=week45-bs --no_mako -->
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
@@ -36,109 +36,101 @@ doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=d
<!-- tocinfo
{'highest level': 2,
'sections': [('Overview of week 45', 2, None, 'overview-of-week-45'),
('Brief code reminder from last wekk',
'sections': [('Plan for week 45', 2, None, 'plan-for-week-45'),
('Material for the lab sessions, additional ways to present '
'classification results and other practicalities',
2,
None,
'brief-code-reminder-from-last-wekk'),
("Boosting, a Bird's Eye View",
'material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities'),
('Searching for Optimal Regularization Parameters $\\lambda$',
2,
None,
'boosting-a-bird-s-eye-view'),
('What is boosting? Additive Modelling/Iterative Fitting',
'searching-for-optimal-regularization-parameters-lambda'),
('Grid Search', 2, None, 'grid-search'),
('Randomized Grid Search', 2, None, 'randomized-grid-search'),
('Wisconsin Cancer Data', 2, None, 'wisconsin-cancer-data'),
('Using the correlation matrix',
2,
None,
'what-is-boosting-additive-modelling-iterative-fitting'),
('Iterative Fitting, Regression and Squared-error Cost Function',
'using-the-correlation-matrix'),
('Discussing the correlation data',
2,
None,
'iterative-fitting-regression-and-squared-error-cost-function'),
('Squared-Error Example and Iterative Fitting',
'discussing-the-correlation-data'),
('Other measures in classification studies: Cancer Data again',
2,
None,
'squared-error-example-and-iterative-fitting'),
('Iterative Fitting, Classification and AdaBoost',
'other-measures-in-classification-studies-cancer-data-again'),
('Material for Lecture Thursday November 9',
2,
None,
'iterative-fitting-classification-and-adaboost'),
('Adaptive Boosting, AdaBoost',
'material-for-lecture-thursday-november-9'),
('Recurrent neural networks (RNNs): Overarching view',
2,
None,
'adaptive-boosting-adaboost'),
('Building up AdaBoost', 2, None, 'building-up-adaboost'),
('Adaptive boosting: AdaBoost, Basic Algorithm',
'recurrent-neural-networks-rnns-overarching-view'),
('A simple example', 2, None, 'a-simple-example'),
('Memoryless models', 3, None, 'memoryless-models'),
('Linear dynamical model', 3, None, 'linear-dynamical-model'),
('Hidden Markov Models', 3, None, 'hidden-markov-models'),
('RNNs', 3, None, 'rnns'),
('Do generative models need to be stochastic?',
3,
None,
'do-generative-models-need-to-be-stochastic'),
('What kinds of behaviour can RNNs exhibit?',
3,
None,
'what-kinds-of-behaviour-can-rnns-exhibit'),
('Basic layout', 2, None, 'basic-layout'),
('We need to specify the initial activity state of all the '
'hidden and output units',
3,
None,
'we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units'),
('We can specify inputs in several ways',
3,
None,
'we-can-specify-inputs-in-several-ways'),
('We can specify targets in several ways',
3,
None,
'we-can-specify-targets-in-several-ways'),
('Backpropagation through time',
3,
None,
'backpropagation-through-time'),
('The backward pass is linear',
3,
None,
'the-backward-pass-is-linear'),
('The problem of exploding or vanishing gradients',
2,
None,
'adaptive-boosting-adaboost-basic-algorithm'),
('Basic Steps of AdaBoost', 2, None, 'basic-steps-of-adaboost'),
('AdaBoost Examples', 2, None, 'adaboost-examples'),
('Gradient boosting: Basics with Steepest Descent/Functional '
'Gradient Descent',
'the-problem-of-exploding-or-vanishing-gradients'),
('Four effective ways to learn an RNN',
2,
None,
'gradient-boosting-basics-with-steepest-descent-functional-gradient-descent'),
('The Squared-Error again! Steepest Descent',
'four-effective-ways-to-learn-an-rnn'),
('Long Short Term Memory (LSTM)',
3,
None,
'long-short-term-memory-lstm'),
('Implementing a memory cell in a neural network',
3,
None,
'implementing-a-memory-cell-in-a-neural-network'),
('An extrapolation example', 2, None, 'an-extrapolation-example'),
('Formatting the Data', 2, None, 'formatting-the-data'),
('Predicting New Points With A Trained Recurrent Neural Network',
2,
None,
'the-squared-error-again-steepest-descent'),
('Steepest Descent Example', 2, None, 'steepest-descent-example'),
('Gradient Boosting, algorithm',
'predicting-new-points-with-a-trained-recurrent-neural-network'),
('Other Things to Try', 2, None, 'other-things-to-try'),
('Other Types of Recurrent Neural Networks',
2,
None,
'gradient-boosting-algorithm'),
('Gradient Boosting, Examples of Regression',
2,
None,
'gradient-boosting-examples-of-regression'),
('Gradient Boosting, Classification Example',
2,
None,
'gradient-boosting-classification-example'),
('XGBoost: Extreme Gradient Boosting',
2,
None,
'xgboost-extreme-gradient-boosting'),
('Regression Case', 2, None, 'regression-case'),
('Xgboost on the Cancer Data',
2,
None,
'xgboost-on-the-cancer-data'),
('Support Vector Machines, overarching aims',
2,
None,
'support-vector-machines-overarching-aims'),
('Hyperplanes and all that', 2, None, 'hyperplanes-and-all-that'),
('What is a hyperplane?', 2, None, 'what-is-a-hyperplane'),
('A $p$-dimensional space of features',
2,
None,
'a-p-dimensional-space-of-features'),
('The two-dimensional case', 2, None, 'the-two-dimensional-case'),
('Getting into the details', 2, None, 'getting-into-the-details'),
('First attempt at a minimization approach',
2,
None,
'first-attempt-at-a-minimization-approach'),
('Solving the equations', 2, None, 'solving-the-equations'),
('Code Example', 2, None, 'code-example'),
('Problems with the Simpler Approach',
2,
None,
'problems-with-the-simpler-approach'),
('A better approach', 2, None, 'a-better-approach'),
('A quick Reminder on Lagrangian Multipliers',
2,
None,
'a-quick-reminder-on-lagrangian-multipliers'),
('Adding the Multiplier', 2, None, 'adding-the-multiplier'),
('Setting up the Problem', 2, None, 'setting-up-the-problem'),
('The problem to solve', 2, None, 'the-problem-to-solve'),
('The last steps', 2, None, 'the-last-steps'),
('A soft classifier', 2, None, 'a-soft-classifier'),
('Soft optmization problem',
2,
None,
'soft-optmization-problem')]}
'other-types-of-recurrent-neural-networks')]}
end of tocinfo -->
<body>
@@ -166,52 +158,46 @@ MathJax.Hub.Config({
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="week45-bs.html">Week 45: Decisions Trees, Random Forests, Bagging and Boosting</a>
<a class="navbar-brand" href="week45-bs.html">Week 45, Recurrent Neural Networks</a>
</div>
<div class="navbar-collapse collapse navbar-responsive-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
<ul class="dropdown-menu">
<!-- navigation toc: --> <li><a href="._week45-bs001.html#overview-of-week-45" style="font-size: 80%;">Overview of week 45</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs002.html#brief-code-reminder-from-last-wekk" style="font-size: 80%;">Brief code reminder from last wekk</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs003.html#boosting-a-bird-s-eye-view" style="font-size: 80%;">Boosting, a Bird's Eye View</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs004.html#what-is-boosting-additive-modelling-iterative-fitting" style="font-size: 80%;">What is boosting? Additive Modelling/Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs005.html#iterative-fitting-regression-and-squared-error-cost-function" style="font-size: 80%;">Iterative Fitting, Regression and Squared-error Cost Function</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs006.html#squared-error-example-and-iterative-fitting" style="font-size: 80%;">Squared-Error Example and Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs007.html#iterative-fitting-classification-and-adaboost" style="font-size: 80%;">Iterative Fitting, Classification and AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs008.html#adaptive-boosting-adaboost" style="font-size: 80%;">Adaptive Boosting, AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs009.html#building-up-adaboost" style="font-size: 80%;">Building up AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="#adaptive-boosting-adaboost-basic-algorithm" style="font-size: 80%;">Adaptive boosting: AdaBoost, Basic Algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs011.html#basic-steps-of-adaboost" style="font-size: 80%;">Basic Steps of AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#adaboost-examples" style="font-size: 80%;">AdaBoost Examples</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#gradient-boosting-basics-with-steepest-descent-functional-gradient-descent" style="font-size: 80%;">Gradient boosting: Basics with Steepest Descent/Functional Gradient Descent</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs014.html#the-squared-error-again-steepest-descent" style="font-size: 80%;">The Squared-Error again! Steepest Descent</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#steepest-descent-example" style="font-size: 80%;">Steepest Descent Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs016.html#gradient-boosting-algorithm" style="font-size: 80%;">Gradient Boosting, algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs017.html#gradient-boosting-examples-of-regression" style="font-size: 80%;">Gradient Boosting, Examples of Regression</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs018.html#gradient-boosting-classification-example" style="font-size: 80%;">Gradient Boosting, Classification Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs019.html#xgboost-extreme-gradient-boosting" style="font-size: 80%;">XGBoost: Extreme Gradient Boosting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs020.html#regression-case" style="font-size: 80%;">Regression Case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs021.html#xgboost-on-the-cancer-data" style="font-size: 80%;">Xgboost on the Cancer Data</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs022.html#support-vector-machines-overarching-aims" style="font-size: 80%;">Support Vector Machines, overarching aims</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs023.html#hyperplanes-and-all-that" style="font-size: 80%;">Hyperplanes and all that</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs024.html#what-is-a-hyperplane" style="font-size: 80%;">What is a hyperplane?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs025.html#a-p-dimensional-space-of-features" style="font-size: 80%;">A \( p \)-dimensional space of features</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs026.html#the-two-dimensional-case" style="font-size: 80%;">The two-dimensional case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs027.html#getting-into-the-details" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs028.html#first-attempt-at-a-minimization-approach" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs029.html#solving-the-equations" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs030.html#code-example" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs031.html#problems-with-the-simpler-approach" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs032.html#a-better-approach" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs033.html#a-quick-reminder-on-lagrangian-multipliers" style="font-size: 80%;">A quick Reminder on Lagrangian Multipliers</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs034.html#adding-the-multiplier" style="font-size: 80%;">Adding the Multiplier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs035.html#setting-up-the-problem" style="font-size: 80%;">Setting up the Problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs036.html#the-problem-to-solve" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs037.html#the-last-steps" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs038.html#a-soft-classifier" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs039.html#soft-optmization-problem" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs001.html#plan-for-week-45" style="font-size: 80%;"><b>Plan for week 45</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs002.html#material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities" style="font-size: 80%;"><b>Material for the lab sessions, additional ways to present classification results and other practicalities</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs003.html#searching-for-optimal-regularization-parameters-lambda" style="font-size: 80%;"><b>Searching for Optimal Regularization Parameters \( \lambda \)</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs004.html#grid-search" style="font-size: 80%;"><b>Grid Search</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs005.html#randomized-grid-search" style="font-size: 80%;"><b>Randomized Grid Search</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs006.html#wisconsin-cancer-data" style="font-size: 80%;"><b>Wisconsin Cancer Data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs007.html#using-the-correlation-matrix" style="font-size: 80%;"><b>Using the correlation matrix</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs008.html#discussing-the-correlation-data" style="font-size: 80%;"><b>Discussing the correlation data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs009.html#other-measures-in-classification-studies-cancer-data-again" style="font-size: 80%;"><b>Other measures in classification studies: Cancer Data again</b></a></li>
<!-- navigation toc: --> <li><a href="#material-for-lecture-thursday-november-9" style="font-size: 80%;"><b>Material for Lecture Thursday November 9</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs011.html#recurrent-neural-networks-rnns-overarching-view" style="font-size: 80%;"><b>Recurrent neural networks (RNNs): Overarching view</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#a-simple-example" style="font-size: 80%;"><b>A simple example</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#memoryless-models" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Memoryless models</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#linear-dynamical-model" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Linear dynamical model</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#hidden-markov-models" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Hidden Markov Models</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#rnns" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;RNNs</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#do-generative-models-need-to-be-stochastic" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Do generative models need to be stochastic?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#what-kinds-of-behaviour-can-rnns-exhibit" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;What kinds of behaviour can RNNs exhibit?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#basic-layout" style="font-size: 80%;"><b>Basic layout</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We need to specify the initial activity state of all the hidden and output units</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-can-specify-inputs-in-several-ways" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We can specify inputs in several ways</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-can-specify-targets-in-several-ways" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We can specify targets in several ways</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#backpropagation-through-time" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Backpropagation through time</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#the-backward-pass-is-linear" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;The backward pass is linear</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs014.html#the-problem-of-exploding-or-vanishing-gradients" style="font-size: 80%;"><b>The problem of exploding or vanishing gradients</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#four-effective-ways-to-learn-an-rnn" style="font-size: 80%;"><b>Four effective ways to learn an RNN</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#long-short-term-memory-lstm" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Long Short Term Memory (LSTM)</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#implementing-a-memory-cell-in-a-neural-network" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Implementing a memory cell in a neural network</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs016.html#an-extrapolation-example" style="font-size: 80%;"><b>An extrapolation example</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs017.html#formatting-the-data" style="font-size: 80%;"><b>Formatting the Data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs018.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;"><b>Predicting New Points With A Trained Recurrent Neural Network</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs019.html#other-things-to-try" style="font-size: 80%;"><b>Other Things to Try</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs020.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;"><b>Other Types of Recurrent Neural Networks</b></a></li>
</ul>
</li>
@@ -223,23 +209,7 @@ MathJax.Hub.Config({
<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p> <!-- add vertical space -->
<a name="part0010"></a>
<!-- !split -->
<h2 id="adaptive-boosting-adaboost-basic-algorithm" class="anchor">Adaptive boosting: AdaBoost, Basic Algorithm </h2>
<p>The algorithm here is rather straightforward. Assume that our weak
classifier is a decision tree and we consider a binary set of outputs
with \( y_i \in \{-1,1\} \) and \( i=0,1,2,\dots,n-1 \) as our set of
observations. Our design matrix is given in terms of the
feature/predictor vectors
\( \boldsymbol{X}=[\boldsymbol{x}_0\boldsymbol{x}_1\dots\boldsymbol{x}_{p-1}] \). Finally, we define also a
classifier determined by our data via a function \( G(x) \). This function tells us how well we are able to classify our outputs/targets \( \boldsymbol{y} \).
</p>
<p>We have already defined the misclassification error \( \mathrm{err} \) as</p>
$$
\mathrm{err}=\frac{1}{n}\sum_{i=0}^{n-1}I(y_i\ne G(x_i)),
$$
<p>where the function \( I() \) is one if we misclassify and zero if we classify correctly. </p>
<h2 id="material-for-lecture-thursday-november-9" class="anchor">Material for Lecture Thursday November 9 </h2>
<p>
<!-- navigation buttons at the bottom of the page -->
@@ -266,7 +236,7 @@ $$
<li><a href="._week45-bs018.html">19</a></li>
<li><a href="._week45-bs019.html">20</a></li>
<li><a href="">...</a></li>
<li><a href="._week45-bs039.html">40</a></li>
<li><a href="._week45-bs020.html">21</a></li>
<li><a href="._week45-bs011.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+126 -152
View File
@@ -8,8 +8,8 @@ doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=d
<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 45: Decisions Trees, Random Forests, Bagging and Boosting">
<title>Week 45: Decisions Trees, Random Forests, Bagging and Boosting</title>
<meta name="description" content="Week 45, Recurrent Neural Networks">
<title>Week 45, Recurrent Neural Networks</title>
<!-- Bootstrap style: bootstrap -->
<!-- doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=default --html_admon=bootstrap_panel --html_output=week45-bs --no_mako -->
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
@@ -36,109 +36,101 @@ doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=d
<!-- tocinfo
{'highest level': 2,
'sections': [('Overview of week 45', 2, None, 'overview-of-week-45'),
('Brief code reminder from last wekk',
'sections': [('Plan for week 45', 2, None, 'plan-for-week-45'),
('Material for the lab sessions, additional ways to present '
'classification results and other practicalities',
2,
None,
'brief-code-reminder-from-last-wekk'),
("Boosting, a Bird's Eye View",
'material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities'),
('Searching for Optimal Regularization Parameters $\\lambda$',
2,
None,
'boosting-a-bird-s-eye-view'),
('What is boosting? Additive Modelling/Iterative Fitting',
'searching-for-optimal-regularization-parameters-lambda'),
('Grid Search', 2, None, 'grid-search'),
('Randomized Grid Search', 2, None, 'randomized-grid-search'),
('Wisconsin Cancer Data', 2, None, 'wisconsin-cancer-data'),
('Using the correlation matrix',
2,
None,
'what-is-boosting-additive-modelling-iterative-fitting'),
('Iterative Fitting, Regression and Squared-error Cost Function',
'using-the-correlation-matrix'),
('Discussing the correlation data',
2,
None,
'iterative-fitting-regression-and-squared-error-cost-function'),
('Squared-Error Example and Iterative Fitting',
'discussing-the-correlation-data'),
('Other measures in classification studies: Cancer Data again',
2,
None,
'squared-error-example-and-iterative-fitting'),
('Iterative Fitting, Classification and AdaBoost',
'other-measures-in-classification-studies-cancer-data-again'),
('Material for Lecture Thursday November 9',
2,
None,
'iterative-fitting-classification-and-adaboost'),
('Adaptive Boosting, AdaBoost',
'material-for-lecture-thursday-november-9'),
('Recurrent neural networks (RNNs): Overarching view',
2,
None,
'adaptive-boosting-adaboost'),
('Building up AdaBoost', 2, None, 'building-up-adaboost'),
('Adaptive boosting: AdaBoost, Basic Algorithm',
'recurrent-neural-networks-rnns-overarching-view'),
('A simple example', 2, None, 'a-simple-example'),
('Memoryless models', 3, None, 'memoryless-models'),
('Linear dynamical model', 3, None, 'linear-dynamical-model'),
('Hidden Markov Models', 3, None, 'hidden-markov-models'),
('RNNs', 3, None, 'rnns'),
('Do generative models need to be stochastic?',
3,
None,
'do-generative-models-need-to-be-stochastic'),
('What kinds of behaviour can RNNs exhibit?',
3,
None,
'what-kinds-of-behaviour-can-rnns-exhibit'),
('Basic layout', 2, None, 'basic-layout'),
('We need to specify the initial activity state of all the '
'hidden and output units',
3,
None,
'we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units'),
('We can specify inputs in several ways',
3,
None,
'we-can-specify-inputs-in-several-ways'),
('We can specify targets in several ways',
3,
None,
'we-can-specify-targets-in-several-ways'),
('Backpropagation through time',
3,
None,
'backpropagation-through-time'),
('The backward pass is linear',
3,
None,
'the-backward-pass-is-linear'),
('The problem of exploding or vanishing gradients',
2,
None,
'adaptive-boosting-adaboost-basic-algorithm'),
('Basic Steps of AdaBoost', 2, None, 'basic-steps-of-adaboost'),
('AdaBoost Examples', 2, None, 'adaboost-examples'),
('Gradient boosting: Basics with Steepest Descent/Functional '
'Gradient Descent',
'the-problem-of-exploding-or-vanishing-gradients'),
('Four effective ways to learn an RNN',
2,
None,
'gradient-boosting-basics-with-steepest-descent-functional-gradient-descent'),
('The Squared-Error again! Steepest Descent',
'four-effective-ways-to-learn-an-rnn'),
('Long Short Term Memory (LSTM)',
3,
None,
'long-short-term-memory-lstm'),
('Implementing a memory cell in a neural network',
3,
None,
'implementing-a-memory-cell-in-a-neural-network'),
('An extrapolation example', 2, None, 'an-extrapolation-example'),
('Formatting the Data', 2, None, 'formatting-the-data'),
('Predicting New Points With A Trained Recurrent Neural Network',
2,
None,
'the-squared-error-again-steepest-descent'),
('Steepest Descent Example', 2, None, 'steepest-descent-example'),
('Gradient Boosting, algorithm',
'predicting-new-points-with-a-trained-recurrent-neural-network'),
('Other Things to Try', 2, None, 'other-things-to-try'),
('Other Types of Recurrent Neural Networks',
2,
None,
'gradient-boosting-algorithm'),
('Gradient Boosting, Examples of Regression',
2,
None,
'gradient-boosting-examples-of-regression'),
('Gradient Boosting, Classification Example',
2,
None,
'gradient-boosting-classification-example'),
('XGBoost: Extreme Gradient Boosting',
2,
None,
'xgboost-extreme-gradient-boosting'),
('Regression Case', 2, None, 'regression-case'),
('Xgboost on the Cancer Data',
2,
None,
'xgboost-on-the-cancer-data'),
('Support Vector Machines, overarching aims',
2,
None,
'support-vector-machines-overarching-aims'),
('Hyperplanes and all that', 2, None, 'hyperplanes-and-all-that'),
('What is a hyperplane?', 2, None, 'what-is-a-hyperplane'),
('A $p$-dimensional space of features',
2,
None,
'a-p-dimensional-space-of-features'),
('The two-dimensional case', 2, None, 'the-two-dimensional-case'),
('Getting into the details', 2, None, 'getting-into-the-details'),
('First attempt at a minimization approach',
2,
None,
'first-attempt-at-a-minimization-approach'),
('Solving the equations', 2, None, 'solving-the-equations'),
('Code Example', 2, None, 'code-example'),
('Problems with the Simpler Approach',
2,
None,
'problems-with-the-simpler-approach'),
('A better approach', 2, None, 'a-better-approach'),
('A quick Reminder on Lagrangian Multipliers',
2,
None,
'a-quick-reminder-on-lagrangian-multipliers'),
('Adding the Multiplier', 2, None, 'adding-the-multiplier'),
('Setting up the Problem', 2, None, 'setting-up-the-problem'),
('The problem to solve', 2, None, 'the-problem-to-solve'),
('The last steps', 2, None, 'the-last-steps'),
('A soft classifier', 2, None, 'a-soft-classifier'),
('Soft optmization problem',
2,
None,
'soft-optmization-problem')]}
'other-types-of-recurrent-neural-networks')]}
end of tocinfo -->
<body>
@@ -166,52 +158,46 @@ MathJax.Hub.Config({
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="week45-bs.html">Week 45: Decisions Trees, Random Forests, Bagging and Boosting</a>
<a class="navbar-brand" href="week45-bs.html">Week 45, Recurrent Neural Networks</a>
</div>
<div class="navbar-collapse collapse navbar-responsive-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
<ul class="dropdown-menu">
<!-- navigation toc: --> <li><a href="._week45-bs001.html#overview-of-week-45" style="font-size: 80%;">Overview of week 45</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs002.html#brief-code-reminder-from-last-wekk" style="font-size: 80%;">Brief code reminder from last wekk</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs003.html#boosting-a-bird-s-eye-view" style="font-size: 80%;">Boosting, a Bird's Eye View</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs004.html#what-is-boosting-additive-modelling-iterative-fitting" style="font-size: 80%;">What is boosting? Additive Modelling/Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs005.html#iterative-fitting-regression-and-squared-error-cost-function" style="font-size: 80%;">Iterative Fitting, Regression and Squared-error Cost Function</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs006.html#squared-error-example-and-iterative-fitting" style="font-size: 80%;">Squared-Error Example and Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs007.html#iterative-fitting-classification-and-adaboost" style="font-size: 80%;">Iterative Fitting, Classification and AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs008.html#adaptive-boosting-adaboost" style="font-size: 80%;">Adaptive Boosting, AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs009.html#building-up-adaboost" style="font-size: 80%;">Building up AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs010.html#adaptive-boosting-adaboost-basic-algorithm" style="font-size: 80%;">Adaptive boosting: AdaBoost, Basic Algorithm</a></li>
<!-- navigation toc: --> <li><a href="#basic-steps-of-adaboost" style="font-size: 80%;">Basic Steps of AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#adaboost-examples" style="font-size: 80%;">AdaBoost Examples</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#gradient-boosting-basics-with-steepest-descent-functional-gradient-descent" style="font-size: 80%;">Gradient boosting: Basics with Steepest Descent/Functional Gradient Descent</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs014.html#the-squared-error-again-steepest-descent" style="font-size: 80%;">The Squared-Error again! Steepest Descent</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#steepest-descent-example" style="font-size: 80%;">Steepest Descent Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs016.html#gradient-boosting-algorithm" style="font-size: 80%;">Gradient Boosting, algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs017.html#gradient-boosting-examples-of-regression" style="font-size: 80%;">Gradient Boosting, Examples of Regression</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs018.html#gradient-boosting-classification-example" style="font-size: 80%;">Gradient Boosting, Classification Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs019.html#xgboost-extreme-gradient-boosting" style="font-size: 80%;">XGBoost: Extreme Gradient Boosting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs020.html#regression-case" style="font-size: 80%;">Regression Case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs021.html#xgboost-on-the-cancer-data" style="font-size: 80%;">Xgboost on the Cancer Data</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs022.html#support-vector-machines-overarching-aims" style="font-size: 80%;">Support Vector Machines, overarching aims</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs023.html#hyperplanes-and-all-that" style="font-size: 80%;">Hyperplanes and all that</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs024.html#what-is-a-hyperplane" style="font-size: 80%;">What is a hyperplane?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs025.html#a-p-dimensional-space-of-features" style="font-size: 80%;">A \( p \)-dimensional space of features</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs026.html#the-two-dimensional-case" style="font-size: 80%;">The two-dimensional case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs027.html#getting-into-the-details" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs028.html#first-attempt-at-a-minimization-approach" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs029.html#solving-the-equations" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs030.html#code-example" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs031.html#problems-with-the-simpler-approach" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs032.html#a-better-approach" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs033.html#a-quick-reminder-on-lagrangian-multipliers" style="font-size: 80%;">A quick Reminder on Lagrangian Multipliers</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs034.html#adding-the-multiplier" style="font-size: 80%;">Adding the Multiplier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs035.html#setting-up-the-problem" style="font-size: 80%;">Setting up the Problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs036.html#the-problem-to-solve" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs037.html#the-last-steps" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs038.html#a-soft-classifier" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs039.html#soft-optmization-problem" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs001.html#plan-for-week-45" style="font-size: 80%;"><b>Plan for week 45</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs002.html#material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities" style="font-size: 80%;"><b>Material for the lab sessions, additional ways to present classification results and other practicalities</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs003.html#searching-for-optimal-regularization-parameters-lambda" style="font-size: 80%;"><b>Searching for Optimal Regularization Parameters \( \lambda \)</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs004.html#grid-search" style="font-size: 80%;"><b>Grid Search</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs005.html#randomized-grid-search" style="font-size: 80%;"><b>Randomized Grid Search</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs006.html#wisconsin-cancer-data" style="font-size: 80%;"><b>Wisconsin Cancer Data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs007.html#using-the-correlation-matrix" style="font-size: 80%;"><b>Using the correlation matrix</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs008.html#discussing-the-correlation-data" style="font-size: 80%;"><b>Discussing the correlation data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs009.html#other-measures-in-classification-studies-cancer-data-again" style="font-size: 80%;"><b>Other measures in classification studies: Cancer Data again</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs010.html#material-for-lecture-thursday-november-9" style="font-size: 80%;"><b>Material for Lecture Thursday November 9</b></a></li>
<!-- navigation toc: --> <li><a href="#recurrent-neural-networks-rnns-overarching-view" style="font-size: 80%;"><b>Recurrent neural networks (RNNs): Overarching view</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#a-simple-example" style="font-size: 80%;"><b>A simple example</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#memoryless-models" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Memoryless models</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#linear-dynamical-model" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Linear dynamical model</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#hidden-markov-models" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Hidden Markov Models</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#rnns" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;RNNs</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#do-generative-models-need-to-be-stochastic" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Do generative models need to be stochastic?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#what-kinds-of-behaviour-can-rnns-exhibit" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;What kinds of behaviour can RNNs exhibit?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#basic-layout" style="font-size: 80%;"><b>Basic layout</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We need to specify the initial activity state of all the hidden and output units</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-can-specify-inputs-in-several-ways" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We can specify inputs in several ways</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-can-specify-targets-in-several-ways" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We can specify targets in several ways</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#backpropagation-through-time" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Backpropagation through time</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#the-backward-pass-is-linear" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;The backward pass is linear</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs014.html#the-problem-of-exploding-or-vanishing-gradients" style="font-size: 80%;"><b>The problem of exploding or vanishing gradients</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#four-effective-ways-to-learn-an-rnn" style="font-size: 80%;"><b>Four effective ways to learn an RNN</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#long-short-term-memory-lstm" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Long Short Term Memory (LSTM)</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#implementing-a-memory-cell-in-a-neural-network" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Implementing a memory cell in a neural network</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs016.html#an-extrapolation-example" style="font-size: 80%;"><b>An extrapolation example</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs017.html#formatting-the-data" style="font-size: 80%;"><b>Formatting the Data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs018.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;"><b>Predicting New Points With A Trained Recurrent Neural Network</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs019.html#other-things-to-try" style="font-size: 80%;"><b>Other Things to Try</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs020.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;"><b>Other Types of Recurrent Neural Networks</b></a></li>
</ul>
</li>
@@ -223,36 +209,26 @@ MathJax.Hub.Config({
<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p> <!-- add vertical space -->
<a name="part0011"></a>
<!-- !split -->
<h2 id="basic-steps-of-adaboost" class="anchor">Basic Steps of AdaBoost </h2>
<h2 id="recurrent-neural-networks-rnns-overarching-view" class="anchor">Recurrent neural networks (RNNs): Overarching view </h2>
<p>With the above definitions we are now ready to set up the algorithm for AdaBoost.
The basic idea is to set up weights which will be used to scale the correctly classified and the misclassified cases.
<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.
</p>
<ol>
<li> We start by initializing all weights to \( w_i = 1/n \), with \( i=0,1,2,\dots n-1 \). It is easy to see that we must have \( \sum_{i=0}^{n-1}w_i = 1 \).</li>
<li> We rewrite the misclassification error as</li>
</ol>
$$
\mathrm{\overline{err}}_m=\frac{\sum_{i=0}^{n-1}w_i^m I(y_i\ne G(x_i))}{\sum_{i=0}^{n-1}w_i},
$$
<ol>
<li> Then we start looping over all attempts at classifying, namely we start an iterative process for \( m=1:M \), where \( M \) is the final number of classifications. Our given classifier could for example be a plain decision tree.
<ol type="a"></li>
<li> Fit then a given classifier to the training set using the weights \( w_i \).</li>
<li> Compute then \( \mathrm{err} \) and figure out which events are classified properly and which are classified wrongly.</li>
<li> Define a quantity \( \alpha_{m} = \log{(1-\mathrm{\overline{err}}_m)/\mathrm{\overline{err}}_m} \)</li>
<li> Set the new weights to \( w_i = w_i\times \exp{(\alpha_m I(y_i\ne G(x_i)} \).</li>
</ol>
<li> Compute the new classifier \( G(x)= \sum_{i=0}^{n-1}\alpha_m I(y_i\ne G(x_i) \).</li>
</ol>
<p>For the iterations with \( m \le 2 \) the weights are modified
individually at each steps. The observations which were misclassified
at iteration \( m-1 \) have a weight which is larger than those which were
classified properly. As this proceeds, the observations which were
difficult to classifiy correctly are given a larger influence. Each
new classification step \( m \) is then forced to concentrate on those
observations that are missed in the previous iterations.
<p>A recurrent neural network (RNN) looks very much like a feedforward
neural network, except that it also has connections pointing
backward.
</p>
<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>
<p>
@@ -279,8 +255,6 @@ observations that are missed in the previous iterations.
<li><a href="._week45-bs018.html">19</a></li>
<li><a href="._week45-bs019.html">20</a></li>
<li><a href="._week45-bs020.html">21</a></li>
<li><a href="">...</a></li>
<li><a href="._week45-bs039.html">40</a></li>
<li><a href="._week45-bs012.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+274 -141
View File
@@ -8,8 +8,8 @@ doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=d
<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 45: Decisions Trees, Random Forests, Bagging and Boosting">
<title>Week 45: Decisions Trees, Random Forests, Bagging and Boosting</title>
<meta name="description" content="Week 45, Recurrent Neural Networks">
<title>Week 45, Recurrent Neural Networks</title>
<!-- Bootstrap style: bootstrap -->
<!-- doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=default --html_admon=bootstrap_panel --html_output=week45-bs --no_mako -->
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
@@ -36,109 +36,101 @@ doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=d
<!-- tocinfo
{'highest level': 2,
'sections': [('Overview of week 45', 2, None, 'overview-of-week-45'),
('Brief code reminder from last wekk',
'sections': [('Plan for week 45', 2, None, 'plan-for-week-45'),
('Material for the lab sessions, additional ways to present '
'classification results and other practicalities',
2,
None,
'brief-code-reminder-from-last-wekk'),
("Boosting, a Bird's Eye View",
'material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities'),
('Searching for Optimal Regularization Parameters $\\lambda$',
2,
None,
'boosting-a-bird-s-eye-view'),
('What is boosting? Additive Modelling/Iterative Fitting',
'searching-for-optimal-regularization-parameters-lambda'),
('Grid Search', 2, None, 'grid-search'),
('Randomized Grid Search', 2, None, 'randomized-grid-search'),
('Wisconsin Cancer Data', 2, None, 'wisconsin-cancer-data'),
('Using the correlation matrix',
2,
None,
'what-is-boosting-additive-modelling-iterative-fitting'),
('Iterative Fitting, Regression and Squared-error Cost Function',
'using-the-correlation-matrix'),
('Discussing the correlation data',
2,
None,
'iterative-fitting-regression-and-squared-error-cost-function'),
('Squared-Error Example and Iterative Fitting',
'discussing-the-correlation-data'),
('Other measures in classification studies: Cancer Data again',
2,
None,
'squared-error-example-and-iterative-fitting'),
('Iterative Fitting, Classification and AdaBoost',
'other-measures-in-classification-studies-cancer-data-again'),
('Material for Lecture Thursday November 9',
2,
None,
'iterative-fitting-classification-and-adaboost'),
('Adaptive Boosting, AdaBoost',
'material-for-lecture-thursday-november-9'),
('Recurrent neural networks (RNNs): Overarching view',
2,
None,
'adaptive-boosting-adaboost'),
('Building up AdaBoost', 2, None, 'building-up-adaboost'),
('Adaptive boosting: AdaBoost, Basic Algorithm',
'recurrent-neural-networks-rnns-overarching-view'),
('A simple example', 2, None, 'a-simple-example'),
('Memoryless models', 3, None, 'memoryless-models'),
('Linear dynamical model', 3, None, 'linear-dynamical-model'),
('Hidden Markov Models', 3, None, 'hidden-markov-models'),
('RNNs', 3, None, 'rnns'),
('Do generative models need to be stochastic?',
3,
None,
'do-generative-models-need-to-be-stochastic'),
('What kinds of behaviour can RNNs exhibit?',
3,
None,
'what-kinds-of-behaviour-can-rnns-exhibit'),
('Basic layout', 2, None, 'basic-layout'),
('We need to specify the initial activity state of all the '
'hidden and output units',
3,
None,
'we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units'),
('We can specify inputs in several ways',
3,
None,
'we-can-specify-inputs-in-several-ways'),
('We can specify targets in several ways',
3,
None,
'we-can-specify-targets-in-several-ways'),
('Backpropagation through time',
3,
None,
'backpropagation-through-time'),
('The backward pass is linear',
3,
None,
'the-backward-pass-is-linear'),
('The problem of exploding or vanishing gradients',
2,
None,
'adaptive-boosting-adaboost-basic-algorithm'),
('Basic Steps of AdaBoost', 2, None, 'basic-steps-of-adaboost'),
('AdaBoost Examples', 2, None, 'adaboost-examples'),
('Gradient boosting: Basics with Steepest Descent/Functional '
'Gradient Descent',
'the-problem-of-exploding-or-vanishing-gradients'),
('Four effective ways to learn an RNN',
2,
None,
'gradient-boosting-basics-with-steepest-descent-functional-gradient-descent'),
('The Squared-Error again! Steepest Descent',
'four-effective-ways-to-learn-an-rnn'),
('Long Short Term Memory (LSTM)',
3,
None,
'long-short-term-memory-lstm'),
('Implementing a memory cell in a neural network',
3,
None,
'implementing-a-memory-cell-in-a-neural-network'),
('An extrapolation example', 2, None, 'an-extrapolation-example'),
('Formatting the Data', 2, None, 'formatting-the-data'),
('Predicting New Points With A Trained Recurrent Neural Network',
2,
None,
'the-squared-error-again-steepest-descent'),
('Steepest Descent Example', 2, None, 'steepest-descent-example'),
('Gradient Boosting, algorithm',
'predicting-new-points-with-a-trained-recurrent-neural-network'),
('Other Things to Try', 2, None, 'other-things-to-try'),
('Other Types of Recurrent Neural Networks',
2,
None,
'gradient-boosting-algorithm'),
('Gradient Boosting, Examples of Regression',
2,
None,
'gradient-boosting-examples-of-regression'),
('Gradient Boosting, Classification Example',
2,
None,
'gradient-boosting-classification-example'),
('XGBoost: Extreme Gradient Boosting',
2,
None,
'xgboost-extreme-gradient-boosting'),
('Regression Case', 2, None, 'regression-case'),
('Xgboost on the Cancer Data',
2,
None,
'xgboost-on-the-cancer-data'),
('Support Vector Machines, overarching aims',
2,
None,
'support-vector-machines-overarching-aims'),
('Hyperplanes and all that', 2, None, 'hyperplanes-and-all-that'),
('What is a hyperplane?', 2, None, 'what-is-a-hyperplane'),
('A $p$-dimensional space of features',
2,
None,
'a-p-dimensional-space-of-features'),
('The two-dimensional case', 2, None, 'the-two-dimensional-case'),
('Getting into the details', 2, None, 'getting-into-the-details'),
('First attempt at a minimization approach',
2,
None,
'first-attempt-at-a-minimization-approach'),
('Solving the equations', 2, None, 'solving-the-equations'),
('Code Example', 2, None, 'code-example'),
('Problems with the Simpler Approach',
2,
None,
'problems-with-the-simpler-approach'),
('A better approach', 2, None, 'a-better-approach'),
('A quick Reminder on Lagrangian Multipliers',
2,
None,
'a-quick-reminder-on-lagrangian-multipliers'),
('Adding the Multiplier', 2, None, 'adding-the-multiplier'),
('Setting up the Problem', 2, None, 'setting-up-the-problem'),
('The problem to solve', 2, None, 'the-problem-to-solve'),
('The last steps', 2, None, 'the-last-steps'),
('A soft classifier', 2, None, 'a-soft-classifier'),
('Soft optmization problem',
2,
None,
'soft-optmization-problem')]}
'other-types-of-recurrent-neural-networks')]}
end of tocinfo -->
<body>
@@ -166,52 +158,46 @@ MathJax.Hub.Config({
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="week45-bs.html">Week 45: Decisions Trees, Random Forests, Bagging and Boosting</a>
<a class="navbar-brand" href="week45-bs.html">Week 45, Recurrent Neural Networks</a>
</div>
<div class="navbar-collapse collapse navbar-responsive-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
<ul class="dropdown-menu">
<!-- navigation toc: --> <li><a href="._week45-bs001.html#overview-of-week-45" style="font-size: 80%;">Overview of week 45</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs002.html#brief-code-reminder-from-last-wekk" style="font-size: 80%;">Brief code reminder from last wekk</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs003.html#boosting-a-bird-s-eye-view" style="font-size: 80%;">Boosting, a Bird's Eye View</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs004.html#what-is-boosting-additive-modelling-iterative-fitting" style="font-size: 80%;">What is boosting? Additive Modelling/Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs005.html#iterative-fitting-regression-and-squared-error-cost-function" style="font-size: 80%;">Iterative Fitting, Regression and Squared-error Cost Function</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs006.html#squared-error-example-and-iterative-fitting" style="font-size: 80%;">Squared-Error Example and Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs007.html#iterative-fitting-classification-and-adaboost" style="font-size: 80%;">Iterative Fitting, Classification and AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs008.html#adaptive-boosting-adaboost" style="font-size: 80%;">Adaptive Boosting, AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs009.html#building-up-adaboost" style="font-size: 80%;">Building up AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs010.html#adaptive-boosting-adaboost-basic-algorithm" style="font-size: 80%;">Adaptive boosting: AdaBoost, Basic Algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs011.html#basic-steps-of-adaboost" style="font-size: 80%;">Basic Steps of AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="#adaboost-examples" style="font-size: 80%;">AdaBoost Examples</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#gradient-boosting-basics-with-steepest-descent-functional-gradient-descent" style="font-size: 80%;">Gradient boosting: Basics with Steepest Descent/Functional Gradient Descent</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs014.html#the-squared-error-again-steepest-descent" style="font-size: 80%;">The Squared-Error again! Steepest Descent</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#steepest-descent-example" style="font-size: 80%;">Steepest Descent Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs016.html#gradient-boosting-algorithm" style="font-size: 80%;">Gradient Boosting, algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs017.html#gradient-boosting-examples-of-regression" style="font-size: 80%;">Gradient Boosting, Examples of Regression</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs018.html#gradient-boosting-classification-example" style="font-size: 80%;">Gradient Boosting, Classification Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs019.html#xgboost-extreme-gradient-boosting" style="font-size: 80%;">XGBoost: Extreme Gradient Boosting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs020.html#regression-case" style="font-size: 80%;">Regression Case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs021.html#xgboost-on-the-cancer-data" style="font-size: 80%;">Xgboost on the Cancer Data</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs022.html#support-vector-machines-overarching-aims" style="font-size: 80%;">Support Vector Machines, overarching aims</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs023.html#hyperplanes-and-all-that" style="font-size: 80%;">Hyperplanes and all that</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs024.html#what-is-a-hyperplane" style="font-size: 80%;">What is a hyperplane?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs025.html#a-p-dimensional-space-of-features" style="font-size: 80%;">A \( p \)-dimensional space of features</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs026.html#the-two-dimensional-case" style="font-size: 80%;">The two-dimensional case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs027.html#getting-into-the-details" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs028.html#first-attempt-at-a-minimization-approach" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs029.html#solving-the-equations" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs030.html#code-example" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs031.html#problems-with-the-simpler-approach" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs032.html#a-better-approach" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs033.html#a-quick-reminder-on-lagrangian-multipliers" style="font-size: 80%;">A quick Reminder on Lagrangian Multipliers</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs034.html#adding-the-multiplier" style="font-size: 80%;">Adding the Multiplier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs035.html#setting-up-the-problem" style="font-size: 80%;">Setting up the Problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs036.html#the-problem-to-solve" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs037.html#the-last-steps" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs038.html#a-soft-classifier" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs039.html#soft-optmization-problem" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs001.html#plan-for-week-45" style="font-size: 80%;"><b>Plan for week 45</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs002.html#material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities" style="font-size: 80%;"><b>Material for the lab sessions, additional ways to present classification results and other practicalities</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs003.html#searching-for-optimal-regularization-parameters-lambda" style="font-size: 80%;"><b>Searching for Optimal Regularization Parameters \( \lambda \)</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs004.html#grid-search" style="font-size: 80%;"><b>Grid Search</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs005.html#randomized-grid-search" style="font-size: 80%;"><b>Randomized Grid Search</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs006.html#wisconsin-cancer-data" style="font-size: 80%;"><b>Wisconsin Cancer Data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs007.html#using-the-correlation-matrix" style="font-size: 80%;"><b>Using the correlation matrix</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs008.html#discussing-the-correlation-data" style="font-size: 80%;"><b>Discussing the correlation data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs009.html#other-measures-in-classification-studies-cancer-data-again" style="font-size: 80%;"><b>Other measures in classification studies: Cancer Data again</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs010.html#material-for-lecture-thursday-november-9" style="font-size: 80%;"><b>Material for Lecture Thursday November 9</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs011.html#recurrent-neural-networks-rnns-overarching-view" style="font-size: 80%;"><b>Recurrent neural networks (RNNs): Overarching view</b></a></li>
<!-- navigation toc: --> <li><a href="#a-simple-example" style="font-size: 80%;"><b>A simple example</b></a></li>
<!-- navigation toc: --> <li><a href="#memoryless-models" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Memoryless models</a></li>
<!-- navigation toc: --> <li><a href="#linear-dynamical-model" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Linear dynamical model</a></li>
<!-- navigation toc: --> <li><a href="#hidden-markov-models" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Hidden Markov Models</a></li>
<!-- navigation toc: --> <li><a href="#rnns" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;RNNs</a></li>
<!-- navigation toc: --> <li><a href="#do-generative-models-need-to-be-stochastic" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Do generative models need to be stochastic?</a></li>
<!-- navigation toc: --> <li><a href="#what-kinds-of-behaviour-can-rnns-exhibit" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;What kinds of behaviour can RNNs exhibit?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#basic-layout" style="font-size: 80%;"><b>Basic layout</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We need to specify the initial activity state of all the hidden and output units</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-can-specify-inputs-in-several-ways" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We can specify inputs in several ways</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-can-specify-targets-in-several-ways" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We can specify targets in several ways</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#backpropagation-through-time" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Backpropagation through time</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#the-backward-pass-is-linear" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;The backward pass is linear</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs014.html#the-problem-of-exploding-or-vanishing-gradients" style="font-size: 80%;"><b>The problem of exploding or vanishing gradients</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#four-effective-ways-to-learn-an-rnn" style="font-size: 80%;"><b>Four effective ways to learn an RNN</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#long-short-term-memory-lstm" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Long Short Term Memory (LSTM)</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#implementing-a-memory-cell-in-a-neural-network" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Implementing a memory cell in a neural network</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs016.html#an-extrapolation-example" style="font-size: 80%;"><b>An extrapolation example</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs017.html#formatting-the-data" style="font-size: 80%;"><b>Formatting the Data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs018.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;"><b>Predicting New Points With A Trained Recurrent Neural Network</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs019.html#other-things-to-try" style="font-size: 80%;"><b>Other Things to Try</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs020.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;"><b>Other Types of Recurrent Neural Networks</b></a></li>
</ul>
</li>
@@ -223,9 +209,7 @@ MathJax.Hub.Config({
<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p> <!-- add vertical space -->
<a name="part0012"></a>
<!-- !split -->
<h2 id="adaboost-examples" class="anchor">AdaBoost Examples </h2>
<p>Using <b>Scikit-Learn</b> it is easy to apply the adaptive boosting algorithm, as done here.</p>
<h2 id="a-simple-example" class="anchor">A simple example </h2>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
@@ -234,19 +218,67 @@ MathJax.Hub.Config({
<div class="inner_cell">
<div class="input_area">
<div class="highlight" style="background: #f8f8f8">
<pre style="line-height: 125%;"><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.ensemble</span> <span style="color: #008000; font-weight: bold">import</span> AdaBoostClassifier
<pre style="line-height: 125%;"><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
ada_clf <span style="color: #666666">=</span> AdaBoostClassifier(
DecisionTreeClassifier(max_depth<span style="color: #666666">=2</span>), n_estimators<span style="color: #666666">=200</span>,
algorithm<span style="color: #666666">=</span><span style="color: #BA2121">&quot;SAMME.R&quot;</span>, learning_rate<span style="color: #666666">=0.01</span>, random_state<span style="color: #666666">=42</span>)
ada_clf<span style="color: #666666">.</span>fit(X_train, y_train)
y_pred <span style="color: #666666">=</span> ada_clf<span style="color: #666666">.</span>predict(X_test)
skplt<span style="color: #666666">.</span>metrics<span style="color: #666666">.</span>plot_confusion_matrix(y_test, y_pred, normalize<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">True</span>)
plt<span style="color: #666666">.</span>show()
y_probas <span style="color: #666666">=</span> ada_clf<span style="color: #666666">.</span>predict_proba(X_test)
skplt<span style="color: #666666">.</span>metrics<span style="color: #666666">.</span>plot_roc(y_test, y_probas)
plt<span style="color: #666666">.</span>show()
skplt<span style="color: #666666">.</span>metrics<span style="color: #666666">.</span>plot_cumulative_gain(y_test, y_probas)
<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()
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">&quot;relu&quot;</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">&quot;relu&quot;</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">&#39;mean_squared_error&#39;</span>, optimizer<span style="color: #666666">=</span><span style="color: #BA2121">&#39;rmsprop&#39;</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)
plt<span style="color: #666666">.</span>plot(df)
plt<span style="color: #666666">.</span>plot(predicted)
plt<span style="color: #666666">.</span>show()
</pre>
</div>
@@ -262,7 +294,111 @@ plt<span style="color: #666666">.</span>show()
</div>
</div>
</div>
<h3 id="memoryless-models" class="anchor">Memoryless models </h3>
<p>Autoregressive models Predict the next term in a sequence from a fixed number of previous terms using <b>delay taps</b>.</p>
<div class="panel panel-default">
<div class="panel-body">
<!-- subsequent paragraphs come in larger fonts, so start with a paragraph -->
<p>These generalize autoregressive
models by using one or more
layers of non-linear hidden units.
</p>
</div>
</div>
<p>If we give our generative model some hidden state, and if we give
this hidden state its own internal dynamics, we get a much more
interesting kind of model.
</p>
<ol>
<li> It can store information in its hidden state for a long time.</li>
<li> If the dynamics is noisy and the way it generates outputs from its hidden state is noisy, we can never know its exact hidden state.</li>
<li> The best we can do is to infer a probability distribution over the</li>
</ol>
<p>space of hidden state vectors.</p>
<p>This inference is only tractable for two types of hidden state model.</p>
<h3 id="linear-dynamical-model" class="anchor">Linear dynamical model </h3>
<p>If we give our generative model some hidden state, and if we give
this hidden state its own internal dynamics, we get a much more
interesting kind of model.
</p>
<ol>
<li> It can store information in its hidden state for a long time.
<ol type="a"></li>
<li> If the dynamics is noisy and the way it generates outputs from its hidden state is noisy, we can never know its exact hidden state.</li>
</ol>
<li> The best we can do is to infer a probability distribution over the space of hidden state vectors.</li>
</ol>
<h3 id="hidden-markov-models" class="anchor">Hidden Markov Models </h3>
<p>Hidden Markov Models have a discrete oneof-\( N \) hidden state. Transitions between states
are stochastic and controlled by a transition
matrix. The outputs produced by a state are
stochastic.
</p>
<ul>
<li> We cannot be sure which state produced a given output. So the state is &#8220;hidden&#8221;.</li>
<li> It is easy to represent a probability distribution across N states with N numbers.</li>
<li> To predict the next output we need to infer the probability distribution over hidden states.</li>
</ul>
<p>HMMs have efficient algorithms for inference and learning</p>
<h3 id="rnns" class="anchor">RNNs </h3>
<p>RNNs are very powerful, because they
combine two properties:
</p>
<ol>
<li> Distributed hidden state that allows them to store a lot of information about the past efficiently.</li>
<li> Non-linear dynamics that allows them to update their hidden state in complicated ways.</li>
</ol>
<p>With enough neurons and time, RNNs
can compute anything that can be
computed by your computer.
</p>
<h3 id="do-generative-models-need-to-be-stochastic" class="anchor">Do generative models need to be stochastic? </h3>
<div class="panel panel-default">
<div class="panel-body">
<!-- subsequent paragraphs come in larger fonts, so start with a paragraph -->
<p>But the posterior probability
distribution over their
hidden states given the
observed data so far is a
deterministic function of the
data.
</p>
</div>
</div>
<div class="panel panel-default">
<div class="panel-body">
<!-- subsequent paragraphs come in larger fonts, so start with a paragraph -->
<p>Think of the hidden state
of an RNN as the
equivalent of the
deterministic probability
distribution over hidden
states in a linear dynamical
system or hidden Markov
model.
</p>
</div>
</div>
<h3 id="what-kinds-of-behaviour-can-rnns-exhibit" class="anchor">What kinds of behaviour can RNNs exhibit? </h3>
<ol>
<li> They can oscillate.</li>
<li> They can settle to point attractors.</li>
<li> They can behave chaotically.</li>
<li> RNNs could potentially learn to implement lots of small programs that each capture a nugget of knowledge and run in parallel, interacting to produce very complicated effects.</li>
</ol>
<p>But the computational power of RNNs makes them very hard to train.</p>
<p>
<!-- navigation buttons at the bottom of the page -->
@@ -287,9 +423,6 @@ plt<span style="color: #666666">.</span>show()
<li><a href="._week45-bs018.html">19</a></li>
<li><a href="._week45-bs019.html">20</a></li>
<li><a href="._week45-bs020.html">21</a></li>
<li><a href="._week45-bs021.html">22</a></li>
<li><a href="">...</a></li>
<li><a href="._week45-bs039.html">40</a></li>
<li><a href="._week45-bs013.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+239 -135
View File
@@ -8,8 +8,8 @@ doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=d
<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 45: Decisions Trees, Random Forests, Bagging and Boosting">
<title>Week 45: Decisions Trees, Random Forests, Bagging and Boosting</title>
<meta name="description" content="Week 45, Recurrent Neural Networks">
<title>Week 45, Recurrent Neural Networks</title>
<!-- Bootstrap style: bootstrap -->
<!-- doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=default --html_admon=bootstrap_panel --html_output=week45-bs --no_mako -->
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
@@ -36,109 +36,101 @@ doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=d
<!-- tocinfo
{'highest level': 2,
'sections': [('Overview of week 45', 2, None, 'overview-of-week-45'),
('Brief code reminder from last wekk',
'sections': [('Plan for week 45', 2, None, 'plan-for-week-45'),
('Material for the lab sessions, additional ways to present '
'classification results and other practicalities',
2,
None,
'brief-code-reminder-from-last-wekk'),
("Boosting, a Bird's Eye View",
'material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities'),
('Searching for Optimal Regularization Parameters $\\lambda$',
2,
None,
'boosting-a-bird-s-eye-view'),
('What is boosting? Additive Modelling/Iterative Fitting',
'searching-for-optimal-regularization-parameters-lambda'),
('Grid Search', 2, None, 'grid-search'),
('Randomized Grid Search', 2, None, 'randomized-grid-search'),
('Wisconsin Cancer Data', 2, None, 'wisconsin-cancer-data'),
('Using the correlation matrix',
2,
None,
'what-is-boosting-additive-modelling-iterative-fitting'),
('Iterative Fitting, Regression and Squared-error Cost Function',
'using-the-correlation-matrix'),
('Discussing the correlation data',
2,
None,
'iterative-fitting-regression-and-squared-error-cost-function'),
('Squared-Error Example and Iterative Fitting',
'discussing-the-correlation-data'),
('Other measures in classification studies: Cancer Data again',
2,
None,
'squared-error-example-and-iterative-fitting'),
('Iterative Fitting, Classification and AdaBoost',
'other-measures-in-classification-studies-cancer-data-again'),
('Material for Lecture Thursday November 9',
2,
None,
'iterative-fitting-classification-and-adaboost'),
('Adaptive Boosting, AdaBoost',
'material-for-lecture-thursday-november-9'),
('Recurrent neural networks (RNNs): Overarching view',
2,
None,
'adaptive-boosting-adaboost'),
('Building up AdaBoost', 2, None, 'building-up-adaboost'),
('Adaptive boosting: AdaBoost, Basic Algorithm',
'recurrent-neural-networks-rnns-overarching-view'),
('A simple example', 2, None, 'a-simple-example'),
('Memoryless models', 3, None, 'memoryless-models'),
('Linear dynamical model', 3, None, 'linear-dynamical-model'),
('Hidden Markov Models', 3, None, 'hidden-markov-models'),
('RNNs', 3, None, 'rnns'),
('Do generative models need to be stochastic?',
3,
None,
'do-generative-models-need-to-be-stochastic'),
('What kinds of behaviour can RNNs exhibit?',
3,
None,
'what-kinds-of-behaviour-can-rnns-exhibit'),
('Basic layout', 2, None, 'basic-layout'),
('We need to specify the initial activity state of all the '
'hidden and output units',
3,
None,
'we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units'),
('We can specify inputs in several ways',
3,
None,
'we-can-specify-inputs-in-several-ways'),
('We can specify targets in several ways',
3,
None,
'we-can-specify-targets-in-several-ways'),
('Backpropagation through time',
3,
None,
'backpropagation-through-time'),
('The backward pass is linear',
3,
None,
'the-backward-pass-is-linear'),
('The problem of exploding or vanishing gradients',
2,
None,
'adaptive-boosting-adaboost-basic-algorithm'),
('Basic Steps of AdaBoost', 2, None, 'basic-steps-of-adaboost'),
('AdaBoost Examples', 2, None, 'adaboost-examples'),
('Gradient boosting: Basics with Steepest Descent/Functional '
'Gradient Descent',
'the-problem-of-exploding-or-vanishing-gradients'),
('Four effective ways to learn an RNN',
2,
None,
'gradient-boosting-basics-with-steepest-descent-functional-gradient-descent'),
('The Squared-Error again! Steepest Descent',
'four-effective-ways-to-learn-an-rnn'),
('Long Short Term Memory (LSTM)',
3,
None,
'long-short-term-memory-lstm'),
('Implementing a memory cell in a neural network',
3,
None,
'implementing-a-memory-cell-in-a-neural-network'),
('An extrapolation example', 2, None, 'an-extrapolation-example'),
('Formatting the Data', 2, None, 'formatting-the-data'),
('Predicting New Points With A Trained Recurrent Neural Network',
2,
None,
'the-squared-error-again-steepest-descent'),
('Steepest Descent Example', 2, None, 'steepest-descent-example'),
('Gradient Boosting, algorithm',
'predicting-new-points-with-a-trained-recurrent-neural-network'),
('Other Things to Try', 2, None, 'other-things-to-try'),
('Other Types of Recurrent Neural Networks',
2,
None,
'gradient-boosting-algorithm'),
('Gradient Boosting, Examples of Regression',
2,
None,
'gradient-boosting-examples-of-regression'),
('Gradient Boosting, Classification Example',
2,
None,
'gradient-boosting-classification-example'),
('XGBoost: Extreme Gradient Boosting',
2,
None,
'xgboost-extreme-gradient-boosting'),
('Regression Case', 2, None, 'regression-case'),
('Xgboost on the Cancer Data',
2,
None,
'xgboost-on-the-cancer-data'),
('Support Vector Machines, overarching aims',
2,
None,
'support-vector-machines-overarching-aims'),
('Hyperplanes and all that', 2, None, 'hyperplanes-and-all-that'),
('What is a hyperplane?', 2, None, 'what-is-a-hyperplane'),
('A $p$-dimensional space of features',
2,
None,
'a-p-dimensional-space-of-features'),
('The two-dimensional case', 2, None, 'the-two-dimensional-case'),
('Getting into the details', 2, None, 'getting-into-the-details'),
('First attempt at a minimization approach',
2,
None,
'first-attempt-at-a-minimization-approach'),
('Solving the equations', 2, None, 'solving-the-equations'),
('Code Example', 2, None, 'code-example'),
('Problems with the Simpler Approach',
2,
None,
'problems-with-the-simpler-approach'),
('A better approach', 2, None, 'a-better-approach'),
('A quick Reminder on Lagrangian Multipliers',
2,
None,
'a-quick-reminder-on-lagrangian-multipliers'),
('Adding the Multiplier', 2, None, 'adding-the-multiplier'),
('Setting up the Problem', 2, None, 'setting-up-the-problem'),
('The problem to solve', 2, None, 'the-problem-to-solve'),
('The last steps', 2, None, 'the-last-steps'),
('A soft classifier', 2, None, 'a-soft-classifier'),
('Soft optmization problem',
2,
None,
'soft-optmization-problem')]}
'other-types-of-recurrent-neural-networks')]}
end of tocinfo -->
<body>
@@ -166,52 +158,46 @@ MathJax.Hub.Config({
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="week45-bs.html">Week 45: Decisions Trees, Random Forests, Bagging and Boosting</a>
<a class="navbar-brand" href="week45-bs.html">Week 45, Recurrent Neural Networks</a>
</div>
<div class="navbar-collapse collapse navbar-responsive-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
<ul class="dropdown-menu">
<!-- navigation toc: --> <li><a href="._week45-bs001.html#overview-of-week-45" style="font-size: 80%;">Overview of week 45</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs002.html#brief-code-reminder-from-last-wekk" style="font-size: 80%;">Brief code reminder from last wekk</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs003.html#boosting-a-bird-s-eye-view" style="font-size: 80%;">Boosting, a Bird's Eye View</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs004.html#what-is-boosting-additive-modelling-iterative-fitting" style="font-size: 80%;">What is boosting? Additive Modelling/Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs005.html#iterative-fitting-regression-and-squared-error-cost-function" style="font-size: 80%;">Iterative Fitting, Regression and Squared-error Cost Function</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs006.html#squared-error-example-and-iterative-fitting" style="font-size: 80%;">Squared-Error Example and Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs007.html#iterative-fitting-classification-and-adaboost" style="font-size: 80%;">Iterative Fitting, Classification and AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs008.html#adaptive-boosting-adaboost" style="font-size: 80%;">Adaptive Boosting, AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs009.html#building-up-adaboost" style="font-size: 80%;">Building up AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs010.html#adaptive-boosting-adaboost-basic-algorithm" style="font-size: 80%;">Adaptive boosting: AdaBoost, Basic Algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs011.html#basic-steps-of-adaboost" style="font-size: 80%;">Basic Steps of AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#adaboost-examples" style="font-size: 80%;">AdaBoost Examples</a></li>
<!-- navigation toc: --> <li><a href="#gradient-boosting-basics-with-steepest-descent-functional-gradient-descent" style="font-size: 80%;">Gradient boosting: Basics with Steepest Descent/Functional Gradient Descent</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs014.html#the-squared-error-again-steepest-descent" style="font-size: 80%;">The Squared-Error again! Steepest Descent</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#steepest-descent-example" style="font-size: 80%;">Steepest Descent Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs016.html#gradient-boosting-algorithm" style="font-size: 80%;">Gradient Boosting, algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs017.html#gradient-boosting-examples-of-regression" style="font-size: 80%;">Gradient Boosting, Examples of Regression</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs018.html#gradient-boosting-classification-example" style="font-size: 80%;">Gradient Boosting, Classification Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs019.html#xgboost-extreme-gradient-boosting" style="font-size: 80%;">XGBoost: Extreme Gradient Boosting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs020.html#regression-case" style="font-size: 80%;">Regression Case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs021.html#xgboost-on-the-cancer-data" style="font-size: 80%;">Xgboost on the Cancer Data</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs022.html#support-vector-machines-overarching-aims" style="font-size: 80%;">Support Vector Machines, overarching aims</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs023.html#hyperplanes-and-all-that" style="font-size: 80%;">Hyperplanes and all that</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs024.html#what-is-a-hyperplane" style="font-size: 80%;">What is a hyperplane?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs025.html#a-p-dimensional-space-of-features" style="font-size: 80%;">A \( p \)-dimensional space of features</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs026.html#the-two-dimensional-case" style="font-size: 80%;">The two-dimensional case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs027.html#getting-into-the-details" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs028.html#first-attempt-at-a-minimization-approach" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs029.html#solving-the-equations" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs030.html#code-example" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs031.html#problems-with-the-simpler-approach" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs032.html#a-better-approach" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs033.html#a-quick-reminder-on-lagrangian-multipliers" style="font-size: 80%;">A quick Reminder on Lagrangian Multipliers</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs034.html#adding-the-multiplier" style="font-size: 80%;">Adding the Multiplier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs035.html#setting-up-the-problem" style="font-size: 80%;">Setting up the Problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs036.html#the-problem-to-solve" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs037.html#the-last-steps" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs038.html#a-soft-classifier" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs039.html#soft-optmization-problem" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs001.html#plan-for-week-45" style="font-size: 80%;"><b>Plan for week 45</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs002.html#material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities" style="font-size: 80%;"><b>Material for the lab sessions, additional ways to present classification results and other practicalities</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs003.html#searching-for-optimal-regularization-parameters-lambda" style="font-size: 80%;"><b>Searching for Optimal Regularization Parameters \( \lambda \)</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs004.html#grid-search" style="font-size: 80%;"><b>Grid Search</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs005.html#randomized-grid-search" style="font-size: 80%;"><b>Randomized Grid Search</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs006.html#wisconsin-cancer-data" style="font-size: 80%;"><b>Wisconsin Cancer Data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs007.html#using-the-correlation-matrix" style="font-size: 80%;"><b>Using the correlation matrix</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs008.html#discussing-the-correlation-data" style="font-size: 80%;"><b>Discussing the correlation data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs009.html#other-measures-in-classification-studies-cancer-data-again" style="font-size: 80%;"><b>Other measures in classification studies: Cancer Data again</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs010.html#material-for-lecture-thursday-november-9" style="font-size: 80%;"><b>Material for Lecture Thursday November 9</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs011.html#recurrent-neural-networks-rnns-overarching-view" style="font-size: 80%;"><b>Recurrent neural networks (RNNs): Overarching view</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#a-simple-example" style="font-size: 80%;"><b>A simple example</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#memoryless-models" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Memoryless models</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#linear-dynamical-model" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Linear dynamical model</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#hidden-markov-models" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Hidden Markov Models</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#rnns" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;RNNs</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#do-generative-models-need-to-be-stochastic" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Do generative models need to be stochastic?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#what-kinds-of-behaviour-can-rnns-exhibit" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;What kinds of behaviour can RNNs exhibit?</a></li>
<!-- navigation toc: --> <li><a href="#basic-layout" style="font-size: 80%;"><b>Basic layout</b></a></li>
<!-- navigation toc: --> <li><a href="#we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We need to specify the initial activity state of all the hidden and output units</a></li>
<!-- navigation toc: --> <li><a href="#we-can-specify-inputs-in-several-ways" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We can specify inputs in several ways</a></li>
<!-- navigation toc: --> <li><a href="#we-can-specify-targets-in-several-ways" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We can specify targets in several ways</a></li>
<!-- navigation toc: --> <li><a href="#backpropagation-through-time" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Backpropagation through time</a></li>
<!-- navigation toc: --> <li><a href="#the-backward-pass-is-linear" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;The backward pass is linear</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs014.html#the-problem-of-exploding-or-vanishing-gradients" style="font-size: 80%;"><b>The problem of exploding or vanishing gradients</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#four-effective-ways-to-learn-an-rnn" style="font-size: 80%;"><b>Four effective ways to learn an RNN</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#long-short-term-memory-lstm" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Long Short Term Memory (LSTM)</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#implementing-a-memory-cell-in-a-neural-network" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Implementing a memory cell in a neural network</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs016.html#an-extrapolation-example" style="font-size: 80%;"><b>An extrapolation example</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs017.html#formatting-the-data" style="font-size: 80%;"><b>Formatting the Data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs018.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;"><b>Predicting New Points With A Trained Recurrent Neural Network</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs019.html#other-things-to-try" style="font-size: 80%;"><b>Other Things to Try</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs020.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;"><b>Other Types of Recurrent Neural Networks</b></a></li>
</ul>
</li>
@@ -223,17 +209,139 @@ MathJax.Hub.Config({
<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p> <!-- add vertical space -->
<a name="part0013"></a>
<!-- !split -->
<h2 id="gradient-boosting-basics-with-steepest-descent-functional-gradient-descent" class="anchor">Gradient boosting: Basics with Steepest Descent/Functional Gradient Descent </h2>
<h2 id="basic-layout" class="anchor">Basic layout </h2>
<p>Gradient boosting is again a similar technique to Adaptive boosting,
it combines so-called weak classifiers or regressors into a strong
method via a series of iterations.
<br/><br/>
<center>
<p><img src="figslides/RNN1.png" width="700" align="bottom"></p>
</center>
<br/><br/>
<h3 id="we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units" class="anchor">We need to specify the initial activity state of all the hidden and output units </h3>
<ol>
<li> We could just fix these initial states to have some default value like 0.5.</li>
<li> But it is better to treat the initial states as learned parameters.</li>
<li> We learn them in the same way as we learn the weights.</li>
<li> Start off with an initial random guess for the initial states.
<ol type="a"></li>
<li> At the end of each training sequence, backpropagate through time all the way to the initial states to get the gradient of the error function with respect to each initial state.</li>
<li> Adjust the initial states by following the negative gradient.</li>
</ol>
</ol>
<h3 id="we-can-specify-inputs-in-several-ways" class="anchor">We can specify inputs in several ways </h3>
<ol>
<li> Specify the initial states of all the units.</li>
<li> Specify the initial states of a subset of the units.</li>
<li> Specify the states of the same subset of the units at every time step.</li>
</ol>
<p>This is the natural way to model most sequential data. </p>
<h3 id="we-can-specify-targets-in-several-ways" class="anchor">We can specify targets in several ways </h3>
<ol>
<li> Specify desired final activities of all the units</li>
<li> Specify desired activities of all units for the last few steps</li>
<li> Good for learning attractors</li>
<li> It is easy to add in extra error derivatives as we backpropagate.</li>
<ul>
<li> Specify the desired activity of a subset of the units.</li>
</ul>
<li> The other units are input or hidden units.</li>
</ol>
<br/><br/>
<center>
<p><img src="figslides/RNN2.png" width="700" align="bottom"></p>
</center>
<br/><br/>
<br/><br/>
<center>
<p><img src="figslides/RNN3.png" width="700" align="bottom"></p>
</center>
<br/><br/>
<br/><br/>
<center>
<p><img src="figslides/RNN4.png" width="700" align="bottom"></p>
</center>
<br/><br/>
<br/><br/>
<center>
<p><img src="figslides/RNN5.png" width="700" align="bottom"></p>
</center>
<br/><br/>
<h3 id="backpropagation-through-time" class="anchor">Backpropagation through time </h3>
<div class="panel panel-default">
<div class="panel-body">
<!-- subsequent paragraphs come in larger fonts, so start with a paragraph -->
<p>We can think of the recurrent net as a layered, feed-forward
net with shared weights and then train the feed-forward net
with weight constraints.
</p>
</div>
</div>
<p>We can also think of this training algorithm in the time domain:</p>
<ol>
<li> The forward pass builds up a stack of the activities of all the units at each time step.</li>
<li> The backward pass peels activities off the stack to compute the error derivatives at each time step.</li>
<li> After the backward pass we add together the derivatives at all the different times for each weight.</li>
</ol>
<h3 id="the-backward-pass-is-linear" class="anchor">The backward pass is linear </h3>
<ol>
<li> There is a big difference between the forward and backward passes.</li>
<li> In the forward pass we use squashing functions (like the logistic) to prevent the activity vectors from exploding.</li>
<li> The backward pass, is completely linear. If you double the error derivatives at the final layer, all the error derivatives will double.</li>
</ol>
<p>The forward pass determines the slope of the linear function used for
backpropagating through each neuron
</p>
<p>In order to understand the method, let us illustrate its basics by
bringing back the essential steps in linear regression, where our cost
function was the least squares function.
</p>
<br/><br/>
<center>
<p><img src="figslides/RNN6.png" width="700" align="bottom"></p>
</center>
<br/><br/>
<br/><br/>
<center>
<p><img src="figslides/RNN7.png" width="700" align="bottom"></p>
</center>
<br/><br/>
<br/><br/>
<center>
<p><img src="figslides/RNN8.png" width="700" align="bottom"></p>
</center>
<br/><br/>
<br/><br/>
<center>
<p><img src="figslides/RNN9.png" width="700" align="bottom"></p>
</center>
<br/><br/>
<br/><br/>
<center>
<p><img src="figslides/RNN10.png" width="700" align="bottom"></p>
</center>
<br/><br/>
<br/><br/>
<center>
<p><img src="figslides/RNN11.png" width="700" align="bottom"></p>
</center>
<br/><br/>
<br/><br/>
<center>
<p><img src="figslides/RNN12.png" width="700" align="bottom"></p>
</center>
<br/><br/>
<p>
<!-- navigation buttons at the bottom of the page -->
@@ -257,10 +365,6 @@ function was the least squares function.
<li><a href="._week45-bs018.html">19</a></li>
<li><a href="._week45-bs019.html">20</a></li>
<li><a href="._week45-bs020.html">21</a></li>
<li><a href="._week45-bs021.html">22</a></li>
<li><a href="._week45-bs022.html">23</a></li>
<li><a href="">...</a></li>
<li><a href="._week45-bs039.html">40</a></li>
<li><a href="._week45-bs014.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+125 -158
View File
@@ -8,8 +8,8 @@ doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=d
<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 45: Decisions Trees, Random Forests, Bagging and Boosting">
<title>Week 45: Decisions Trees, Random Forests, Bagging and Boosting</title>
<meta name="description" content="Week 45, Recurrent Neural Networks">
<title>Week 45, Recurrent Neural Networks</title>
<!-- Bootstrap style: bootstrap -->
<!-- doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=default --html_admon=bootstrap_panel --html_output=week45-bs --no_mako -->
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
@@ -36,109 +36,101 @@ doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=d
<!-- tocinfo
{'highest level': 2,
'sections': [('Overview of week 45', 2, None, 'overview-of-week-45'),
('Brief code reminder from last wekk',
'sections': [('Plan for week 45', 2, None, 'plan-for-week-45'),
('Material for the lab sessions, additional ways to present '
'classification results and other practicalities',
2,
None,
'brief-code-reminder-from-last-wekk'),
("Boosting, a Bird's Eye View",
'material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities'),
('Searching for Optimal Regularization Parameters $\\lambda$',
2,
None,
'boosting-a-bird-s-eye-view'),
('What is boosting? Additive Modelling/Iterative Fitting',
'searching-for-optimal-regularization-parameters-lambda'),
('Grid Search', 2, None, 'grid-search'),
('Randomized Grid Search', 2, None, 'randomized-grid-search'),
('Wisconsin Cancer Data', 2, None, 'wisconsin-cancer-data'),
('Using the correlation matrix',
2,
None,
'what-is-boosting-additive-modelling-iterative-fitting'),
('Iterative Fitting, Regression and Squared-error Cost Function',
'using-the-correlation-matrix'),
('Discussing the correlation data',
2,
None,
'iterative-fitting-regression-and-squared-error-cost-function'),
('Squared-Error Example and Iterative Fitting',
'discussing-the-correlation-data'),
('Other measures in classification studies: Cancer Data again',
2,
None,
'squared-error-example-and-iterative-fitting'),
('Iterative Fitting, Classification and AdaBoost',
'other-measures-in-classification-studies-cancer-data-again'),
('Material for Lecture Thursday November 9',
2,
None,
'iterative-fitting-classification-and-adaboost'),
('Adaptive Boosting, AdaBoost',
'material-for-lecture-thursday-november-9'),
('Recurrent neural networks (RNNs): Overarching view',
2,
None,
'adaptive-boosting-adaboost'),
('Building up AdaBoost', 2, None, 'building-up-adaboost'),
('Adaptive boosting: AdaBoost, Basic Algorithm',
'recurrent-neural-networks-rnns-overarching-view'),
('A simple example', 2, None, 'a-simple-example'),
('Memoryless models', 3, None, 'memoryless-models'),
('Linear dynamical model', 3, None, 'linear-dynamical-model'),
('Hidden Markov Models', 3, None, 'hidden-markov-models'),
('RNNs', 3, None, 'rnns'),
('Do generative models need to be stochastic?',
3,
None,
'do-generative-models-need-to-be-stochastic'),
('What kinds of behaviour can RNNs exhibit?',
3,
None,
'what-kinds-of-behaviour-can-rnns-exhibit'),
('Basic layout', 2, None, 'basic-layout'),
('We need to specify the initial activity state of all the '
'hidden and output units',
3,
None,
'we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units'),
('We can specify inputs in several ways',
3,
None,
'we-can-specify-inputs-in-several-ways'),
('We can specify targets in several ways',
3,
None,
'we-can-specify-targets-in-several-ways'),
('Backpropagation through time',
3,
None,
'backpropagation-through-time'),
('The backward pass is linear',
3,
None,
'the-backward-pass-is-linear'),
('The problem of exploding or vanishing gradients',
2,
None,
'adaptive-boosting-adaboost-basic-algorithm'),
('Basic Steps of AdaBoost', 2, None, 'basic-steps-of-adaboost'),
('AdaBoost Examples', 2, None, 'adaboost-examples'),
('Gradient boosting: Basics with Steepest Descent/Functional '
'Gradient Descent',
'the-problem-of-exploding-or-vanishing-gradients'),
('Four effective ways to learn an RNN',
2,
None,
'gradient-boosting-basics-with-steepest-descent-functional-gradient-descent'),
('The Squared-Error again! Steepest Descent',
'four-effective-ways-to-learn-an-rnn'),
('Long Short Term Memory (LSTM)',
3,
None,
'long-short-term-memory-lstm'),
('Implementing a memory cell in a neural network',
3,
None,
'implementing-a-memory-cell-in-a-neural-network'),
('An extrapolation example', 2, None, 'an-extrapolation-example'),
('Formatting the Data', 2, None, 'formatting-the-data'),
('Predicting New Points With A Trained Recurrent Neural Network',
2,
None,
'the-squared-error-again-steepest-descent'),
('Steepest Descent Example', 2, None, 'steepest-descent-example'),
('Gradient Boosting, algorithm',
'predicting-new-points-with-a-trained-recurrent-neural-network'),
('Other Things to Try', 2, None, 'other-things-to-try'),
('Other Types of Recurrent Neural Networks',
2,
None,
'gradient-boosting-algorithm'),
('Gradient Boosting, Examples of Regression',
2,
None,
'gradient-boosting-examples-of-regression'),
('Gradient Boosting, Classification Example',
2,
None,
'gradient-boosting-classification-example'),
('XGBoost: Extreme Gradient Boosting',
2,
None,
'xgboost-extreme-gradient-boosting'),
('Regression Case', 2, None, 'regression-case'),
('Xgboost on the Cancer Data',
2,
None,
'xgboost-on-the-cancer-data'),
('Support Vector Machines, overarching aims',
2,
None,
'support-vector-machines-overarching-aims'),
('Hyperplanes and all that', 2, None, 'hyperplanes-and-all-that'),
('What is a hyperplane?', 2, None, 'what-is-a-hyperplane'),
('A $p$-dimensional space of features',
2,
None,
'a-p-dimensional-space-of-features'),
('The two-dimensional case', 2, None, 'the-two-dimensional-case'),
('Getting into the details', 2, None, 'getting-into-the-details'),
('First attempt at a minimization approach',
2,
None,
'first-attempt-at-a-minimization-approach'),
('Solving the equations', 2, None, 'solving-the-equations'),
('Code Example', 2, None, 'code-example'),
('Problems with the Simpler Approach',
2,
None,
'problems-with-the-simpler-approach'),
('A better approach', 2, None, 'a-better-approach'),
('A quick Reminder on Lagrangian Multipliers',
2,
None,
'a-quick-reminder-on-lagrangian-multipliers'),
('Adding the Multiplier', 2, None, 'adding-the-multiplier'),
('Setting up the Problem', 2, None, 'setting-up-the-problem'),
('The problem to solve', 2, None, 'the-problem-to-solve'),
('The last steps', 2, None, 'the-last-steps'),
('A soft classifier', 2, None, 'a-soft-classifier'),
('Soft optmization problem',
2,
None,
'soft-optmization-problem')]}
'other-types-of-recurrent-neural-networks')]}
end of tocinfo -->
<body>
@@ -166,52 +158,46 @@ MathJax.Hub.Config({
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="week45-bs.html">Week 45: Decisions Trees, Random Forests, Bagging and Boosting</a>
<a class="navbar-brand" href="week45-bs.html">Week 45, Recurrent Neural Networks</a>
</div>
<div class="navbar-collapse collapse navbar-responsive-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
<ul class="dropdown-menu">
<!-- navigation toc: --> <li><a href="._week45-bs001.html#overview-of-week-45" style="font-size: 80%;">Overview of week 45</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs002.html#brief-code-reminder-from-last-wekk" style="font-size: 80%;">Brief code reminder from last wekk</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs003.html#boosting-a-bird-s-eye-view" style="font-size: 80%;">Boosting, a Bird's Eye View</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs004.html#what-is-boosting-additive-modelling-iterative-fitting" style="font-size: 80%;">What is boosting? Additive Modelling/Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs005.html#iterative-fitting-regression-and-squared-error-cost-function" style="font-size: 80%;">Iterative Fitting, Regression and Squared-error Cost Function</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs006.html#squared-error-example-and-iterative-fitting" style="font-size: 80%;">Squared-Error Example and Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs007.html#iterative-fitting-classification-and-adaboost" style="font-size: 80%;">Iterative Fitting, Classification and AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs008.html#adaptive-boosting-adaboost" style="font-size: 80%;">Adaptive Boosting, AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs009.html#building-up-adaboost" style="font-size: 80%;">Building up AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs010.html#adaptive-boosting-adaboost-basic-algorithm" style="font-size: 80%;">Adaptive boosting: AdaBoost, Basic Algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs011.html#basic-steps-of-adaboost" style="font-size: 80%;">Basic Steps of AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#adaboost-examples" style="font-size: 80%;">AdaBoost Examples</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#gradient-boosting-basics-with-steepest-descent-functional-gradient-descent" style="font-size: 80%;">Gradient boosting: Basics with Steepest Descent/Functional Gradient Descent</a></li>
<!-- navigation toc: --> <li><a href="#the-squared-error-again-steepest-descent" style="font-size: 80%;">The Squared-Error again! Steepest Descent</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#steepest-descent-example" style="font-size: 80%;">Steepest Descent Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs016.html#gradient-boosting-algorithm" style="font-size: 80%;">Gradient Boosting, algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs017.html#gradient-boosting-examples-of-regression" style="font-size: 80%;">Gradient Boosting, Examples of Regression</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs018.html#gradient-boosting-classification-example" style="font-size: 80%;">Gradient Boosting, Classification Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs019.html#xgboost-extreme-gradient-boosting" style="font-size: 80%;">XGBoost: Extreme Gradient Boosting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs020.html#regression-case" style="font-size: 80%;">Regression Case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs021.html#xgboost-on-the-cancer-data" style="font-size: 80%;">Xgboost on the Cancer Data</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs022.html#support-vector-machines-overarching-aims" style="font-size: 80%;">Support Vector Machines, overarching aims</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs023.html#hyperplanes-and-all-that" style="font-size: 80%;">Hyperplanes and all that</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs024.html#what-is-a-hyperplane" style="font-size: 80%;">What is a hyperplane?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs025.html#a-p-dimensional-space-of-features" style="font-size: 80%;">A \( p \)-dimensional space of features</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs026.html#the-two-dimensional-case" style="font-size: 80%;">The two-dimensional case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs027.html#getting-into-the-details" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs028.html#first-attempt-at-a-minimization-approach" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs029.html#solving-the-equations" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs030.html#code-example" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs031.html#problems-with-the-simpler-approach" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs032.html#a-better-approach" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs033.html#a-quick-reminder-on-lagrangian-multipliers" style="font-size: 80%;">A quick Reminder on Lagrangian Multipliers</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs034.html#adding-the-multiplier" style="font-size: 80%;">Adding the Multiplier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs035.html#setting-up-the-problem" style="font-size: 80%;">Setting up the Problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs036.html#the-problem-to-solve" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs037.html#the-last-steps" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs038.html#a-soft-classifier" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs039.html#soft-optmization-problem" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs001.html#plan-for-week-45" style="font-size: 80%;"><b>Plan for week 45</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs002.html#material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities" style="font-size: 80%;"><b>Material for the lab sessions, additional ways to present classification results and other practicalities</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs003.html#searching-for-optimal-regularization-parameters-lambda" style="font-size: 80%;"><b>Searching for Optimal Regularization Parameters \( \lambda \)</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs004.html#grid-search" style="font-size: 80%;"><b>Grid Search</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs005.html#randomized-grid-search" style="font-size: 80%;"><b>Randomized Grid Search</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs006.html#wisconsin-cancer-data" style="font-size: 80%;"><b>Wisconsin Cancer Data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs007.html#using-the-correlation-matrix" style="font-size: 80%;"><b>Using the correlation matrix</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs008.html#discussing-the-correlation-data" style="font-size: 80%;"><b>Discussing the correlation data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs009.html#other-measures-in-classification-studies-cancer-data-again" style="font-size: 80%;"><b>Other measures in classification studies: Cancer Data again</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs010.html#material-for-lecture-thursday-november-9" style="font-size: 80%;"><b>Material for Lecture Thursday November 9</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs011.html#recurrent-neural-networks-rnns-overarching-view" style="font-size: 80%;"><b>Recurrent neural networks (RNNs): Overarching view</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#a-simple-example" style="font-size: 80%;"><b>A simple example</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#memoryless-models" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Memoryless models</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#linear-dynamical-model" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Linear dynamical model</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#hidden-markov-models" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Hidden Markov Models</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#rnns" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;RNNs</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#do-generative-models-need-to-be-stochastic" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Do generative models need to be stochastic?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#what-kinds-of-behaviour-can-rnns-exhibit" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;What kinds of behaviour can RNNs exhibit?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#basic-layout" style="font-size: 80%;"><b>Basic layout</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We need to specify the initial activity state of all the hidden and output units</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-can-specify-inputs-in-several-ways" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We can specify inputs in several ways</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-can-specify-targets-in-several-ways" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We can specify targets in several ways</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#backpropagation-through-time" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Backpropagation through time</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#the-backward-pass-is-linear" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;The backward pass is linear</a></li>
<!-- navigation toc: --> <li><a href="#the-problem-of-exploding-or-vanishing-gradients" style="font-size: 80%;"><b>The problem of exploding or vanishing gradients</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#four-effective-ways-to-learn-an-rnn" style="font-size: 80%;"><b>Four effective ways to learn an RNN</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#long-short-term-memory-lstm" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Long Short Term Memory (LSTM)</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#implementing-a-memory-cell-in-a-neural-network" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Implementing a memory cell in a neural network</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs016.html#an-extrapolation-example" style="font-size: 80%;"><b>An extrapolation example</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs017.html#formatting-the-data" style="font-size: 80%;"><b>Formatting the Data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs018.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;"><b>Predicting New Points With A Trained Recurrent Neural Network</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs019.html#other-things-to-try" style="font-size: 80%;"><b>Other Things to Try</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs020.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;"><b>Other Types of Recurrent Neural Networks</b></a></li>
</ul>
</li>
@@ -222,36 +208,22 @@ MathJax.Hub.Config({
<div class="container">
<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p> <!-- add vertical space -->
<a name="part0014"></a>
<!-- !split -->
<h2 id="the-squared-error-again-steepest-descent" class="anchor">The Squared-Error again! Steepest Descent </h2>
<p>We start again with our cost function \( {\cal C}(\boldsymbol{y}m\boldsymbol{f})=\sum_{i=0}^{n-1}{\cal L}(y_i, f(x_i)) \) where we want to minimize
This means that for every iteration, we need to optimize
</p>
$$
(\hat{\boldsymbol{f}}) = \mathrm{argmin}_{\boldsymbol{f}}\hspace{0.1cm} \sum_{i=0}^{n-1}(y_i-f(x_i))^2.
$$
<p>We define a real function \( h_m(x) \) that defines our final function \( f_M(x) \) as</p>
$$
f_M(x) = \sum_{m=0}^M h_m(x).
$$
<p>In the steepest decent approach we approximate \( h_m(x) = -\rho_m g_m(x) \), where \( \rho_m \) is a scalar and \( g_m(x) \) the gradient defined as</p>
$$
g_m(x_i) = \left[ \frac{\partial {\cal L}(y_i, f(x_i))}{\partial f(x_i)}\right]_{f(x_i)=f_{m-1}(x_i)}.
$$
<p>With the new gradient we can update \( f_m(x) = f_{m-1}(x) -\rho_m g_m(x) \). Using the above squared-error function we see that
the gradient is \( g_m(x_i) = -2(y_i-f(x_i)) \).
</p>
<p>Choosing \( f_0(x)=0 \) we obtain \( g_m(x) = -2y_i \) and inserting this into the minimization problem for the cost function we have</p>
$$
(\rho_1) = \mathrm{argmin}_{\rho}\hspace{0.1cm} \sum_{i=0}^{n-1}(y_i+2\rho y_i)^2.
$$
<!-- !split -->
<h2 id="the-problem-of-exploding-or-vanishing-gradients" class="anchor">The problem of exploding or vanishing gradients </h2>
<ul>
<li> What happens to the magnitude of the gradients as we backpropagate through many layers?
<ol type="a"></li>
<li> If the weights are small, the gradients shrink exponentially.</li>
<li> If the weights are big the gradients grow exponentially.</li>
</ol>
<li> Typical feed-forward neural nets can cope with these exponential effects because they only have a few hidden layers.</li>
<li> In an RNN trained on long sequences (e.g. 100 time steps) the gradients can easily explode or vanish.
<ol type="a"></li>
<li> We can avoid this by initializing the weights very carefully.</li>
</ol>
<li> Even with good initial weights, its very hard to detect that the current target output depends on an input from many time-steps ago.</li>
</ul>
<p>RNNs have difficulty dealing with long-range dependencies. </p>
<p>
<!-- navigation buttons at the bottom of the page -->
@@ -274,11 +246,6 @@ $$
<li><a href="._week45-bs018.html">19</a></li>
<li><a href="._week45-bs019.html">20</a></li>
<li><a href="._week45-bs020.html">21</a></li>
<li><a href="._week45-bs021.html">22</a></li>
<li><a href="._week45-bs022.html">23</a></li>
<li><a href="._week45-bs023.html">24</a></li>
<li><a href="">...</a></li>
<li><a href="._week45-bs039.html">40</a></li>
<li><a href="._week45-bs015.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+201 -139
View File
@@ -8,8 +8,8 @@ doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=d
<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 45: Decisions Trees, Random Forests, Bagging and Boosting">
<title>Week 45: Decisions Trees, Random Forests, Bagging and Boosting</title>
<meta name="description" content="Week 45, Recurrent Neural Networks">
<title>Week 45, Recurrent Neural Networks</title>
<!-- Bootstrap style: bootstrap -->
<!-- doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=default --html_admon=bootstrap_panel --html_output=week45-bs --no_mako -->
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
@@ -36,109 +36,101 @@ doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=d
<!-- tocinfo
{'highest level': 2,
'sections': [('Overview of week 45', 2, None, 'overview-of-week-45'),
('Brief code reminder from last wekk',
'sections': [('Plan for week 45', 2, None, 'plan-for-week-45'),
('Material for the lab sessions, additional ways to present '
'classification results and other practicalities',
2,
None,
'brief-code-reminder-from-last-wekk'),
("Boosting, a Bird's Eye View",
'material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities'),
('Searching for Optimal Regularization Parameters $\\lambda$',
2,
None,
'boosting-a-bird-s-eye-view'),
('What is boosting? Additive Modelling/Iterative Fitting',
'searching-for-optimal-regularization-parameters-lambda'),
('Grid Search', 2, None, 'grid-search'),
('Randomized Grid Search', 2, None, 'randomized-grid-search'),
('Wisconsin Cancer Data', 2, None, 'wisconsin-cancer-data'),
('Using the correlation matrix',
2,
None,
'what-is-boosting-additive-modelling-iterative-fitting'),
('Iterative Fitting, Regression and Squared-error Cost Function',
'using-the-correlation-matrix'),
('Discussing the correlation data',
2,
None,
'iterative-fitting-regression-and-squared-error-cost-function'),
('Squared-Error Example and Iterative Fitting',
'discussing-the-correlation-data'),
('Other measures in classification studies: Cancer Data again',
2,
None,
'squared-error-example-and-iterative-fitting'),
('Iterative Fitting, Classification and AdaBoost',
'other-measures-in-classification-studies-cancer-data-again'),
('Material for Lecture Thursday November 9',
2,
None,
'iterative-fitting-classification-and-adaboost'),
('Adaptive Boosting, AdaBoost',
'material-for-lecture-thursday-november-9'),
('Recurrent neural networks (RNNs): Overarching view',
2,
None,
'adaptive-boosting-adaboost'),
('Building up AdaBoost', 2, None, 'building-up-adaboost'),
('Adaptive boosting: AdaBoost, Basic Algorithm',
'recurrent-neural-networks-rnns-overarching-view'),
('A simple example', 2, None, 'a-simple-example'),
('Memoryless models', 3, None, 'memoryless-models'),
('Linear dynamical model', 3, None, 'linear-dynamical-model'),
('Hidden Markov Models', 3, None, 'hidden-markov-models'),
('RNNs', 3, None, 'rnns'),
('Do generative models need to be stochastic?',
3,
None,
'do-generative-models-need-to-be-stochastic'),
('What kinds of behaviour can RNNs exhibit?',
3,
None,
'what-kinds-of-behaviour-can-rnns-exhibit'),
('Basic layout', 2, None, 'basic-layout'),
('We need to specify the initial activity state of all the '
'hidden and output units',
3,
None,
'we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units'),
('We can specify inputs in several ways',
3,
None,
'we-can-specify-inputs-in-several-ways'),
('We can specify targets in several ways',
3,
None,
'we-can-specify-targets-in-several-ways'),
('Backpropagation through time',
3,
None,
'backpropagation-through-time'),
('The backward pass is linear',
3,
None,
'the-backward-pass-is-linear'),
('The problem of exploding or vanishing gradients',
2,
None,
'adaptive-boosting-adaboost-basic-algorithm'),
('Basic Steps of AdaBoost', 2, None, 'basic-steps-of-adaboost'),
('AdaBoost Examples', 2, None, 'adaboost-examples'),
('Gradient boosting: Basics with Steepest Descent/Functional '
'Gradient Descent',
'the-problem-of-exploding-or-vanishing-gradients'),
('Four effective ways to learn an RNN',
2,
None,
'gradient-boosting-basics-with-steepest-descent-functional-gradient-descent'),
('The Squared-Error again! Steepest Descent',
'four-effective-ways-to-learn-an-rnn'),
('Long Short Term Memory (LSTM)',
3,
None,
'long-short-term-memory-lstm'),
('Implementing a memory cell in a neural network',
3,
None,
'implementing-a-memory-cell-in-a-neural-network'),
('An extrapolation example', 2, None, 'an-extrapolation-example'),
('Formatting the Data', 2, None, 'formatting-the-data'),
('Predicting New Points With A Trained Recurrent Neural Network',
2,
None,
'the-squared-error-again-steepest-descent'),
('Steepest Descent Example', 2, None, 'steepest-descent-example'),
('Gradient Boosting, algorithm',
'predicting-new-points-with-a-trained-recurrent-neural-network'),
('Other Things to Try', 2, None, 'other-things-to-try'),
('Other Types of Recurrent Neural Networks',
2,
None,
'gradient-boosting-algorithm'),
('Gradient Boosting, Examples of Regression',
2,
None,
'gradient-boosting-examples-of-regression'),
('Gradient Boosting, Classification Example',
2,
None,
'gradient-boosting-classification-example'),
('XGBoost: Extreme Gradient Boosting',
2,
None,
'xgboost-extreme-gradient-boosting'),
('Regression Case', 2, None, 'regression-case'),
('Xgboost on the Cancer Data',
2,
None,
'xgboost-on-the-cancer-data'),
('Support Vector Machines, overarching aims',
2,
None,
'support-vector-machines-overarching-aims'),
('Hyperplanes and all that', 2, None, 'hyperplanes-and-all-that'),
('What is a hyperplane?', 2, None, 'what-is-a-hyperplane'),
('A $p$-dimensional space of features',
2,
None,
'a-p-dimensional-space-of-features'),
('The two-dimensional case', 2, None, 'the-two-dimensional-case'),
('Getting into the details', 2, None, 'getting-into-the-details'),
('First attempt at a minimization approach',
2,
None,
'first-attempt-at-a-minimization-approach'),
('Solving the equations', 2, None, 'solving-the-equations'),
('Code Example', 2, None, 'code-example'),
('Problems with the Simpler Approach',
2,
None,
'problems-with-the-simpler-approach'),
('A better approach', 2, None, 'a-better-approach'),
('A quick Reminder on Lagrangian Multipliers',
2,
None,
'a-quick-reminder-on-lagrangian-multipliers'),
('Adding the Multiplier', 2, None, 'adding-the-multiplier'),
('Setting up the Problem', 2, None, 'setting-up-the-problem'),
('The problem to solve', 2, None, 'the-problem-to-solve'),
('The last steps', 2, None, 'the-last-steps'),
('A soft classifier', 2, None, 'a-soft-classifier'),
('Soft optmization problem',
2,
None,
'soft-optmization-problem')]}
'other-types-of-recurrent-neural-networks')]}
end of tocinfo -->
<body>
@@ -166,52 +158,46 @@ MathJax.Hub.Config({
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="week45-bs.html">Week 45: Decisions Trees, Random Forests, Bagging and Boosting</a>
<a class="navbar-brand" href="week45-bs.html">Week 45, Recurrent Neural Networks</a>
</div>
<div class="navbar-collapse collapse navbar-responsive-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
<ul class="dropdown-menu">
<!-- navigation toc: --> <li><a href="._week45-bs001.html#overview-of-week-45" style="font-size: 80%;">Overview of week 45</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs002.html#brief-code-reminder-from-last-wekk" style="font-size: 80%;">Brief code reminder from last wekk</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs003.html#boosting-a-bird-s-eye-view" style="font-size: 80%;">Boosting, a Bird's Eye View</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs004.html#what-is-boosting-additive-modelling-iterative-fitting" style="font-size: 80%;">What is boosting? Additive Modelling/Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs005.html#iterative-fitting-regression-and-squared-error-cost-function" style="font-size: 80%;">Iterative Fitting, Regression and Squared-error Cost Function</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs006.html#squared-error-example-and-iterative-fitting" style="font-size: 80%;">Squared-Error Example and Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs007.html#iterative-fitting-classification-and-adaboost" style="font-size: 80%;">Iterative Fitting, Classification and AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs008.html#adaptive-boosting-adaboost" style="font-size: 80%;">Adaptive Boosting, AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs009.html#building-up-adaboost" style="font-size: 80%;">Building up AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs010.html#adaptive-boosting-adaboost-basic-algorithm" style="font-size: 80%;">Adaptive boosting: AdaBoost, Basic Algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs011.html#basic-steps-of-adaboost" style="font-size: 80%;">Basic Steps of AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#adaboost-examples" style="font-size: 80%;">AdaBoost Examples</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#gradient-boosting-basics-with-steepest-descent-functional-gradient-descent" style="font-size: 80%;">Gradient boosting: Basics with Steepest Descent/Functional Gradient Descent</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs014.html#the-squared-error-again-steepest-descent" style="font-size: 80%;">The Squared-Error again! Steepest Descent</a></li>
<!-- navigation toc: --> <li><a href="#steepest-descent-example" style="font-size: 80%;">Steepest Descent Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs016.html#gradient-boosting-algorithm" style="font-size: 80%;">Gradient Boosting, algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs017.html#gradient-boosting-examples-of-regression" style="font-size: 80%;">Gradient Boosting, Examples of Regression</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs018.html#gradient-boosting-classification-example" style="font-size: 80%;">Gradient Boosting, Classification Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs019.html#xgboost-extreme-gradient-boosting" style="font-size: 80%;">XGBoost: Extreme Gradient Boosting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs020.html#regression-case" style="font-size: 80%;">Regression Case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs021.html#xgboost-on-the-cancer-data" style="font-size: 80%;">Xgboost on the Cancer Data</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs022.html#support-vector-machines-overarching-aims" style="font-size: 80%;">Support Vector Machines, overarching aims</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs023.html#hyperplanes-and-all-that" style="font-size: 80%;">Hyperplanes and all that</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs024.html#what-is-a-hyperplane" style="font-size: 80%;">What is a hyperplane?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs025.html#a-p-dimensional-space-of-features" style="font-size: 80%;">A \( p \)-dimensional space of features</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs026.html#the-two-dimensional-case" style="font-size: 80%;">The two-dimensional case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs027.html#getting-into-the-details" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs028.html#first-attempt-at-a-minimization-approach" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs029.html#solving-the-equations" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs030.html#code-example" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs031.html#problems-with-the-simpler-approach" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs032.html#a-better-approach" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs033.html#a-quick-reminder-on-lagrangian-multipliers" style="font-size: 80%;">A quick Reminder on Lagrangian Multipliers</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs034.html#adding-the-multiplier" style="font-size: 80%;">Adding the Multiplier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs035.html#setting-up-the-problem" style="font-size: 80%;">Setting up the Problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs036.html#the-problem-to-solve" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs037.html#the-last-steps" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs038.html#a-soft-classifier" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs039.html#soft-optmization-problem" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs001.html#plan-for-week-45" style="font-size: 80%;"><b>Plan for week 45</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs002.html#material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities" style="font-size: 80%;"><b>Material for the lab sessions, additional ways to present classification results and other practicalities</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs003.html#searching-for-optimal-regularization-parameters-lambda" style="font-size: 80%;"><b>Searching for Optimal Regularization Parameters \( \lambda \)</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs004.html#grid-search" style="font-size: 80%;"><b>Grid Search</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs005.html#randomized-grid-search" style="font-size: 80%;"><b>Randomized Grid Search</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs006.html#wisconsin-cancer-data" style="font-size: 80%;"><b>Wisconsin Cancer Data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs007.html#using-the-correlation-matrix" style="font-size: 80%;"><b>Using the correlation matrix</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs008.html#discussing-the-correlation-data" style="font-size: 80%;"><b>Discussing the correlation data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs009.html#other-measures-in-classification-studies-cancer-data-again" style="font-size: 80%;"><b>Other measures in classification studies: Cancer Data again</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs010.html#material-for-lecture-thursday-november-9" style="font-size: 80%;"><b>Material for Lecture Thursday November 9</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs011.html#recurrent-neural-networks-rnns-overarching-view" style="font-size: 80%;"><b>Recurrent neural networks (RNNs): Overarching view</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#a-simple-example" style="font-size: 80%;"><b>A simple example</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#memoryless-models" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Memoryless models</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#linear-dynamical-model" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Linear dynamical model</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#hidden-markov-models" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Hidden Markov Models</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#rnns" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;RNNs</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#do-generative-models-need-to-be-stochastic" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Do generative models need to be stochastic?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#what-kinds-of-behaviour-can-rnns-exhibit" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;What kinds of behaviour can RNNs exhibit?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#basic-layout" style="font-size: 80%;"><b>Basic layout</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We need to specify the initial activity state of all the hidden and output units</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-can-specify-inputs-in-several-ways" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We can specify inputs in several ways</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-can-specify-targets-in-several-ways" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We can specify targets in several ways</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#backpropagation-through-time" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Backpropagation through time</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#the-backward-pass-is-linear" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;The backward pass is linear</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs014.html#the-problem-of-exploding-or-vanishing-gradients" style="font-size: 80%;"><b>The problem of exploding or vanishing gradients</b></a></li>
<!-- navigation toc: --> <li><a href="#four-effective-ways-to-learn-an-rnn" style="font-size: 80%;"><b>Four effective ways to learn an RNN</b></a></li>
<!-- navigation toc: --> <li><a href="#long-short-term-memory-lstm" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Long Short Term Memory (LSTM)</a></li>
<!-- navigation toc: --> <li><a href="#implementing-a-memory-cell-in-a-neural-network" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Implementing a memory cell in a neural network</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs016.html#an-extrapolation-example" style="font-size: 80%;"><b>An extrapolation example</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs017.html#formatting-the-data" style="font-size: 80%;"><b>Formatting the Data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs018.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;"><b>Predicting New Points With A Trained Recurrent Neural Network</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs019.html#other-things-to-try" style="font-size: 80%;"><b>Other Things to Try</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs020.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;"><b>Other Types of Recurrent Neural Networks</b></a></li>
</ul>
</li>
@@ -223,19 +209,101 @@ MathJax.Hub.Config({
<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p> <!-- add vertical space -->
<a name="part0015"></a>
<!-- !split -->
<h2 id="steepest-descent-example" class="anchor">Steepest Descent Example </h2>
<h2 id="four-effective-ways-to-learn-an-rnn" class="anchor">Four effective ways to learn an RNN </h2>
<ol>
<li> Long Short Term Memory Make the RNN out of little modules that are designed to remember values for a long time.</li>
<li> Hessian Free Optimization: Deal with the vanishing gradients problem by using a fancy optimizer that can detect directions with a tiny gradient but even smaller curvature.</li>
<li> Echo State Networks: Initialize the input a hidden and hidden-hidden and output-hidden connections very carefully so that the hidden state has a huge reservoir of weakly coupled oscillators which can be selectively driven by the input.</li>
<ul>
<li> ESNs only need to learn the hidden-output connections.</li>
</ul>
<li> Good initialization with momentum Initialize like in Echo State Networks, but then learn all of the connections using momentum</li>
</ol>
<h3 id="long-short-term-memory-lstm" class="anchor">Long Short Term Memory (LSTM) </h3>
<p>Optimizing with respect to \( \rho \) we obtain (taking the derivative) that \( \rho_1 = -1/2 \). We have then that</p>
$$
f_1(x) = f_{0}(x) -\rho_1 g_1(x)=-y_i.
$$
<p>LSTM uses a memory cell for
modeling long-range dependencies and avoid vanishing gradient
problems.
</p>
<p>We can then proceed and compute</p>
$$
g_2(x_i) = \left[ \frac{\partial {\cal L}(y_i, f(x_i))}{\partial f(x_i)}\right]_{f(x_i)=f_{1}(x_i)=y_i}=-4y_i,
$$
<ol>
<li> Introduced by Hochreiter and Schmidhuber (1997) who solved the problem of getting an RNN to remember things for a long time (like hundreds of time steps).</li>
<li> They designed a memory cell using logistic and linear units with multiplicative interactions.</li>
<li> Information gets into the cell whenever its &#8220;write&#8221; gate is on.</li>
<li> The information stays in the cell so long as its <b>keep</b> gate is on.</li>
<li> Information can be read from the cell by turning on its <b>read</b> gate.</li>
</ol>
<h3 id="implementing-a-memory-cell-in-a-neural-network" class="anchor">Implementing a memory cell in a neural network </h3>
<p>To preserve information for a long time in
the activities of an RNN, we use a circuit
that implements an analog memory cell.
</p>
<p>and find a new value for \( \rho_2=-1/2 \) and continue till we have reached \( m=M \). We can modify the steepest descent method, or steepest boosting, by introducing what is called <b>gradient boosting</b>. </p>
<ol>
<li> A linear unit that has a self-link with a weight of 1 will maintain its state.</li>
<li> Information is stored in the cell by activating its write gate.</li>
<li> Information is retrieved by activating the read gate.</li>
<li> We can backpropagate through this circuit because logistics are have nice derivatives.</li>
</ol>
<br/><br/>
<center>
<p><img src="figslides/RNN13.png" width="700" align="bottom"></p>
</center>
<br/><br/>
<br/><br/>
<center>
<p><img src="figslides/RNN14.png" width="700" align="bottom"></p>
</center>
<br/><br/>
<br/><br/>
<center>
<p><img src="figslides/RNN15.png" width="700" align="bottom"></p>
</center>
<br/><br/>
<br/><br/>
<center>
<p><img src="figslides/RNN16.png" width="700" align="bottom"></p>
</center>
<br/><br/>
<br/><br/>
<center>
<p><img src="figslides/RNN17.png" width="700" align="bottom"></p>
</center>
<br/><br/>
<br/><br/>
<center>
<p><img src="figslides/RNN18.png" width="700" align="bottom"></p>
</center>
<br/><br/>
<br/><br/>
<center>
<p><img src="figslides/RNN19.png" width="700" align="bottom"></p>
</center>
<br/><br/>
<br/><br/>
<center>
<p><img src="figslides/RNN20.png" width="700" align="bottom"></p>
</center>
<br/><br/>
<br/><br/>
<center>
<p><img src="figslides/RNN21.png" width="700" align="bottom"></p>
</center>
<br/><br/>
<br/><br/>
<center>
<p><img src="figslides/RNN22.png" width="700" align="bottom"></p>
</center>
<br/><br/>
<p>
<!-- navigation buttons at the bottom of the page -->
@@ -257,12 +325,6 @@ $$
<li><a href="._week45-bs018.html">19</a></li>
<li><a href="._week45-bs019.html">20</a></li>
<li><a href="._week45-bs020.html">21</a></li>
<li><a href="._week45-bs021.html">22</a></li>
<li><a href="._week45-bs022.html">23</a></li>
<li><a href="._week45-bs023.html">24</a></li>
<li><a href="._week45-bs024.html">25</a></li>
<li><a href="">...</a></li>
<li><a href="._week45-bs039.html">40</a></li>
<li><a href="._week45-bs016.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+163 -149
View File
@@ -8,8 +8,8 @@ doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=d
<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 45: Decisions Trees, Random Forests, Bagging and Boosting">
<title>Week 45: Decisions Trees, Random Forests, Bagging and Boosting</title>
<meta name="description" content="Week 45, Recurrent Neural Networks">
<title>Week 45, Recurrent Neural Networks</title>
<!-- Bootstrap style: bootstrap -->
<!-- doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=default --html_admon=bootstrap_panel --html_output=week45-bs --no_mako -->
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
@@ -36,109 +36,101 @@ doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=d
<!-- tocinfo
{'highest level': 2,
'sections': [('Overview of week 45', 2, None, 'overview-of-week-45'),
('Brief code reminder from last wekk',
'sections': [('Plan for week 45', 2, None, 'plan-for-week-45'),
('Material for the lab sessions, additional ways to present '
'classification results and other practicalities',
2,
None,
'brief-code-reminder-from-last-wekk'),
("Boosting, a Bird's Eye View",
'material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities'),
('Searching for Optimal Regularization Parameters $\\lambda$',
2,
None,
'boosting-a-bird-s-eye-view'),
('What is boosting? Additive Modelling/Iterative Fitting',
'searching-for-optimal-regularization-parameters-lambda'),
('Grid Search', 2, None, 'grid-search'),
('Randomized Grid Search', 2, None, 'randomized-grid-search'),
('Wisconsin Cancer Data', 2, None, 'wisconsin-cancer-data'),
('Using the correlation matrix',
2,
None,
'what-is-boosting-additive-modelling-iterative-fitting'),
('Iterative Fitting, Regression and Squared-error Cost Function',
'using-the-correlation-matrix'),
('Discussing the correlation data',
2,
None,
'iterative-fitting-regression-and-squared-error-cost-function'),
('Squared-Error Example and Iterative Fitting',
'discussing-the-correlation-data'),
('Other measures in classification studies: Cancer Data again',
2,
None,
'squared-error-example-and-iterative-fitting'),
('Iterative Fitting, Classification and AdaBoost',
'other-measures-in-classification-studies-cancer-data-again'),
('Material for Lecture Thursday November 9',
2,
None,
'iterative-fitting-classification-and-adaboost'),
('Adaptive Boosting, AdaBoost',
'material-for-lecture-thursday-november-9'),
('Recurrent neural networks (RNNs): Overarching view',
2,
None,
'adaptive-boosting-adaboost'),
('Building up AdaBoost', 2, None, 'building-up-adaboost'),
('Adaptive boosting: AdaBoost, Basic Algorithm',
'recurrent-neural-networks-rnns-overarching-view'),
('A simple example', 2, None, 'a-simple-example'),
('Memoryless models', 3, None, 'memoryless-models'),
('Linear dynamical model', 3, None, 'linear-dynamical-model'),
('Hidden Markov Models', 3, None, 'hidden-markov-models'),
('RNNs', 3, None, 'rnns'),
('Do generative models need to be stochastic?',
3,
None,
'do-generative-models-need-to-be-stochastic'),
('What kinds of behaviour can RNNs exhibit?',
3,
None,
'what-kinds-of-behaviour-can-rnns-exhibit'),
('Basic layout', 2, None, 'basic-layout'),
('We need to specify the initial activity state of all the '
'hidden and output units',
3,
None,
'we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units'),
('We can specify inputs in several ways',
3,
None,
'we-can-specify-inputs-in-several-ways'),
('We can specify targets in several ways',
3,
None,
'we-can-specify-targets-in-several-ways'),
('Backpropagation through time',
3,
None,
'backpropagation-through-time'),
('The backward pass is linear',
3,
None,
'the-backward-pass-is-linear'),
('The problem of exploding or vanishing gradients',
2,
None,
'adaptive-boosting-adaboost-basic-algorithm'),
('Basic Steps of AdaBoost', 2, None, 'basic-steps-of-adaboost'),
('AdaBoost Examples', 2, None, 'adaboost-examples'),
('Gradient boosting: Basics with Steepest Descent/Functional '
'Gradient Descent',
'the-problem-of-exploding-or-vanishing-gradients'),
('Four effective ways to learn an RNN',
2,
None,
'gradient-boosting-basics-with-steepest-descent-functional-gradient-descent'),
('The Squared-Error again! Steepest Descent',
'four-effective-ways-to-learn-an-rnn'),
('Long Short Term Memory (LSTM)',
3,
None,
'long-short-term-memory-lstm'),
('Implementing a memory cell in a neural network',
3,
None,
'implementing-a-memory-cell-in-a-neural-network'),
('An extrapolation example', 2, None, 'an-extrapolation-example'),
('Formatting the Data', 2, None, 'formatting-the-data'),
('Predicting New Points With A Trained Recurrent Neural Network',
2,
None,
'the-squared-error-again-steepest-descent'),
('Steepest Descent Example', 2, None, 'steepest-descent-example'),
('Gradient Boosting, algorithm',
'predicting-new-points-with-a-trained-recurrent-neural-network'),
('Other Things to Try', 2, None, 'other-things-to-try'),
('Other Types of Recurrent Neural Networks',
2,
None,
'gradient-boosting-algorithm'),
('Gradient Boosting, Examples of Regression',
2,
None,
'gradient-boosting-examples-of-regression'),
('Gradient Boosting, Classification Example',
2,
None,
'gradient-boosting-classification-example'),
('XGBoost: Extreme Gradient Boosting',
2,
None,
'xgboost-extreme-gradient-boosting'),
('Regression Case', 2, None, 'regression-case'),
('Xgboost on the Cancer Data',
2,
None,
'xgboost-on-the-cancer-data'),
('Support Vector Machines, overarching aims',
2,
None,
'support-vector-machines-overarching-aims'),
('Hyperplanes and all that', 2, None, 'hyperplanes-and-all-that'),
('What is a hyperplane?', 2, None, 'what-is-a-hyperplane'),
('A $p$-dimensional space of features',
2,
None,
'a-p-dimensional-space-of-features'),
('The two-dimensional case', 2, None, 'the-two-dimensional-case'),
('Getting into the details', 2, None, 'getting-into-the-details'),
('First attempt at a minimization approach',
2,
None,
'first-attempt-at-a-minimization-approach'),
('Solving the equations', 2, None, 'solving-the-equations'),
('Code Example', 2, None, 'code-example'),
('Problems with the Simpler Approach',
2,
None,
'problems-with-the-simpler-approach'),
('A better approach', 2, None, 'a-better-approach'),
('A quick Reminder on Lagrangian Multipliers',
2,
None,
'a-quick-reminder-on-lagrangian-multipliers'),
('Adding the Multiplier', 2, None, 'adding-the-multiplier'),
('Setting up the Problem', 2, None, 'setting-up-the-problem'),
('The problem to solve', 2, None, 'the-problem-to-solve'),
('The last steps', 2, None, 'the-last-steps'),
('A soft classifier', 2, None, 'a-soft-classifier'),
('Soft optmization problem',
2,
None,
'soft-optmization-problem')]}
'other-types-of-recurrent-neural-networks')]}
end of tocinfo -->
<body>
@@ -166,52 +158,46 @@ MathJax.Hub.Config({
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="week45-bs.html">Week 45: Decisions Trees, Random Forests, Bagging and Boosting</a>
<a class="navbar-brand" href="week45-bs.html">Week 45, Recurrent Neural Networks</a>
</div>
<div class="navbar-collapse collapse navbar-responsive-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
<ul class="dropdown-menu">
<!-- navigation toc: --> <li><a href="._week45-bs001.html#overview-of-week-45" style="font-size: 80%;">Overview of week 45</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs002.html#brief-code-reminder-from-last-wekk" style="font-size: 80%;">Brief code reminder from last wekk</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs003.html#boosting-a-bird-s-eye-view" style="font-size: 80%;">Boosting, a Bird's Eye View</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs004.html#what-is-boosting-additive-modelling-iterative-fitting" style="font-size: 80%;">What is boosting? Additive Modelling/Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs005.html#iterative-fitting-regression-and-squared-error-cost-function" style="font-size: 80%;">Iterative Fitting, Regression and Squared-error Cost Function</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs006.html#squared-error-example-and-iterative-fitting" style="font-size: 80%;">Squared-Error Example and Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs007.html#iterative-fitting-classification-and-adaboost" style="font-size: 80%;">Iterative Fitting, Classification and AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs008.html#adaptive-boosting-adaboost" style="font-size: 80%;">Adaptive Boosting, AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs009.html#building-up-adaboost" style="font-size: 80%;">Building up AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs010.html#adaptive-boosting-adaboost-basic-algorithm" style="font-size: 80%;">Adaptive boosting: AdaBoost, Basic Algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs011.html#basic-steps-of-adaboost" style="font-size: 80%;">Basic Steps of AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#adaboost-examples" style="font-size: 80%;">AdaBoost Examples</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#gradient-boosting-basics-with-steepest-descent-functional-gradient-descent" style="font-size: 80%;">Gradient boosting: Basics with Steepest Descent/Functional Gradient Descent</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs014.html#the-squared-error-again-steepest-descent" style="font-size: 80%;">The Squared-Error again! Steepest Descent</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#steepest-descent-example" style="font-size: 80%;">Steepest Descent Example</a></li>
<!-- navigation toc: --> <li><a href="#gradient-boosting-algorithm" style="font-size: 80%;">Gradient Boosting, algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs017.html#gradient-boosting-examples-of-regression" style="font-size: 80%;">Gradient Boosting, Examples of Regression</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs018.html#gradient-boosting-classification-example" style="font-size: 80%;">Gradient Boosting, Classification Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs019.html#xgboost-extreme-gradient-boosting" style="font-size: 80%;">XGBoost: Extreme Gradient Boosting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs020.html#regression-case" style="font-size: 80%;">Regression Case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs021.html#xgboost-on-the-cancer-data" style="font-size: 80%;">Xgboost on the Cancer Data</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs022.html#support-vector-machines-overarching-aims" style="font-size: 80%;">Support Vector Machines, overarching aims</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs023.html#hyperplanes-and-all-that" style="font-size: 80%;">Hyperplanes and all that</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs024.html#what-is-a-hyperplane" style="font-size: 80%;">What is a hyperplane?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs025.html#a-p-dimensional-space-of-features" style="font-size: 80%;">A \( p \)-dimensional space of features</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs026.html#the-two-dimensional-case" style="font-size: 80%;">The two-dimensional case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs027.html#getting-into-the-details" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs028.html#first-attempt-at-a-minimization-approach" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs029.html#solving-the-equations" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs030.html#code-example" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs031.html#problems-with-the-simpler-approach" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs032.html#a-better-approach" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs033.html#a-quick-reminder-on-lagrangian-multipliers" style="font-size: 80%;">A quick Reminder on Lagrangian Multipliers</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs034.html#adding-the-multiplier" style="font-size: 80%;">Adding the Multiplier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs035.html#setting-up-the-problem" style="font-size: 80%;">Setting up the Problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs036.html#the-problem-to-solve" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs037.html#the-last-steps" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs038.html#a-soft-classifier" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs039.html#soft-optmization-problem" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs001.html#plan-for-week-45" style="font-size: 80%;"><b>Plan for week 45</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs002.html#material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities" style="font-size: 80%;"><b>Material for the lab sessions, additional ways to present classification results and other practicalities</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs003.html#searching-for-optimal-regularization-parameters-lambda" style="font-size: 80%;"><b>Searching for Optimal Regularization Parameters \( \lambda \)</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs004.html#grid-search" style="font-size: 80%;"><b>Grid Search</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs005.html#randomized-grid-search" style="font-size: 80%;"><b>Randomized Grid Search</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs006.html#wisconsin-cancer-data" style="font-size: 80%;"><b>Wisconsin Cancer Data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs007.html#using-the-correlation-matrix" style="font-size: 80%;"><b>Using the correlation matrix</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs008.html#discussing-the-correlation-data" style="font-size: 80%;"><b>Discussing the correlation data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs009.html#other-measures-in-classification-studies-cancer-data-again" style="font-size: 80%;"><b>Other measures in classification studies: Cancer Data again</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs010.html#material-for-lecture-thursday-november-9" style="font-size: 80%;"><b>Material for Lecture Thursday November 9</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs011.html#recurrent-neural-networks-rnns-overarching-view" style="font-size: 80%;"><b>Recurrent neural networks (RNNs): Overarching view</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#a-simple-example" style="font-size: 80%;"><b>A simple example</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#memoryless-models" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Memoryless models</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#linear-dynamical-model" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Linear dynamical model</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#hidden-markov-models" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Hidden Markov Models</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#rnns" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;RNNs</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#do-generative-models-need-to-be-stochastic" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Do generative models need to be stochastic?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#what-kinds-of-behaviour-can-rnns-exhibit" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;What kinds of behaviour can RNNs exhibit?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#basic-layout" style="font-size: 80%;"><b>Basic layout</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We need to specify the initial activity state of all the hidden and output units</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-can-specify-inputs-in-several-ways" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We can specify inputs in several ways</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-can-specify-targets-in-several-ways" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We can specify targets in several ways</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#backpropagation-through-time" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Backpropagation through time</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#the-backward-pass-is-linear" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;The backward pass is linear</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs014.html#the-problem-of-exploding-or-vanishing-gradients" style="font-size: 80%;"><b>The problem of exploding or vanishing gradients</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#four-effective-ways-to-learn-an-rnn" style="font-size: 80%;"><b>Four effective ways to learn an RNN</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#long-short-term-memory-lstm" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Long Short Term Memory (LSTM)</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#implementing-a-memory-cell-in-a-neural-network" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Implementing a memory cell in a neural network</a></li>
<!-- navigation toc: --> <li><a href="#an-extrapolation-example" style="font-size: 80%;"><b>An extrapolation example</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs017.html#formatting-the-data" style="font-size: 80%;"><b>Formatting the Data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs018.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;"><b>Predicting New Points With A Trained Recurrent Neural Network</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs019.html#other-things-to-try" style="font-size: 80%;"><b>Other Things to Try</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs020.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;"><b>Other Types of Recurrent Neural Networks</b></a></li>
</ul>
</li>
@@ -223,29 +209,64 @@ MathJax.Hub.Config({
<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p> <!-- add vertical space -->
<a name="part0016"></a>
<!-- !split -->
<h2 id="gradient-boosting-algorithm" class="anchor">Gradient Boosting, algorithm </h2>
<h2 id="an-extrapolation-example" class="anchor">An extrapolation example </h2>
<p>Steepest descent is however not much used, since it only optimizes \( f \) at a fixed set of \( n \) points,
so we do not learn a function that can generalize. However, we can modify the algorithm by
fitting a weak learner to approximate the negative gradient signal.
<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>
<p>Suppose we have a cost function \( C(f)=\sum_{i=0}^{n-1}L(y_i, f(x_i)) \) where \( y_i \) is our target and \( f(x_i) \) the function which is meant to model \( y_i \). The above cost function could be our standard squared-error function</p>
$$
C(\boldsymbol{y},\boldsymbol{f})=\sum_{i=0}^{n-1}(y_i-f(x_i))^2.
$$
<p>The way we proceed in an iterative fashion is to</p>
<ol>
<li> Initialize our estimate \( f_0(x) \).</li>
<li> For \( m=1:M \), we
<ol type="a"></li>
<li> compute the negative gradient vector \( \boldsymbol{u}_m = -\partial C(\boldsymbol{y},\boldsymbol{f})/\partial \boldsymbol{f}(x) \) at \( f(x) = f_{m-1}(x) \);</li>
<li> fit the so-called base-learner to the negative gradient \( h_m(u_m,x) \);</li>
<li> update the estimate \( f_m(x) = f_{m-1}(x)+h_m(u_m,x) \);</li>
</ol>
<li> The final estimate is then \( f_M(x) = \sum_{m=1}^M h_m(u_m,x) \).</li>
</ol>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight" style="background: #f8f8f8">
<pre style="line-height: 125%;"><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">&#39;VaryDimension&#39;</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>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_subarea output_stream output_stdout output_text">
</div>
</div>
</div>
</div>
</div>
<p>
<!-- navigation buttons at the bottom of the page -->
<ul class="pagination">
@@ -265,13 +286,6 @@ $$
<li><a href="._week45-bs018.html">19</a></li>
<li><a href="._week45-bs019.html">20</a></li>
<li><a href="._week45-bs020.html">21</a></li>
<li><a href="._week45-bs021.html">22</a></li>
<li><a href="._week45-bs022.html">23</a></li>
<li><a href="._week45-bs023.html">24</a></li>
<li><a href="._week45-bs024.html">25</a></li>
<li><a href="._week45-bs025.html">26</a></li>
<li><a href="">...</a></li>
<li><a href="._week45-bs039.html">40</a></li>
<li><a href="._week45-bs017.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+213 -168
View File
@@ -8,8 +8,8 @@ doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=d
<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 45: Decisions Trees, Random Forests, Bagging and Boosting">
<title>Week 45: Decisions Trees, Random Forests, Bagging and Boosting</title>
<meta name="description" content="Week 45, Recurrent Neural Networks">
<title>Week 45, Recurrent Neural Networks</title>
<!-- Bootstrap style: bootstrap -->
<!-- doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=default --html_admon=bootstrap_panel --html_output=week45-bs --no_mako -->
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
@@ -36,109 +36,101 @@ doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=d
<!-- tocinfo
{'highest level': 2,
'sections': [('Overview of week 45', 2, None, 'overview-of-week-45'),
('Brief code reminder from last wekk',
'sections': [('Plan for week 45', 2, None, 'plan-for-week-45'),
('Material for the lab sessions, additional ways to present '
'classification results and other practicalities',
2,
None,
'brief-code-reminder-from-last-wekk'),
("Boosting, a Bird's Eye View",
'material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities'),
('Searching for Optimal Regularization Parameters $\\lambda$',
2,
None,
'boosting-a-bird-s-eye-view'),
('What is boosting? Additive Modelling/Iterative Fitting',
'searching-for-optimal-regularization-parameters-lambda'),
('Grid Search', 2, None, 'grid-search'),
('Randomized Grid Search', 2, None, 'randomized-grid-search'),
('Wisconsin Cancer Data', 2, None, 'wisconsin-cancer-data'),
('Using the correlation matrix',
2,
None,
'what-is-boosting-additive-modelling-iterative-fitting'),
('Iterative Fitting, Regression and Squared-error Cost Function',
'using-the-correlation-matrix'),
('Discussing the correlation data',
2,
None,
'iterative-fitting-regression-and-squared-error-cost-function'),
('Squared-Error Example and Iterative Fitting',
'discussing-the-correlation-data'),
('Other measures in classification studies: Cancer Data again',
2,
None,
'squared-error-example-and-iterative-fitting'),
('Iterative Fitting, Classification and AdaBoost',
'other-measures-in-classification-studies-cancer-data-again'),
('Material for Lecture Thursday November 9',
2,
None,
'iterative-fitting-classification-and-adaboost'),
('Adaptive Boosting, AdaBoost',
'material-for-lecture-thursday-november-9'),
('Recurrent neural networks (RNNs): Overarching view',
2,
None,
'adaptive-boosting-adaboost'),
('Building up AdaBoost', 2, None, 'building-up-adaboost'),
('Adaptive boosting: AdaBoost, Basic Algorithm',
'recurrent-neural-networks-rnns-overarching-view'),
('A simple example', 2, None, 'a-simple-example'),
('Memoryless models', 3, None, 'memoryless-models'),
('Linear dynamical model', 3, None, 'linear-dynamical-model'),
('Hidden Markov Models', 3, None, 'hidden-markov-models'),
('RNNs', 3, None, 'rnns'),
('Do generative models need to be stochastic?',
3,
None,
'do-generative-models-need-to-be-stochastic'),
('What kinds of behaviour can RNNs exhibit?',
3,
None,
'what-kinds-of-behaviour-can-rnns-exhibit'),
('Basic layout', 2, None, 'basic-layout'),
('We need to specify the initial activity state of all the '
'hidden and output units',
3,
None,
'we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units'),
('We can specify inputs in several ways',
3,
None,
'we-can-specify-inputs-in-several-ways'),
('We can specify targets in several ways',
3,
None,
'we-can-specify-targets-in-several-ways'),
('Backpropagation through time',
3,
None,
'backpropagation-through-time'),
('The backward pass is linear',
3,
None,
'the-backward-pass-is-linear'),
('The problem of exploding or vanishing gradients',
2,
None,
'adaptive-boosting-adaboost-basic-algorithm'),
('Basic Steps of AdaBoost', 2, None, 'basic-steps-of-adaboost'),
('AdaBoost Examples', 2, None, 'adaboost-examples'),
('Gradient boosting: Basics with Steepest Descent/Functional '
'Gradient Descent',
'the-problem-of-exploding-or-vanishing-gradients'),
('Four effective ways to learn an RNN',
2,
None,
'gradient-boosting-basics-with-steepest-descent-functional-gradient-descent'),
('The Squared-Error again! Steepest Descent',
'four-effective-ways-to-learn-an-rnn'),
('Long Short Term Memory (LSTM)',
3,
None,
'long-short-term-memory-lstm'),
('Implementing a memory cell in a neural network',
3,
None,
'implementing-a-memory-cell-in-a-neural-network'),
('An extrapolation example', 2, None, 'an-extrapolation-example'),
('Formatting the Data', 2, None, 'formatting-the-data'),
('Predicting New Points With A Trained Recurrent Neural Network',
2,
None,
'the-squared-error-again-steepest-descent'),
('Steepest Descent Example', 2, None, 'steepest-descent-example'),
('Gradient Boosting, algorithm',
'predicting-new-points-with-a-trained-recurrent-neural-network'),
('Other Things to Try', 2, None, 'other-things-to-try'),
('Other Types of Recurrent Neural Networks',
2,
None,
'gradient-boosting-algorithm'),
('Gradient Boosting, Examples of Regression',
2,
None,
'gradient-boosting-examples-of-regression'),
('Gradient Boosting, Classification Example',
2,
None,
'gradient-boosting-classification-example'),
('XGBoost: Extreme Gradient Boosting',
2,
None,
'xgboost-extreme-gradient-boosting'),
('Regression Case', 2, None, 'regression-case'),
('Xgboost on the Cancer Data',
2,
None,
'xgboost-on-the-cancer-data'),
('Support Vector Machines, overarching aims',
2,
None,
'support-vector-machines-overarching-aims'),
('Hyperplanes and all that', 2, None, 'hyperplanes-and-all-that'),
('What is a hyperplane?', 2, None, 'what-is-a-hyperplane'),
('A $p$-dimensional space of features',
2,
None,
'a-p-dimensional-space-of-features'),
('The two-dimensional case', 2, None, 'the-two-dimensional-case'),
('Getting into the details', 2, None, 'getting-into-the-details'),
('First attempt at a minimization approach',
2,
None,
'first-attempt-at-a-minimization-approach'),
('Solving the equations', 2, None, 'solving-the-equations'),
('Code Example', 2, None, 'code-example'),
('Problems with the Simpler Approach',
2,
None,
'problems-with-the-simpler-approach'),
('A better approach', 2, None, 'a-better-approach'),
('A quick Reminder on Lagrangian Multipliers',
2,
None,
'a-quick-reminder-on-lagrangian-multipliers'),
('Adding the Multiplier', 2, None, 'adding-the-multiplier'),
('Setting up the Problem', 2, None, 'setting-up-the-problem'),
('The problem to solve', 2, None, 'the-problem-to-solve'),
('The last steps', 2, None, 'the-last-steps'),
('A soft classifier', 2, None, 'a-soft-classifier'),
('Soft optmization problem',
2,
None,
'soft-optmization-problem')]}
'other-types-of-recurrent-neural-networks')]}
end of tocinfo -->
<body>
@@ -166,52 +158,46 @@ MathJax.Hub.Config({
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="week45-bs.html">Week 45: Decisions Trees, Random Forests, Bagging and Boosting</a>
<a class="navbar-brand" href="week45-bs.html">Week 45, Recurrent Neural Networks</a>
</div>
<div class="navbar-collapse collapse navbar-responsive-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
<ul class="dropdown-menu">
<!-- navigation toc: --> <li><a href="._week45-bs001.html#overview-of-week-45" style="font-size: 80%;">Overview of week 45</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs002.html#brief-code-reminder-from-last-wekk" style="font-size: 80%;">Brief code reminder from last wekk</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs003.html#boosting-a-bird-s-eye-view" style="font-size: 80%;">Boosting, a Bird's Eye View</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs004.html#what-is-boosting-additive-modelling-iterative-fitting" style="font-size: 80%;">What is boosting? Additive Modelling/Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs005.html#iterative-fitting-regression-and-squared-error-cost-function" style="font-size: 80%;">Iterative Fitting, Regression and Squared-error Cost Function</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs006.html#squared-error-example-and-iterative-fitting" style="font-size: 80%;">Squared-Error Example and Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs007.html#iterative-fitting-classification-and-adaboost" style="font-size: 80%;">Iterative Fitting, Classification and AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs008.html#adaptive-boosting-adaboost" style="font-size: 80%;">Adaptive Boosting, AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs009.html#building-up-adaboost" style="font-size: 80%;">Building up AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs010.html#adaptive-boosting-adaboost-basic-algorithm" style="font-size: 80%;">Adaptive boosting: AdaBoost, Basic Algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs011.html#basic-steps-of-adaboost" style="font-size: 80%;">Basic Steps of AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#adaboost-examples" style="font-size: 80%;">AdaBoost Examples</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#gradient-boosting-basics-with-steepest-descent-functional-gradient-descent" style="font-size: 80%;">Gradient boosting: Basics with Steepest Descent/Functional Gradient Descent</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs014.html#the-squared-error-again-steepest-descent" style="font-size: 80%;">The Squared-Error again! Steepest Descent</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#steepest-descent-example" style="font-size: 80%;">Steepest Descent Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs016.html#gradient-boosting-algorithm" style="font-size: 80%;">Gradient Boosting, algorithm</a></li>
<!-- navigation toc: --> <li><a href="#gradient-boosting-examples-of-regression" style="font-size: 80%;">Gradient Boosting, Examples of Regression</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs018.html#gradient-boosting-classification-example" style="font-size: 80%;">Gradient Boosting, Classification Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs019.html#xgboost-extreme-gradient-boosting" style="font-size: 80%;">XGBoost: Extreme Gradient Boosting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs020.html#regression-case" style="font-size: 80%;">Regression Case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs021.html#xgboost-on-the-cancer-data" style="font-size: 80%;">Xgboost on the Cancer Data</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs022.html#support-vector-machines-overarching-aims" style="font-size: 80%;">Support Vector Machines, overarching aims</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs023.html#hyperplanes-and-all-that" style="font-size: 80%;">Hyperplanes and all that</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs024.html#what-is-a-hyperplane" style="font-size: 80%;">What is a hyperplane?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs025.html#a-p-dimensional-space-of-features" style="font-size: 80%;">A \( p \)-dimensional space of features</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs026.html#the-two-dimensional-case" style="font-size: 80%;">The two-dimensional case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs027.html#getting-into-the-details" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs028.html#first-attempt-at-a-minimization-approach" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs029.html#solving-the-equations" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs030.html#code-example" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs031.html#problems-with-the-simpler-approach" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs032.html#a-better-approach" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs033.html#a-quick-reminder-on-lagrangian-multipliers" style="font-size: 80%;">A quick Reminder on Lagrangian Multipliers</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs034.html#adding-the-multiplier" style="font-size: 80%;">Adding the Multiplier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs035.html#setting-up-the-problem" style="font-size: 80%;">Setting up the Problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs036.html#the-problem-to-solve" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs037.html#the-last-steps" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs038.html#a-soft-classifier" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs039.html#soft-optmization-problem" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs001.html#plan-for-week-45" style="font-size: 80%;"><b>Plan for week 45</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs002.html#material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities" style="font-size: 80%;"><b>Material for the lab sessions, additional ways to present classification results and other practicalities</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs003.html#searching-for-optimal-regularization-parameters-lambda" style="font-size: 80%;"><b>Searching for Optimal Regularization Parameters \( \lambda \)</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs004.html#grid-search" style="font-size: 80%;"><b>Grid Search</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs005.html#randomized-grid-search" style="font-size: 80%;"><b>Randomized Grid Search</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs006.html#wisconsin-cancer-data" style="font-size: 80%;"><b>Wisconsin Cancer Data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs007.html#using-the-correlation-matrix" style="font-size: 80%;"><b>Using the correlation matrix</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs008.html#discussing-the-correlation-data" style="font-size: 80%;"><b>Discussing the correlation data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs009.html#other-measures-in-classification-studies-cancer-data-again" style="font-size: 80%;"><b>Other measures in classification studies: Cancer Data again</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs010.html#material-for-lecture-thursday-november-9" style="font-size: 80%;"><b>Material for Lecture Thursday November 9</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs011.html#recurrent-neural-networks-rnns-overarching-view" style="font-size: 80%;"><b>Recurrent neural networks (RNNs): Overarching view</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#a-simple-example" style="font-size: 80%;"><b>A simple example</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#memoryless-models" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Memoryless models</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#linear-dynamical-model" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Linear dynamical model</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#hidden-markov-models" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Hidden Markov Models</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#rnns" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;RNNs</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#do-generative-models-need-to-be-stochastic" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Do generative models need to be stochastic?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#what-kinds-of-behaviour-can-rnns-exhibit" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;What kinds of behaviour can RNNs exhibit?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#basic-layout" style="font-size: 80%;"><b>Basic layout</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We need to specify the initial activity state of all the hidden and output units</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-can-specify-inputs-in-several-ways" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We can specify inputs in several ways</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-can-specify-targets-in-several-ways" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We can specify targets in several ways</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#backpropagation-through-time" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Backpropagation through time</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#the-backward-pass-is-linear" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;The backward pass is linear</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs014.html#the-problem-of-exploding-or-vanishing-gradients" style="font-size: 80%;"><b>The problem of exploding or vanishing gradients</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#four-effective-ways-to-learn-an-rnn" style="font-size: 80%;"><b>Four effective ways to learn an RNN</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#long-short-term-memory-lstm" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Long Short Term Memory (LSTM)</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#implementing-a-memory-cell-in-a-neural-network" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Implementing a memory cell in a neural network</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs016.html#an-extrapolation-example" style="font-size: 80%;"><b>An extrapolation example</b></a></li>
<!-- navigation toc: --> <li><a href="#formatting-the-data" style="font-size: 80%;"><b>Formatting the Data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs018.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;"><b>Predicting New Points With A Trained Recurrent Neural Network</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs019.html#other-things-to-try" style="font-size: 80%;"><b>Other Things to Try</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs020.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;"><b>Other Types of Recurrent Neural Networks</b></a></li>
</ul>
</li>
@@ -223,7 +209,43 @@ MathJax.Hub.Config({
<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p> <!-- add vertical space -->
<a name="part0017"></a>
<!-- !split -->
<h2 id="gradient-boosting-examples-of-regression" class="anchor">Gradient Boosting, Examples of Regression </h2>
<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>
<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. -->
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="cell border-box-sizing code_cell rendered">
@@ -231,47 +253,78 @@ MathJax.Hub.Config({
<div class="inner_cell">
<div class="input_area">
<div class="highlight" style="background: #f8f8f8">
<pre style="line-height: 125%;"><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">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">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> train_test_split
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.ensemble</span> <span style="color: #008000; font-weight: bold">import</span> GradientBoostingRegressor
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">scikitplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">skplt</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.metrics</span> <span style="color: #008000; font-weight: bold">import</span> mean_squared_error
<pre style="line-height: 125%;"><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">&quot;&quot;&quot;</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"> &quot;&quot;&quot;</span>
n <span style="color: #666666">=</span> <span style="color: #666666">100</span>
maxdegree <span style="color: #666666">=</span> <span style="color: #666666">6</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: #408080; font-style: italic"># Make data set.</span>
x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linspace(<span style="color: #666666">-3</span>, <span style="color: #666666">3</span>, n)<span style="color: #666666">.</span>reshape(<span style="color: #666666">-1</span>, <span style="color: #666666">1</span>)
y <span style="color: #666666">=</span> np<span style="color: #666666">.</span>exp(<span style="color: #666666">-</span>x<span style="color: #666666">**2</span>) <span style="color: #666666">+</span> <span style="color: #666666">1.5</span> <span style="color: #666666">*</span> np<span style="color: #666666">.</span>exp(<span style="color: #666666">-</span>(x<span style="color: #666666">-2</span>)<span style="color: #666666">**2</span>)<span style="color: #666666">+</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>normal(<span style="color: #666666">0</span>, <span style="color: #666666">0.1</span>, x<span style="color: #666666">.</span>shape)
<span style="color: #008000; font-weight: bold">return</span> rnn_input, rnn_output
error <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros(maxdegree)
bias <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros(maxdegree)
variance <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros(maxdegree)
polydegree <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros(maxdegree)
X_train, X_test, y_train, y_test <span style="color: #666666">=</span> train_test_split(x, y, test_size<span style="color: #666666">=0.2</span>)
<span style="color: #008000; font-weight: bold">for</span> degree <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #666666">1</span>,maxdegree):
model <span style="color: #666666">=</span> GradientBoostingRegressor(max_depth<span style="color: #666666">=</span>degree, n_estimators<span style="color: #666666">=100</span>, learning_rate<span style="color: #666666">=1.0</span>)
model<span style="color: #666666">.</span>fit(X_train,y_train)
y_pred <span style="color: #666666">=</span> model<span style="color: #666666">.</span>predict(X_test)
polydegree[degree] <span style="color: #666666">=</span> degree
error[degree] <span style="color: #666666">=</span> np<span style="color: #666666">.</span>mean( np<span style="color: #666666">.</span>mean((y_test <span style="color: #666666">-</span> y_pred)<span style="color: #666666">**2</span>) )
bias[degree] <span style="color: #666666">=</span> np<span style="color: #666666">.</span>mean( (y_test <span style="color: #666666">-</span> np<span style="color: #666666">.</span>mean(y_pred))<span style="color: #666666">**2</span> )
variance[degree] <span style="color: #666666">=</span> np<span style="color: #666666">.</span>mean( np<span style="color: #666666">.</span>var(y_pred) )
<span style="color: #008000">print</span>(<span style="color: #BA2121">&#39;Max depth:&#39;</span>, degree)
<span style="color: #008000">print</span>(<span style="color: #BA2121">&#39;Error:&#39;</span>, error[degree])
<span style="color: #008000">print</span>(<span style="color: #BA2121">&#39;Bias^2:&#39;</span>, bias[degree])
<span style="color: #008000">print</span>(<span style="color: #BA2121">&#39;Var:&#39;</span>, variance[degree])
<span style="color: #008000">print</span>(<span style="color: #BA2121">&#39;</span><span style="color: #BB6688; font-weight: bold">{}</span><span style="color: #BA2121"> &gt;= </span><span style="color: #BB6688; font-weight: bold">{}</span><span style="color: #BA2121"> + </span><span style="color: #BB6688; font-weight: bold">{}</span><span style="color: #BA2121"> = </span><span style="color: #BB6688; font-weight: bold">{}</span><span style="color: #BA2121">&#39;</span><span style="color: #666666">.</span>format(error[degree], bias[degree], variance[degree], bias[degree]<span style="color: #666666">+</span>variance[degree]))
<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>
plt<span style="color: #666666">.</span>xlim(<span style="color: #666666">1</span>,maxdegree<span style="color: #666666">-1</span>)
plt<span style="color: #666666">.</span>plot(polydegree, error, label<span style="color: #666666">=</span><span style="color: #BA2121">&#39;Error&#39;</span>)
plt<span style="color: #666666">.</span>plot(polydegree, bias, label<span style="color: #666666">=</span><span style="color: #BA2121">&#39;bias&#39;</span>)
plt<span style="color: #666666">.</span>plot(polydegree, variance, label<span style="color: #666666">=</span><span style="color: #BA2121">&#39;Variance&#39;</span>)
plt<span style="color: #666666">.</span>legend()
save_fig(<span style="color: #BA2121">&quot;gdregression&quot;</span>)
plt<span style="color: #666666">.</span>show()
<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">&quot;&quot;&quot;</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 &quot;x data&quot;. 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"> &quot;&quot;&quot;</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">&quot;RNN&quot;</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">&quot;dense&quot;</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">&quot;mean_squared_error&quot;</span>, optimizer<span style="color: #666666">=</span><span style="color: #BA2121">&quot;adam&quot;</span>)
<span style="color: #008000; font-weight: bold">return</span> model
</pre>
</div>
</div>
@@ -306,14 +359,6 @@ plt<span style="color: #666666">.</span>show()
<li><a href="._week45-bs018.html">19</a></li>
<li><a href="._week45-bs019.html">20</a></li>
<li><a href="._week45-bs020.html">21</a></li>
<li><a href="._week45-bs021.html">22</a></li>
<li><a href="._week45-bs022.html">23</a></li>
<li><a href="._week45-bs023.html">24</a></li>
<li><a href="._week45-bs024.html">25</a></li>
<li><a href="._week45-bs025.html">26</a></li>
<li><a href="._week45-bs026.html">27</a></li>
<li><a href="">...</a></li>
<li><a href="._week45-bs039.html">40</a></li>
<li><a href="._week45-bs018.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+197 -168
View File
@@ -8,8 +8,8 @@ doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=d
<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 45: Decisions Trees, Random Forests, Bagging and Boosting">
<title>Week 45: Decisions Trees, Random Forests, Bagging and Boosting</title>
<meta name="description" content="Week 45, Recurrent Neural Networks">
<title>Week 45, Recurrent Neural Networks</title>
<!-- Bootstrap style: bootstrap -->
<!-- doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=default --html_admon=bootstrap_panel --html_output=week45-bs --no_mako -->
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
@@ -36,109 +36,101 @@ doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=d
<!-- tocinfo
{'highest level': 2,
'sections': [('Overview of week 45', 2, None, 'overview-of-week-45'),
('Brief code reminder from last wekk',
'sections': [('Plan for week 45', 2, None, 'plan-for-week-45'),
('Material for the lab sessions, additional ways to present '
'classification results and other practicalities',
2,
None,
'brief-code-reminder-from-last-wekk'),
("Boosting, a Bird's Eye View",
'material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities'),
('Searching for Optimal Regularization Parameters $\\lambda$',
2,
None,
'boosting-a-bird-s-eye-view'),
('What is boosting? Additive Modelling/Iterative Fitting',
'searching-for-optimal-regularization-parameters-lambda'),
('Grid Search', 2, None, 'grid-search'),
('Randomized Grid Search', 2, None, 'randomized-grid-search'),
('Wisconsin Cancer Data', 2, None, 'wisconsin-cancer-data'),
('Using the correlation matrix',
2,
None,
'what-is-boosting-additive-modelling-iterative-fitting'),
('Iterative Fitting, Regression and Squared-error Cost Function',
'using-the-correlation-matrix'),
('Discussing the correlation data',
2,
None,
'iterative-fitting-regression-and-squared-error-cost-function'),
('Squared-Error Example and Iterative Fitting',
'discussing-the-correlation-data'),
('Other measures in classification studies: Cancer Data again',
2,
None,
'squared-error-example-and-iterative-fitting'),
('Iterative Fitting, Classification and AdaBoost',
'other-measures-in-classification-studies-cancer-data-again'),
('Material for Lecture Thursday November 9',
2,
None,
'iterative-fitting-classification-and-adaboost'),
('Adaptive Boosting, AdaBoost',
'material-for-lecture-thursday-november-9'),
('Recurrent neural networks (RNNs): Overarching view',
2,
None,
'adaptive-boosting-adaboost'),
('Building up AdaBoost', 2, None, 'building-up-adaboost'),
('Adaptive boosting: AdaBoost, Basic Algorithm',
'recurrent-neural-networks-rnns-overarching-view'),
('A simple example', 2, None, 'a-simple-example'),
('Memoryless models', 3, None, 'memoryless-models'),
('Linear dynamical model', 3, None, 'linear-dynamical-model'),
('Hidden Markov Models', 3, None, 'hidden-markov-models'),
('RNNs', 3, None, 'rnns'),
('Do generative models need to be stochastic?',
3,
None,
'do-generative-models-need-to-be-stochastic'),
('What kinds of behaviour can RNNs exhibit?',
3,
None,
'what-kinds-of-behaviour-can-rnns-exhibit'),
('Basic layout', 2, None, 'basic-layout'),
('We need to specify the initial activity state of all the '
'hidden and output units',
3,
None,
'we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units'),
('We can specify inputs in several ways',
3,
None,
'we-can-specify-inputs-in-several-ways'),
('We can specify targets in several ways',
3,
None,
'we-can-specify-targets-in-several-ways'),
('Backpropagation through time',
3,
None,
'backpropagation-through-time'),
('The backward pass is linear',
3,
None,
'the-backward-pass-is-linear'),
('The problem of exploding or vanishing gradients',
2,
None,
'adaptive-boosting-adaboost-basic-algorithm'),
('Basic Steps of AdaBoost', 2, None, 'basic-steps-of-adaboost'),
('AdaBoost Examples', 2, None, 'adaboost-examples'),
('Gradient boosting: Basics with Steepest Descent/Functional '
'Gradient Descent',
'the-problem-of-exploding-or-vanishing-gradients'),
('Four effective ways to learn an RNN',
2,
None,
'gradient-boosting-basics-with-steepest-descent-functional-gradient-descent'),
('The Squared-Error again! Steepest Descent',
'four-effective-ways-to-learn-an-rnn'),
('Long Short Term Memory (LSTM)',
3,
None,
'long-short-term-memory-lstm'),
('Implementing a memory cell in a neural network',
3,
None,
'implementing-a-memory-cell-in-a-neural-network'),
('An extrapolation example', 2, None, 'an-extrapolation-example'),
('Formatting the Data', 2, None, 'formatting-the-data'),
('Predicting New Points With A Trained Recurrent Neural Network',
2,
None,
'the-squared-error-again-steepest-descent'),
('Steepest Descent Example', 2, None, 'steepest-descent-example'),
('Gradient Boosting, algorithm',
'predicting-new-points-with-a-trained-recurrent-neural-network'),
('Other Things to Try', 2, None, 'other-things-to-try'),
('Other Types of Recurrent Neural Networks',
2,
None,
'gradient-boosting-algorithm'),
('Gradient Boosting, Examples of Regression',
2,
None,
'gradient-boosting-examples-of-regression'),
('Gradient Boosting, Classification Example',
2,
None,
'gradient-boosting-classification-example'),
('XGBoost: Extreme Gradient Boosting',
2,
None,
'xgboost-extreme-gradient-boosting'),
('Regression Case', 2, None, 'regression-case'),
('Xgboost on the Cancer Data',
2,
None,
'xgboost-on-the-cancer-data'),
('Support Vector Machines, overarching aims',
2,
None,
'support-vector-machines-overarching-aims'),
('Hyperplanes and all that', 2, None, 'hyperplanes-and-all-that'),
('What is a hyperplane?', 2, None, 'what-is-a-hyperplane'),
('A $p$-dimensional space of features',
2,
None,
'a-p-dimensional-space-of-features'),
('The two-dimensional case', 2, None, 'the-two-dimensional-case'),
('Getting into the details', 2, None, 'getting-into-the-details'),
('First attempt at a minimization approach',
2,
None,
'first-attempt-at-a-minimization-approach'),
('Solving the equations', 2, None, 'solving-the-equations'),
('Code Example', 2, None, 'code-example'),
('Problems with the Simpler Approach',
2,
None,
'problems-with-the-simpler-approach'),
('A better approach', 2, None, 'a-better-approach'),
('A quick Reminder on Lagrangian Multipliers',
2,
None,
'a-quick-reminder-on-lagrangian-multipliers'),
('Adding the Multiplier', 2, None, 'adding-the-multiplier'),
('Setting up the Problem', 2, None, 'setting-up-the-problem'),
('The problem to solve', 2, None, 'the-problem-to-solve'),
('The last steps', 2, None, 'the-last-steps'),
('A soft classifier', 2, None, 'a-soft-classifier'),
('Soft optmization problem',
2,
None,
'soft-optmization-problem')]}
'other-types-of-recurrent-neural-networks')]}
end of tocinfo -->
<body>
@@ -166,52 +158,46 @@ MathJax.Hub.Config({
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="week45-bs.html">Week 45: Decisions Trees, Random Forests, Bagging and Boosting</a>
<a class="navbar-brand" href="week45-bs.html">Week 45, Recurrent Neural Networks</a>
</div>
<div class="navbar-collapse collapse navbar-responsive-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
<ul class="dropdown-menu">
<!-- navigation toc: --> <li><a href="._week45-bs001.html#overview-of-week-45" style="font-size: 80%;">Overview of week 45</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs002.html#brief-code-reminder-from-last-wekk" style="font-size: 80%;">Brief code reminder from last wekk</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs003.html#boosting-a-bird-s-eye-view" style="font-size: 80%;">Boosting, a Bird's Eye View</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs004.html#what-is-boosting-additive-modelling-iterative-fitting" style="font-size: 80%;">What is boosting? Additive Modelling/Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs005.html#iterative-fitting-regression-and-squared-error-cost-function" style="font-size: 80%;">Iterative Fitting, Regression and Squared-error Cost Function</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs006.html#squared-error-example-and-iterative-fitting" style="font-size: 80%;">Squared-Error Example and Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs007.html#iterative-fitting-classification-and-adaboost" style="font-size: 80%;">Iterative Fitting, Classification and AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs008.html#adaptive-boosting-adaboost" style="font-size: 80%;">Adaptive Boosting, AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs009.html#building-up-adaboost" style="font-size: 80%;">Building up AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs010.html#adaptive-boosting-adaboost-basic-algorithm" style="font-size: 80%;">Adaptive boosting: AdaBoost, Basic Algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs011.html#basic-steps-of-adaboost" style="font-size: 80%;">Basic Steps of AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#adaboost-examples" style="font-size: 80%;">AdaBoost Examples</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#gradient-boosting-basics-with-steepest-descent-functional-gradient-descent" style="font-size: 80%;">Gradient boosting: Basics with Steepest Descent/Functional Gradient Descent</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs014.html#the-squared-error-again-steepest-descent" style="font-size: 80%;">The Squared-Error again! Steepest Descent</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#steepest-descent-example" style="font-size: 80%;">Steepest Descent Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs016.html#gradient-boosting-algorithm" style="font-size: 80%;">Gradient Boosting, algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs017.html#gradient-boosting-examples-of-regression" style="font-size: 80%;">Gradient Boosting, Examples of Regression</a></li>
<!-- navigation toc: --> <li><a href="#gradient-boosting-classification-example" style="font-size: 80%;">Gradient Boosting, Classification Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs019.html#xgboost-extreme-gradient-boosting" style="font-size: 80%;">XGBoost: Extreme Gradient Boosting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs020.html#regression-case" style="font-size: 80%;">Regression Case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs021.html#xgboost-on-the-cancer-data" style="font-size: 80%;">Xgboost on the Cancer Data</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs022.html#support-vector-machines-overarching-aims" style="font-size: 80%;">Support Vector Machines, overarching aims</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs023.html#hyperplanes-and-all-that" style="font-size: 80%;">Hyperplanes and all that</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs024.html#what-is-a-hyperplane" style="font-size: 80%;">What is a hyperplane?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs025.html#a-p-dimensional-space-of-features" style="font-size: 80%;">A \( p \)-dimensional space of features</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs026.html#the-two-dimensional-case" style="font-size: 80%;">The two-dimensional case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs027.html#getting-into-the-details" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs028.html#first-attempt-at-a-minimization-approach" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs029.html#solving-the-equations" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs030.html#code-example" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs031.html#problems-with-the-simpler-approach" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs032.html#a-better-approach" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs033.html#a-quick-reminder-on-lagrangian-multipliers" style="font-size: 80%;">A quick Reminder on Lagrangian Multipliers</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs034.html#adding-the-multiplier" style="font-size: 80%;">Adding the Multiplier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs035.html#setting-up-the-problem" style="font-size: 80%;">Setting up the Problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs036.html#the-problem-to-solve" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs037.html#the-last-steps" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs038.html#a-soft-classifier" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs039.html#soft-optmization-problem" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs001.html#plan-for-week-45" style="font-size: 80%;"><b>Plan for week 45</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs002.html#material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities" style="font-size: 80%;"><b>Material for the lab sessions, additional ways to present classification results and other practicalities</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs003.html#searching-for-optimal-regularization-parameters-lambda" style="font-size: 80%;"><b>Searching for Optimal Regularization Parameters \( \lambda \)</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs004.html#grid-search" style="font-size: 80%;"><b>Grid Search</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs005.html#randomized-grid-search" style="font-size: 80%;"><b>Randomized Grid Search</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs006.html#wisconsin-cancer-data" style="font-size: 80%;"><b>Wisconsin Cancer Data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs007.html#using-the-correlation-matrix" style="font-size: 80%;"><b>Using the correlation matrix</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs008.html#discussing-the-correlation-data" style="font-size: 80%;"><b>Discussing the correlation data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs009.html#other-measures-in-classification-studies-cancer-data-again" style="font-size: 80%;"><b>Other measures in classification studies: Cancer Data again</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs010.html#material-for-lecture-thursday-november-9" style="font-size: 80%;"><b>Material for Lecture Thursday November 9</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs011.html#recurrent-neural-networks-rnns-overarching-view" style="font-size: 80%;"><b>Recurrent neural networks (RNNs): Overarching view</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#a-simple-example" style="font-size: 80%;"><b>A simple example</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#memoryless-models" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Memoryless models</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#linear-dynamical-model" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Linear dynamical model</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#hidden-markov-models" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Hidden Markov Models</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#rnns" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;RNNs</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#do-generative-models-need-to-be-stochastic" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Do generative models need to be stochastic?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#what-kinds-of-behaviour-can-rnns-exhibit" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;What kinds of behaviour can RNNs exhibit?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#basic-layout" style="font-size: 80%;"><b>Basic layout</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We need to specify the initial activity state of all the hidden and output units</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-can-specify-inputs-in-several-ways" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We can specify inputs in several ways</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-can-specify-targets-in-several-ways" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We can specify targets in several ways</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#backpropagation-through-time" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Backpropagation through time</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#the-backward-pass-is-linear" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;The backward pass is linear</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs014.html#the-problem-of-exploding-or-vanishing-gradients" style="font-size: 80%;"><b>The problem of exploding or vanishing gradients</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#four-effective-ways-to-learn-an-rnn" style="font-size: 80%;"><b>Four effective ways to learn an RNN</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#long-short-term-memory-lstm" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Long Short Term Memory (LSTM)</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#implementing-a-memory-cell-in-a-neural-network" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Implementing a memory cell in a neural network</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs016.html#an-extrapolation-example" style="font-size: 80%;"><b>An extrapolation example</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs017.html#formatting-the-data" style="font-size: 80%;"><b>Formatting the Data</b></a></li>
<!-- navigation toc: --> <li><a href="#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;"><b>Predicting New Points With A Trained Recurrent Neural Network</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs019.html#other-things-to-try" style="font-size: 80%;"><b>Other Things to Try</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs020.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;"><b>Other Types of Recurrent Neural Networks</b></a></li>
</ul>
</li>
@@ -223,7 +209,8 @@ MathJax.Hub.Config({
<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p> <!-- add vertical space -->
<a name="part0018"></a>
<!-- !split -->
<h2 id="gradient-boosting-classification-example" class="anchor">Gradient Boosting, Classification Example </h2>
<h2 id="predicting-new-points-with-a-trained-recurrent-neural-network" class="anchor">Predicting New Points With A Trained Recurrent Neural Network </h2>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="cell border-box-sizing code_cell rendered">
@@ -231,46 +218,97 @@ MathJax.Hub.Config({
<div class="inner_cell">
<div class="input_area">
<div class="highlight" style="background: #f8f8f8">
<pre style="line-height: 125%;"><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">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">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> train_test_split
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.datasets</span> <span style="color: #008000; font-weight: bold">import</span> load_breast_cancer
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">scikitplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">skplt</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.ensemble</span> <span style="color: #008000; font-weight: bold">import</span> GradientBoostingClassifier
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> cross_validate
<pre style="line-height: 125%;"><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">&quot;&quot;&quot;</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"> &quot;&quot;&quot;</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"># Load the data</span>
cancer <span style="color: #666666">=</span> load_breast_cancer()
<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>
X_train, X_test, y_train, y_test <span style="color: #666666">=</span> train_test_split(cancer<span style="color: #666666">.</span>data,cancer<span style="color: #666666">.</span>target,random_state<span style="color: #666666">=0</span>)
<span style="color: #008000">print</span>(X_train<span style="color: #666666">.</span>shape)
<span style="color: #008000">print</span>(X_test<span style="color: #666666">.</span>shape)
<span style="color: #408080; font-style: italic">#now scale the data</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> StandardScaler
scaler <span style="color: #666666">=</span> StandardScaler()
scaler<span style="color: #666666">.</span>fit(X_train)
X_train_scaled <span style="color: #666666">=</span> scaler<span style="color: #666666">.</span>transform(X_train)
X_test_scaled <span style="color: #666666">=</span> scaler<span style="color: #666666">.</span>transform(X_test)
<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">&#39;MSE: &#39;</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">&#39;Predicted&#39;</span><span style="color: #666666">+</span><span style="color: #008000">str</span>(dim)<span style="color: #666666">+</span><span style="color: #BA2121">&#39;.csv&#39;</span>
np<span style="color: #666666">.</span>savetxt(name, y_pred, delimiter<span style="color: #666666">=</span><span style="color: #BA2121">&#39;,&#39;</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">&quot;true&quot;</span>, linewidth<span style="color: #666666">=3</span>)
ax<span style="color: #666666">.</span>plot(x1, y_pred, <span style="color: #BA2121">&#39;g-.&#39;</span>,label<span style="color: #666666">=</span><span style="color: #BA2121">&quot;predicted&quot;</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">&#39;red&#39;</span>)
plt<span style="color: #666666">.</span>show()
gd_clf <span style="color: #666666">=</span> GradientBoostingClassifier(max_depth<span style="color: #666666">=3</span>, n_estimators<span style="color: #666666">=100</span>, learning_rate<span style="color: #666666">=1.0</span>)
gd_clf<span style="color: #666666">.</span>fit(X_train_scaled, y_train)
<span style="color: #408080; font-style: italic">#Cross validation</span>
accuracy <span style="color: #666666">=</span> cross_validate(gd_clf,X_test_scaled,y_test,cv<span style="color: #666666">=10</span>)[<span style="color: #BA2121">&#39;test_score&#39;</span>]
<span style="color: #008000">print</span>(accuracy)
<span style="color: #008000">print</span>(<span style="color: #BA2121">&quot;Test set accuracy with Gradient boosting and scaled data: </span><span style="color: #BB6688; font-weight: bold">{:.2f}</span><span style="color: #BA2121">&quot;</span><span style="color: #666666">.</span>format(gd_clf<span style="color: #666666">.</span>score(X_test_scaled,y_test)))
<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: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">scikitplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">skplt</span>
y_pred <span style="color: #666666">=</span> gd_clf<span style="color: #666666">.</span>predict(X_test_scaled)
skplt<span style="color: #666666">.</span>metrics<span style="color: #666666">.</span>plot_confusion_matrix(y_test, y_pred, normalize<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">True</span>)
save_fig(<span style="color: #BA2121">&quot;gdclassiffierconfusion&quot;</span>)
plt<span style="color: #666666">.</span>show()
y_probas <span style="color: #666666">=</span> gd_clf<span style="color: #666666">.</span>predict_proba(X_test_scaled)
skplt<span style="color: #666666">.</span>metrics<span style="color: #666666">.</span>plot_roc(y_test, y_probas)
save_fig(<span style="color: #BA2121">&quot;gdclassiffierroc&quot;</span>)
plt<span style="color: #666666">.</span>show()
skplt<span style="color: #666666">.</span>metrics<span style="color: #666666">.</span>plot_cumulative_gain(y_test, y_probas)
save_fig(<span style="color: #BA2121">&quot;gdclassiffiercgain&quot;</span>)
<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(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">&quot;loss&quot;</span>,<span style="color: #BA2121">&quot;val_loss&quot;</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">&quot;loss&quot;</span>)
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">&quot;epoch&quot;</span>)
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">&quot;The final validation loss: </span><span style="color: #BB6688; font-weight: bold">{}</span><span style="color: #BA2121">&quot;</span><span style="color: #666666">.</span>format(hist<span style="color: #666666">.</span>history[<span style="color: #BA2121">&quot;val_loss&quot;</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">&#39;Time: &#39;</span>, end<span style="color: #666666">-</span>start)
</pre>
</div>
</div>
@@ -304,15 +342,6 @@ plt<span style="color: #666666">.</span>show()
<li class="active"><a href="._week45-bs018.html">19</a></li>
<li><a href="._week45-bs019.html">20</a></li>
<li><a href="._week45-bs020.html">21</a></li>
<li><a href="._week45-bs021.html">22</a></li>
<li><a href="._week45-bs022.html">23</a></li>
<li><a href="._week45-bs023.html">24</a></li>
<li><a href="._week45-bs024.html">25</a></li>
<li><a href="._week45-bs025.html">26</a></li>
<li><a href="._week45-bs026.html">27</a></li>
<li><a href="._week45-bs027.html">28</a></li>
<li><a href="">...</a></li>
<li><a href="._week45-bs039.html">40</a></li>
<li><a href="._week45-bs019.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+227 -145
View File
@@ -8,8 +8,8 @@ doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=d
<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 45: Decisions Trees, Random Forests, Bagging and Boosting">
<title>Week 45: Decisions Trees, Random Forests, Bagging and Boosting</title>
<meta name="description" content="Week 45, Recurrent Neural Networks">
<title>Week 45, Recurrent Neural Networks</title>
<!-- Bootstrap style: bootstrap -->
<!-- doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=default --html_admon=bootstrap_panel --html_output=week45-bs --no_mako -->
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
@@ -36,109 +36,101 @@ doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=d
<!-- tocinfo
{'highest level': 2,
'sections': [('Overview of week 45', 2, None, 'overview-of-week-45'),
('Brief code reminder from last wekk',
'sections': [('Plan for week 45', 2, None, 'plan-for-week-45'),
('Material for the lab sessions, additional ways to present '
'classification results and other practicalities',
2,
None,
'brief-code-reminder-from-last-wekk'),
("Boosting, a Bird's Eye View",
'material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities'),
('Searching for Optimal Regularization Parameters $\\lambda$',
2,
None,
'boosting-a-bird-s-eye-view'),
('What is boosting? Additive Modelling/Iterative Fitting',
'searching-for-optimal-regularization-parameters-lambda'),
('Grid Search', 2, None, 'grid-search'),
('Randomized Grid Search', 2, None, 'randomized-grid-search'),
('Wisconsin Cancer Data', 2, None, 'wisconsin-cancer-data'),
('Using the correlation matrix',
2,
None,
'what-is-boosting-additive-modelling-iterative-fitting'),
('Iterative Fitting, Regression and Squared-error Cost Function',
'using-the-correlation-matrix'),
('Discussing the correlation data',
2,
None,
'iterative-fitting-regression-and-squared-error-cost-function'),
('Squared-Error Example and Iterative Fitting',
'discussing-the-correlation-data'),
('Other measures in classification studies: Cancer Data again',
2,
None,
'squared-error-example-and-iterative-fitting'),
('Iterative Fitting, Classification and AdaBoost',
'other-measures-in-classification-studies-cancer-data-again'),
('Material for Lecture Thursday November 9',
2,
None,
'iterative-fitting-classification-and-adaboost'),
('Adaptive Boosting, AdaBoost',
'material-for-lecture-thursday-november-9'),
('Recurrent neural networks (RNNs): Overarching view',
2,
None,
'adaptive-boosting-adaboost'),
('Building up AdaBoost', 2, None, 'building-up-adaboost'),
('Adaptive boosting: AdaBoost, Basic Algorithm',
'recurrent-neural-networks-rnns-overarching-view'),
('A simple example', 2, None, 'a-simple-example'),
('Memoryless models', 3, None, 'memoryless-models'),
('Linear dynamical model', 3, None, 'linear-dynamical-model'),
('Hidden Markov Models', 3, None, 'hidden-markov-models'),
('RNNs', 3, None, 'rnns'),
('Do generative models need to be stochastic?',
3,
None,
'do-generative-models-need-to-be-stochastic'),
('What kinds of behaviour can RNNs exhibit?',
3,
None,
'what-kinds-of-behaviour-can-rnns-exhibit'),
('Basic layout', 2, None, 'basic-layout'),
('We need to specify the initial activity state of all the '
'hidden and output units',
3,
None,
'we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units'),
('We can specify inputs in several ways',
3,
None,
'we-can-specify-inputs-in-several-ways'),
('We can specify targets in several ways',
3,
None,
'we-can-specify-targets-in-several-ways'),
('Backpropagation through time',
3,
None,
'backpropagation-through-time'),
('The backward pass is linear',
3,
None,
'the-backward-pass-is-linear'),
('The problem of exploding or vanishing gradients',
2,
None,
'adaptive-boosting-adaboost-basic-algorithm'),
('Basic Steps of AdaBoost', 2, None, 'basic-steps-of-adaboost'),
('AdaBoost Examples', 2, None, 'adaboost-examples'),
('Gradient boosting: Basics with Steepest Descent/Functional '
'Gradient Descent',
'the-problem-of-exploding-or-vanishing-gradients'),
('Four effective ways to learn an RNN',
2,
None,
'gradient-boosting-basics-with-steepest-descent-functional-gradient-descent'),
('The Squared-Error again! Steepest Descent',
'four-effective-ways-to-learn-an-rnn'),
('Long Short Term Memory (LSTM)',
3,
None,
'long-short-term-memory-lstm'),
('Implementing a memory cell in a neural network',
3,
None,
'implementing-a-memory-cell-in-a-neural-network'),
('An extrapolation example', 2, None, 'an-extrapolation-example'),
('Formatting the Data', 2, None, 'formatting-the-data'),
('Predicting New Points With A Trained Recurrent Neural Network',
2,
None,
'the-squared-error-again-steepest-descent'),
('Steepest Descent Example', 2, None, 'steepest-descent-example'),
('Gradient Boosting, algorithm',
'predicting-new-points-with-a-trained-recurrent-neural-network'),
('Other Things to Try', 2, None, 'other-things-to-try'),
('Other Types of Recurrent Neural Networks',
2,
None,
'gradient-boosting-algorithm'),
('Gradient Boosting, Examples of Regression',
2,
None,
'gradient-boosting-examples-of-regression'),
('Gradient Boosting, Classification Example',
2,
None,
'gradient-boosting-classification-example'),
('XGBoost: Extreme Gradient Boosting',
2,
None,
'xgboost-extreme-gradient-boosting'),
('Regression Case', 2, None, 'regression-case'),
('Xgboost on the Cancer Data',
2,
None,
'xgboost-on-the-cancer-data'),
('Support Vector Machines, overarching aims',
2,
None,
'support-vector-machines-overarching-aims'),
('Hyperplanes and all that', 2, None, 'hyperplanes-and-all-that'),
('What is a hyperplane?', 2, None, 'what-is-a-hyperplane'),
('A $p$-dimensional space of features',
2,
None,
'a-p-dimensional-space-of-features'),
('The two-dimensional case', 2, None, 'the-two-dimensional-case'),
('Getting into the details', 2, None, 'getting-into-the-details'),
('First attempt at a minimization approach',
2,
None,
'first-attempt-at-a-minimization-approach'),
('Solving the equations', 2, None, 'solving-the-equations'),
('Code Example', 2, None, 'code-example'),
('Problems with the Simpler Approach',
2,
None,
'problems-with-the-simpler-approach'),
('A better approach', 2, None, 'a-better-approach'),
('A quick Reminder on Lagrangian Multipliers',
2,
None,
'a-quick-reminder-on-lagrangian-multipliers'),
('Adding the Multiplier', 2, None, 'adding-the-multiplier'),
('Setting up the Problem', 2, None, 'setting-up-the-problem'),
('The problem to solve', 2, None, 'the-problem-to-solve'),
('The last steps', 2, None, 'the-last-steps'),
('A soft classifier', 2, None, 'a-soft-classifier'),
('Soft optmization problem',
2,
None,
'soft-optmization-problem')]}
'other-types-of-recurrent-neural-networks')]}
end of tocinfo -->
<body>
@@ -166,52 +158,46 @@ MathJax.Hub.Config({
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="week45-bs.html">Week 45: Decisions Trees, Random Forests, Bagging and Boosting</a>
<a class="navbar-brand" href="week45-bs.html">Week 45, Recurrent Neural Networks</a>
</div>
<div class="navbar-collapse collapse navbar-responsive-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
<ul class="dropdown-menu">
<!-- navigation toc: --> <li><a href="._week45-bs001.html#overview-of-week-45" style="font-size: 80%;">Overview of week 45</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs002.html#brief-code-reminder-from-last-wekk" style="font-size: 80%;">Brief code reminder from last wekk</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs003.html#boosting-a-bird-s-eye-view" style="font-size: 80%;">Boosting, a Bird's Eye View</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs004.html#what-is-boosting-additive-modelling-iterative-fitting" style="font-size: 80%;">What is boosting? Additive Modelling/Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs005.html#iterative-fitting-regression-and-squared-error-cost-function" style="font-size: 80%;">Iterative Fitting, Regression and Squared-error Cost Function</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs006.html#squared-error-example-and-iterative-fitting" style="font-size: 80%;">Squared-Error Example and Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs007.html#iterative-fitting-classification-and-adaboost" style="font-size: 80%;">Iterative Fitting, Classification and AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs008.html#adaptive-boosting-adaboost" style="font-size: 80%;">Adaptive Boosting, AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs009.html#building-up-adaboost" style="font-size: 80%;">Building up AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs010.html#adaptive-boosting-adaboost-basic-algorithm" style="font-size: 80%;">Adaptive boosting: AdaBoost, Basic Algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs011.html#basic-steps-of-adaboost" style="font-size: 80%;">Basic Steps of AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#adaboost-examples" style="font-size: 80%;">AdaBoost Examples</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#gradient-boosting-basics-with-steepest-descent-functional-gradient-descent" style="font-size: 80%;">Gradient boosting: Basics with Steepest Descent/Functional Gradient Descent</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs014.html#the-squared-error-again-steepest-descent" style="font-size: 80%;">The Squared-Error again! Steepest Descent</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#steepest-descent-example" style="font-size: 80%;">Steepest Descent Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs016.html#gradient-boosting-algorithm" style="font-size: 80%;">Gradient Boosting, algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs017.html#gradient-boosting-examples-of-regression" style="font-size: 80%;">Gradient Boosting, Examples of Regression</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs018.html#gradient-boosting-classification-example" style="font-size: 80%;">Gradient Boosting, Classification Example</a></li>
<!-- navigation toc: --> <li><a href="#xgboost-extreme-gradient-boosting" style="font-size: 80%;">XGBoost: Extreme Gradient Boosting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs020.html#regression-case" style="font-size: 80%;">Regression Case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs021.html#xgboost-on-the-cancer-data" style="font-size: 80%;">Xgboost on the Cancer Data</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs022.html#support-vector-machines-overarching-aims" style="font-size: 80%;">Support Vector Machines, overarching aims</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs023.html#hyperplanes-and-all-that" style="font-size: 80%;">Hyperplanes and all that</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs024.html#what-is-a-hyperplane" style="font-size: 80%;">What is a hyperplane?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs025.html#a-p-dimensional-space-of-features" style="font-size: 80%;">A \( p \)-dimensional space of features</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs026.html#the-two-dimensional-case" style="font-size: 80%;">The two-dimensional case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs027.html#getting-into-the-details" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs028.html#first-attempt-at-a-minimization-approach" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs029.html#solving-the-equations" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs030.html#code-example" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs031.html#problems-with-the-simpler-approach" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs032.html#a-better-approach" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs033.html#a-quick-reminder-on-lagrangian-multipliers" style="font-size: 80%;">A quick Reminder on Lagrangian Multipliers</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs034.html#adding-the-multiplier" style="font-size: 80%;">Adding the Multiplier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs035.html#setting-up-the-problem" style="font-size: 80%;">Setting up the Problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs036.html#the-problem-to-solve" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs037.html#the-last-steps" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs038.html#a-soft-classifier" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs039.html#soft-optmization-problem" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs001.html#plan-for-week-45" style="font-size: 80%;"><b>Plan for week 45</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs002.html#material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities" style="font-size: 80%;"><b>Material for the lab sessions, additional ways to present classification results and other practicalities</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs003.html#searching-for-optimal-regularization-parameters-lambda" style="font-size: 80%;"><b>Searching for Optimal Regularization Parameters \( \lambda \)</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs004.html#grid-search" style="font-size: 80%;"><b>Grid Search</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs005.html#randomized-grid-search" style="font-size: 80%;"><b>Randomized Grid Search</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs006.html#wisconsin-cancer-data" style="font-size: 80%;"><b>Wisconsin Cancer Data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs007.html#using-the-correlation-matrix" style="font-size: 80%;"><b>Using the correlation matrix</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs008.html#discussing-the-correlation-data" style="font-size: 80%;"><b>Discussing the correlation data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs009.html#other-measures-in-classification-studies-cancer-data-again" style="font-size: 80%;"><b>Other measures in classification studies: Cancer Data again</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs010.html#material-for-lecture-thursday-november-9" style="font-size: 80%;"><b>Material for Lecture Thursday November 9</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs011.html#recurrent-neural-networks-rnns-overarching-view" style="font-size: 80%;"><b>Recurrent neural networks (RNNs): Overarching view</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#a-simple-example" style="font-size: 80%;"><b>A simple example</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#memoryless-models" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Memoryless models</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#linear-dynamical-model" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Linear dynamical model</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#hidden-markov-models" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Hidden Markov Models</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#rnns" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;RNNs</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#do-generative-models-need-to-be-stochastic" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Do generative models need to be stochastic?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#what-kinds-of-behaviour-can-rnns-exhibit" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;What kinds of behaviour can RNNs exhibit?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#basic-layout" style="font-size: 80%;"><b>Basic layout</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We need to specify the initial activity state of all the hidden and output units</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-can-specify-inputs-in-several-ways" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We can specify inputs in several ways</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-can-specify-targets-in-several-ways" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We can specify targets in several ways</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#backpropagation-through-time" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Backpropagation through time</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#the-backward-pass-is-linear" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;The backward pass is linear</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs014.html#the-problem-of-exploding-or-vanishing-gradients" style="font-size: 80%;"><b>The problem of exploding or vanishing gradients</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#four-effective-ways-to-learn-an-rnn" style="font-size: 80%;"><b>Four effective ways to learn an RNN</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#long-short-term-memory-lstm" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Long Short Term Memory (LSTM)</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#implementing-a-memory-cell-in-a-neural-network" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Implementing a memory cell in a neural network</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs016.html#an-extrapolation-example" style="font-size: 80%;"><b>An extrapolation example</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs017.html#formatting-the-data" style="font-size: 80%;"><b>Formatting the Data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs018.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;"><b>Predicting New Points With A Trained Recurrent Neural Network</b></a></li>
<!-- navigation toc: --> <li><a href="#other-things-to-try" style="font-size: 80%;"><b>Other Things to Try</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs020.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;"><b>Other Types of Recurrent Neural Networks</b></a></li>
</ul>
</li>
@@ -223,22 +209,128 @@ MathJax.Hub.Config({
<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p> <!-- add vertical space -->
<a name="part0019"></a>
<!-- !split -->
<h2 id="xgboost-extreme-gradient-boosting" class="anchor">XGBoost: Extreme Gradient Boosting </h2>
<h2 id="other-things-to-try" class="anchor">Other Things to Try </h2>
<p><a href="https://github.com/dmlc/xgboost" target="_self">XGBoost</a> or Extreme Gradient
Boosting, is an optimized distributed gradient boosting library
designed to be highly efficient, flexible and portable. It implements
machine learning algorithms under the Gradient Boosting
framework. XGBoost provides a parallel tree boosting that solve many
data science problems in a fast and accurate way. See the <a href="https://arxiv.org/abs/1603.02754" target="_self">article by Chen and Guestrin</a>.
<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>.
</p>
<p>The authors design and build a highly scalable end-to-end tree
boosting system. It has a theoretically justified weighted quantile
sketch for efficient proposal calculation. It introduces a novel sparsity-aware algorithm for parallel tree learning and an effective cache-aware block structure for out-of-core tree learning.
</p>
<p>It is now the algorithm which wins essentially all ML competitions!!!</p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight" style="background: #f8f8f8">
<pre style="line-height: 125%;"><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">&quot;&quot;&quot;</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 &quot;x data&quot;. 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"> &quot;&quot;&quot;</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">&#39;sigmoid&#39;</span>,
name<span style="color: #666666">=</span><span style="color: #BA2121">&quot;RNN1&quot;</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">&#39;sigmoid&#39;</span>,
stateful <span style="color: #666666">=</span> stateful,
name<span style="color: #666666">=</span><span style="color: #BA2121">&quot;RNN2&quot;</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">&quot;dense&quot;</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">&quot;mean_squared_error&quot;</span>, optimizer<span style="color: #666666">=</span><span style="color: #BA2121">&quot;adam&quot;</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">&quot;loss&quot;</span>,<span style="color: #BA2121">&quot;val_loss&quot;</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">&quot;loss&quot;</span>)
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">&quot;epoch&quot;</span>)
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">&quot;The final validation loss: </span><span style="color: #BB6688; font-weight: bold">{}</span><span style="color: #BA2121">&quot;</span><span style="color: #666666">.</span>format(hist<span style="color: #666666">.</span>history[<span style="color: #BA2121">&quot;val_loss&quot;</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">&#39;Time: &#39;</span>, end<span style="color: #666666">-</span>start)
</pre>
</div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_subarea output_stream output_stdout output_text">
</div>
</div>
</div>
</div>
</div>
<p>
<!-- navigation buttons at the bottom of the page -->
@@ -256,16 +348,6 @@ sketch for efficient proposal calculation. It introduces a novel sparsity-aware
<li><a href="._week45-bs018.html">19</a></li>
<li class="active"><a href="._week45-bs019.html">20</a></li>
<li><a href="._week45-bs020.html">21</a></li>
<li><a href="._week45-bs021.html">22</a></li>
<li><a href="._week45-bs022.html">23</a></li>
<li><a href="._week45-bs023.html">24</a></li>
<li><a href="._week45-bs024.html">25</a></li>
<li><a href="._week45-bs025.html">26</a></li>
<li><a href="._week45-bs026.html">27</a></li>
<li><a href="._week45-bs027.html">28</a></li>
<li><a href="._week45-bs028.html">29</a></li>
<li><a href="">...</a></li>
<li><a href="._week45-bs039.html">40</a></li>
<li><a href="._week45-bs020.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+309 -169
View File
@@ -8,8 +8,8 @@ doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=d
<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 45: Decisions Trees, Random Forests, Bagging and Boosting">
<title>Week 45: Decisions Trees, Random Forests, Bagging and Boosting</title>
<meta name="description" content="Week 45, Recurrent Neural Networks">
<title>Week 45, Recurrent Neural Networks</title>
<!-- Bootstrap style: bootstrap -->
<!-- doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=default --html_admon=bootstrap_panel --html_output=week45-bs --no_mako -->
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
@@ -36,109 +36,101 @@ doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=d
<!-- tocinfo
{'highest level': 2,
'sections': [('Overview of week 45', 2, None, 'overview-of-week-45'),
('Brief code reminder from last wekk',
'sections': [('Plan for week 45', 2, None, 'plan-for-week-45'),
('Material for the lab sessions, additional ways to present '
'classification results and other practicalities',
2,
None,
'brief-code-reminder-from-last-wekk'),
("Boosting, a Bird's Eye View",
'material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities'),
('Searching for Optimal Regularization Parameters $\\lambda$',
2,
None,
'boosting-a-bird-s-eye-view'),
('What is boosting? Additive Modelling/Iterative Fitting',
'searching-for-optimal-regularization-parameters-lambda'),
('Grid Search', 2, None, 'grid-search'),
('Randomized Grid Search', 2, None, 'randomized-grid-search'),
('Wisconsin Cancer Data', 2, None, 'wisconsin-cancer-data'),
('Using the correlation matrix',
2,
None,
'what-is-boosting-additive-modelling-iterative-fitting'),
('Iterative Fitting, Regression and Squared-error Cost Function',
'using-the-correlation-matrix'),
('Discussing the correlation data',
2,
None,
'iterative-fitting-regression-and-squared-error-cost-function'),
('Squared-Error Example and Iterative Fitting',
'discussing-the-correlation-data'),
('Other measures in classification studies: Cancer Data again',
2,
None,
'squared-error-example-and-iterative-fitting'),
('Iterative Fitting, Classification and AdaBoost',
'other-measures-in-classification-studies-cancer-data-again'),
('Material for Lecture Thursday November 9',
2,
None,
'iterative-fitting-classification-and-adaboost'),
('Adaptive Boosting, AdaBoost',
'material-for-lecture-thursday-november-9'),
('Recurrent neural networks (RNNs): Overarching view',
2,
None,
'adaptive-boosting-adaboost'),
('Building up AdaBoost', 2, None, 'building-up-adaboost'),
('Adaptive boosting: AdaBoost, Basic Algorithm',
'recurrent-neural-networks-rnns-overarching-view'),
('A simple example', 2, None, 'a-simple-example'),
('Memoryless models', 3, None, 'memoryless-models'),
('Linear dynamical model', 3, None, 'linear-dynamical-model'),
('Hidden Markov Models', 3, None, 'hidden-markov-models'),
('RNNs', 3, None, 'rnns'),
('Do generative models need to be stochastic?',
3,
None,
'do-generative-models-need-to-be-stochastic'),
('What kinds of behaviour can RNNs exhibit?',
3,
None,
'what-kinds-of-behaviour-can-rnns-exhibit'),
('Basic layout', 2, None, 'basic-layout'),
('We need to specify the initial activity state of all the '
'hidden and output units',
3,
None,
'we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units'),
('We can specify inputs in several ways',
3,
None,
'we-can-specify-inputs-in-several-ways'),
('We can specify targets in several ways',
3,
None,
'we-can-specify-targets-in-several-ways'),
('Backpropagation through time',
3,
None,
'backpropagation-through-time'),
('The backward pass is linear',
3,
None,
'the-backward-pass-is-linear'),
('The problem of exploding or vanishing gradients',
2,
None,
'adaptive-boosting-adaboost-basic-algorithm'),
('Basic Steps of AdaBoost', 2, None, 'basic-steps-of-adaboost'),
('AdaBoost Examples', 2, None, 'adaboost-examples'),
('Gradient boosting: Basics with Steepest Descent/Functional '
'Gradient Descent',
'the-problem-of-exploding-or-vanishing-gradients'),
('Four effective ways to learn an RNN',
2,
None,
'gradient-boosting-basics-with-steepest-descent-functional-gradient-descent'),
('The Squared-Error again! Steepest Descent',
'four-effective-ways-to-learn-an-rnn'),
('Long Short Term Memory (LSTM)',
3,
None,
'long-short-term-memory-lstm'),
('Implementing a memory cell in a neural network',
3,
None,
'implementing-a-memory-cell-in-a-neural-network'),
('An extrapolation example', 2, None, 'an-extrapolation-example'),
('Formatting the Data', 2, None, 'formatting-the-data'),
('Predicting New Points With A Trained Recurrent Neural Network',
2,
None,
'the-squared-error-again-steepest-descent'),
('Steepest Descent Example', 2, None, 'steepest-descent-example'),
('Gradient Boosting, algorithm',
'predicting-new-points-with-a-trained-recurrent-neural-network'),
('Other Things to Try', 2, None, 'other-things-to-try'),
('Other Types of Recurrent Neural Networks',
2,
None,
'gradient-boosting-algorithm'),
('Gradient Boosting, Examples of Regression',
2,
None,
'gradient-boosting-examples-of-regression'),
('Gradient Boosting, Classification Example',
2,
None,
'gradient-boosting-classification-example'),
('XGBoost: Extreme Gradient Boosting',
2,
None,
'xgboost-extreme-gradient-boosting'),
('Regression Case', 2, None, 'regression-case'),
('Xgboost on the Cancer Data',
2,
None,
'xgboost-on-the-cancer-data'),
('Support Vector Machines, overarching aims',
2,
None,
'support-vector-machines-overarching-aims'),
('Hyperplanes and all that', 2, None, 'hyperplanes-and-all-that'),
('What is a hyperplane?', 2, None, 'what-is-a-hyperplane'),
('A $p$-dimensional space of features',
2,
None,
'a-p-dimensional-space-of-features'),
('The two-dimensional case', 2, None, 'the-two-dimensional-case'),
('Getting into the details', 2, None, 'getting-into-the-details'),
('First attempt at a minimization approach',
2,
None,
'first-attempt-at-a-minimization-approach'),
('Solving the equations', 2, None, 'solving-the-equations'),
('Code Example', 2, None, 'code-example'),
('Problems with the Simpler Approach',
2,
None,
'problems-with-the-simpler-approach'),
('A better approach', 2, None, 'a-better-approach'),
('A quick Reminder on Lagrangian Multipliers',
2,
None,
'a-quick-reminder-on-lagrangian-multipliers'),
('Adding the Multiplier', 2, None, 'adding-the-multiplier'),
('Setting up the Problem', 2, None, 'setting-up-the-problem'),
('The problem to solve', 2, None, 'the-problem-to-solve'),
('The last steps', 2, None, 'the-last-steps'),
('A soft classifier', 2, None, 'a-soft-classifier'),
('Soft optmization problem',
2,
None,
'soft-optmization-problem')]}
'other-types-of-recurrent-neural-networks')]}
end of tocinfo -->
<body>
@@ -166,52 +158,46 @@ MathJax.Hub.Config({
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="week45-bs.html">Week 45: Decisions Trees, Random Forests, Bagging and Boosting</a>
<a class="navbar-brand" href="week45-bs.html">Week 45, Recurrent Neural Networks</a>
</div>
<div class="navbar-collapse collapse navbar-responsive-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
<ul class="dropdown-menu">
<!-- navigation toc: --> <li><a href="._week45-bs001.html#overview-of-week-45" style="font-size: 80%;">Overview of week 45</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs002.html#brief-code-reminder-from-last-wekk" style="font-size: 80%;">Brief code reminder from last wekk</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs003.html#boosting-a-bird-s-eye-view" style="font-size: 80%;">Boosting, a Bird's Eye View</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs004.html#what-is-boosting-additive-modelling-iterative-fitting" style="font-size: 80%;">What is boosting? Additive Modelling/Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs005.html#iterative-fitting-regression-and-squared-error-cost-function" style="font-size: 80%;">Iterative Fitting, Regression and Squared-error Cost Function</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs006.html#squared-error-example-and-iterative-fitting" style="font-size: 80%;">Squared-Error Example and Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs007.html#iterative-fitting-classification-and-adaboost" style="font-size: 80%;">Iterative Fitting, Classification and AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs008.html#adaptive-boosting-adaboost" style="font-size: 80%;">Adaptive Boosting, AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs009.html#building-up-adaboost" style="font-size: 80%;">Building up AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs010.html#adaptive-boosting-adaboost-basic-algorithm" style="font-size: 80%;">Adaptive boosting: AdaBoost, Basic Algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs011.html#basic-steps-of-adaboost" style="font-size: 80%;">Basic Steps of AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#adaboost-examples" style="font-size: 80%;">AdaBoost Examples</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#gradient-boosting-basics-with-steepest-descent-functional-gradient-descent" style="font-size: 80%;">Gradient boosting: Basics with Steepest Descent/Functional Gradient Descent</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs014.html#the-squared-error-again-steepest-descent" style="font-size: 80%;">The Squared-Error again! Steepest Descent</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#steepest-descent-example" style="font-size: 80%;">Steepest Descent Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs016.html#gradient-boosting-algorithm" style="font-size: 80%;">Gradient Boosting, algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs017.html#gradient-boosting-examples-of-regression" style="font-size: 80%;">Gradient Boosting, Examples of Regression</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs018.html#gradient-boosting-classification-example" style="font-size: 80%;">Gradient Boosting, Classification Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs019.html#xgboost-extreme-gradient-boosting" style="font-size: 80%;">XGBoost: Extreme Gradient Boosting</a></li>
<!-- navigation toc: --> <li><a href="#regression-case" style="font-size: 80%;">Regression Case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs021.html#xgboost-on-the-cancer-data" style="font-size: 80%;">Xgboost on the Cancer Data</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs022.html#support-vector-machines-overarching-aims" style="font-size: 80%;">Support Vector Machines, overarching aims</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs023.html#hyperplanes-and-all-that" style="font-size: 80%;">Hyperplanes and all that</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs024.html#what-is-a-hyperplane" style="font-size: 80%;">What is a hyperplane?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs025.html#a-p-dimensional-space-of-features" style="font-size: 80%;">A \( p \)-dimensional space of features</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs026.html#the-two-dimensional-case" style="font-size: 80%;">The two-dimensional case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs027.html#getting-into-the-details" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs028.html#first-attempt-at-a-minimization-approach" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs029.html#solving-the-equations" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs030.html#code-example" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs031.html#problems-with-the-simpler-approach" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs032.html#a-better-approach" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs033.html#a-quick-reminder-on-lagrangian-multipliers" style="font-size: 80%;">A quick Reminder on Lagrangian Multipliers</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs034.html#adding-the-multiplier" style="font-size: 80%;">Adding the Multiplier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs035.html#setting-up-the-problem" style="font-size: 80%;">Setting up the Problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs036.html#the-problem-to-solve" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs037.html#the-last-steps" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs038.html#a-soft-classifier" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs039.html#soft-optmization-problem" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs001.html#plan-for-week-45" style="font-size: 80%;"><b>Plan for week 45</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs002.html#material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities" style="font-size: 80%;"><b>Material for the lab sessions, additional ways to present classification results and other practicalities</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs003.html#searching-for-optimal-regularization-parameters-lambda" style="font-size: 80%;"><b>Searching for Optimal Regularization Parameters \( \lambda \)</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs004.html#grid-search" style="font-size: 80%;"><b>Grid Search</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs005.html#randomized-grid-search" style="font-size: 80%;"><b>Randomized Grid Search</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs006.html#wisconsin-cancer-data" style="font-size: 80%;"><b>Wisconsin Cancer Data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs007.html#using-the-correlation-matrix" style="font-size: 80%;"><b>Using the correlation matrix</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs008.html#discussing-the-correlation-data" style="font-size: 80%;"><b>Discussing the correlation data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs009.html#other-measures-in-classification-studies-cancer-data-again" style="font-size: 80%;"><b>Other measures in classification studies: Cancer Data again</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs010.html#material-for-lecture-thursday-november-9" style="font-size: 80%;"><b>Material for Lecture Thursday November 9</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs011.html#recurrent-neural-networks-rnns-overarching-view" style="font-size: 80%;"><b>Recurrent neural networks (RNNs): Overarching view</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#a-simple-example" style="font-size: 80%;"><b>A simple example</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#memoryless-models" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Memoryless models</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#linear-dynamical-model" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Linear dynamical model</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#hidden-markov-models" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Hidden Markov Models</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#rnns" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;RNNs</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#do-generative-models-need-to-be-stochastic" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Do generative models need to be stochastic?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#what-kinds-of-behaviour-can-rnns-exhibit" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;What kinds of behaviour can RNNs exhibit?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#basic-layout" style="font-size: 80%;"><b>Basic layout</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We need to specify the initial activity state of all the hidden and output units</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-can-specify-inputs-in-several-ways" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We can specify inputs in several ways</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-can-specify-targets-in-several-ways" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We can specify targets in several ways</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#backpropagation-through-time" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Backpropagation through time</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#the-backward-pass-is-linear" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;The backward pass is linear</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs014.html#the-problem-of-exploding-or-vanishing-gradients" style="font-size: 80%;"><b>The problem of exploding or vanishing gradients</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#four-effective-ways-to-learn-an-rnn" style="font-size: 80%;"><b>Four effective ways to learn an RNN</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#long-short-term-memory-lstm" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Long Short Term Memory (LSTM)</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#implementing-a-memory-cell-in-a-neural-network" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Implementing a memory cell in a neural network</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs016.html#an-extrapolation-example" style="font-size: 80%;"><b>An extrapolation example</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs017.html#formatting-the-data" style="font-size: 80%;"><b>Formatting the Data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs018.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;"><b>Predicting New Points With A Trained Recurrent Neural Network</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs019.html#other-things-to-try" style="font-size: 80%;"><b>Other Things to Try</b></a></li>
<!-- navigation toc: --> <li><a href="#other-types-of-recurrent-neural-networks" style="font-size: 80%;"><b>Other Types of Recurrent Neural Networks</b></a></li>
</ul>
</li>
@@ -223,7 +209,24 @@ MathJax.Hub.Config({
<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p> <!-- add vertical space -->
<a name="part0020"></a>
<!-- !split -->
<h2 id="regression-case" class="anchor">Regression Case </h2>
<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>
<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>.
</p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
@@ -232,47 +235,196 @@ MathJax.Hub.Config({
<div class="inner_cell">
<div class="input_area">
<div class="highlight" style="background: #f8f8f8">
<pre style="line-height: 125%;"><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">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">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> train_test_split
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">xgboost</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">xgb</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">scikitplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">skplt</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.metrics</span> <span style="color: #008000; font-weight: bold">import</span> mean_squared_error
<pre style="line-height: 125%;"><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">&quot;&quot;&quot;</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 &quot;x data&quot;. 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"> &quot;&quot;&quot;</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">&quot;RNN&quot;</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">&#39;tanh&#39;</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">&quot;RNN1&quot;</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">&#39;tanh&#39;</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">&quot;dense&quot;</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">&#39;mean_squared_error&#39;</span>, optimizer<span style="color: #666666">=</span><span style="color: #BA2121">&#39;adam&#39;</span>)
<span style="color: #408080; font-style: italic"># Return the model</span>
<span style="color: #008000; font-weight: bold">return</span> model
n <span style="color: #666666">=</span> <span style="color: #666666">100</span>
maxdegree <span style="color: #666666">=</span> <span style="color: #666666">6</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">&quot;&quot;&quot;</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 &quot;x data&quot;. 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"> &quot;&quot;&quot;</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">&#39;relu&#39;</span>, name<span style="color: #666666">=</span><span style="color: #BA2121">&#39;dnn&#39;</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">&#39;relu&#39;</span>, name<span style="color: #666666">=</span><span style="color: #BA2121">&#39;dnn1&#39;</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">&quot;RNN1&quot;</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">&quot;RNN&quot;</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">&quot;dense&quot;</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">&#39;mean_squared_error&#39;</span>, optimizer<span style="color: #666666">=</span><span style="color: #BA2121">&#39;adam&#39;</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"># Make data set.</span>
x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linspace(<span style="color: #666666">-3</span>, <span style="color: #666666">3</span>, n)<span style="color: #666666">.</span>reshape(<span style="color: #666666">-1</span>, <span style="color: #666666">1</span>)
y <span style="color: #666666">=</span> np<span style="color: #666666">.</span>exp(<span style="color: #666666">-</span>x<span style="color: #666666">**2</span>) <span style="color: #666666">+</span> <span style="color: #666666">1.5</span> <span style="color: #666666">*</span> np<span style="color: #666666">.</span>exp(<span style="color: #666666">-</span>(x<span style="color: #666666">-2</span>)<span style="color: #666666">**2</span>)<span style="color: #666666">+</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>normal(<span style="color: #666666">0</span>, <span style="color: #666666">0.1</span>, x<span style="color: #666666">.</span>shape)
<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)
error <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros(maxdegree)
bias <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros(maxdegree)
variance <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros(maxdegree)
polydegree <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros(maxdegree)
X_train, X_test, y_train, y_test <span style="color: #666666">=</span> train_test_split(x, y, test_size<span style="color: #666666">=0.2</span>)
<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: #008000; font-weight: bold">for</span> degree <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(maxdegree):
model <span style="color: #666666">=</span> xgb<span style="color: #666666">.</span>XGBRegressor(objective <span style="color: #666666">=</span><span style="color: #BA2121">&#39;reg:squarederror&#39;</span>, colsaobjective <span style="color: #666666">=</span><span style="color: #BA2121">&#39;reg:squarederror&#39;</span>, colsample_bytree <span style="color: #666666">=</span> <span style="color: #666666">0.3</span>, learning_rate <span style="color: #666666">=</span> <span style="color: #666666">0.1</span>,max_depth <span style="color: #666666">=</span> degree, alpha <span style="color: #666666">=</span> <span style="color: #666666">10</span>, n_estimators <span style="color: #666666">=</span> <span style="color: #666666">200</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]
model<span style="color: #666666">.</span>fit(X_train,y_train)
y_pred <span style="color: #666666">=</span> model<span style="color: #666666">.</span>predict(X_test)
polydegree[degree] <span style="color: #666666">=</span> degree
error[degree] <span style="color: #666666">=</span> np<span style="color: #666666">.</span>mean( np<span style="color: #666666">.</span>mean((y_test <span style="color: #666666">-</span> y_pred)<span style="color: #666666">**2</span>) )
bias[degree] <span style="color: #666666">=</span> np<span style="color: #666666">.</span>mean( (y_test <span style="color: #666666">-</span> np<span style="color: #666666">.</span>mean(y_pred))<span style="color: #666666">**2</span> )
variance[degree] <span style="color: #666666">=</span> np<span style="color: #666666">.</span>mean( np<span style="color: #666666">.</span>var(y_pred) )
<span style="color: #008000">print</span>(<span style="color: #BA2121">&#39;Max depth:&#39;</span>, degree)
<span style="color: #008000">print</span>(<span style="color: #BA2121">&#39;Error:&#39;</span>, error[degree])
<span style="color: #008000">print</span>(<span style="color: #BA2121">&#39;Bias^2:&#39;</span>, bias[degree])
<span style="color: #008000">print</span>(<span style="color: #BA2121">&#39;Var:&#39;</span>, variance[degree])
<span style="color: #008000">print</span>(<span style="color: #BA2121">&#39;</span><span style="color: #BB6688; font-weight: bold">{}</span><span style="color: #BA2121"> &gt;= </span><span style="color: #BB6688; font-weight: bold">{}</span><span style="color: #BA2121"> + </span><span style="color: #BB6688; font-weight: bold">{}</span><span style="color: #BA2121"> = </span><span style="color: #BB6688; font-weight: bold">{}</span><span style="color: #BA2121">&#39;</span><span style="color: #666666">.</span>format(error[degree], bias[degree], variance[degree], bias[degree]<span style="color: #666666">+</span>variance[degree]))
plt<span style="color: #666666">.</span>xlim(<span style="color: #666666">1</span>,maxdegree<span style="color: #666666">-1</span>)
plt<span style="color: #666666">.</span>plot(polydegree, error, label<span style="color: #666666">=</span><span style="color: #BA2121">&#39;Error&#39;</span>)
plt<span style="color: #666666">.</span>plot(polydegree, bias, label<span style="color: #666666">=</span><span style="color: #BA2121">&#39;bias&#39;</span>)
plt<span style="color: #666666">.</span>plot(polydegree, variance, label<span style="color: #666666">=</span><span style="color: #BA2121">&#39;Variance&#39;</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>
<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">&quot;loss&quot;</span>,<span style="color: #BA2121">&quot;val_loss&quot;</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">&quot;loss&quot;</span>)
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">&quot;epoch&quot;</span>)
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">&quot;The final validation loss: </span><span style="color: #BB6688; font-weight: bold">{}</span><span style="color: #BA2121">&quot;</span><span style="color: #666666">.</span>format(hist<span style="color: #666666">.</span>history[<span style="color: #BA2121">&quot;val_loss&quot;</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">&#39;Time: &#39;</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">&quot;loss&quot;</span>,<span style="color: #BA2121">&quot;val_loss&quot;</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">&quot;loss&quot;</span>)
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">&quot;epoch&quot;</span>)
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">&quot;The final validation loss: </span><span style="color: #BB6688; font-weight: bold">{}</span><span style="color: #BA2121">&quot;</span><span style="color: #666666">.</span>format(hist<span style="color: #666666">.</span>history[<span style="color: #BA2121">&quot;val_loss&quot;</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">&quot;true&quot;</span>, linewidth<span style="color: #666666">=3</span>)
ax<span style="color: #666666">.</span>plot(X_tot, y_pred, <span style="color: #BA2121">&#39;g-.&#39;</span>,label<span style="color: #666666">=</span><span style="color: #BA2121">&quot;predicted&quot;</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">&#39;red&#39;</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">&#39;Time: &#39;</span>, end<span style="color: #666666">-</span>start)
</pre>
</div>
</div>
@@ -304,18 +456,6 @@ plt<span style="color: #666666">.</span>show()
<li><a href="._week45-bs018.html">19</a></li>
<li><a href="._week45-bs019.html">20</a></li>
<li class="active"><a href="._week45-bs020.html">21</a></li>
<li><a href="._week45-bs021.html">22</a></li>
<li><a href="._week45-bs022.html">23</a></li>
<li><a href="._week45-bs023.html">24</a></li>
<li><a href="._week45-bs024.html">25</a></li>
<li><a href="._week45-bs025.html">26</a></li>
<li><a href="._week45-bs026.html">27</a></li>
<li><a href="._week45-bs027.html">28</a></li>
<li><a href="._week45-bs028.html">29</a></li>
<li><a href="._week45-bs029.html">30</a></li>
<li><a href="">...</a></li>
<li><a href="._week45-bs039.html">40</a></li>
<li><a href="._week45-bs021.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
</div> <!-- end container -->
+113 -127
View File
@@ -8,8 +8,8 @@ doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=d
<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 45: Decisions Trees, Random Forests, Bagging and Boosting">
<title>Week 45: Decisions Trees, Random Forests, Bagging and Boosting</title>
<meta name="description" content="Week 45, Recurrent Neural Networks">
<title>Week 45, Recurrent Neural Networks</title>
<!-- Bootstrap style: bootstrap -->
<!-- doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=default --html_admon=bootstrap_panel --html_output=week45-bs --no_mako -->
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
@@ -36,109 +36,101 @@ doconce format html week45.do.txt --html_style=bootstrap --pygments_html_style=d
<!-- tocinfo
{'highest level': 2,
'sections': [('Overview of week 45', 2, None, 'overview-of-week-45'),
('Brief code reminder from last wekk',
'sections': [('Plan for week 45', 2, None, 'plan-for-week-45'),
('Material for the lab sessions, additional ways to present '
'classification results and other practicalities',
2,
None,
'brief-code-reminder-from-last-wekk'),
("Boosting, a Bird's Eye View",
'material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities'),
('Searching for Optimal Regularization Parameters $\\lambda$',
2,
None,
'boosting-a-bird-s-eye-view'),
('What is boosting? Additive Modelling/Iterative Fitting',
'searching-for-optimal-regularization-parameters-lambda'),
('Grid Search', 2, None, 'grid-search'),
('Randomized Grid Search', 2, None, 'randomized-grid-search'),
('Wisconsin Cancer Data', 2, None, 'wisconsin-cancer-data'),
('Using the correlation matrix',
2,
None,
'what-is-boosting-additive-modelling-iterative-fitting'),
('Iterative Fitting, Regression and Squared-error Cost Function',
'using-the-correlation-matrix'),
('Discussing the correlation data',
2,
None,
'iterative-fitting-regression-and-squared-error-cost-function'),
('Squared-Error Example and Iterative Fitting',
'discussing-the-correlation-data'),
('Other measures in classification studies: Cancer Data again',
2,
None,
'squared-error-example-and-iterative-fitting'),
('Iterative Fitting, Classification and AdaBoost',
'other-measures-in-classification-studies-cancer-data-again'),
('Material for Lecture Thursday November 9',
2,
None,
'iterative-fitting-classification-and-adaboost'),
('Adaptive Boosting, AdaBoost',
'material-for-lecture-thursday-november-9'),
('Recurrent neural networks (RNNs): Overarching view',
2,
None,
'adaptive-boosting-adaboost'),
('Building up AdaBoost', 2, None, 'building-up-adaboost'),
('Adaptive boosting: AdaBoost, Basic Algorithm',
'recurrent-neural-networks-rnns-overarching-view'),
('A simple example', 2, None, 'a-simple-example'),
('Memoryless models', 3, None, 'memoryless-models'),
('Linear dynamical model', 3, None, 'linear-dynamical-model'),
('Hidden Markov Models', 3, None, 'hidden-markov-models'),
('RNNs', 3, None, 'rnns'),
('Do generative models need to be stochastic?',
3,
None,
'do-generative-models-need-to-be-stochastic'),
('What kinds of behaviour can RNNs exhibit?',
3,
None,
'what-kinds-of-behaviour-can-rnns-exhibit'),
('Basic layout', 2, None, 'basic-layout'),
('We need to specify the initial activity state of all the '
'hidden and output units',
3,
None,
'we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units'),
('We can specify inputs in several ways',
3,
None,
'we-can-specify-inputs-in-several-ways'),
('We can specify targets in several ways',
3,
None,
'we-can-specify-targets-in-several-ways'),
('Backpropagation through time',
3,
None,
'backpropagation-through-time'),
('The backward pass is linear',
3,
None,
'the-backward-pass-is-linear'),
('The problem of exploding or vanishing gradients',
2,
None,
'adaptive-boosting-adaboost-basic-algorithm'),
('Basic Steps of AdaBoost', 2, None, 'basic-steps-of-adaboost'),
('AdaBoost Examples', 2, None, 'adaboost-examples'),
('Gradient boosting: Basics with Steepest Descent/Functional '
'Gradient Descent',
'the-problem-of-exploding-or-vanishing-gradients'),
('Four effective ways to learn an RNN',
2,
None,
'gradient-boosting-basics-with-steepest-descent-functional-gradient-descent'),
('The Squared-Error again! Steepest Descent',
'four-effective-ways-to-learn-an-rnn'),
('Long Short Term Memory (LSTM)',
3,
None,
'long-short-term-memory-lstm'),
('Implementing a memory cell in a neural network',
3,
None,
'implementing-a-memory-cell-in-a-neural-network'),
('An extrapolation example', 2, None, 'an-extrapolation-example'),
('Formatting the Data', 2, None, 'formatting-the-data'),
('Predicting New Points With A Trained Recurrent Neural Network',
2,
None,
'the-squared-error-again-steepest-descent'),
('Steepest Descent Example', 2, None, 'steepest-descent-example'),
('Gradient Boosting, algorithm',
'predicting-new-points-with-a-trained-recurrent-neural-network'),
('Other Things to Try', 2, None, 'other-things-to-try'),
('Other Types of Recurrent Neural Networks',
2,
None,
'gradient-boosting-algorithm'),
('Gradient Boosting, Examples of Regression',
2,
None,
'gradient-boosting-examples-of-regression'),
('Gradient Boosting, Classification Example',
2,
None,
'gradient-boosting-classification-example'),
('XGBoost: Extreme Gradient Boosting',
2,
None,
'xgboost-extreme-gradient-boosting'),
('Regression Case', 2, None, 'regression-case'),
('Xgboost on the Cancer Data',
2,
None,
'xgboost-on-the-cancer-data'),
('Support Vector Machines, overarching aims',
2,
None,
'support-vector-machines-overarching-aims'),
('Hyperplanes and all that', 2, None, 'hyperplanes-and-all-that'),
('What is a hyperplane?', 2, None, 'what-is-a-hyperplane'),
('A $p$-dimensional space of features',
2,
None,
'a-p-dimensional-space-of-features'),
('The two-dimensional case', 2, None, 'the-two-dimensional-case'),
('Getting into the details', 2, None, 'getting-into-the-details'),
('First attempt at a minimization approach',
2,
None,
'first-attempt-at-a-minimization-approach'),
('Solving the equations', 2, None, 'solving-the-equations'),
('Code Example', 2, None, 'code-example'),
('Problems with the Simpler Approach',
2,
None,
'problems-with-the-simpler-approach'),
('A better approach', 2, None, 'a-better-approach'),
('A quick Reminder on Lagrangian Multipliers',
2,
None,
'a-quick-reminder-on-lagrangian-multipliers'),
('Adding the Multiplier', 2, None, 'adding-the-multiplier'),
('Setting up the Problem', 2, None, 'setting-up-the-problem'),
('The problem to solve', 2, None, 'the-problem-to-solve'),
('The last steps', 2, None, 'the-last-steps'),
('A soft classifier', 2, None, 'a-soft-classifier'),
('Soft optmization problem',
2,
None,
'soft-optmization-problem')]}
'other-types-of-recurrent-neural-networks')]}
end of tocinfo -->
<body>
@@ -166,52 +158,46 @@ MathJax.Hub.Config({
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="week45-bs.html">Week 45: Decisions Trees, Random Forests, Bagging and Boosting</a>
<a class="navbar-brand" href="week45-bs.html">Week 45, Recurrent Neural Networks</a>
</div>
<div class="navbar-collapse collapse navbar-responsive-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
<ul class="dropdown-menu">
<!-- navigation toc: --> <li><a href="._week45-bs001.html#overview-of-week-45" style="font-size: 80%;">Overview of week 45</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs002.html#brief-code-reminder-from-last-wekk" style="font-size: 80%;">Brief code reminder from last wekk</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs003.html#boosting-a-bird-s-eye-view" style="font-size: 80%;">Boosting, a Bird's Eye View</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs004.html#what-is-boosting-additive-modelling-iterative-fitting" style="font-size: 80%;">What is boosting? Additive Modelling/Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs005.html#iterative-fitting-regression-and-squared-error-cost-function" style="font-size: 80%;">Iterative Fitting, Regression and Squared-error Cost Function</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs006.html#squared-error-example-and-iterative-fitting" style="font-size: 80%;">Squared-Error Example and Iterative Fitting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs007.html#iterative-fitting-classification-and-adaboost" style="font-size: 80%;">Iterative Fitting, Classification and AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs008.html#adaptive-boosting-adaboost" style="font-size: 80%;">Adaptive Boosting, AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs009.html#building-up-adaboost" style="font-size: 80%;">Building up AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs010.html#adaptive-boosting-adaboost-basic-algorithm" style="font-size: 80%;">Adaptive boosting: AdaBoost, Basic Algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs011.html#basic-steps-of-adaboost" style="font-size: 80%;">Basic Steps of AdaBoost</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#adaboost-examples" style="font-size: 80%;">AdaBoost Examples</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#gradient-boosting-basics-with-steepest-descent-functional-gradient-descent" style="font-size: 80%;">Gradient boosting: Basics with Steepest Descent/Functional Gradient Descent</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs014.html#the-squared-error-again-steepest-descent" style="font-size: 80%;">The Squared-Error again! Steepest Descent</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#steepest-descent-example" style="font-size: 80%;">Steepest Descent Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs016.html#gradient-boosting-algorithm" style="font-size: 80%;">Gradient Boosting, algorithm</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs017.html#gradient-boosting-examples-of-regression" style="font-size: 80%;">Gradient Boosting, Examples of Regression</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs018.html#gradient-boosting-classification-example" style="font-size: 80%;">Gradient Boosting, Classification Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs019.html#xgboost-extreme-gradient-boosting" style="font-size: 80%;">XGBoost: Extreme Gradient Boosting</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs020.html#regression-case" style="font-size: 80%;">Regression Case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs021.html#xgboost-on-the-cancer-data" style="font-size: 80%;">Xgboost on the Cancer Data</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs022.html#support-vector-machines-overarching-aims" style="font-size: 80%;">Support Vector Machines, overarching aims</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs023.html#hyperplanes-and-all-that" style="font-size: 80%;">Hyperplanes and all that</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs024.html#what-is-a-hyperplane" style="font-size: 80%;">What is a hyperplane?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs025.html#a-p-dimensional-space-of-features" style="font-size: 80%;">A \( p \)-dimensional space of features</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs026.html#the-two-dimensional-case" style="font-size: 80%;">The two-dimensional case</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs027.html#getting-into-the-details" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs028.html#first-attempt-at-a-minimization-approach" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs029.html#solving-the-equations" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs030.html#code-example" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs031.html#problems-with-the-simpler-approach" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs032.html#a-better-approach" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs033.html#a-quick-reminder-on-lagrangian-multipliers" style="font-size: 80%;">A quick Reminder on Lagrangian Multipliers</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs034.html#adding-the-multiplier" style="font-size: 80%;">Adding the Multiplier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs035.html#setting-up-the-problem" style="font-size: 80%;">Setting up the Problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs036.html#the-problem-to-solve" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs037.html#the-last-steps" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs038.html#a-soft-classifier" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs039.html#soft-optmization-problem" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs001.html#plan-for-week-45" style="font-size: 80%;"><b>Plan for week 45</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs002.html#material-for-the-lab-sessions-additional-ways-to-present-classification-results-and-other-practicalities" style="font-size: 80%;"><b>Material for the lab sessions, additional ways to present classification results and other practicalities</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs003.html#searching-for-optimal-regularization-parameters-lambda" style="font-size: 80%;"><b>Searching for Optimal Regularization Parameters \( \lambda \)</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs004.html#grid-search" style="font-size: 80%;"><b>Grid Search</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs005.html#randomized-grid-search" style="font-size: 80%;"><b>Randomized Grid Search</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs006.html#wisconsin-cancer-data" style="font-size: 80%;"><b>Wisconsin Cancer Data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs007.html#using-the-correlation-matrix" style="font-size: 80%;"><b>Using the correlation matrix</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs008.html#discussing-the-correlation-data" style="font-size: 80%;"><b>Discussing the correlation data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs009.html#other-measures-in-classification-studies-cancer-data-again" style="font-size: 80%;"><b>Other measures in classification studies: Cancer Data again</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs010.html#material-for-lecture-thursday-november-9" style="font-size: 80%;"><b>Material for Lecture Thursday November 9</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs011.html#recurrent-neural-networks-rnns-overarching-view" style="font-size: 80%;"><b>Recurrent neural networks (RNNs): Overarching view</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#a-simple-example" style="font-size: 80%;"><b>A simple example</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#memoryless-models" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Memoryless models</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#linear-dynamical-model" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Linear dynamical model</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#hidden-markov-models" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Hidden Markov Models</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#rnns" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;RNNs</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#do-generative-models-need-to-be-stochastic" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Do generative models need to be stochastic?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs012.html#what-kinds-of-behaviour-can-rnns-exhibit" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;What kinds of behaviour can RNNs exhibit?</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#basic-layout" style="font-size: 80%;"><b>Basic layout</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-need-to-specify-the-initial-activity-state-of-all-the-hidden-and-output-units" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We need to specify the initial activity state of all the hidden and output units</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-can-specify-inputs-in-several-ways" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We can specify inputs in several ways</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#we-can-specify-targets-in-several-ways" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;We can specify targets in several ways</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#backpropagation-through-time" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Backpropagation through time</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs013.html#the-backward-pass-is-linear" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;The backward pass is linear</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs014.html#the-problem-of-exploding-or-vanishing-gradients" style="font-size: 80%;"><b>The problem of exploding or vanishing gradients</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#four-effective-ways-to-learn-an-rnn" style="font-size: 80%;"><b>Four effective ways to learn an RNN</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#long-short-term-memory-lstm" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Long Short Term Memory (LSTM)</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs015.html#implementing-a-memory-cell-in-a-neural-network" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Implementing a memory cell in a neural network</a></li>
<!-- navigation toc: --> <li><a href="._week45-bs016.html#an-extrapolation-example" style="font-size: 80%;"><b>An extrapolation example</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs017.html#formatting-the-data" style="font-size: 80%;"><b>Formatting the Data</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs018.html#predicting-new-points-with-a-trained-recurrent-neural-network" style="font-size: 80%;"><b>Predicting New Points With A Trained Recurrent Neural Network</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs019.html#other-things-to-try" style="font-size: 80%;"><b>Other Things to Try</b></a></li>
<!-- navigation toc: --> <li><a href="._week45-bs020.html#other-types-of-recurrent-neural-networks" style="font-size: 80%;"><b>Other Types of Recurrent Neural Networks</b></a></li>
</ul>
</li>
@@ -225,7 +211,7 @@ MathJax.Hub.Config({
<!-- ------------------- main content ---------------------- -->
<div class="jumbotron">
<center>
<h1>Week 45: Decisions Trees, Random Forests, Bagging and Boosting</h1>
<h1>Week 45, Recurrent Neural Networks</h1>
</center> <!-- document title -->
<!-- author(s): Morten Hjorth-Jensen -->
@@ -241,7 +227,7 @@ MathJax.Hub.Config({
</center>
<br>
<center>
<h4>Nov 11, 2022</h4>
<h4>November 6-10</h4>
</center> <!-- date -->
<br>
@@ -266,7 +252,7 @@ MathJax.Hub.Config({
<li><a href="._week45-bs008.html">9</a></li>
<li><a href="._week45-bs009.html">10</a></li>
<li><a href="">...</a></li>
<li><a href="._week45-bs039.html">40</a></li>
<li><a href="._week45-bs020.html">21</a></li>
<li><a href="._week45-bs001.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
@@ -280,7 +266,7 @@ MathJax.Hub.Config({
</footer>
-->
<center style="font-size:80%">
<!-- copyright --> &copy; 1999-2022, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license
<!-- copyright --> &copy; 1999-2023, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license
</center>
</body>
</html>
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
Binary file not shown.
File diff suppressed because it is too large Load Diff
Binary file not shown.

After

Width:  |  Height:  |  Size: 235 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 309 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 307 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 358 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 260 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 286 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 270 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 317 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 327 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 334 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 313 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 179 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 287 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 294 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 333 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 367 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 378 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 261 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 279 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 350 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 390 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 389 KiB

File diff suppressed because it is too large Load Diff