2793 lines
203 KiB
HTML
2793 lines
203 KiB
HTML
|
||
<!DOCTYPE html>
|
||
|
||
|
||
<html lang="en" data-content_root="./" >
|
||
|
||
<head>
|
||
<meta charset="utf-8" />
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
|
||
|
||
<title>Week 41 Neural networks and constructing a neural network code — Applied Data Analysis and Machine Learning</title>
|
||
|
||
|
||
|
||
<script data-cfasync="false">
|
||
document.documentElement.dataset.mode = localStorage.getItem("mode") || "";
|
||
document.documentElement.dataset.theme = localStorage.getItem("theme") || "";
|
||
</script>
|
||
<!--
|
||
this give us a css class that will be invisible only if js is disabled
|
||
-->
|
||
<noscript>
|
||
<style>
|
||
.pst-js-only { display: none !important; }
|
||
|
||
</style>
|
||
</noscript>
|
||
|
||
<!-- Loaded before other Sphinx assets -->
|
||
<link href="_static/styles/theme.css?digest=26a4bc78f4c0ddb94549" rel="stylesheet" />
|
||
<link href="_static/styles/pydata-sphinx-theme.css?digest=26a4bc78f4c0ddb94549" rel="stylesheet" />
|
||
|
||
<link rel="stylesheet" type="text/css" href="_static/pygments.css?v=fa44fd50" />
|
||
<link rel="stylesheet" type="text/css" href="_static/styles/sphinx-book-theme.css?v=a3416100" />
|
||
<link rel="stylesheet" type="text/css" href="_static/togglebutton.css?v=13237357" />
|
||
<link rel="stylesheet" type="text/css" href="_static/copybutton.css?v=76b2166b" />
|
||
<link rel="stylesheet" type="text/css" href="_static/mystnb.4510f1fc1dee50b3e5859aac5469c37c29e427902b24a333a5f9fcb2f0b3ac41.css" />
|
||
<link rel="stylesheet" type="text/css" href="_static/sphinx-thebe.css?v=4fa983c6" />
|
||
<link rel="stylesheet" type="text/css" href="_static/sphinx-design.min.css?v=95c83b7e" />
|
||
|
||
<!-- So that users can add custom icons -->
|
||
<script src="_static/scripts/fontawesome.js?digest=26a4bc78f4c0ddb94549"></script>
|
||
<!-- Pre-loaded scripts that we'll load fully later -->
|
||
<link rel="preload" as="script" href="_static/scripts/bootstrap.js?digest=26a4bc78f4c0ddb94549" />
|
||
<link rel="preload" as="script" href="_static/scripts/pydata-sphinx-theme.js?digest=26a4bc78f4c0ddb94549" />
|
||
|
||
<script src="_static/documentation_options.js?v=9eb32ce0"></script>
|
||
<script src="_static/doctools.js?v=9a2dae69"></script>
|
||
<script src="_static/sphinx_highlight.js?v=dc90522c"></script>
|
||
<script src="_static/clipboard.min.js?v=a7894cd8"></script>
|
||
<script src="_static/copybutton.js?v=f281be69"></script>
|
||
<script src="_static/scripts/sphinx-book-theme.js?v=887ef09a"></script>
|
||
<script>let toggleHintShow = 'Click to show';</script>
|
||
<script>let toggleHintHide = 'Click to hide';</script>
|
||
<script>let toggleOpenOnPrint = 'true';</script>
|
||
<script src="_static/togglebutton.js?v=4a39c7ea"></script>
|
||
<script>var togglebuttonSelector = '.toggle, .admonition.dropdown';</script>
|
||
<script src="_static/design-tabs.js?v=f930bc37"></script>
|
||
<script>const THEBE_JS_URL = "https://unpkg.com/thebe@0.8.2/lib/index.js"; const thebe_selector = ".thebe,.cell"; const thebe_selector_input = "pre"; const thebe_selector_output = ".output, .cell_output"</script>
|
||
<script async="async" src="_static/sphinx-thebe.js?v=c100c467"></script>
|
||
<script>var togglebuttonSelector = '.toggle, .admonition.dropdown';</script>
|
||
<script>const THEBE_JS_URL = "https://unpkg.com/thebe@0.8.2/lib/index.js"; const thebe_selector = ".thebe,.cell"; const thebe_selector_input = "pre"; const thebe_selector_output = ".output, .cell_output"</script>
|
||
<script>window.MathJax = {"options": {"processHtmlClass": "tex2jax_process|mathjax_process|math|output_area"}}</script>
|
||
<script defer="defer" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
|
||
<script>DOCUMENTATION_OPTIONS.pagename = 'week41';</script>
|
||
<link rel="index" title="Index" href="genindex.html" />
|
||
<link rel="search" title="Search" href="search.html" />
|
||
<link rel="next" title="Exercises week 42" href="exercisesweek42.html" />
|
||
<link rel="prev" title="Exercises week 41" href="exercisesweek41.html" />
|
||
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
||
<meta name="docsearch:language" content="en"/>
|
||
<meta name="docsearch:version" content="" />
|
||
</head>
|
||
|
||
|
||
<body data-bs-spy="scroll" data-bs-target=".bd-toc-nav" data-offset="180" data-bs-root-margin="0px 0px -60%" data-default-mode="">
|
||
|
||
|
||
|
||
<div id="pst-skip-link" class="skip-link d-print-none"><a href="#main-content">Skip to main content</a></div>
|
||
|
||
<div id="pst-scroll-pixel-helper"></div>
|
||
|
||
<button type="button" class="btn rounded-pill" id="pst-back-to-top">
|
||
<i class="fa-solid fa-arrow-up"></i>Back to top</button>
|
||
|
||
|
||
<dialog id="pst-search-dialog">
|
||
|
||
<form class="bd-search d-flex align-items-center"
|
||
action="search.html"
|
||
method="get">
|
||
<i class="fa-solid fa-magnifying-glass"></i>
|
||
<input type="search"
|
||
class="form-control"
|
||
name="q"
|
||
placeholder="Search this book..."
|
||
aria-label="Search this book..."
|
||
autocomplete="off"
|
||
autocorrect="off"
|
||
autocapitalize="off"
|
||
spellcheck="false"/>
|
||
<span class="search-button__kbd-shortcut"><kbd class="kbd-shortcut__modifier">Ctrl</kbd>+<kbd>K</kbd></span>
|
||
</form>
|
||
</dialog>
|
||
|
||
<div class="pst-async-banner-revealer d-none">
|
||
<aside id="bd-header-version-warning" class="d-none d-print-none" aria-label="Version warning"></aside>
|
||
</div>
|
||
|
||
|
||
<header class="bd-header navbar navbar-expand-lg bd-navbar d-print-none">
|
||
</header>
|
||
|
||
|
||
<div class="bd-container">
|
||
<div class="bd-container__inner bd-page-width">
|
||
|
||
|
||
|
||
<dialog id="pst-primary-sidebar-modal"></dialog>
|
||
<div id="pst-primary-sidebar" class="bd-sidebar-primary bd-sidebar">
|
||
|
||
|
||
|
||
<div class="sidebar-header-items sidebar-primary__section">
|
||
|
||
|
||
|
||
|
||
</div>
|
||
|
||
<div class="sidebar-primary-items__start sidebar-primary__section">
|
||
<div class="sidebar-primary-item">
|
||
|
||
|
||
|
||
|
||
|
||
<a class="navbar-brand logo" href="intro.html">
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<img src="_static/logo.png" class="logo__image only-light" alt="Applied Data Analysis and Machine Learning - Home"/>
|
||
<img src="_static/logo.png" class="logo__image only-dark pst-js-only" alt="Applied Data Analysis and Machine Learning - Home"/>
|
||
|
||
|
||
</a></div>
|
||
<div class="sidebar-primary-item">
|
||
|
||
<button class="btn search-button-field search-button__button pst-js-only" title="Search" aria-label="Search" data-bs-placement="bottom" data-bs-toggle="tooltip">
|
||
<i class="fa-solid fa-magnifying-glass"></i>
|
||
<span class="search-button__default-text">Search</span>
|
||
<span class="search-button__kbd-shortcut"><kbd class="kbd-shortcut__modifier">Ctrl</kbd>+<kbd class="kbd-shortcut__modifier">K</kbd></span>
|
||
</button></div>
|
||
<div class="sidebar-primary-item"><nav class="bd-links bd-docs-nav" aria-label="Main">
|
||
<div class="bd-toc-item navbar-nav active">
|
||
|
||
<ul class="nav bd-sidenav bd-sidenav__home-link">
|
||
<li class="toctree-l1">
|
||
<a class="reference internal" href="intro.html">
|
||
Applied Data Analysis and Machine Learning
|
||
</a>
|
||
</li>
|
||
</ul>
|
||
<p aria-level="2" class="caption" role="heading"><span class="caption-text">About the course</span></p>
|
||
<ul class="nav bd-sidenav">
|
||
<li class="toctree-l1"><a class="reference internal" href="schedule.html">Teaching schedule with links to material</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="teachers.html">Teachers and Grading</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="textbooks.html">Textbooks</a></li>
|
||
|
||
</ul>
|
||
<p aria-level="2" class="caption" role="heading"><span class="caption-text">Review of Statistics with Resampling Techniques and Linear Algebra</span></p>
|
||
<ul class="nav bd-sidenav">
|
||
<li class="toctree-l1"><a class="reference internal" href="statistics.html">1. Elements of Probability Theory and Statistical Data Analysis</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="linalg.html">2. Linear Algebra, Handling of Arrays and more Python Features</a></li>
|
||
</ul>
|
||
<p aria-level="2" class="caption" role="heading"><span class="caption-text">From Regression to Support Vector Machines</span></p>
|
||
<ul class="nav bd-sidenav">
|
||
<li class="toctree-l1"><a class="reference internal" href="chapter1.html">3. Linear Regression</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="chapter2.html">4. Ridge and Lasso Regression</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="chapter3.html">5. Resampling Methods</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="chapter4.html">6. Logistic Regression</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="chapteroptimization.html">7. Optimization, the central part of any Machine Learning algortithm</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="chapter5.html">8. Support Vector Machines, overarching aims</a></li>
|
||
</ul>
|
||
<p aria-level="2" class="caption" role="heading"><span class="caption-text">Decision Trees, Ensemble Methods and Boosting</span></p>
|
||
<ul class="nav bd-sidenav">
|
||
<li class="toctree-l1"><a class="reference internal" href="chapter6.html">9. Decision trees, overarching aims</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="chapter7.html">10. Ensemble Methods: From a Single Tree to Many Trees and Extreme Boosting, Meet the Jungle of Methods</a></li>
|
||
</ul>
|
||
<p aria-level="2" class="caption" role="heading"><span class="caption-text">Dimensionality Reduction</span></p>
|
||
<ul class="nav bd-sidenav">
|
||
<li class="toctree-l1"><a class="reference internal" href="chapter8.html">11. Basic ideas of the Principal Component Analysis (PCA)</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="clustering.html">12. Clustering and Unsupervised Learning</a></li>
|
||
</ul>
|
||
<p aria-level="2" class="caption" role="heading"><span class="caption-text">Deep Learning Methods</span></p>
|
||
<ul class="nav bd-sidenav">
|
||
<li class="toctree-l1"><a class="reference internal" href="chapter9.html">13. Neural networks</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="chapter10.html">14. Building a Feed Forward Neural Network</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="chapter11.html">15. Solving Differential Equations with Deep Learning</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="chapter12.html">16. Convolutional Neural Networks</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="chapter13.html">17. Recurrent neural networks: Overarching view</a></li>
|
||
|
||
</ul>
|
||
<p aria-level="2" class="caption" role="heading"><span class="caption-text">Weekly material, notes and exercises</span></p>
|
||
<ul class="current nav bd-sidenav">
|
||
<li class="toctree-l1"><a class="reference internal" href="exercisesweek34.html">Exercises week 34</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="week34.html">Week 34: Introduction to the course, Logistics and Practicalities</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="exercisesweek35.html">Exercises week 35</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="week35.html">Week 35: From Ordinary Linear Regression to Ridge and Lasso Regression</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="exercisesweek36.html">Exercises week 36</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="week36.html">Week 36: Linear Regression and Statistical interpretations</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="exercisesweek37.html">Exercises week 37</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="week37.html">Week 37: Statistical interpretations and Resampling Methods</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="exercisesweek38.html">Exercises week 38</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="week38.html">Week 38: Logistic Regression and Optimization</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="exercisesweek39.html">Exercises week 39</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="week39.html">Week 39: Optimization and Gradient Methods</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="week40.html">Week 40: Gradient descent methods (continued) and start Neural networks</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="exercisesweek41.html">Exercises week 41</a></li>
|
||
|
||
|
||
<li class="toctree-l1 current active"><a class="current reference internal" href="#">Week 41 Neural networks and constructing a neural network code</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="exercisesweek42.html">Exercises week 42</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="toctree-l1"><a class="reference internal" href="week42.html">Week 42 Constructing a Neural Network code with examples</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="additionweek42.html">Exercises Week 42: Logistic Regression and Optimization, reminders from week 38 and week 40</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="week43.html">Week 43: Deep Learning: Constructing a Neural Network code and solving differential equations</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="exercisesweek43.html">Exercises week 43</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="toctree-l1"><a class="reference internal" href="week44.html">Week 44, Convolutional Neural Networks (CNN)</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="week45.html">Week 45, Convolutional Neural Networks (CCNs) and Recurrent Neural Networks (RNNs)</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="week46.html">Week 46: Decision Trees, Ensemble methods and Random Forests</a></li>
|
||
</ul>
|
||
<p aria-level="2" class="caption" role="heading"><span class="caption-text">Projects</span></p>
|
||
<ul class="nav bd-sidenav">
|
||
<li class="toctree-l1"><a class="reference internal" href="project1.html">Project 1 on Machine Learning, deadline October 7 (midnight), 2024</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="project2.html">Project 2 on Machine Learning, deadline November 4 (Midnight)</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="project3.html">Project 3 on Machine Learning, deadline December 9 (midnight), 2024</a></li>
|
||
|
||
</ul>
|
||
|
||
</div>
|
||
</nav></div>
|
||
</div>
|
||
|
||
|
||
<div class="sidebar-primary-items__end sidebar-primary__section">
|
||
</div>
|
||
|
||
<div id="rtd-footer-container"></div>
|
||
|
||
|
||
</div>
|
||
|
||
<main id="main-content" class="bd-main" role="main">
|
||
|
||
|
||
|
||
<div class="sbt-scroll-pixel-helper"></div>
|
||
|
||
<div class="bd-content">
|
||
<div class="bd-article-container">
|
||
|
||
<div class="bd-header-article d-print-none">
|
||
<div class="header-article-items header-article__inner">
|
||
|
||
<div class="header-article-items__start">
|
||
|
||
<div class="header-article-item"><button class="sidebar-toggle primary-toggle btn btn-sm" title="Toggle primary sidebar" data-bs-placement="bottom" data-bs-toggle="tooltip">
|
||
<span class="fa-solid fa-bars"></span>
|
||
</button></div>
|
||
|
||
</div>
|
||
|
||
|
||
<div class="header-article-items__end">
|
||
|
||
<div class="header-article-item">
|
||
|
||
<div class="article-header-buttons">
|
||
|
||
|
||
|
||
|
||
|
||
<div class="dropdown dropdown-launch-buttons">
|
||
<button class="btn dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false" aria-label="Launch interactive content">
|
||
<i class="fas fa-rocket"></i>
|
||
</button>
|
||
<ul class="dropdown-menu">
|
||
|
||
|
||
|
||
<li><a href="https://mybinder.org/v2/git/https%3A//compphysics.github.io/MachineLearning/doc/LectureNotes/_build/html/index.html/master?urlpath=tree/week41.ipynb" target="_blank"
|
||
class="btn btn-sm dropdown-item"
|
||
title="Launch on Binder"
|
||
data-bs-placement="left" data-bs-toggle="tooltip"
|
||
>
|
||
|
||
|
||
<span class="btn__icon-container">
|
||
|
||
<img alt="Binder logo" src="_static/images/logo_binder.svg">
|
||
</span>
|
||
<span class="btn__text-container">Binder</span>
|
||
</a>
|
||
</li>
|
||
|
||
</ul>
|
||
</div>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<div class="dropdown dropdown-download-buttons">
|
||
<button class="btn dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false" aria-label="Download this page">
|
||
<i class="fas fa-download"></i>
|
||
</button>
|
||
<ul class="dropdown-menu">
|
||
|
||
|
||
|
||
<li><a href="_sources/week41.ipynb" target="_blank"
|
||
class="btn btn-sm btn-download-source-button dropdown-item"
|
||
title="Download source file"
|
||
data-bs-placement="left" data-bs-toggle="tooltip"
|
||
>
|
||
|
||
|
||
<span class="btn__icon-container">
|
||
<i class="fas fa-file"></i>
|
||
</span>
|
||
<span class="btn__text-container">.ipynb</span>
|
||
</a>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
<li>
|
||
<button onclick="window.print()"
|
||
class="btn btn-sm btn-download-pdf-button dropdown-item"
|
||
title="Print to PDF"
|
||
data-bs-placement="left" data-bs-toggle="tooltip"
|
||
>
|
||
|
||
|
||
<span class="btn__icon-container">
|
||
<i class="fas fa-file-pdf"></i>
|
||
</span>
|
||
<span class="btn__text-container">.pdf</span>
|
||
</button>
|
||
</li>
|
||
|
||
</ul>
|
||
</div>
|
||
|
||
|
||
|
||
|
||
<button onclick="toggleFullScreen()"
|
||
class="btn btn-sm btn-fullscreen-button"
|
||
title="Fullscreen mode"
|
||
data-bs-placement="bottom" data-bs-toggle="tooltip"
|
||
>
|
||
|
||
|
||
<span class="btn__icon-container">
|
||
<i class="fas fa-expand"></i>
|
||
</span>
|
||
|
||
</button>
|
||
|
||
|
||
|
||
<button class="btn btn-sm nav-link pst-navbar-icon theme-switch-button pst-js-only" aria-label="Color mode" data-bs-title="Color mode" data-bs-placement="bottom" data-bs-toggle="tooltip">
|
||
<i class="theme-switch fa-solid fa-sun fa-lg" data-mode="light" title="Light"></i>
|
||
<i class="theme-switch fa-solid fa-moon fa-lg" data-mode="dark" title="Dark"></i>
|
||
<i class="theme-switch fa-solid fa-circle-half-stroke fa-lg" data-mode="auto" title="System Settings"></i>
|
||
</button>
|
||
|
||
|
||
<button class="btn btn-sm pst-navbar-icon search-button search-button__button pst-js-only" title="Search" aria-label="Search" data-bs-placement="bottom" data-bs-toggle="tooltip">
|
||
<i class="fa-solid fa-magnifying-glass fa-lg"></i>
|
||
</button>
|
||
<button class="sidebar-toggle secondary-toggle btn btn-sm" title="Toggle secondary sidebar" data-bs-placement="bottom" data-bs-toggle="tooltip">
|
||
<span class="fa-solid fa-list"></span>
|
||
</button>
|
||
</div></div>
|
||
|
||
</div>
|
||
|
||
</div>
|
||
</div>
|
||
|
||
|
||
|
||
<div id="jb-print-docs-body" class="onlyprint">
|
||
<h1>Week 41 Neural networks and constructing a neural network code</h1>
|
||
<!-- Table of contents -->
|
||
<div id="print-main-content">
|
||
<div id="jb-print-toc">
|
||
|
||
<div>
|
||
<h2> Contents </h2>
|
||
</div>
|
||
<nav aria-label="Page">
|
||
<ul class="visible nav section-nav flex-column">
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#plan-for-week-41-october-7-11">Plan for week 41, October 7-11</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#material-for-the-lecture-on-monday-october-7-2024">Material for the lecture on Monday October 7, 2024</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#material-for-the-active-learning-sessions-on-tuesday-and-wednesday">Material for the active learning sessions on Tuesday and Wednesday</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#lecture-monday-october-7">Lecture Monday October 7</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#introduction-to-neural-networks">Introduction to Neural networks</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#artificial-neurons">Artificial neurons</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#neural-network-types">Neural network types</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#feed-forward-neural-networks">Feed-forward neural networks</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#convolutional-neural-network">Convolutional Neural Network</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#recurrent-neural-networks">Recurrent neural networks</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#other-types-of-networks">Other types of networks</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#multilayer-perceptrons">Multilayer perceptrons</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#why-multilayer-perceptrons">Why multilayer perceptrons?</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#illustration-of-a-single-perceptron-model-and-a-multi-perceptron-model">Illustration of a single perceptron model and a multi-perceptron model</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#examples-of-xor-or-and-and-gates">Examples of XOR, OR and AND gates</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#does-logistic-regression-do-a-better-job">Does Logistic Regression do a better Job?</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#adding-neural-networks">Adding Neural Networks</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#mathematics-of-deep-learning">Mathematics of deep learning</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#reminder-on-books-with-hands-on-material-and-codes">Reminder on books with hands-on material and codes</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#reading-recommendations">Reading recommendations</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#mathematics-of-deep-learning-and-neural-networks">Mathematics of deep learning and neural networks</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#basics-of-an-nn">Basics of an NN</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#overarching-view-of-a-neural-network">Overarching view of a neural network</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#the-optimization-problem">The optimization problem</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#parameters-of-neural-networks">Parameters of neural networks</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#other-ingredients-of-a-neural-network">Other ingredients of a neural network</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#other-parameters">Other parameters</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#universal-approximation-theorem">Universal approximation theorem</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#some-parallels-from-real-analysis">Some parallels from real analysis</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#the-approximation-theorem-in-words">The approximation theorem in words</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#more-on-the-general-approximation-theorem">More on the general approximation theorem</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#class-of-functions-we-can-approximate">Class of functions we can approximate</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#setting-up-the-equations-for-a-neural-network">Setting up the equations for a neural network</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#layout-of-a-neural-network-with-three-hidden-layers">Layout of a neural network with three hidden layers</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#definitions">Definitions</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#inputs-to-the-activation-function">Inputs to the activation function</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#derivatives-and-the-chain-rule">Derivatives and the chain rule</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#derivative-of-the-cost-function">Derivative of the cost function</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#simpler-examples-first-and-automatic-differentiation">Simpler examples first, and automatic differentiation</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#reminder-on-the-chain-rule-and-gradients">Reminder on the chain rule and gradients</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#multivariable-functions">Multivariable functions</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#automatic-differentiation-through-examples">Automatic differentiation through examples</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#simple-example">Simple example</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#smarter-way-of-evaluating-the-above-function">Smarter way of evaluating the above function</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#reducing-the-number-of-operations">Reducing the number of operations</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#chain-rule-forward-and-reverse-modes">Chain rule, forward and reverse modes</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#forward-and-reverse-modes">Forward and reverse modes</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#more-complicated-function">More complicated function</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#counting-the-number-of-floating-point-operations">Counting the number of floating point operations</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#defining-intermediate-operations">Defining intermediate operations</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#new-expression-for-the-derivative">New expression for the derivative</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#final-derivatives">Final derivatives</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#in-general-not-this-simple">In general not this simple</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#automatic-differentiation">Automatic differentiation</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#chain-rule">Chain rule</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#first-network-example-simple-percepetron-with-one-input">First network example, simple percepetron with one input</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#layout-of-a-simple-neural-network-with-no-hidden-layer">Layout of a simple neural network with no hidden layer</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#optimizing-the-parameters">Optimizing the parameters</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#adding-a-hidden-layer">Adding a hidden layer</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#layout-of-a-simple-neural-network-with-one-hidden-layer">Layout of a simple neural network with one hidden layer</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#the-derivatives">The derivatives</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#important-observations">Important observations</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#the-training">The training</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#code-example">Code example</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#exercise-1-including-more-data">Exercise 1: Including more data</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#simple-neural-network-and-the-back-propagation-equations">Simple neural network and the back propagation equations</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#layout-of-a-simple-neural-network-with-two-input-nodes-one-hidden-layer-and-one-output-node">Layout of a simple neural network with two input nodes, one hidden layer and one output node</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#the-ouput-layer">The ouput layer</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#compact-expressions">Compact expressions</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#output-layer">Output layer</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#explicit-derivatives">Explicit derivatives</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#derivatives-of-the-hidden-layer">Derivatives of the hidden layer</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#final-expression">Final expression</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#completing-the-list">Completing the list</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#final-expressions-for-the-biases-of-the-hidden-layer">Final expressions for the biases of the hidden layer</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#gradient-expressions">Gradient expressions</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#exercise-2-extended-program">Exercise 2: Extended program</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#getting-serious-the-back-propagation-equations-for-a-neural-network">Getting serious, the back propagation equations for a neural network</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#analyzing-the-last-results">Analyzing the last results</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#more-considerations">More considerations</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#derivatives-in-terms-of-z-j-l">Derivatives in terms of <span class="math notranslate nohighlight">\(z_j^L\)</span></a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#bringing-it-together">Bringing it together</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#final-back-propagating-equation">Final back propagating equation</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#using-the-chain-rule-and-summing-over-all-k-entries">Using the chain rule and summing over all <span class="math notranslate nohighlight">\(k\)</span> entries</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#setting-up-the-back-propagation-algorithm">Setting up the back propagation algorithm</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#setting-up-the-back-propagation-algorithm-part-2">Setting up the back propagation algorithm, part 2</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#setting-up-the-back-propagation-algorithm-part-3">Setting up the Back propagation algorithm, part 3</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#updating-the-gradients">Updating the gradients</a><ul class="nav section-nav flex-column">
|
||
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#activation-functions">Activation functions</a></li>
|
||
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#activation-functions-logistic-and-hyperbolic-ones">Activation functions, Logistic and Hyperbolic ones</a></li>
|
||
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#relevance">Relevance</a></li>
|
||
</ul>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#fine-tuning-neural-network-hyperparameters">Fine-tuning neural network hyperparameters</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#hidden-layers">Hidden layers</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#vanishing-gradients">Vanishing gradients</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#exploding-gradients">Exploding gradients</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#is-the-logistic-activation-function-sigmoid-our-choice">Is the Logistic activation function (Sigmoid) our choice?</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#logistic-function-as-the-root-of-problems">Logistic function as the root of problems</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#the-derivative-of-the-logistic-funtion">The derivative of the Logistic funtion</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#insights-from-the-paper-by-glorot-and-bengio">Insights from the paper by Glorot and Bengio</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#the-relu-function-family">The RELU function family</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#elu-function">ELU function</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#which-activation-function-should-we-use">Which activation function should we use?</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#more-on-activation-functions-output-layers">More on activation functions, output layers</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#batch-normalization">Batch Normalization</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#dropout">Dropout</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#gradient-clipping">Gradient Clipping</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#a-top-down-perspective-on-neural-networks">A top-down perspective on Neural networks</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#more-top-down-perspectives">More top-down perspectives</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#limitations-of-supervised-learning-with-deep-networks">Limitations of supervised learning with deep networks</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#limitations-of-nns">Limitations of NNs</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#homogeneous-data">Homogeneous data</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#more-limitations">More limitations</a></li>
|
||
</ul>
|
||
</nav>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
|
||
|
||
<div id="searchbox"></div>
|
||
<article class="bd-article">
|
||
|
||
<!-- HTML file automatically generated from DocOnce source (https://github.com/doconce/doconce/)
|
||
doconce format html week41.do.txt --no_mako -->
|
||
<!-- dom:TITLE: Week 41 Neural networks and constructing a neural network code --><section class="tex2jax_ignore mathjax_ignore" id="week-41-neural-networks-and-constructing-a-neural-network-code">
|
||
<h1>Week 41 Neural networks and constructing a neural network code<a class="headerlink" href="#week-41-neural-networks-and-constructing-a-neural-network-code" title="Link to this heading">#</a></h1>
|
||
<p><strong>Morten Hjorth-Jensen</strong>, Department of Physics, University of Oslo</p>
|
||
<p>Date: <strong>Week 41</strong></p>
|
||
<section id="plan-for-week-41-october-7-11">
|
||
<h2>Plan for week 41, October 7-11<a class="headerlink" href="#plan-for-week-41-october-7-11" title="Link to this heading">#</a></h2>
|
||
</section>
|
||
<section id="material-for-the-lecture-on-monday-october-7-2024">
|
||
<h2>Material for the lecture on Monday October 7, 2024<a class="headerlink" href="#material-for-the-lecture-on-monday-october-7-2024" title="Link to this heading">#</a></h2>
|
||
<ol class="arabic simple">
|
||
<li><p>Neural Networks, setting up the basic steps, from the simple perceptron model to the multi-layer perceptron model.</p></li>
|
||
<li><p>Building our own Feed-forward Neural Network</p></li>
|
||
</ol>
|
||
<!-- * [Video of lecture notes](https://youtu.be/5-RRTO9uDvI) -->
|
||
<!-- * [Whiteboard notes](https://github.com/CompPhysics/MachineLearning/blob/master/doc/HandWrittenNotes/2024/NotesOctober7.pdf) -->
|
||
<p><strong>Readings and Videos:</strong></p>
|
||
<ol class="arabic simple">
|
||
<li><p>These lecture notes</p></li>
|
||
<li><p>Rashcka et al chapter 11</p></li>
|
||
<li><p>For neural networks we recommend Goodfellow et al chapter 6.</p></li>
|
||
</ol>
|
||
<p>a. Neural Networks demystified at <a class="reference external" href="https://www.youtube.com/watch?v=bxe2T-V8XRs&amp;list=PLiaHhY2iBX9hdHaRr6b7XevZtgZRa1PoU&amp;ab_channel=WelchLabs">https://www.youtube.com/watch?v=bxe2T-V8XRs&list=PLiaHhY2iBX9hdHaRr6b7XevZtgZRa1PoU&ab_channel=WelchLabs</a></p>
|
||
<ol class="arabic simple" start="2">
|
||
<li><p>Building Neural Networks from scratch at <a class="reference external" href="https://www.youtube.com/watch?v=Wo5dMEP_BbI&amp;list=PLQVvvaa0QuDcjD5BAw2DxE6OF2tius3V3&amp;ab_channel=sentdex">https://www.youtube.com/watch?v=Wo5dMEP_BbI&list=PLQVvvaa0QuDcjD5BAw2DxE6OF2tius3V3&ab_channel=sentdex</a></p></li>
|
||
<li><p>Video on Neural Networks at <a class="reference external" href="https://www.youtube.com/watch?v=CqOfi41LfDw">https://www.youtube.com/watch?v=CqOfi41LfDw</a></p></li>
|
||
<li><p>Video on the back propagation algorithm at <a class="reference external" href="https://www.youtube.com/watch?v=Ilg3gGewQ5U">https://www.youtube.com/watch?v=Ilg3gGewQ5U</a></p></li>
|
||
</ol>
|
||
<p>We also recommend Michael Nielsen’s intuitive approach to the neural networks and the universal approximation theorem, see the slides at <a class="reference external" href="http://neuralnetworksanddeeplearning.com/chap4.html">http://neuralnetworksanddeeplearning.com/chap4.html</a>.</p>
|
||
</section>
|
||
<section id="material-for-the-active-learning-sessions-on-tuesday-and-wednesday">
|
||
<h2>Material for the active learning sessions on Tuesday and Wednesday<a class="headerlink" href="#material-for-the-active-learning-sessions-on-tuesday-and-wednesday" title="Link to this heading">#</a></h2>
|
||
<ul class="simple">
|
||
<li><p>Exercise on writing your own stochastic gradient and gradient descent codes. This exercise continues next week with studies of automatic differentiation</p></li>
|
||
<li><p>One lecture at the beginning of each session on the material from weeks 39 and 40 and how to write your own gradient descent code</p></li>
|
||
<li><p>Discussion of project 2</p></li>
|
||
<li><p>Your task before the sessions: revisit the material from weeks 39 and 40 and in particular the material from week 40 on stochastic gradient descent</p></li>
|
||
</ul>
|
||
</section>
|
||
<section id="lecture-monday-october-7">
|
||
<h2>Lecture Monday October 7<a class="headerlink" href="#lecture-monday-october-7" title="Link to this heading">#</a></h2>
|
||
</section>
|
||
<section id="introduction-to-neural-networks">
|
||
<h2>Introduction to Neural networks<a class="headerlink" href="#introduction-to-neural-networks" title="Link to this heading">#</a></h2>
|
||
<p>Artificial neural networks are computational systems that can learn to
|
||
perform tasks by considering examples, generally without being
|
||
programmed with any task-specific rules. It is supposed to mimic a
|
||
biological system, wherein neurons interact by sending signals in the
|
||
form of mathematical functions between layers. All layers can contain
|
||
an arbitrary number of neurons, and each connection is represented by
|
||
a weight variable.</p>
|
||
</section>
|
||
<section id="artificial-neurons">
|
||
<h2>Artificial neurons<a class="headerlink" href="#artificial-neurons" title="Link to this heading">#</a></h2>
|
||
<p>The field of artificial neural networks has a long history of
|
||
development, and is closely connected with the advancement of computer
|
||
science and computers in general. A model of artificial neurons was
|
||
first developed by McCulloch and Pitts in 1943 to study signal
|
||
processing in the brain and has later been refined by others. The
|
||
general idea is to mimic neural networks in the human brain, which is
|
||
composed of billions of neurons that communicate with each other by
|
||
sending electrical signals. Each neuron accumulates its incoming
|
||
signals, which must exceed an activation threshold to yield an
|
||
output. If the threshold is not overcome, the neuron remains inactive,
|
||
i.e. has zero output.</p>
|
||
<p>This behaviour has inspired a simple mathematical model for an artificial neuron.</p>
|
||
<!-- Equation labels as ordinary links -->
|
||
<div id="artificialNeuron"></div>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\begin{equation}
|
||
y = f\left(\sum_{i=1}^n w_ix_i\right) = f(u)
|
||
\label{artificialNeuron} \tag{1}
|
||
\end{equation}
|
||
\]</div>
|
||
<p>Here, the output <span class="math notranslate nohighlight">\(y\)</span> of the neuron is the value of its activation function, which have as input
|
||
a weighted sum of signals <span class="math notranslate nohighlight">\(x_i, \dots ,x_n\)</span> received by <span class="math notranslate nohighlight">\(n\)</span> other neurons.</p>
|
||
<p>Conceptually, it is helpful to divide neural networks into four
|
||
categories:</p>
|
||
<ol class="arabic simple">
|
||
<li><p>general purpose neural networks for supervised learning,</p></li>
|
||
<li><p>neural networks designed specifically for image processing, the most prominent example of this class being Convolutional Neural Networks (CNNs),</p></li>
|
||
<li><p>neural networks for sequential data such as Recurrent Neural Networks (RNNs), and</p></li>
|
||
<li><p>neural networks for unsupervised learning such as Deep Boltzmann Machines.</p></li>
|
||
</ol>
|
||
<p>In natural science, DNNs and CNNs have already found numerous
|
||
applications. In statistical physics, they have been applied to detect
|
||
phase transitions in 2D Ising and Potts models, lattice gauge
|
||
theories, and different phases of polymers, or solving the
|
||
Navier-Stokes equation in weather forecasting. Deep learning has also
|
||
found interesting applications in quantum physics. Various quantum
|
||
phase transitions can be detected and studied using DNNs and CNNs,
|
||
topological phases, and even non-equilibrium many-body
|
||
localization. Representing quantum states as DNNs quantum state
|
||
tomography are among some of the impressive achievements to reveal the
|
||
potential of DNNs to facilitate the study of quantum systems.</p>
|
||
<p>In quantum information theory, it has been shown that one can perform
|
||
gate decompositions with the help of neural.</p>
|
||
<p>The applications are not limited to the natural sciences. There is a
|
||
plethora of applications in essentially all disciplines, from the
|
||
humanities to life science and medicine.</p>
|
||
</section>
|
||
<section id="neural-network-types">
|
||
<h2>Neural network types<a class="headerlink" href="#neural-network-types" title="Link to this heading">#</a></h2>
|
||
<p>An artificial neural network (ANN), is a computational model that
|
||
consists of layers of connected neurons, or nodes or units. We will
|
||
refer to these interchangeably as units or nodes, and sometimes as
|
||
neurons.</p>
|
||
<p>It is supposed to mimic a biological nervous system by letting each
|
||
neuron interact with other neurons by sending signals in the form of
|
||
mathematical functions between layers. A wide variety of different
|
||
ANNs have been developed, but most of them consist of an input layer,
|
||
an output layer and eventual layers in-between, called <em>hidden
|
||
layers</em>. All layers can contain an arbitrary number of nodes, and each
|
||
connection between two nodes is associated with a weight variable.</p>
|
||
<p>Neural networks (also called neural nets) are neural-inspired
|
||
nonlinear models for supervised learning. As we will see, neural nets
|
||
can be viewed as natural, more powerful extensions of supervised
|
||
learning methods such as linear and logistic regression and soft-max
|
||
methods we discussed earlier.</p>
|
||
</section>
|
||
<section id="feed-forward-neural-networks">
|
||
<h2>Feed-forward neural networks<a class="headerlink" href="#feed-forward-neural-networks" title="Link to this heading">#</a></h2>
|
||
<p>The feed-forward neural network (FFNN) was the first and simplest type
|
||
of ANNs that were devised. In this network, the information moves in
|
||
only one direction: forward through the layers.</p>
|
||
<p>Nodes are represented by circles, while the arrows display the
|
||
connections between the nodes, including the direction of information
|
||
flow. Additionally, each arrow corresponds to a weight variable
|
||
(figure to come). We observe that each node in a layer is connected
|
||
to <em>all</em> nodes in the subsequent layer, making this a so-called
|
||
<em>fully-connected</em> FFNN.</p>
|
||
</section>
|
||
<section id="convolutional-neural-network">
|
||
<h2>Convolutional Neural Network<a class="headerlink" href="#convolutional-neural-network" title="Link to this heading">#</a></h2>
|
||
<p>A different variant of FFNNs are <em>convolutional neural networks</em>
|
||
(CNNs), which have a connectivity pattern inspired by the animal
|
||
visual cortex. Individual neurons in the visual cortex only respond to
|
||
stimuli from small sub-regions of the visual field, called a receptive
|
||
field. This makes the neurons well-suited to exploit the strong
|
||
spatially local correlation present in natural images. The response of
|
||
each neuron can be approximated mathematically as a convolution
|
||
operation. (figure to come)</p>
|
||
<p>Convolutional neural networks emulate the behaviour of neurons in the
|
||
visual cortex by enforcing a <em>local</em> connectivity pattern between
|
||
nodes of adjacent layers: Each node in a convolutional layer is
|
||
connected only to a subset of the nodes in the previous layer, in
|
||
contrast to the fully-connected FFNN. Often, CNNs consist of several
|
||
convolutional layers that learn local features of the input, with a
|
||
fully-connected layer at the end, which gathers all the local data and
|
||
produces the outputs. They have wide applications in image and video
|
||
recognition.</p>
|
||
</section>
|
||
<section id="recurrent-neural-networks">
|
||
<h2>Recurrent neural networks<a class="headerlink" href="#recurrent-neural-networks" title="Link to this heading">#</a></h2>
|
||
<p>So far we have only mentioned ANNs where information flows in one
|
||
direction: forward. <em>Recurrent neural networks</em> on the other hand,
|
||
have connections between nodes that form directed <em>cycles</em>. This
|
||
creates a form of internal memory which are able to capture
|
||
information on what has been calculated before; the output is
|
||
dependent on the previous computations. Recurrent NNs make use of
|
||
sequential information by performing the same task for every element
|
||
in a sequence, where each element depends on previous elements. An
|
||
example of such information is sentences, making recurrent NNs
|
||
especially well-suited for handwriting and speech recognition.</p>
|
||
</section>
|
||
<section id="other-types-of-networks">
|
||
<h2>Other types of networks<a class="headerlink" href="#other-types-of-networks" title="Link to this heading">#</a></h2>
|
||
<p>There are many other kinds of ANNs that have been developed. One type
|
||
that is specifically designed for interpolation in multidimensional
|
||
space is the radial basis function (RBF) network. RBFs are typically
|
||
made up of three layers: an input layer, a hidden layer with
|
||
non-linear radial symmetric activation functions and a linear output
|
||
layer (‘’linear’’ here means that each node in the output layer has a
|
||
linear activation function). The layers are normally fully-connected
|
||
and there are no cycles, thus RBFs can be viewed as a type of
|
||
fully-connected FFNN. They are however usually treated as a separate
|
||
type of NN due the unusual activation functions.</p>
|
||
</section>
|
||
<section id="multilayer-perceptrons">
|
||
<h2>Multilayer perceptrons<a class="headerlink" href="#multilayer-perceptrons" title="Link to this heading">#</a></h2>
|
||
<p>One uses often so-called fully-connected feed-forward neural networks
|
||
with three or more layers (an input layer, one or more hidden layers
|
||
and an output layer) consisting of neurons that have non-linear
|
||
activation functions.</p>
|
||
<p>Such networks are often called <em>multilayer perceptrons</em> (MLPs).</p>
|
||
</section>
|
||
<section id="why-multilayer-perceptrons">
|
||
<h2>Why multilayer perceptrons?<a class="headerlink" href="#why-multilayer-perceptrons" title="Link to this heading">#</a></h2>
|
||
<p>According to the <em>Universal approximation theorem</em>, a feed-forward
|
||
neural network with just a single hidden layer containing a finite
|
||
number of neurons can approximate a continuous multidimensional
|
||
function to arbitrary accuracy, assuming the activation function for
|
||
the hidden layer is a <strong>non-constant, bounded and
|
||
monotonically-increasing continuous function</strong>.</p>
|
||
<p>Note that the requirements on the activation function only applies to
|
||
the hidden layer, the output nodes are always assumed to be linear, so
|
||
as to not restrict the range of output values.</p>
|
||
</section>
|
||
<section id="illustration-of-a-single-perceptron-model-and-a-multi-perceptron-model">
|
||
<h2>Illustration of a single perceptron model and a multi-perceptron model<a class="headerlink" href="#illustration-of-a-single-perceptron-model-and-a-multi-perceptron-model" title="Link to this heading">#</a></h2>
|
||
<!-- dom:FIGURE: [figures/nns.png, width=600 frac=0.8] In a) we show a single perceptron model while in b) we dispay a network with two hidden layers, an input layer and an output layer. -->
|
||
<!-- begin figure -->
|
||
<p><img src="figures/nns.png" width="600"><p style="font-size: 0.9em"><i>Figure 1: In a) we show a single perceptron model while in b) we dispay a network with two hidden layers, an input layer and an output layer.</i></p></p>
|
||
<!-- end figure --></section>
|
||
<section id="examples-of-xor-or-and-and-gates">
|
||
<h2>Examples of XOR, OR and AND gates<a class="headerlink" href="#examples-of-xor-or-and-and-gates" title="Link to this heading">#</a></h2>
|
||
<p>Let us first try to fit various gates using standard linear
|
||
regression. The gates we are thinking of are the classical XOR, OR and
|
||
AND gates, well-known elements in computer science. The tables here
|
||
show how we can set up the inputs <span class="math notranslate nohighlight">\(x_1\)</span> and <span class="math notranslate nohighlight">\(x_2\)</span> in order to yield a
|
||
specific target <span class="math notranslate nohighlight">\(y_i\)</span>.</p>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="sd">"""</span>
|
||
<span class="sd">Simple code that tests XOR, OR and AND gates with linear regression</span>
|
||
<span class="sd">"""</span>
|
||
|
||
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
|
||
<span class="c1"># Design matrix</span>
|
||
<span class="n">X</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">],[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">]],</span><span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float64</span><span class="p">)</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"The X.TX matrix:</span><span class="si">{</span><span class="n">X</span><span class="o">.</span><span class="n">T</span><span class="w"> </span><span class="o">@</span><span class="w"> </span><span class="n">X</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
|
||
<span class="n">Xinv</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">pinv</span><span class="p">(</span><span class="n">X</span><span class="o">.</span><span class="n">T</span> <span class="o">@</span> <span class="n">X</span><span class="p">)</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"The invers of X.TX matrix:</span><span class="si">{</span><span class="n">Xinv</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
|
||
|
||
<span class="c1"># The XOR gate </span>
|
||
<span class="n">yXOR</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span> <span class="p">[</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span> <span class="p">,</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">])</span>
|
||
<span class="n">ThetaXOR</span> <span class="o">=</span> <span class="n">Xinv</span> <span class="o">@</span> <span class="n">X</span><span class="o">.</span><span class="n">T</span> <span class="o">@</span> <span class="n">yXOR</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"The values of theta for the XOR gate:</span><span class="si">{</span><span class="n">ThetaXOR</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"The linear regression prediction for the XOR gate:</span><span class="si">{</span><span class="n">X</span><span class="w"> </span><span class="o">@</span><span class="w"> </span><span class="n">ThetaXOR</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
|
||
|
||
|
||
<span class="c1"># The OR gate </span>
|
||
<span class="n">yOR</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span> <span class="p">[</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span> <span class="p">,</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">])</span>
|
||
<span class="n">ThetaOR</span> <span class="o">=</span> <span class="n">Xinv</span> <span class="o">@</span> <span class="n">X</span><span class="o">.</span><span class="n">T</span> <span class="o">@</span> <span class="n">yOR</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"The values of theta for the OR gate:</span><span class="si">{</span><span class="n">ThetaOR</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"The linear regression prediction for the OR gate:</span><span class="si">{</span><span class="n">X</span><span class="w"> </span><span class="o">@</span><span class="w"> </span><span class="n">ThetaOR</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
|
||
|
||
|
||
<span class="c1"># The OR gate </span>
|
||
<span class="n">yAND</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span> <span class="p">[</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span> <span class="p">,</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">])</span>
|
||
<span class="n">ThetaAND</span> <span class="o">=</span> <span class="n">Xinv</span> <span class="o">@</span> <span class="n">X</span><span class="o">.</span><span class="n">T</span> <span class="o">@</span> <span class="n">yAND</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"The values of theta for the AND gate:</span><span class="si">{</span><span class="n">ThetaAND</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"The linear regression prediction for the AND gate:</span><span class="si">{</span><span class="n">X</span><span class="w"> </span><span class="o">@</span><span class="w"> </span><span class="n">ThetaAND</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="cell_output docutils container">
|
||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>The X.TX matrix:[[4. 2. 2.]
|
||
[2. 2. 1.]
|
||
[2. 1. 2.]]
|
||
The invers of X.TX matrix:[[ 7.50000000e-01 -5.00000000e-01 -5.00000000e-01]
|
||
[-5.00000000e-01 1.00000000e+00 -2.27693602e-16]
|
||
[-5.00000000e-01 9.94484047e-17 1.00000000e+00]]
|
||
The values of theta for the XOR gate:[ 5.00000000e-01 -2.22044605e-16 -1.11022302e-16]
|
||
The linear regression prediction for the XOR gate:[0.5 0.5 0.5 0.5]
|
||
The values of theta for the OR gate:[0.25 0.5 0.5 ]
|
||
The linear regression prediction for the OR gate:[0.25 0.75 0.75 1.25]
|
||
The values of theta for the AND gate:[-0.25 0.5 0.5 ]
|
||
The linear regression prediction for the AND gate:[-0.25 0.25 0.25 0.75]
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<p>What is happening here?</p>
|
||
</section>
|
||
<section id="does-logistic-regression-do-a-better-job">
|
||
<h2>Does Logistic Regression do a better Job?<a class="headerlink" href="#does-logistic-regression-do-a-better-job" title="Link to this heading">#</a></h2>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="o">%</span><span class="k">matplotlib</span> inline
|
||
|
||
<span class="sd">"""</span>
|
||
<span class="sd">Simple code that tests XOR and OR gates with linear regression</span>
|
||
<span class="sd">and logistic regression</span>
|
||
<span class="sd">"""</span>
|
||
|
||
<span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>
|
||
<span class="kn">from</span> <span class="nn">sklearn.linear_model</span> <span class="kn">import</span> <span class="n">LogisticRegression</span>
|
||
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
|
||
|
||
<span class="c1"># Design matrix</span>
|
||
<span class="n">X</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">],[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">]],</span><span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float64</span><span class="p">)</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"The X.TX matrix:</span><span class="si">{</span><span class="n">X</span><span class="o">.</span><span class="n">T</span><span class="w"> </span><span class="o">@</span><span class="w"> </span><span class="n">X</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
|
||
<span class="n">Xinv</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">pinv</span><span class="p">(</span><span class="n">X</span><span class="o">.</span><span class="n">T</span> <span class="o">@</span> <span class="n">X</span><span class="p">)</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"The invers of X.TX matrix:</span><span class="si">{</span><span class="n">Xinv</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
|
||
|
||
<span class="c1"># The XOR gate </span>
|
||
<span class="n">yXOR</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span> <span class="p">[</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span> <span class="p">,</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">])</span>
|
||
<span class="n">ThetaXOR</span> <span class="o">=</span> <span class="n">Xinv</span> <span class="o">@</span> <span class="n">X</span><span class="o">.</span><span class="n">T</span> <span class="o">@</span> <span class="n">yXOR</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"The values of theta for the XOR gate:</span><span class="si">{</span><span class="n">ThetaXOR</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"The linear regression prediction for the XOR gate:</span><span class="si">{</span><span class="n">X</span><span class="w"> </span><span class="o">@</span><span class="w"> </span><span class="n">ThetaXOR</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
|
||
|
||
|
||
<span class="c1"># The OR gate </span>
|
||
<span class="n">yOR</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span> <span class="p">[</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span> <span class="p">,</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">])</span>
|
||
<span class="n">ThetaOR</span> <span class="o">=</span> <span class="n">Xinv</span> <span class="o">@</span> <span class="n">X</span><span class="o">.</span><span class="n">T</span> <span class="o">@</span> <span class="n">yOR</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"The values of theta for the OR gate:</span><span class="si">{</span><span class="n">ThetaOR</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"The linear regression prediction for the OR gate:</span><span class="si">{</span><span class="n">X</span><span class="w"> </span><span class="o">@</span><span class="w"> </span><span class="n">ThetaOR</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
|
||
|
||
|
||
<span class="c1"># The OR gate </span>
|
||
<span class="n">yAND</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span> <span class="p">[</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span> <span class="p">,</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">])</span>
|
||
<span class="n">ThetaAND</span> <span class="o">=</span> <span class="n">Xinv</span> <span class="o">@</span> <span class="n">X</span><span class="o">.</span><span class="n">T</span> <span class="o">@</span> <span class="n">yAND</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"The values of theta for the AND gate:</span><span class="si">{</span><span class="n">ThetaAND</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"The linear regression prediction for the AND gate:</span><span class="si">{</span><span class="n">X</span><span class="w"> </span><span class="o">@</span><span class="w"> </span><span class="n">ThetaAND</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
|
||
|
||
<span class="c1"># Now we change to logistic regression</span>
|
||
|
||
|
||
<span class="c1"># Logistic Regression</span>
|
||
<span class="n">logreg</span> <span class="o">=</span> <span class="n">LogisticRegression</span><span class="p">()</span>
|
||
<span class="n">logreg</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">yOR</span><span class="p">)</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s2">"Test set accuracy with Logistic Regression for OR gate: </span><span class="si">{:.2f}</span><span class="s2">"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">logreg</span><span class="o">.</span><span class="n">score</span><span class="p">(</span><span class="n">X</span><span class="p">,</span><span class="n">yOR</span><span class="p">)))</span>
|
||
|
||
<span class="n">logreg</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">yXOR</span><span class="p">)</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s2">"Test set accuracy with Logistic Regression for XOR gate: </span><span class="si">{:.2f}</span><span class="s2">"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">logreg</span><span class="o">.</span><span class="n">score</span><span class="p">(</span><span class="n">X</span><span class="p">,</span><span class="n">yXOR</span><span class="p">)))</span>
|
||
|
||
|
||
<span class="n">logreg</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">yAND</span><span class="p">)</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s2">"Test set accuracy with Logistic Regression for AND gate: </span><span class="si">{:.2f}</span><span class="s2">"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">logreg</span><span class="o">.</span><span class="n">score</span><span class="p">(</span><span class="n">X</span><span class="p">,</span><span class="n">yAND</span><span class="p">)))</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="cell_output docutils container">
|
||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>The X.TX matrix:[[4. 2. 2.]
|
||
[2. 2. 1.]
|
||
[2. 1. 2.]]
|
||
The invers of X.TX matrix:[[ 7.50000000e-01 -5.00000000e-01 -5.00000000e-01]
|
||
[-5.00000000e-01 1.00000000e+00 -2.27693602e-16]
|
||
[-5.00000000e-01 9.94484047e-17 1.00000000e+00]]
|
||
The values of theta for the XOR gate:[ 5.00000000e-01 -2.22044605e-16 -1.11022302e-16]
|
||
The linear regression prediction for the XOR gate:[0.5 0.5 0.5 0.5]
|
||
The values of theta for the OR gate:[0.25 0.5 0.5 ]
|
||
The linear regression prediction for the OR gate:[0.25 0.75 0.75 1.25]
|
||
The values of theta for the AND gate:[-0.25 0.5 0.5 ]
|
||
The linear regression prediction for the AND gate:[-0.25 0.25 0.25 0.75]
|
||
Test set accuracy with Logistic Regression for OR gate: 0.75
|
||
Test set accuracy with Logistic Regression for XOR gate: 0.50
|
||
Test set accuracy with Logistic Regression for AND gate: 0.75
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<p>Not exactly impressive, but somewhat better.</p>
|
||
</section>
|
||
<section id="adding-neural-networks">
|
||
<h2>Adding Neural Networks<a class="headerlink" href="#adding-neural-networks" title="Link to this heading">#</a></h2>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># and now neural networks with Scikit-Learn and the XOR</span>
|
||
|
||
<span class="kn">from</span> <span class="nn">sklearn.neural_network</span> <span class="kn">import</span> <span class="n">MLPClassifier</span>
|
||
<span class="kn">from</span> <span class="nn">sklearn.datasets</span> <span class="kn">import</span> <span class="n">make_classification</span>
|
||
<span class="n">X</span><span class="p">,</span> <span class="n">yXOR</span> <span class="o">=</span> <span class="n">make_classification</span><span class="p">(</span><span class="n">n_samples</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <span class="n">random_state</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
|
||
<span class="n">FFNN</span> <span class="o">=</span> <span class="n">MLPClassifier</span><span class="p">(</span><span class="n">random_state</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">max_iter</span><span class="o">=</span><span class="mi">300</span><span class="p">)</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">yXOR</span><span class="p">)</span>
|
||
<span class="n">FFNN</span><span class="o">.</span><span class="n">predict_proba</span><span class="p">(</span><span class="n">X</span><span class="p">)</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Test set accuracy with Feed Forward Neural Network for XOR gate:</span><span class="si">{</span><span class="n">FFNN</span><span class="o">.</span><span class="n">score</span><span class="p">(</span><span class="n">X</span><span class="p">,</span><span class="w"> </span><span class="n">yXOR</span><span class="p">)</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="cell_output docutils container">
|
||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Test set accuracy with Feed Forward Neural Network for XOR gate:1.0
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
<section id="mathematics-of-deep-learning">
|
||
<h2>Mathematics of deep learning<a class="headerlink" href="#mathematics-of-deep-learning" title="Link to this heading">#</a></h2>
|
||
<p><strong>Two recent books online.</strong></p>
|
||
<ol class="arabic simple">
|
||
<li><p><a class="reference external" href="https://arxiv.org/abs/2105.04026">The Modern Mathematics of Deep Learning, by Julius Berner, Philipp Grohs, Gitta Kutyniok, Philipp Petersen</a>, published as <a class="reference external" href="https://doi.org/10.1017/9781009025096.002">Mathematical Aspects of Deep Learning, pp. 1-111. Cambridge University Press, 2022</a></p></li>
|
||
<li><p><a class="reference external" href="https://doi.org/10.48550/arXiv.2310.20360">Mathematical Introduction to Deep Learning: Methods, Implementations, and Theory, Arnulf Jentzen, Benno Kuckuck, Philippe von Wurstemberger</a></p></li>
|
||
</ol>
|
||
</section>
|
||
<section id="reminder-on-books-with-hands-on-material-and-codes">
|
||
<h2>Reminder on books with hands-on material and codes<a class="headerlink" href="#reminder-on-books-with-hands-on-material-and-codes" title="Link to this heading">#</a></h2>
|
||
<ul class="simple">
|
||
<li><p><a class="reference external" href="https://sebastianraschka.com/blog/2022/ml-pytorch-book.html">Sebastian Rashcka et al, Machine learning with Sickit-Learn and PyTorch</a></p></li>
|
||
</ul>
|
||
</section>
|
||
<section id="reading-recommendations">
|
||
<h2>Reading recommendations<a class="headerlink" href="#reading-recommendations" title="Link to this heading">#</a></h2>
|
||
<ol class="arabic simple">
|
||
<li><p>Rashkca et al., chapter 11, jupyter-notebook sent separately, from <a class="reference external" href="https://github.com/rasbt/machine-learning-book">GitHub</a></p></li>
|
||
<li><p>Goodfellow et al, chapter 6 and 7 contain most of the neural network background.</p></li>
|
||
</ol>
|
||
</section>
|
||
<section id="mathematics-of-deep-learning-and-neural-networks">
|
||
<h2>Mathematics of deep learning and neural networks<a class="headerlink" href="#mathematics-of-deep-learning-and-neural-networks" title="Link to this heading">#</a></h2>
|
||
<p>Neural networks, in its so-called feed-forward form, where each
|
||
iterations contains a feed-forward stage and a back-propgagation
|
||
stage, consist of series of affine matrix-matrix and matrix-vector
|
||
multiplications. The unknown parameters (the so-called biases and
|
||
weights which deternine the architecture of a neural network), are
|
||
uptaded iteratively using the so-called back-propagation algorithm.
|
||
This algorithm corresponds to the so-called reverse mode of
|
||
automatic differentation.</p>
|
||
</section>
|
||
<section id="basics-of-an-nn">
|
||
<h2>Basics of an NN<a class="headerlink" href="#basics-of-an-nn" title="Link to this heading">#</a></h2>
|
||
<p>A neural network consists of a series of hidden layers, in addition to
|
||
the input and output layers. Each layer <span class="math notranslate nohighlight">\(l\)</span> has a set of parameters
|
||
<span class="math notranslate nohighlight">\(\boldsymbol{\Theta}^{(l)}=(\boldsymbol{W}^{(l)},\boldsymbol{b}^{(l)})\)</span> which are related to the
|
||
parameters in other layers through a series of affine transformations,
|
||
for a standard NN these are matrix-matrix and matrix-vector
|
||
multiplications. For all layers we will simply use a collective variable <span class="math notranslate nohighlight">\(\boldsymbol{\Theta}\)</span>.</p>
|
||
<p>It consist of two basic steps:</p>
|
||
<ol class="arabic simple">
|
||
<li><p>a feed forward stage which takes a given input and produces a final output which is compared with the target values through our cost/loss function.</p></li>
|
||
<li><p>a back-propagation state where the unknown parameters <span class="math notranslate nohighlight">\(\boldsymbol{\Theta}\)</span> are updated through the optimization of the their gradients. The expressions for the gradients are obtained via the chain rule, starting from the derivative of the cost/function.</p></li>
|
||
</ol>
|
||
<p>These two steps make up one iteration. This iterative process is continued till we reach an eventual stopping criterion.</p>
|
||
</section>
|
||
<section id="overarching-view-of-a-neural-network">
|
||
<h2>Overarching view of a neural network<a class="headerlink" href="#overarching-view-of-a-neural-network" title="Link to this heading">#</a></h2>
|
||
<p>The architecture of a neural network defines our model. This model
|
||
aims at describing some function <span class="math notranslate nohighlight">\(f(\boldsymbol{x}\)</span> which represents
|
||
some final result (outputs or tagrget values) given a specific inpput
|
||
<span class="math notranslate nohighlight">\(\boldsymbol{x}\)</span>. Note that here <span class="math notranslate nohighlight">\(\boldsymbol{y}\)</span> and <span class="math notranslate nohighlight">\(\boldsymbol{x}\)</span> are not limited to be
|
||
vectors.</p>
|
||
<p>The architecture consists of</p>
|
||
<ol class="arabic simple">
|
||
<li><p>An input and an output layer where the input layer is defined by the inputs <span class="math notranslate nohighlight">\(\boldsymbol{x}\)</span>. The output layer produces the model ouput <span class="math notranslate nohighlight">\(\boldsymbol{\tilde{y}}\)</span> which is compared with the target value <span class="math notranslate nohighlight">\(\boldsymbol{y}\)</span></p></li>
|
||
<li><p>A given number of hidden layers and neurons/nodes/units for each layer (this may vary)</p></li>
|
||
<li><p>A given activation function <span class="math notranslate nohighlight">\(\sigma(\boldsymbol{z})\)</span> with arguments <span class="math notranslate nohighlight">\(\boldsymbol{z}\)</span> to be defined below. The activation functions may differ from layer to layer.</p></li>
|
||
<li><p>The last layer, normally called <strong>output</strong> layer has normally an activation function tailored to the specific problem</p></li>
|
||
<li><p>Finally we define a so-called cost or loss function which is used to gauge the quality of our model.</p></li>
|
||
</ol>
|
||
</section>
|
||
<section id="the-optimization-problem">
|
||
<h2>The optimization problem<a class="headerlink" href="#the-optimization-problem" title="Link to this heading">#</a></h2>
|
||
<p>The cost function is a function of the unknown parameters
|
||
<span class="math notranslate nohighlight">\(\boldsymbol{\Theta}\)</span> where the latter is a container for all possible
|
||
parameters needed to define a neural network</p>
|
||
<p>If we are dealing with a regression task a typical cost/loss function
|
||
is the mean squared error</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
C(\boldsymbol{\Theta})=\frac{1}{n}\left\{\left(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\theta}\right)^T\left(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\theta}\right)\right\}.
|
||
\]</div>
|
||
<p>This function represents one of many possible ways to define
|
||
the so-called cost function. Note that here we have assumed a linear dependence in terms of the paramters <span class="math notranslate nohighlight">\(\boldsymbol{\Theta}\)</span>. This is in general not the case.</p>
|
||
</section>
|
||
<section id="parameters-of-neural-networks">
|
||
<h2>Parameters of neural networks<a class="headerlink" href="#parameters-of-neural-networks" title="Link to this heading">#</a></h2>
|
||
<p>For neural networks the parameters
|
||
<span class="math notranslate nohighlight">\(\boldsymbol{\Theta}\)</span> are given by the so-called weights and biases (to be
|
||
defined below).</p>
|
||
<p>The weights are given by matrix elements <span class="math notranslate nohighlight">\(w_{ij}^{(l)}\)</span> where the
|
||
superscript indicates the layer number. The biases are typically given
|
||
by vector elements representing each single node of a given layer,
|
||
that is <span class="math notranslate nohighlight">\(b_j^{(l)}\)</span>.</p>
|
||
</section>
|
||
<section id="other-ingredients-of-a-neural-network">
|
||
<h2>Other ingredients of a neural network<a class="headerlink" href="#other-ingredients-of-a-neural-network" title="Link to this heading">#</a></h2>
|
||
<p>Having defined the architecture of a neural network, the optimization
|
||
of the cost function with respect to the parameters <span class="math notranslate nohighlight">\(\boldsymbol{\Theta}\)</span>,
|
||
involves the calculations of gradients and their optimization. The
|
||
gradients represent the derivatives of a multidimensional object and
|
||
are often approximated by various gradient methods, including</p>
|
||
<ol class="arabic simple">
|
||
<li><p>various quasi-Newton methods,</p></li>
|
||
<li><p>plain gradient descent (GD) with a constant learning rate <span class="math notranslate nohighlight">\(\eta\)</span>,</p></li>
|
||
<li><p>GD with momentum and other approximations to the learning rates such as</p></li>
|
||
</ol>
|
||
<ul class="simple">
|
||
<li><p>Adapative gradient (ADAgrad)</p></li>
|
||
<li><p>Root mean-square propagation (RMSprop)</p></li>
|
||
<li><p>Adaptive gradient with momentum (ADAM) and many other</p></li>
|
||
</ul>
|
||
<ol class="arabic simple" start="4">
|
||
<li><p>Stochastic gradient descent and various families of learning rate approximations</p></li>
|
||
</ol>
|
||
</section>
|
||
<section id="other-parameters">
|
||
<h2>Other parameters<a class="headerlink" href="#other-parameters" title="Link to this heading">#</a></h2>
|
||
<p>In addition to the above, there are often additional hyperparamaters
|
||
which are included in the setup of a neural network. These will be
|
||
discussed below.</p>
|
||
</section>
|
||
<section id="universal-approximation-theorem">
|
||
<h2>Universal approximation theorem<a class="headerlink" href="#universal-approximation-theorem" title="Link to this heading">#</a></h2>
|
||
<p>The universal approximation theorem plays a central role in deep
|
||
learning. <a class="reference external" href="https://link.springer.com/article/10.1007/BF02551274">Cybenko (1989)</a> showed
|
||
the following:</p>
|
||
<p>Let <span class="math notranslate nohighlight">\(\sigma\)</span> be any continuous sigmoidal function such that</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[\begin{split}
|
||
\sigma(z) = \left\{\begin{array}{cc} 1 & z\rightarrow \infty\\ 0 & z \rightarrow -\infty \end{array}\right.
|
||
\end{split}\]</div>
|
||
<p>Given a continuous and deterministic function <span class="math notranslate nohighlight">\(F(\boldsymbol{x})\)</span> on the unit
|
||
cube in <span class="math notranslate nohighlight">\(d\)</span>-dimensions <span class="math notranslate nohighlight">\(F\in [0,1]^d\)</span>, <span class="math notranslate nohighlight">\(x\in [0,1]^d\)</span> and a parameter
|
||
<span class="math notranslate nohighlight">\(\epsilon >0\)</span>, there is a one-layer (hidden) neural network
|
||
<span class="math notranslate nohighlight">\(f(\boldsymbol{x};\boldsymbol{\Theta})\)</span> with <span class="math notranslate nohighlight">\(\boldsymbol{\Theta}=(\boldsymbol{W},\boldsymbol{b})\)</span> and <span class="math notranslate nohighlight">\(\boldsymbol{W}\in
|
||
\mathbb{R}^{m\times n}\)</span> and <span class="math notranslate nohighlight">\(\boldsymbol{b}\in \mathbb{R}^{n}\)</span>, for which</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\vert F(\boldsymbol{x})-f(\boldsymbol{x};\boldsymbol{\Theta})\vert < \epsilon \hspace{0.1cm} \forall \boldsymbol{x}\in[0,1]^d.
|
||
\]</div>
|
||
</section>
|
||
<section id="some-parallels-from-real-analysis">
|
||
<h2>Some parallels from real analysis<a class="headerlink" href="#some-parallels-from-real-analysis" title="Link to this heading">#</a></h2>
|
||
<p>For those of you familiar with for example the <a class="reference external" href="https://en.wikipedia.org/wiki/Stone%E2%80%93Weierstrass_theorem">Stone-Weierstrass
|
||
theorem</a>
|
||
for polynomial approximations or the convergence criterion for Fourier
|
||
series, there are similarities in the derivation of the proof for
|
||
neural networks.</p>
|
||
</section>
|
||
<section id="the-approximation-theorem-in-words">
|
||
<h2>The approximation theorem in words<a class="headerlink" href="#the-approximation-theorem-in-words" title="Link to this heading">#</a></h2>
|
||
<p><strong>Any continuous function <span class="math notranslate nohighlight">\(y=F(\boldsymbol{x})\)</span> supported on the unit cube in
|
||
<span class="math notranslate nohighlight">\(d\)</span>-dimensions can be approximated by a one-layer sigmoidal network to
|
||
arbitrary accuracy.</strong></p>
|
||
<p><a class="reference external" href="https://www.sciencedirect.com/science/article/abs/pii/089360809190009T">Hornik (1991)</a> extended the theorem by letting any non-constant, bounded activation function to be included using that the expectation value</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\mathbb{E}[\vert F(\boldsymbol{x})\vert^2] =\int_{\boldsymbol{x}\in D} \vert F(\boldsymbol{x})\vert^2p(\boldsymbol{x})d\boldsymbol{x} < \infty.
|
||
\]</div>
|
||
<p>Then we have</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\mathbb{E}[\vert F(\boldsymbol{x})-f(\boldsymbol{x};\boldsymbol{\Theta})\vert^2] =\int_{\boldsymbol{x}\in D} \vert F(\boldsymbol{x})-f(\boldsymbol{x};\boldsymbol{\Theta})\vert^2p(\boldsymbol{x})d\boldsymbol{x} < \epsilon.
|
||
\]</div>
|
||
</section>
|
||
<section id="more-on-the-general-approximation-theorem">
|
||
<h2>More on the general approximation theorem<a class="headerlink" href="#more-on-the-general-approximation-theorem" title="Link to this heading">#</a></h2>
|
||
<p>None of the proofs give any insight into the relation between the
|
||
number of of hidden layers and nodes and the approximation error
|
||
<span class="math notranslate nohighlight">\(\epsilon\)</span>, nor the magnitudes of <span class="math notranslate nohighlight">\(\boldsymbol{W}\)</span> and <span class="math notranslate nohighlight">\(\boldsymbol{b}\)</span>.</p>
|
||
<p>Neural networks (NNs) have what we may call a kind of universality no matter what function we want to compute.</p>
|
||
<p>It does not mean that an NN can be used to exactly compute any function. Rather, we get an approximation that is as good as we want.</p>
|
||
</section>
|
||
<section id="class-of-functions-we-can-approximate">
|
||
<h2>Class of functions we can approximate<a class="headerlink" href="#class-of-functions-we-can-approximate" title="Link to this heading">#</a></h2>
|
||
<p>The class of functions that can be approximated are the continuous ones.
|
||
If the function <span class="math notranslate nohighlight">\(F(\boldsymbol{x})\)</span> is discontinuous, it won’t in general be possible to approximate it. However, an NN may still give an approximation even if we fail in some points.</p>
|
||
</section>
|
||
<section id="setting-up-the-equations-for-a-neural-network">
|
||
<h2>Setting up the equations for a neural network<a class="headerlink" href="#setting-up-the-equations-for-a-neural-network" title="Link to this heading">#</a></h2>
|
||
<p>The questions we want to ask are how do changes in the biases and the
|
||
weights in our network change the cost function and how can we use the
|
||
final output to modify the weights and biases?</p>
|
||
<p>To derive these equations let us start with a plain regression problem
|
||
and define our cost function as</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
{\cal C}(\boldsymbol{\Theta}) = \frac{1}{2}\sum_{i=1}^n\left(y_i - \tilde{y}_i\right)^2,
|
||
\]</div>
|
||
<p>where the <span class="math notranslate nohighlight">\(y_i\)</span>s are our <span class="math notranslate nohighlight">\(n\)</span> targets (the values we want to
|
||
reproduce), while the outputs of the network after having propagated
|
||
all inputs <span class="math notranslate nohighlight">\(\boldsymbol{x}\)</span> are given by <span class="math notranslate nohighlight">\(\boldsymbol{\tilde{y}}_i\)</span>.</p>
|
||
</section>
|
||
<section id="layout-of-a-neural-network-with-three-hidden-layers">
|
||
<h2>Layout of a neural network with three hidden layers<a class="headerlink" href="#layout-of-a-neural-network-with-three-hidden-layers" title="Link to this heading">#</a></h2>
|
||
<!-- dom:FIGURE: [figures/nn1.png, width=900 frac=1.0] -->
|
||
<!-- begin figure -->
|
||
<p><img src="figures/nn1.png" width="900"><p style="font-size: 0.9em"><i>Figure 1: </i></p></p>
|
||
<!-- end figure --></section>
|
||
<section id="definitions">
|
||
<h2>Definitions<a class="headerlink" href="#definitions" title="Link to this heading">#</a></h2>
|
||
<p>With our definition of the targets <span class="math notranslate nohighlight">\(\boldsymbol{y}\)</span>, the outputs of the
|
||
network <span class="math notranslate nohighlight">\(\boldsymbol{\tilde{y}}\)</span> and the inputs <span class="math notranslate nohighlight">\(\boldsymbol{x}\)</span> we
|
||
define now the activation <span class="math notranslate nohighlight">\(z_j^l\)</span> of node/neuron/unit <span class="math notranslate nohighlight">\(j\)</span> of the
|
||
<span class="math notranslate nohighlight">\(l\)</span>-th layer as a function of the bias, the weights which add up from
|
||
the previous layer <span class="math notranslate nohighlight">\(l-1\)</span> and the forward passes/outputs
|
||
<span class="math notranslate nohighlight">\(\hat{a}^{l-1}\)</span> from the previous layer as</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
z_j^l = \sum_{i=1}^{M_{l-1}}w_{ij}^la_i^{l-1}+b_j^l,
|
||
\]</div>
|
||
<p>where <span class="math notranslate nohighlight">\(b_k^l\)</span> are the biases from layer <span class="math notranslate nohighlight">\(l\)</span>. Here <span class="math notranslate nohighlight">\(M_{l-1}\)</span>
|
||
represents the total number of nodes/neurons/units of layer <span class="math notranslate nohighlight">\(l-1\)</span>. The
|
||
figure in the whiteboard notes illustrates this equation. We can rewrite this in a more
|
||
compact form as the matrix-vector products we discussed earlier,</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\hat{z}^l = \left(\hat{W}^l\right)^T\hat{a}^{l-1}+\hat{b}^l.
|
||
\]</div>
|
||
</section>
|
||
<section id="inputs-to-the-activation-function">
|
||
<h2>Inputs to the activation function<a class="headerlink" href="#inputs-to-the-activation-function" title="Link to this heading">#</a></h2>
|
||
<p>With the activation values <span class="math notranslate nohighlight">\(\boldsymbol{z}^l\)</span> we can in turn define the
|
||
output of layer <span class="math notranslate nohighlight">\(l\)</span> as <span class="math notranslate nohighlight">\(\boldsymbol{a}^l = f(\boldsymbol{z}^l)\)</span> where <span class="math notranslate nohighlight">\(f\)</span> is our
|
||
activation function. In the examples here we will use the sigmoid
|
||
function discussed in our logistic regression lectures. We will also use the same activation function <span class="math notranslate nohighlight">\(f\)</span> for all layers
|
||
and their nodes. It means we have</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
a_j^l = \sigma(z_j^l) = \frac{1}{1+\exp{-(z_j^l)}}.
|
||
\]</div>
|
||
</section>
|
||
<section id="derivatives-and-the-chain-rule">
|
||
<h2>Derivatives and the chain rule<a class="headerlink" href="#derivatives-and-the-chain-rule" title="Link to this heading">#</a></h2>
|
||
<p>From the definition of the activation <span class="math notranslate nohighlight">\(z_j^l\)</span> we have</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial z_j^l}{\partial w_{ij}^l} = a_i^{l-1},
|
||
\]</div>
|
||
<p>and</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial z_j^l}{\partial a_i^{l-1}} = w_{ji}^l.
|
||
\]</div>
|
||
<p>With our definition of the activation function we have that (note that this function depends only on <span class="math notranslate nohighlight">\(z_j^l\)</span>)</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial a_j^l}{\partial z_j^{l}} = a_j^l(1-a_j^l)=\sigma(z_j^l)(1-\sigma(z_j^l)).
|
||
\]</div>
|
||
</section>
|
||
<section id="derivative-of-the-cost-function">
|
||
<h2>Derivative of the cost function<a class="headerlink" href="#derivative-of-the-cost-function" title="Link to this heading">#</a></h2>
|
||
<p>With these definitions we can now compute the derivative of the cost function in terms of the weights.</p>
|
||
<p>Let us specialize to the output layer <span class="math notranslate nohighlight">\(l=L\)</span>. Our cost function is</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
{\cal C}(\boldsymbol{\Theta}^L) = \frac{1}{2}\sum_{i=1}^n\left(y_i - \tilde{y}_i\right)^2=\frac{1}{2}\sum_{i=1}^n\left(a_i^L - y_i\right)^2,
|
||
\]</div>
|
||
<p>The derivative of this function with respect to the weights is</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial{\cal C}(\boldsymbol{\Theta}^L)}{\partial w_{jk}^L} = \left(a_j^L - y_j\right)\frac{\partial a_j^L}{\partial w_{jk}^{L}},
|
||
\]</div>
|
||
<p>The last partial derivative can easily be computed and reads (by applying the chain rule)</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial a_j^L}{\partial w_{jk}^{L}} = \frac{\partial a_j^L}{\partial z_{j}^{L}}\frac{\partial z_j^L}{\partial w_{jk}^{L}}=a_j^L(1-a_j^L)a_k^{L-1}.
|
||
\]</div>
|
||
</section>
|
||
<section id="simpler-examples-first-and-automatic-differentiation">
|
||
<h2>Simpler examples first, and automatic differentiation<a class="headerlink" href="#simpler-examples-first-and-automatic-differentiation" title="Link to this heading">#</a></h2>
|
||
<p>In order to understand the back propagation algorithm and its
|
||
derivation (an implementation of the chain rule), let us first digress
|
||
with some simple examples. These examples are also meant to motivate
|
||
the link with back propagation and <a class="reference external" href="https://en.wikipedia.org/wiki/Automatic_differentiation">automatic differentiation</a>.</p>
|
||
</section>
|
||
<section id="reminder-on-the-chain-rule-and-gradients">
|
||
<h2>Reminder on the chain rule and gradients<a class="headerlink" href="#reminder-on-the-chain-rule-and-gradients" title="Link to this heading">#</a></h2>
|
||
<p>If we have a multivariate function <span class="math notranslate nohighlight">\(f(x,y)\)</span> where <span class="math notranslate nohighlight">\(x=x(t)\)</span> and <span class="math notranslate nohighlight">\(y=y(t)\)</span> are functions of a variable <span class="math notranslate nohighlight">\(t\)</span>, we have that the gradient of <span class="math notranslate nohighlight">\(f\)</span> with respect to <span class="math notranslate nohighlight">\(t\)</span> (without the explicit unit vector components)</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[\begin{split}
|
||
\frac{df}{dt} = \begin{bmatrix}\frac{\partial f}{\partial x} & \frac{\partial f}{\partial y} \end{bmatrix} \begin{bmatrix}\frac{\partial x}{\partial t} \\ \frac{\partial y}{\partial t} \end{bmatrix}=\frac{\partial f}{\partial x} \frac{\partial x}{\partial t} +\frac{\partial f}{\partial y} \frac{\partial y}{\partial t}.
|
||
\end{split}\]</div>
|
||
</section>
|
||
<section id="multivariable-functions">
|
||
<h2>Multivariable functions<a class="headerlink" href="#multivariable-functions" title="Link to this heading">#</a></h2>
|
||
<p>If we have a multivariate function <span class="math notranslate nohighlight">\(f(x,y)\)</span> where <span class="math notranslate nohighlight">\(x=x(t,s)\)</span> and <span class="math notranslate nohighlight">\(y=y(t,s)\)</span> are functions of the variables <span class="math notranslate nohighlight">\(t\)</span> and <span class="math notranslate nohighlight">\(s\)</span>, we have that the partial derivatives</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial f}{\partial s}=\frac{\partial f}{\partial x}\frac{\partial x}{\partial s}+\frac{\partial f}{\partial y}\frac{\partial y}{\partial s},
|
||
\]</div>
|
||
<p>and</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial f}{\partial t}=\frac{\partial f}{\partial x}\frac{\partial x}{\partial t}+\frac{\partial f}{\partial y}\frac{\partial y}{\partial t}.
|
||
\]</div>
|
||
<p>the gradient of <span class="math notranslate nohighlight">\(f\)</span> with respect to <span class="math notranslate nohighlight">\(t\)</span> and <span class="math notranslate nohighlight">\(s\)</span> (without the explicit unit vector components)</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[\begin{split}
|
||
\frac{df}{d(s,t)} = \begin{bmatrix}\frac{\partial f}{\partial x} & \frac{\partial f}{\partial y} \end{bmatrix} \begin{bmatrix}\frac{\partial x}{\partial s} &\frac{\partial x}{\partial t} \\ \frac{\partial y}{\partial s} & \frac{\partial y}{\partial t} \end{bmatrix}.
|
||
\end{split}\]</div>
|
||
</section>
|
||
<section id="automatic-differentiation-through-examples">
|
||
<h2>Automatic differentiation through examples<a class="headerlink" href="#automatic-differentiation-through-examples" title="Link to this heading">#</a></h2>
|
||
<p>A great introduction to automatic differentiation is given by Baydin et al., see <a class="reference external" href="https://arxiv.org/abs/1502.05767">https://arxiv.org/abs/1502.05767</a>.</p>
|
||
<p>Automatic differentiation is a represented by a repeated application
|
||
of the chain rule on well-known functions and allows for the
|
||
calculation of derivatives to numerical precision. It is not the same
|
||
as the calculation of symbolic derivatives via for example SymPy, nor
|
||
does it use approximative formulae based on Taylor-expansions of a
|
||
function around a given value. The latter are error prone due to
|
||
truncation errors and values of the step size <span class="math notranslate nohighlight">\(\Delta\)</span>.</p>
|
||
</section>
|
||
<section id="simple-example">
|
||
<h2>Simple example<a class="headerlink" href="#simple-example" title="Link to this heading">#</a></h2>
|
||
<p>Our first example is rather simple,</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
f(x) =\exp{x^2},
|
||
\]</div>
|
||
<p>with derivative</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
f'(x) =2x\exp{x^2}.
|
||
\]</div>
|
||
<p>We can use SymPy to extract the pertinent lines of Python code through the following simple example</p>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">division</span>
|
||
<span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="o">*</span>
|
||
<span class="n">x</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">'x'</span><span class="p">)</span>
|
||
<span class="n">expr</span> <span class="o">=</span> <span class="n">exp</span><span class="p">(</span><span class="n">x</span><span class="o">*</span><span class="n">x</span><span class="p">)</span>
|
||
<span class="n">simplify</span><span class="p">(</span><span class="n">expr</span><span class="p">)</span>
|
||
<span class="n">derivative</span> <span class="o">=</span> <span class="n">diff</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span><span class="n">x</span><span class="p">)</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="n">python</span><span class="p">(</span><span class="n">expr</span><span class="p">))</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="n">python</span><span class="p">(</span><span class="n">derivative</span><span class="p">))</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="cell_output docutils container">
|
||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>x = Symbol('x')
|
||
e = exp(x**2)
|
||
x = Symbol('x')
|
||
e = 2*x*exp(x**2)
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
<section id="smarter-way-of-evaluating-the-above-function">
|
||
<h2>Smarter way of evaluating the above function<a class="headerlink" href="#smarter-way-of-evaluating-the-above-function" title="Link to this heading">#</a></h2>
|
||
<p>If we study this function, we note that we can reduce the number of operations by introducing an intermediate variable</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
a = x^2,
|
||
\]</div>
|
||
<p>leading to</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
f(x) = f(a(x)) = b= \exp{a}.
|
||
\]</div>
|
||
<p>We now assume that all operations can be counted in terms of equal
|
||
floating point operations. This means that in order to calculate
|
||
<span class="math notranslate nohighlight">\(f(x)\)</span> we need first to square <span class="math notranslate nohighlight">\(x\)</span> and then compute the exponential. We
|
||
have thus two floating point operations only.</p>
|
||
</section>
|
||
<section id="reducing-the-number-of-operations">
|
||
<h2>Reducing the number of operations<a class="headerlink" href="#reducing-the-number-of-operations" title="Link to this heading">#</a></h2>
|
||
<p>With the introduction of a precalculated quantity <span class="math notranslate nohighlight">\(a\)</span> and thereby <span class="math notranslate nohighlight">\(f(x)\)</span> we have that the derivative can be written as</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
f'(x) = 2xb,
|
||
\]</div>
|
||
<p>which reduces the number of operations from four in the orginal
|
||
expression to two. This means that if we need to compute <span class="math notranslate nohighlight">\(f(x)\)</span> and
|
||
its derivative (a common task in optimizations), we have reduced the
|
||
number of operations from six to four in total.</p>
|
||
<p><strong>Note</strong> that the usage of a symbolic software like SymPy does not
|
||
include such simplifications and the calculations of the function and
|
||
the derivatives yield in general more floating point operations.</p>
|
||
</section>
|
||
<section id="chain-rule-forward-and-reverse-modes">
|
||
<h2>Chain rule, forward and reverse modes<a class="headerlink" href="#chain-rule-forward-and-reverse-modes" title="Link to this heading">#</a></h2>
|
||
<p>In the above example we have introduced the variables <span class="math notranslate nohighlight">\(a\)</span> and <span class="math notranslate nohighlight">\(b\)</span>, and our function is</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
f(x) = f(a(x)) = b= \exp{a},
|
||
\]</div>
|
||
<p>with <span class="math notranslate nohighlight">\(a=x^2\)</span>. We can decompose the derivative of <span class="math notranslate nohighlight">\(f\)</span> with respect to <span class="math notranslate nohighlight">\(x\)</span> as</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{df}{dx}=\frac{df}{db}\frac{db}{da}\frac{da}{dx}.
|
||
\]</div>
|
||
<p>We note that since <span class="math notranslate nohighlight">\(b=f(x)\)</span> that</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{df}{db}=1,
|
||
\]</div>
|
||
<p>leading to</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{df}{dx}=\frac{db}{da}\frac{da}{dx}=2x\exp{x^2},
|
||
\]</div>
|
||
<p>as before.</p>
|
||
</section>
|
||
<section id="forward-and-reverse-modes">
|
||
<h2>Forward and reverse modes<a class="headerlink" href="#forward-and-reverse-modes" title="Link to this heading">#</a></h2>
|
||
<p>We have that</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{df}{dx}=\frac{df}{db}\frac{db}{da}\frac{da}{dx},
|
||
\]</div>
|
||
<p>which we can rewrite either as</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{df}{dx}=\left[\frac{df}{db}\frac{db}{da}\right]\frac{da}{dx},
|
||
\]</div>
|
||
<p>or</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{df}{dx}=\frac{df}{db}\left[\frac{db}{da}\frac{da}{dx}\right].
|
||
\]</div>
|
||
<p>The first expression is called reverse mode (or back propagation)
|
||
since we start by evaluating the derivatives at the end point and then
|
||
propagate backwards. This is the standard way of evaluating
|
||
derivatives (gradients) when optimizing the parameters of a neural
|
||
network. In the context of deep learning this is computationally
|
||
more efficient since the output of a neural network consists of either
|
||
one or some few other output variables.</p>
|
||
<p>The second equation defines the so-called <strong>forward mode</strong>.</p>
|
||
</section>
|
||
<section id="more-complicated-function">
|
||
<h2>More complicated function<a class="headerlink" href="#more-complicated-function" title="Link to this heading">#</a></h2>
|
||
<p>We increase our ambitions and introduce a slightly more complicated function</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
f(x) =\sqrt{x^2+exp{x^2}},
|
||
\]</div>
|
||
<p>with derivative</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
f'(x) =\frac{x(1+\exp{x^2})}{\sqrt{x^2+exp{x^2}}}.
|
||
\]</div>
|
||
<p>The corresponding SymPy code reads</p>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">division</span>
|
||
<span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="o">*</span>
|
||
<span class="n">x</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">'x'</span><span class="p">)</span>
|
||
<span class="n">expr</span> <span class="o">=</span> <span class="n">sqrt</span><span class="p">(</span><span class="n">x</span><span class="o">*</span><span class="n">x</span><span class="o">+</span><span class="n">exp</span><span class="p">(</span><span class="n">x</span><span class="o">*</span><span class="n">x</span><span class="p">))</span>
|
||
<span class="n">simplify</span><span class="p">(</span><span class="n">expr</span><span class="p">)</span>
|
||
<span class="n">derivative</span> <span class="o">=</span> <span class="n">diff</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span><span class="n">x</span><span class="p">)</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="n">python</span><span class="p">(</span><span class="n">expr</span><span class="p">))</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="n">python</span><span class="p">(</span><span class="n">derivative</span><span class="p">))</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="cell_output docutils container">
|
||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>x = Symbol('x')
|
||
e = sqrt(x**2 + exp(x**2))
|
||
x = Symbol('x')
|
||
e = (x*exp(x**2) + x)/sqrt(x**2 + exp(x**2))
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
<section id="counting-the-number-of-floating-point-operations">
|
||
<h2>Counting the number of floating point operations<a class="headerlink" href="#counting-the-number-of-floating-point-operations" title="Link to this heading">#</a></h2>
|
||
<p>A simple count of operations shows that we need five operations for
|
||
the function itself and ten for the derivative. Fifteen operations in total if we wish to proceed with the above codes.</p>
|
||
<p>Can we reduce this to
|
||
say half the number of operations?</p>
|
||
</section>
|
||
<section id="defining-intermediate-operations">
|
||
<h2>Defining intermediate operations<a class="headerlink" href="#defining-intermediate-operations" title="Link to this heading">#</a></h2>
|
||
<p>We can indeed reduce the number of operation to half of those listed in the brute force approach above.
|
||
We define the following quantities</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
a = x^2,
|
||
\]</div>
|
||
<p>and</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
b = \exp{x^2} = \exp{a},
|
||
\]</div>
|
||
<p>and</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
c= a+b,
|
||
\]</div>
|
||
<p>and</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
d=f(x)=\sqrt{c}.
|
||
\]</div>
|
||
</section>
|
||
<section id="new-expression-for-the-derivative">
|
||
<h2>New expression for the derivative<a class="headerlink" href="#new-expression-for-the-derivative" title="Link to this heading">#</a></h2>
|
||
<p>With these definitions we obtain the following partial derivatives</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial a}{\partial x} = 2x,
|
||
\]</div>
|
||
<p>and</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial b}{\partial a} = \exp{a},
|
||
\]</div>
|
||
<p>and</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial c}{\partial a} = 1,
|
||
\]</div>
|
||
<p>and</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial c}{\partial b} = 1,
|
||
\]</div>
|
||
<p>and</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial d}{\partial c} = \frac{1}{2\sqrt{c}},
|
||
\]</div>
|
||
<p>and finally</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial f}{\partial d} = 1.
|
||
\]</div>
|
||
</section>
|
||
<section id="final-derivatives">
|
||
<h2>Final derivatives<a class="headerlink" href="#final-derivatives" title="Link to this heading">#</a></h2>
|
||
<p>Our final derivatives are thus</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial f}{\partial c} = \frac{\partial f}{\partial d} \frac{\partial d}{\partial c} = \frac{1}{2\sqrt{c}},
|
||
\]</div>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial f}{\partial b} = \frac{\partial f}{\partial c} \frac{\partial c}{\partial b} = \frac{1}{2\sqrt{c}},
|
||
\]</div>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial f}{\partial a} = \frac{\partial f}{\partial c} \frac{\partial c}{\partial a}+
|
||
\frac{\partial f}{\partial b} \frac{\partial b}{\partial a} = \frac{1+\exp{a}}{2\sqrt{c}},
|
||
\]</div>
|
||
<p>and finally</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial f}{\partial x} = \frac{\partial f}{\partial a} \frac{\partial a}{\partial x} = \frac{x(1+\exp{a})}{\sqrt{c}},
|
||
\]</div>
|
||
<p>which is just</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial f}{\partial x} = \frac{x(1+b)}{d},
|
||
\]</div>
|
||
<p>and requires only three operations if we can reuse all intermediate variables.</p>
|
||
</section>
|
||
<section id="in-general-not-this-simple">
|
||
<h2>In general not this simple<a class="headerlink" href="#in-general-not-this-simple" title="Link to this heading">#</a></h2>
|
||
<p>In general, see the generalization below, unless we can obtain simple
|
||
analytical expressions which we can simplify further, the final
|
||
implementation of automatic differentiation involves repeated
|
||
calculations (and thereby operations) of derivatives of elementary
|
||
functions.</p>
|
||
</section>
|
||
<section id="automatic-differentiation">
|
||
<h2>Automatic differentiation<a class="headerlink" href="#automatic-differentiation" title="Link to this heading">#</a></h2>
|
||
<p>We can make this example more formal. Automatic differentiation is a
|
||
formalization of the previous example (see graph).</p>
|
||
<p>We define <span class="math notranslate nohighlight">\(\boldsymbol{x}\in x_1,\dots, x_l\)</span> input variables to a given function <span class="math notranslate nohighlight">\(f(\boldsymbol{x})\)</span> and <span class="math notranslate nohighlight">\(x_{l+1},\dots, x_L\)</span> intermediate variables.</p>
|
||
<p>In the above example we have only one input variable, <span class="math notranslate nohighlight">\(l=1\)</span> and four intermediate variables, that is</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\begin{bmatrix} x_1=x & x_2 = x^2=a & x_3 =\exp{a}= b & x_4=c=a+b & x_5 = \sqrt{c}=d \end{bmatrix}.
|
||
\]</div>
|
||
<p>Furthemore, for <span class="math notranslate nohighlight">\(i=l+1, \dots, L\)</span> (here <span class="math notranslate nohighlight">\(i=2,3,4,5\)</span> and <span class="math notranslate nohighlight">\(f=x_L=d\)</span>), we
|
||
define the elementary functions <span class="math notranslate nohighlight">\(g_i(x_{Pa(x_i)})\)</span> where <span class="math notranslate nohighlight">\(x_{Pa(x_i)}\)</span> are the parent nodes of the variable <span class="math notranslate nohighlight">\(x_i\)</span>.</p>
|
||
<p>In our case, we have for example for <span class="math notranslate nohighlight">\(x_3=g_3(x_{Pa(x_i)})=\exp{a}\)</span>, that <span class="math notranslate nohighlight">\(g_3=\exp{()}\)</span> and <span class="math notranslate nohighlight">\(x_{Pa(x_3)}=a\)</span>.</p>
|
||
</section>
|
||
<section id="chain-rule">
|
||
<h2>Chain rule<a class="headerlink" href="#chain-rule" title="Link to this heading">#</a></h2>
|
||
<p>We can now compute the gradients by back-propagating the derivatives using the chain rule.
|
||
We have defined</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial f}{\partial x_L} = 1,
|
||
\]</div>
|
||
<p>which allows us to find the derivatives of the various variables <span class="math notranslate nohighlight">\(x_i\)</span> as</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial f}{\partial x_i} = \sum_{x_j:x_i\in Pa(x_j)}\frac{\partial f}{\partial x_j} \frac{\partial x_j}{\partial x_i}=\sum_{x_j:x_i\in Pa(x_j)}\frac{\partial f}{\partial x_j} \frac{\partial g_j}{\partial x_i}.
|
||
\]</div>
|
||
<p>Whenever we have a function which can be expressed as a computation
|
||
graph and the various functions can be expressed in terms of
|
||
elementary functions that are differentiable, then automatic
|
||
differentiation works. The functions may not need to be elementary
|
||
functions, they could also be computer programs, although not all
|
||
programs can be automatically differentiated.</p>
|
||
</section>
|
||
<section id="first-network-example-simple-percepetron-with-one-input">
|
||
<h2>First network example, simple percepetron with one input<a class="headerlink" href="#first-network-example-simple-percepetron-with-one-input" title="Link to this heading">#</a></h2>
|
||
<p>As yet another example we define now a simple perceptron model with
|
||
all quantities given by scalars. We consider only one input variable
|
||
<span class="math notranslate nohighlight">\(x\)</span> and one target value <span class="math notranslate nohighlight">\(y\)</span>. We define an activation function
|
||
<span class="math notranslate nohighlight">\(\sigma_1\)</span> which takes as input</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
z_1 = w_1x+b_1,
|
||
\]</div>
|
||
<p>where <span class="math notranslate nohighlight">\(w_1\)</span> is the weight and <span class="math notranslate nohighlight">\(b_1\)</span> is the bias. These are the
|
||
parameters we want to optimize. The output is <span class="math notranslate nohighlight">\(a_1=\sigma(z_1)\)</span> (see
|
||
graph from whiteboard notes). This output is then fed into the
|
||
<strong>cost/loss</strong> function, which we here for the sake of simplicity just
|
||
define as the squared error</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
C(x;w_1,b_1)=\frac{1}{2}(a_1-y)^2.
|
||
\]</div>
|
||
</section>
|
||
<section id="layout-of-a-simple-neural-network-with-no-hidden-layer">
|
||
<h2>Layout of a simple neural network with no hidden layer<a class="headerlink" href="#layout-of-a-simple-neural-network-with-no-hidden-layer" title="Link to this heading">#</a></h2>
|
||
<!-- dom:FIGURE: [figures/simplenn1.png, width=900 frac=1.0] -->
|
||
<!-- begin figure -->
|
||
<p><img src="figures/simplenn1.png" width="900"><p style="font-size: 0.9em"><i>Figure 1: </i></p></p>
|
||
<!-- end figure --></section>
|
||
<section id="optimizing-the-parameters">
|
||
<h2>Optimizing the parameters<a class="headerlink" href="#optimizing-the-parameters" title="Link to this heading">#</a></h2>
|
||
<p>In setting up the feed forward and back propagation parts of the
|
||
algorithm, we need now the derivative of the various variables we want
|
||
to train.</p>
|
||
<p>We need</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial C}{\partial w_1} \hspace{0.1cm}\mathrm{and}\hspace{0.1cm}\frac{\partial C}{\partial b_1}.
|
||
\]</div>
|
||
<p>Using the chain rule we find</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial C}{\partial w_1}=\frac{\partial C}{\partial a_1}\frac{\partial a_1}{\partial z_1}\frac{\partial z_1}{\partial w_1}=(a_1-y)\sigma_1'x,
|
||
\]</div>
|
||
<p>and</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial C}{\partial b_1}=\frac{\partial C}{\partial a_1}\frac{\partial a_1}{\partial z_1}\frac{\partial z_1}{\partial b_1}=(a_1-y)\sigma_1',
|
||
\]</div>
|
||
<p>which we later will just define as</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial C}{\partial a_1}\frac{\partial a_1}{\partial z_1}=\delta_1.
|
||
\]</div>
|
||
</section>
|
||
<section id="adding-a-hidden-layer">
|
||
<h2>Adding a hidden layer<a class="headerlink" href="#adding-a-hidden-layer" title="Link to this heading">#</a></h2>
|
||
<p>We change our simple model to (see graph)
|
||
a network with just one hidden layer but with scalar variables only.</p>
|
||
<p>Our output variable changes to <span class="math notranslate nohighlight">\(a_2\)</span> and <span class="math notranslate nohighlight">\(a_1\)</span> is now the output from the hidden node and <span class="math notranslate nohighlight">\(a_0=x\)</span>.
|
||
We have then</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
z_1 = w_1a_0+b_1 \hspace{0.1cm} \wedge a_1 = \sigma_1(z_1),
|
||
\]</div>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
z_2 = w_2a_1+b_2 \hspace{0.1cm} \wedge a_2 = \sigma_2(z_2),
|
||
\]</div>
|
||
<p>and the cost function</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
C(x;\boldsymbol{\Theta})=\frac{1}{2}(a_2-y)^2,
|
||
\]</div>
|
||
<p>with <span class="math notranslate nohighlight">\(\boldsymbol{\Theta}=[w_1,w_2,b_1,b_2]\)</span>.</p>
|
||
</section>
|
||
<section id="layout-of-a-simple-neural-network-with-one-hidden-layer">
|
||
<h2>Layout of a simple neural network with one hidden layer<a class="headerlink" href="#layout-of-a-simple-neural-network-with-one-hidden-layer" title="Link to this heading">#</a></h2>
|
||
<!-- dom:FIGURE: [figures/simplenn2.png, width=900 frac=1.0] -->
|
||
<!-- begin figure -->
|
||
<p><img src="figures/simplenn2.png" width="900"><p style="font-size: 0.9em"><i>Figure 1: </i></p></p>
|
||
<!-- end figure --></section>
|
||
<section id="the-derivatives">
|
||
<h2>The derivatives<a class="headerlink" href="#the-derivatives" title="Link to this heading">#</a></h2>
|
||
<p>The derivatives are now, using the chain rule again</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial C}{\partial w_2}=\frac{\partial C}{\partial a_2}\frac{\partial a_2}{\partial z_2}\frac{\partial z_2}{\partial w_2}=(a_2-y)\sigma_2'a_1=\delta_2a_1,
|
||
\]</div>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial C}{\partial b_2}=\frac{\partial C}{\partial a_2}\frac{\partial a_2}{\partial z_2}\frac{\partial z_2}{\partial b_2}=(a_2-y)\sigma_2'=\delta_2,
|
||
\]</div>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial C}{\partial w_1}=\frac{\partial C}{\partial a_2}\frac{\partial a_2}{\partial z_2}\frac{\partial z_2}{\partial a_1}\frac{\partial a_1}{\partial z_1}\frac{\partial z_1}{\partial w_1}=(a_2-y)\sigma_2'a_1\sigma_1'a_0,
|
||
\]</div>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial C}{\partial b_1}=\frac{\partial C}{\partial a_2}\frac{\partial a_2}{\partial z_2}\frac{\partial z_2}{\partial a_1}\frac{\partial a_1}{\partial z_1}\frac{\partial z_1}{\partial b_1}=(a_2-y)\sigma_2'\sigma_1'=\delta_1.
|
||
\]</div>
|
||
<p>Can you generalize this to more than one hidden layer?</p>
|
||
</section>
|
||
<section id="important-observations">
|
||
<h2>Important observations<a class="headerlink" href="#important-observations" title="Link to this heading">#</a></h2>
|
||
<p>From the above equations we see that the derivatives of the activation
|
||
functions play a central role. If they vanish, the training may
|
||
stop. This is called the vanishing gradient problem, see discussions below. If they become
|
||
large, the parameters <span class="math notranslate nohighlight">\(w_i\)</span> and <span class="math notranslate nohighlight">\(b_i\)</span> may simply go to infinity. This
|
||
is referenced as the exploding gradient problem.</p>
|
||
</section>
|
||
<section id="the-training">
|
||
<h2>The training<a class="headerlink" href="#the-training" title="Link to this heading">#</a></h2>
|
||
<p>The training of the parameters is done through various gradient descent approximations with</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
w_{i}\leftarrow w_{i}- \eta \delta_i a_{i-1},
|
||
\]</div>
|
||
<p>and</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
b_i \leftarrow b_i-\eta \delta_i,
|
||
\]</div>
|
||
<p>with <span class="math notranslate nohighlight">\(\eta\)</span> is the learning rate.</p>
|
||
<p>One iteration consists of one feed forward step and one back-propagation step. Each back-propagation step does one update of the parameters <span class="math notranslate nohighlight">\(\boldsymbol{\Theta}\)</span>.</p>
|
||
<p>For the first hidden layer <span class="math notranslate nohighlight">\(a_{i-1}=a_0=x\)</span> for this simple model.</p>
|
||
</section>
|
||
<section id="code-example">
|
||
<h2>Code example<a class="headerlink" href="#code-example" title="Link to this heading">#</a></h2>
|
||
<p>The code here implements the above model with one hidden layer and
|
||
scalar variables for the same function we studied in the previous
|
||
example. The code is however set up so that we can add multiple
|
||
inputs <span class="math notranslate nohighlight">\(x\)</span> and target values <span class="math notranslate nohighlight">\(y\)</span>. Note also that we have the
|
||
possibility of defining a feature matrix <span class="math notranslate nohighlight">\(\boldsymbol{X}\)</span> with more than just
|
||
one column for the input values. This will turn useful in our next example. We have also defined matrices and vectors for all of our operations although it is not necessary here.</p>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
|
||
<span class="c1"># We use the Sigmoid function as activation function</span>
|
||
<span class="k">def</span> <span class="nf">sigmoid</span><span class="p">(</span><span class="n">z</span><span class="p">):</span>
|
||
<span class="k">return</span> <span class="mf">1.0</span><span class="o">/</span><span class="p">(</span><span class="mf">1.0</span><span class="o">+</span><span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">z</span><span class="p">))</span>
|
||
|
||
<span class="k">def</span> <span class="nf">forwardpropagation</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
|
||
<span class="c1"># weighted sum of inputs to the hidden layer</span>
|
||
<span class="n">z_1</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">matmul</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">w_1</span><span class="p">)</span> <span class="o">+</span> <span class="n">b_1</span>
|
||
<span class="c1"># activation in the hidden layer</span>
|
||
<span class="n">a_1</span> <span class="o">=</span> <span class="n">sigmoid</span><span class="p">(</span><span class="n">z_1</span><span class="p">)</span>
|
||
<span class="c1"># weighted sum of inputs to the output layer</span>
|
||
<span class="n">z_2</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">matmul</span><span class="p">(</span><span class="n">a_1</span><span class="p">,</span> <span class="n">w_2</span><span class="p">)</span> <span class="o">+</span> <span class="n">b_2</span>
|
||
<span class="n">a_2</span> <span class="o">=</span> <span class="n">z_2</span>
|
||
<span class="k">return</span> <span class="n">a_1</span><span class="p">,</span> <span class="n">a_2</span>
|
||
|
||
<span class="k">def</span> <span class="nf">backpropagation</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
|
||
<span class="n">a_1</span><span class="p">,</span> <span class="n">a_2</span> <span class="o">=</span> <span class="n">forwardpropagation</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
|
||
<span class="c1"># parameter delta for the output layer, note that a_2=z_2 and its derivative wrt z_2 is just 1</span>
|
||
<span class="n">delta_2</span> <span class="o">=</span> <span class="n">a_2</span> <span class="o">-</span> <span class="n">y</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="mf">0.5</span><span class="o">*</span><span class="p">((</span><span class="n">a_2</span><span class="o">-</span><span class="n">y</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span><span class="p">))</span>
|
||
<span class="c1"># delta for the hidden layer</span>
|
||
<span class="n">delta_1</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">matmul</span><span class="p">(</span><span class="n">delta_2</span><span class="p">,</span> <span class="n">w_2</span><span class="o">.</span><span class="n">T</span><span class="p">)</span> <span class="o">*</span> <span class="n">a_1</span> <span class="o">*</span> <span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="n">a_1</span><span class="p">)</span>
|
||
<span class="c1"># gradients for the output layer</span>
|
||
<span class="n">output_weights_gradient</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">matmul</span><span class="p">(</span><span class="n">a_1</span><span class="o">.</span><span class="n">T</span><span class="p">,</span> <span class="n">delta_2</span><span class="p">)</span>
|
||
<span class="n">output_bias_gradient</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">delta_2</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
|
||
<span class="c1"># gradient for the hidden layer</span>
|
||
<span class="n">hidden_weights_gradient</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">matmul</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">T</span><span class="p">,</span> <span class="n">delta_1</span><span class="p">)</span>
|
||
<span class="n">hidden_bias_gradient</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">delta_1</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
|
||
<span class="k">return</span> <span class="n">output_weights_gradient</span><span class="p">,</span> <span class="n">output_bias_gradient</span><span class="p">,</span> <span class="n">hidden_weights_gradient</span><span class="p">,</span> <span class="n">hidden_bias_gradient</span>
|
||
|
||
|
||
<span class="c1"># ensure the same random numbers appear every time</span>
|
||
<span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
|
||
<span class="c1"># Input variable</span>
|
||
<span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mf">4.0</span><span class="p">],</span><span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float64</span><span class="p">)</span>
|
||
<span class="c1"># Target values</span>
|
||
<span class="n">y</span> <span class="o">=</span> <span class="mi">2</span><span class="o">*</span><span class="n">x</span><span class="o">+</span><span class="mf">1.0</span>
|
||
|
||
<span class="c1"># Defining the neural network, only scalars here</span>
|
||
<span class="n">n_inputs</span> <span class="o">=</span> <span class="n">x</span><span class="o">.</span><span class="n">shape</span>
|
||
<span class="n">n_features</span> <span class="o">=</span> <span class="mi">1</span>
|
||
<span class="n">n_hidden_neurons</span> <span class="o">=</span> <span class="mi">1</span>
|
||
<span class="n">n_outputs</span> <span class="o">=</span> <span class="mi">1</span>
|
||
|
||
<span class="c1"># Initialize the network</span>
|
||
<span class="c1"># weights and bias in the hidden layer</span>
|
||
<span class="n">w_1</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="n">n_features</span><span class="p">,</span> <span class="n">n_hidden_neurons</span><span class="p">)</span>
|
||
<span class="n">b_1</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">n_hidden_neurons</span><span class="p">)</span> <span class="o">+</span> <span class="mf">0.01</span>
|
||
|
||
<span class="c1"># weights and bias in the output layer</span>
|
||
<span class="n">w_2</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="n">n_hidden_neurons</span><span class="p">,</span> <span class="n">n_outputs</span><span class="p">)</span>
|
||
<span class="n">b_2</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">n_outputs</span><span class="p">)</span> <span class="o">+</span> <span class="mf">0.01</span>
|
||
|
||
<span class="n">eta</span> <span class="o">=</span> <span class="mf">0.1</span>
|
||
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">50</span><span class="p">):</span>
|
||
<span class="c1"># calculate gradients</span>
|
||
<span class="n">derivW2</span><span class="p">,</span> <span class="n">derivB2</span><span class="p">,</span> <span class="n">derivW1</span><span class="p">,</span> <span class="n">derivB1</span> <span class="o">=</span> <span class="n">backpropagation</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
|
||
<span class="c1"># update weights and biases</span>
|
||
<span class="n">w_2</span> <span class="o">-=</span> <span class="n">eta</span> <span class="o">*</span> <span class="n">derivW2</span>
|
||
<span class="n">b_2</span> <span class="o">-=</span> <span class="n">eta</span> <span class="o">*</span> <span class="n">derivB2</span>
|
||
<span class="n">w_1</span> <span class="o">-=</span> <span class="n">eta</span> <span class="o">*</span> <span class="n">derivW1</span>
|
||
<span class="n">b_1</span> <span class="o">-=</span> <span class="n">eta</span> <span class="o">*</span> <span class="n">derivB1</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="cell_output docutils container">
|
||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>[36.89563074]
|
||
[23.62323175]
|
||
[15.1251681]
|
||
[9.68402334]
|
||
[6.20020163]
|
||
[3.96963458]
|
||
[2.54150298]
|
||
[1.62714703]
|
||
[1.0417409]
|
||
[0.66694492]
|
||
[0.42699034]
|
||
[0.27336592]
|
||
[0.17501258]
|
||
[0.11204514]
|
||
[0.07173249]
|
||
[0.04592382]
|
||
[0.02940083]
|
||
[0.01882264]
|
||
[0.01205039]
|
||
[0.00771475]
|
||
[0.00493903]
|
||
[0.003162]
|
||
[0.00202433]
|
||
[0.00129599]
|
||
[0.0008297]
|
||
[0.00053118]
|
||
[0.00034006]
|
||
[0.00021771]
|
||
[0.00013938]
|
||
[8.92313548e-05]
|
||
[5.71263851e-05]
|
||
[3.6572612e-05]
|
||
[2.34139775e-05]
|
||
[1.49897504e-05]
|
||
[9.5965161e-06]
|
||
[6.14373934e-06]
|
||
[3.93325371e-06]
|
||
[2.51808934e-06]
|
||
[1.61209378e-06]
|
||
[1.03207075e-06]
|
||
[6.60737006e-07]
|
||
[4.23007231e-07]
|
||
[2.70811405e-07]
|
||
[1.73374853e-07]
|
||
[1.10995472e-07]
|
||
[7.10598715e-08]
|
||
[4.54928947e-08]
|
||
[2.91247848e-08]
|
||
[1.86458368e-08]
|
||
[1.19371605e-08]
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<p>We see that after some few iterations (the results do depend on the learning rate however), we get an error which is rather small.</p>
|
||
</section>
|
||
<section id="exercise-1-including-more-data">
|
||
<h2>Exercise 1: Including more data<a class="headerlink" href="#exercise-1-including-more-data" title="Link to this heading">#</a></h2>
|
||
<p>Try to increase the amount of input and
|
||
target/output data. Try also to perform calculations for more values
|
||
of the learning rates. Feel free to add either hyperparameters with an
|
||
<span class="math notranslate nohighlight">\(l_1\)</span> norm or an <span class="math notranslate nohighlight">\(l_2\)</span> norm and discuss your results.
|
||
Discuss your results as functions of the amount of training data and various learning rates.</p>
|
||
<p><strong>Challenge:</strong> Try to change the activation functions and replace the hard-coded analytical expressions with automatic derivation via either <strong>autograd</strong> or <strong>JAX</strong>.</p>
|
||
</section>
|
||
<section id="simple-neural-network-and-the-back-propagation-equations">
|
||
<h2>Simple neural network and the back propagation equations<a class="headerlink" href="#simple-neural-network-and-the-back-propagation-equations" title="Link to this heading">#</a></h2>
|
||
<p>Let us now try to increase our level of ambition and attempt at setting
|
||
up the equations for a neural network with two input nodes, one hidden
|
||
layer with two hidden nodes and one output layer with one output node/neuron only (see graph)..</p>
|
||
<p>We need to define the following parameters and variables with the input layer (layer <span class="math notranslate nohighlight">\((0)\)</span>)
|
||
where we label the nodes <span class="math notranslate nohighlight">\(x_0\)</span> and <span class="math notranslate nohighlight">\(x_1\)</span></p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
x_0 = a_0^{(0)} \wedge x_1 = a_1^{(0)}.
|
||
\]</div>
|
||
<p>The hidden layer (layer <span class="math notranslate nohighlight">\((1)\)</span>) has nodes which yield the outputs <span class="math notranslate nohighlight">\(a_0^{(1)}\)</span> and <span class="math notranslate nohighlight">\(a_1^{(1)}\)</span>) with weight <span class="math notranslate nohighlight">\(\boldsymbol{w}\)</span> and bias <span class="math notranslate nohighlight">\(\boldsymbol{b}\)</span> parameters</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
w_{ij}^{(1)}=\left\{w_{00}^{(1)},w_{01}^{(1)},w_{10}^{(1)},w_{11}^{(1)}\right\} \wedge b^{(1)}=\left\{b_0^{(1)},b_1^{(1)}\right\}.
|
||
\]</div>
|
||
</section>
|
||
<section id="layout-of-a-simple-neural-network-with-two-input-nodes-one-hidden-layer-and-one-output-node">
|
||
<h2>Layout of a simple neural network with two input nodes, one hidden layer and one output node<a class="headerlink" href="#layout-of-a-simple-neural-network-with-two-input-nodes-one-hidden-layer-and-one-output-node" title="Link to this heading">#</a></h2>
|
||
<!-- dom:FIGURE: [figures/simplenn3.png, width=900 frac=1.0] -->
|
||
<!-- begin figure -->
|
||
<p><img src="figures/simplenn3.png" width="900"><p style="font-size: 0.9em"><i>Figure 1: </i></p></p>
|
||
<!-- end figure --></section>
|
||
<section id="the-ouput-layer">
|
||
<h2>The ouput layer<a class="headerlink" href="#the-ouput-layer" title="Link to this heading">#</a></h2>
|
||
<p>Finally, we have the ouput layer given by layer label <span class="math notranslate nohighlight">\((2)\)</span> with output <span class="math notranslate nohighlight">\(a^{(2)}\)</span> and weights and biases to be determined given by the variables</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
w_{i}^{(2)}=\left\{w_{0}^{(2)},w_{1}^{(2)}\right\} \wedge b^{(2)}.
|
||
\]</div>
|
||
<p>Our output is <span class="math notranslate nohighlight">\(\tilde{y}=a^{(2)}\)</span> and we define a generic cost function <span class="math notranslate nohighlight">\(C(a^{(2)},y;\boldsymbol{\Theta})\)</span> where <span class="math notranslate nohighlight">\(y\)</span> is the target value (a scalar here).
|
||
The parameters we need to optimize are given by</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\boldsymbol{\Theta}=\left\{w_{00}^{(1)},w_{01}^{(1)},w_{10}^{(1)},w_{11}^{(1)},w_{0}^{(2)},w_{1}^{(2)},b_0^{(1)},b_1^{(1)},b^{(2)}\right\}.
|
||
\]</div>
|
||
</section>
|
||
<section id="compact-expressions">
|
||
<h2>Compact expressions<a class="headerlink" href="#compact-expressions" title="Link to this heading">#</a></h2>
|
||
<p>We can define the inputs to the activation functions for the various layers in terms of various matrix-vector multiplications and vector additions.
|
||
The inputs to the first hidden layer are</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[\begin{split}
|
||
\begin{bmatrix}z_0^{(1)} \\ z_1^{(1)} \end{bmatrix}=\begin{bmatrix}w_{00}^{(1)} & w_{01}^{(1)}\\ w_{10}^{(1)} &w_{11}^{(1)} \end{bmatrix}\begin{bmatrix}a_0^{(0)} \\ a_1^{(0)} \end{bmatrix}+\begin{bmatrix}b_0^{(1)} \\ b_1^{(1)} \end{bmatrix},
|
||
\end{split}\]</div>
|
||
<p>with outputs</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[\begin{split}
|
||
\begin{bmatrix}a_0^{(1)} \\ a_1^{(1)} \end{bmatrix}=\begin{bmatrix}\sigma^{(1)}(z_0^{(1)}) \\ \sigma^{(1)}(z_1^{(1)}) \end{bmatrix}.
|
||
\end{split}\]</div>
|
||
</section>
|
||
<section id="output-layer">
|
||
<h2>Output layer<a class="headerlink" href="#output-layer" title="Link to this heading">#</a></h2>
|
||
<p>For the final output layer we have the inputs to the final activation function</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
z^{(2)} = w_{0}^{(2)}a_0^{(1)} +w_{1}^{(2)}a_1^{(1)}+b^{(2)},
|
||
\]</div>
|
||
<p>resulting in the output</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
a^{(2)}=\sigma^{(2)}(z^{(2)}).
|
||
\]</div>
|
||
</section>
|
||
<section id="explicit-derivatives">
|
||
<h2>Explicit derivatives<a class="headerlink" href="#explicit-derivatives" title="Link to this heading">#</a></h2>
|
||
<p>In total we have nine parameters which we need to train. Using the
|
||
chain rule (or just the back-propagation algorithm) we can find all
|
||
derivatives. Since we will use automatic differentiation in reverse
|
||
mode, we start with the derivatives of the cost function with respect
|
||
to the parameters of the output layer, namely</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial C}{\partial w_{i}^{(2)}}=\frac{\partial C}{\partial a^{(2)}}\frac{\partial a^{(2)}}{\partial z^{(2)}}\frac{\partial z^{(2)}}{\partial w_{i}^{(2)}}=\delta^{(2)}a_i^{(1)},
|
||
\]</div>
|
||
<p>with</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\delta^{(2)}=\frac{\partial C}{\partial a^{(2)}}\frac{\partial a^{(2)}}{\partial z^{(2)}}
|
||
\]</div>
|
||
<p>and finally</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial C}{\partial b^{(2)}}=\frac{\partial C}{\partial a^{(2)}}\frac{\partial a^{(2)}}{\partial z^{(2)}}\frac{\partial z^{(2)}}{\partial b^{(2)}}=\delta^{(2)}.
|
||
\]</div>
|
||
</section>
|
||
<section id="derivatives-of-the-hidden-layer">
|
||
<h2>Derivatives of the hidden layer<a class="headerlink" href="#derivatives-of-the-hidden-layer" title="Link to this heading">#</a></h2>
|
||
<p>Using the chain rule we have the following expressions for say one of the weight parameters (it is easy to generalize to the other weight parameters)</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial C}{\partial w_{00}^{(1)}}=\frac{\partial C}{\partial a^{(2)}}\frac{\partial a^{(2)}}{\partial z^{(2)}}
|
||
\frac{\partial z^{(2)}}{\partial z_0^{(1)}}\frac{\partial z_0^{(1)}}{\partial w_{00}^{(1)}}= \delta^{(2)}\frac{\partial z^{(2)}}{\partial z_0^{(1)}}\frac{\partial z_0^{(1)}}{\partial w_{00}^{(1)}},
|
||
\]</div>
|
||
<p>which, noting that</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
z^{(2)} =w_0^{(2)}a_0^{(1)}+w_1^{(2)}a_1^{(1)}+b^{(2)},
|
||
\]</div>
|
||
<p>allows us to rewrite</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial z^{(2)}}{\partial z_0^{(1)}}\frac{\partial z_0^{(1)}}{\partial w_{00}^{(1)}}=w_0^{(2)}\frac{\partial a_0^{(1)}}{\partial z_0^{(1)}}a_0^{(1)}.
|
||
\]</div>
|
||
</section>
|
||
<section id="final-expression">
|
||
<h2>Final expression<a class="headerlink" href="#final-expression" title="Link to this heading">#</a></h2>
|
||
<p>Defining</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\delta_0^{(1)}=w_0^{(2)}\frac{\partial a_0^{(1)}}{\partial z_0^{(1)}}\delta^{(2)},
|
||
\]</div>
|
||
<p>we have</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial C}{\partial w_{00}^{(1)}}=\delta_0^{(1)}a_0^{(1)}.
|
||
\]</div>
|
||
<p>Similarly, we obtain</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial C}{\partial w_{01}^{(1)}}=\delta_0^{(1)}a_1^{(1)}.
|
||
\]</div>
|
||
</section>
|
||
<section id="completing-the-list">
|
||
<h2>Completing the list<a class="headerlink" href="#completing-the-list" title="Link to this heading">#</a></h2>
|
||
<p>Similarly, we find</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial C}{\partial w_{10}^{(1)}}=\delta_1^{(1)}a_0^{(1)},
|
||
\]</div>
|
||
<p>and</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial C}{\partial w_{11}^{(1)}}=\delta_1^{(1)}a_1^{(1)},
|
||
\]</div>
|
||
<p>where we have defined</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\delta_1^{(1)}=w_1^{(2)}\frac{\partial a_1^{(1)}}{\partial z_1^{(1)}}\delta^{(2)}.
|
||
\]</div>
|
||
</section>
|
||
<section id="final-expressions-for-the-biases-of-the-hidden-layer">
|
||
<h2>Final expressions for the biases of the hidden layer<a class="headerlink" href="#final-expressions-for-the-biases-of-the-hidden-layer" title="Link to this heading">#</a></h2>
|
||
<p>For the sake of completeness, we list the derivatives of the biases, which are</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial C}{\partial b_{0}^{(1)}}=\delta_0^{(1)},
|
||
\]</div>
|
||
<p>and</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial C}{\partial b_{1}^{(1)}}=\delta_1^{(1)}.
|
||
\]</div>
|
||
<p>As we will see below, these expressions can be generalized in a more compact form.</p>
|
||
</section>
|
||
<section id="gradient-expressions">
|
||
<h2>Gradient expressions<a class="headerlink" href="#gradient-expressions" title="Link to this heading">#</a></h2>
|
||
<p>For this specific model, with just one output node and two hidden
|
||
nodes, the gradient descent equations take the following form for output layer</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
w_{i}^{(2)}\leftarrow w_{i}^{(2)}- \eta \delta^{(2)} a_{i}^{(1)},
|
||
\]</div>
|
||
<p>and</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
b^{(2)} \leftarrow b^{(2)}-\eta \delta^{(2)},
|
||
\]</div>
|
||
<p>and</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
w_{ij}^{(1)}\leftarrow w_{ij}^{(1)}- \eta \delta_{i}^{(1)} a_{j}^{(0)},
|
||
\]</div>
|
||
<p>and</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
b_{i}^{(1)} \leftarrow b_{i}^{(1)}-\eta \delta_{i}^{(1)},
|
||
\]</div>
|
||
<p>where <span class="math notranslate nohighlight">\(\eta\)</span> is the learning rate.</p>
|
||
</section>
|
||
<section id="exercise-2-extended-program">
|
||
<h2>Exercise 2: Extended program<a class="headerlink" href="#exercise-2-extended-program" title="Link to this heading">#</a></h2>
|
||
<p>We extend our simple code to a function which depends on two variable <span class="math notranslate nohighlight">\(x_0\)</span> and <span class="math notranslate nohighlight">\(x_1\)</span>, that is</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
y=f(x_0,x_1)=x_0^2+3x_0x_1+x_1^2+5.
|
||
\]</div>
|
||
<p>We feed our network with <span class="math notranslate nohighlight">\(n=100\)</span> entries <span class="math notranslate nohighlight">\(x_0\)</span> and <span class="math notranslate nohighlight">\(x_1\)</span>. We have thus two features represented by these variable and an input matrix/design matrix <span class="math notranslate nohighlight">\(\boldsymbol{X}\in \mathbf{R}^{n\times 2}\)</span></p>
|
||
<div class="math notranslate nohighlight">
|
||
\[\begin{split}
|
||
\boldsymbol{X}=\begin{bmatrix} x_{00} & x_{01} \\ x_{00} & x_{01} \\ x_{10} & x_{11} \\ x_{20} & x_{21} \\ \dots & \dots \\ \dots & \dots \\ x_{n-20} & x_{n-21} \\ x_{n-10} & x_{n-11} \end{bmatrix}.
|
||
\end{split}\]</div>
|
||
<p>Write a code, based on the previous code examples, which takes as input these data and fit the above function.
|
||
You can extend your code to include automatic differentiation.</p>
|
||
<p>With these examples, we are now ready to embark upon the writing of more a general code for neural networks.</p>
|
||
</section>
|
||
<section id="getting-serious-the-back-propagation-equations-for-a-neural-network">
|
||
<h2>Getting serious, the back propagation equations for a neural network<a class="headerlink" href="#getting-serious-the-back-propagation-equations-for-a-neural-network" title="Link to this heading">#</a></h2>
|
||
<p>Now it is time to move away from one node in each layer only. Our inputs are also represented either by several inputs.</p>
|
||
<p>We have thus</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial{\cal C}((\boldsymbol{\Theta}^L)}{\partial w_{jk}^L} = \left(a_j^L - y_j\right)a_j^L(1-a_j^L)a_k^{L-1},
|
||
\]</div>
|
||
<p>Defining</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\delta_j^L = a_j^L(1-a_j^L)\left(a_j^L - y_j\right) = \sigma'(z_j^L)\frac{\partial {\cal C}}{\partial (a_j^L)},
|
||
\]</div>
|
||
<p>and using the Hadamard product of two vectors we can write this as</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\boldsymbol{\delta}^L = \sigma'(\hat{z}^L)\circ\frac{\partial {\cal C}}{\partial (\boldsymbol{a}^L)}.
|
||
\]</div>
|
||
</section>
|
||
<section id="analyzing-the-last-results">
|
||
<h2>Analyzing the last results<a class="headerlink" href="#analyzing-the-last-results" title="Link to this heading">#</a></h2>
|
||
<p>This is an important expression. The second term on the right handside
|
||
measures how fast the cost function is changing as a function of the <span class="math notranslate nohighlight">\(j\)</span>th
|
||
output activation. If, for example, the cost function doesn’t depend
|
||
much on a particular output node <span class="math notranslate nohighlight">\(j\)</span>, then <span class="math notranslate nohighlight">\(\delta_j^L\)</span> will be small,
|
||
which is what we would expect. The first term on the right, measures
|
||
how fast the activation function <span class="math notranslate nohighlight">\(f\)</span> is changing at a given activation
|
||
value <span class="math notranslate nohighlight">\(z_j^L\)</span>.</p>
|
||
</section>
|
||
<section id="more-considerations">
|
||
<h2>More considerations<a class="headerlink" href="#more-considerations" title="Link to this heading">#</a></h2>
|
||
<p>Notice that everything in the above equations is easily computed. In
|
||
particular, we compute <span class="math notranslate nohighlight">\(z_j^L\)</span> while computing the behaviour of the
|
||
network, and it is only a small additional overhead to compute
|
||
<span class="math notranslate nohighlight">\(\sigma'(z^L_j)\)</span>. The exact form of the derivative with respect to the
|
||
output depends on the form of the cost function.
|
||
However, provided the cost function is known there should be little
|
||
trouble in calculating</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial {\cal C}}{\partial (a_j^L)}
|
||
\]</div>
|
||
<p>With the definition of <span class="math notranslate nohighlight">\(\delta_j^L\)</span> we have a more compact definition of the derivative of the cost function in terms of the weights, namely</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial{\cal C}}{\partial w_{jk}^L} = \delta_j^La_k^{L-1}.
|
||
\]</div>
|
||
</section>
|
||
<section id="derivatives-in-terms-of-z-j-l">
|
||
<h2>Derivatives in terms of <span class="math notranslate nohighlight">\(z_j^L\)</span><a class="headerlink" href="#derivatives-in-terms-of-z-j-l" title="Link to this heading">#</a></h2>
|
||
<p>It is also easy to see that our previous equation can be written as</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\delta_j^L =\frac{\partial {\cal C}}{\partial z_j^L}= \frac{\partial {\cal C}}{\partial a_j^L}\frac{\partial a_j^L}{\partial z_j^L},
|
||
\]</div>
|
||
<p>which can also be interpreted as the partial derivative of the cost function with respect to the biases <span class="math notranslate nohighlight">\(b_j^L\)</span>, namely</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\delta_j^L = \frac{\partial {\cal C}}{\partial b_j^L}\frac{\partial b_j^L}{\partial z_j^L}=\frac{\partial {\cal C}}{\partial b_j^L},
|
||
\]</div>
|
||
<p>That is, the error <span class="math notranslate nohighlight">\(\delta_j^L\)</span> is exactly equal to the rate of change of the cost function as a function of the bias.</p>
|
||
</section>
|
||
<section id="bringing-it-together">
|
||
<h2>Bringing it together<a class="headerlink" href="#bringing-it-together" title="Link to this heading">#</a></h2>
|
||
<p>We have now three equations that are essential for the computations of the derivatives of the cost function at the output layer. These equations are needed to start the algorithm and they are</p>
|
||
<!-- Equation labels as ordinary links -->
|
||
<div id="_auto1"></div>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\begin{equation}
|
||
\frac{\partial{\cal C}(\hat{W^L})}{\partial w_{jk}^L} = \delta_j^La_k^{L-1},
|
||
\label{_auto1} \tag{2}
|
||
\end{equation}
|
||
\]</div>
|
||
<p>and</p>
|
||
<!-- Equation labels as ordinary links -->
|
||
<div id="_auto2"></div>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\begin{equation}
|
||
\delta_j^L = \sigma'(z_j^L)\frac{\partial {\cal C}}{\partial (a_j^L)},
|
||
\label{_auto2} \tag{3}
|
||
\end{equation}
|
||
\]</div>
|
||
<p>and</p>
|
||
<!-- Equation labels as ordinary links -->
|
||
<div id="_auto3"></div>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\begin{equation}
|
||
\delta_j^L = \frac{\partial {\cal C}}{\partial b_j^L},
|
||
\label{_auto3} \tag{4}
|
||
\end{equation}
|
||
\]</div>
|
||
</section>
|
||
<section id="final-back-propagating-equation">
|
||
<h2>Final back propagating equation<a class="headerlink" href="#final-back-propagating-equation" title="Link to this heading">#</a></h2>
|
||
<p>We have that (replacing <span class="math notranslate nohighlight">\(L\)</span> with a general layer <span class="math notranslate nohighlight">\(l\)</span>)</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\delta_j^l =\frac{\partial {\cal C}}{\partial z_j^l}.
|
||
\]</div>
|
||
<p>We want to express this in terms of the equations for layer <span class="math notranslate nohighlight">\(l+1\)</span>.</p>
|
||
</section>
|
||
<section id="using-the-chain-rule-and-summing-over-all-k-entries">
|
||
<h2>Using the chain rule and summing over all <span class="math notranslate nohighlight">\(k\)</span> entries<a class="headerlink" href="#using-the-chain-rule-and-summing-over-all-k-entries" title="Link to this heading">#</a></h2>
|
||
<p>We obtain</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\delta_j^l =\sum_k \frac{\partial {\cal C}}{\partial z_k^{l+1}}\frac{\partial z_k^{l+1}}{\partial z_j^{l}}=\sum_k \delta_k^{l+1}\frac{\partial z_k^{l+1}}{\partial z_j^{l}},
|
||
\]</div>
|
||
<p>and recalling that</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
z_j^{l+1} = \sum_{i=1}^{M_{l}}w_{ij}^{l+1}a_i^{l}+b_j^{l+1},
|
||
\]</div>
|
||
<p>with <span class="math notranslate nohighlight">\(M_l\)</span> being the number of nodes in layer <span class="math notranslate nohighlight">\(l\)</span>, we obtain</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\delta_j^l =\sum_k \delta_k^{l+1}w_{kj}^{l+1}\sigma'(z_j^l),
|
||
\]</div>
|
||
<p>This is our final equation.</p>
|
||
<p>We are now ready to set up the algorithm for back propagation and learning the weights and biases.</p>
|
||
</section>
|
||
<section id="setting-up-the-back-propagation-algorithm">
|
||
<h2>Setting up the back propagation algorithm<a class="headerlink" href="#setting-up-the-back-propagation-algorithm" title="Link to this heading">#</a></h2>
|
||
<p>The four equations provide us with a way of computing the gradient of the cost function. Let us write this out in the form of an algorithm.</p>
|
||
<p><strong>First</strong>, we set up the input data <span class="math notranslate nohighlight">\(\hat{x}\)</span> and the activations
|
||
<span class="math notranslate nohighlight">\(\hat{z}_1\)</span> of the input layer and compute the activation function and
|
||
the pertinent outputs <span class="math notranslate nohighlight">\(\hat{a}^1\)</span>.</p>
|
||
<p><strong>Secondly</strong>, we perform then the feed forward till we reach the output
|
||
layer and compute all <span class="math notranslate nohighlight">\(\hat{z}_l\)</span> of the input layer and compute the
|
||
activation function and the pertinent outputs <span class="math notranslate nohighlight">\(\hat{a}^l\)</span> for
|
||
<span class="math notranslate nohighlight">\(l=1,2,3,\dots,L\)</span>.</p>
|
||
<p><strong>Notation</strong>: The first hidden layer has <span class="math notranslate nohighlight">\(l=1\)</span> as label and the final output layer has <span class="math notranslate nohighlight">\(l=L\)</span>.</p>
|
||
</section>
|
||
<section id="setting-up-the-back-propagation-algorithm-part-2">
|
||
<h2>Setting up the back propagation algorithm, part 2<a class="headerlink" href="#setting-up-the-back-propagation-algorithm-part-2" title="Link to this heading">#</a></h2>
|
||
<p>Thereafter we compute the ouput error <span class="math notranslate nohighlight">\(\hat{\delta}^L\)</span> by computing all</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\delta_j^L = \sigma'(z_j^L)\frac{\partial {\cal C}}{\partial (a_j^L)}.
|
||
\]</div>
|
||
<p>Then we compute the back propagate error for each <span class="math notranslate nohighlight">\(l=L-1,L-2,\dots,1\)</span> as</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\delta_j^l = \sum_k \delta_k^{l+1}w_{kj}^{l+1}\sigma'(z_j^l).
|
||
\]</div>
|
||
</section>
|
||
<section id="setting-up-the-back-propagation-algorithm-part-3">
|
||
<h2>Setting up the Back propagation algorithm, part 3<a class="headerlink" href="#setting-up-the-back-propagation-algorithm-part-3" title="Link to this heading">#</a></h2>
|
||
<p>Finally, we update the weights and the biases using gradient descent
|
||
for each <span class="math notranslate nohighlight">\(l=L-1,L-2,\dots,1\)</span> and update the weights and biases
|
||
according to the rules</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
w_{jk}^l\leftarrow = w_{jk}^l- \eta \delta_j^la_k^{l-1},
|
||
\]</div>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
b_j^l \leftarrow b_j^l-\eta \frac{\partial {\cal C}}{\partial b_j^l}=b_j^l-\eta \delta_j^l,
|
||
\]</div>
|
||
<p>with <span class="math notranslate nohighlight">\(\eta\)</span> being the learning rate.</p>
|
||
</section>
|
||
<section id="updating-the-gradients">
|
||
<h2>Updating the gradients<a class="headerlink" href="#updating-the-gradients" title="Link to this heading">#</a></h2>
|
||
<p>With the back propagate error for each <span class="math notranslate nohighlight">\(l=L-1,L-2,\dots,1\)</span> as</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\delta_j^l = \sum_k \delta_k^{l+1}w_{kj}^{l+1}sigma'(z_j^l),
|
||
\]</div>
|
||
<p>we update the weights and the biases using gradient descent for each <span class="math notranslate nohighlight">\(l=L-1,L-2,\dots,1\)</span> and update the weights and biases according to the rules</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
w_{jk}^l\leftarrow = w_{jk}^l- \eta \delta_j^la_k^{l-1},
|
||
\]</div>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
b_j^l \leftarrow b_j^l-\eta \frac{\partial {\cal C}}{\partial b_j^l}=b_j^l-\eta \delta_j^l,
|
||
\]</div>
|
||
<section id="activation-functions">
|
||
<h3>Activation functions<a class="headerlink" href="#activation-functions" title="Link to this heading">#</a></h3>
|
||
<p>A property that characterizes a neural network, other than its
|
||
connectivity, is the choice of activation function(s). As described
|
||
in, the following restrictions are imposed on an activation function
|
||
for a FFNN to fulfill the universal approximation theorem</p>
|
||
<ul class="simple">
|
||
<li><p>Non-constant</p></li>
|
||
<li><p>Bounded</p></li>
|
||
<li><p>Monotonically-increasing</p></li>
|
||
<li><p>Continuous</p></li>
|
||
</ul>
|
||
</section>
|
||
<section id="activation-functions-logistic-and-hyperbolic-ones">
|
||
<h3>Activation functions, Logistic and Hyperbolic ones<a class="headerlink" href="#activation-functions-logistic-and-hyperbolic-ones" title="Link to this heading">#</a></h3>
|
||
<p>The second requirement excludes all linear functions. Furthermore, in
|
||
a MLP with only linear activation functions, each layer simply
|
||
performs a linear transformation of its inputs.</p>
|
||
<p>Regardless of the number of layers, the output of the NN will be
|
||
nothing but a linear function of the inputs. Thus we need to introduce
|
||
some kind of non-linearity to the NN to be able to fit non-linear
|
||
functions Typical examples are the logistic <em>Sigmoid</em></p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
f(x) = \frac{1}{1 + e^{-x}},
|
||
\]</div>
|
||
<p>and the <em>hyperbolic tangent</em> function</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
f(x) = \tanh(x)
|
||
\]</div>
|
||
</section>
|
||
<section id="relevance">
|
||
<h3>Relevance<a class="headerlink" href="#relevance" title="Link to this heading">#</a></h3>
|
||
<p>The <em>sigmoid</em> function are more biologically plausible because the
|
||
output of inactive neurons are zero. Such activation function are
|
||
called <em>one-sided</em>. However, it has been shown that the hyperbolic
|
||
tangent performs better than the sigmoid for training MLPs. has
|
||
become the most popular for <em>deep neural networks</em></p>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="sd">"""The sigmoid function (or the logistic curve) is a </span>
|
||
<span class="sd">function that takes any real number, z, and outputs a number (0,1).</span>
|
||
<span class="sd">It is useful in neural networks for assigning weights on a relative scale.</span>
|
||
<span class="sd">The value z is the weighted sum of parameters involved in the learning algorithm."""</span>
|
||
|
||
<span class="kn">import</span> <span class="nn">numpy</span>
|
||
<span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>
|
||
<span class="kn">import</span> <span class="nn">math</span> <span class="k">as</span> <span class="nn">mt</span>
|
||
|
||
<span class="n">z</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="o">-</span><span class="mi">5</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mf">.1</span><span class="p">)</span>
|
||
<span class="n">sigma_fn</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">vectorize</span><span class="p">(</span><span class="k">lambda</span> <span class="n">z</span><span class="p">:</span> <span class="mi">1</span><span class="o">/</span><span class="p">(</span><span class="mi">1</span><span class="o">+</span><span class="n">numpy</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">z</span><span class="p">)))</span>
|
||
<span class="n">sigma</span> <span class="o">=</span> <span class="n">sigma_fn</span><span class="p">(</span><span class="n">z</span><span class="p">)</span>
|
||
|
||
<span class="n">fig</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">()</span>
|
||
<span class="n">ax</span> <span class="o">=</span> <span class="n">fig</span><span class="o">.</span><span class="n">add_subplot</span><span class="p">(</span><span class="mi">111</span><span class="p">)</span>
|
||
<span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">z</span><span class="p">,</span> <span class="n">sigma</span><span class="p">)</span>
|
||
<span class="n">ax</span><span class="o">.</span><span class="n">set_ylim</span><span class="p">([</span><span class="o">-</span><span class="mf">0.1</span><span class="p">,</span> <span class="mf">1.1</span><span class="p">])</span>
|
||
<span class="n">ax</span><span class="o">.</span><span class="n">set_xlim</span><span class="p">([</span><span class="o">-</span><span class="mi">5</span><span class="p">,</span><span class="mi">5</span><span class="p">])</span>
|
||
<span class="n">ax</span><span class="o">.</span><span class="n">grid</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>
|
||
<span class="n">ax</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s1">'z'</span><span class="p">)</span>
|
||
<span class="n">ax</span><span class="o">.</span><span class="n">set_title</span><span class="p">(</span><span class="s1">'sigmoid function'</span><span class="p">)</span>
|
||
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
|
||
|
||
<span class="sd">"""Step Function"""</span>
|
||
<span class="n">z</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="o">-</span><span class="mi">5</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mf">.02</span><span class="p">)</span>
|
||
<span class="n">step_fn</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">vectorize</span><span class="p">(</span><span class="k">lambda</span> <span class="n">z</span><span class="p">:</span> <span class="mf">1.0</span> <span class="k">if</span> <span class="n">z</span> <span class="o">>=</span> <span class="mf">0.0</span> <span class="k">else</span> <span class="mf">0.0</span><span class="p">)</span>
|
||
<span class="n">step</span> <span class="o">=</span> <span class="n">step_fn</span><span class="p">(</span><span class="n">z</span><span class="p">)</span>
|
||
|
||
<span class="n">fig</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">()</span>
|
||
<span class="n">ax</span> <span class="o">=</span> <span class="n">fig</span><span class="o">.</span><span class="n">add_subplot</span><span class="p">(</span><span class="mi">111</span><span class="p">)</span>
|
||
<span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">z</span><span class="p">,</span> <span class="n">step</span><span class="p">)</span>
|
||
<span class="n">ax</span><span class="o">.</span><span class="n">set_ylim</span><span class="p">([</span><span class="o">-</span><span class="mf">0.5</span><span class="p">,</span> <span class="mf">1.5</span><span class="p">])</span>
|
||
<span class="n">ax</span><span class="o">.</span><span class="n">set_xlim</span><span class="p">([</span><span class="o">-</span><span class="mi">5</span><span class="p">,</span><span class="mi">5</span><span class="p">])</span>
|
||
<span class="n">ax</span><span class="o">.</span><span class="n">grid</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>
|
||
<span class="n">ax</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s1">'z'</span><span class="p">)</span>
|
||
<span class="n">ax</span><span class="o">.</span><span class="n">set_title</span><span class="p">(</span><span class="s1">'step function'</span><span class="p">)</span>
|
||
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
|
||
|
||
<span class="sd">"""Sine Function"""</span>
|
||
<span class="n">z</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="o">-</span><span class="mi">2</span><span class="o">*</span><span class="n">mt</span><span class="o">.</span><span class="n">pi</span><span class="p">,</span> <span class="mi">2</span><span class="o">*</span><span class="n">mt</span><span class="o">.</span><span class="n">pi</span><span class="p">,</span> <span class="mf">0.1</span><span class="p">)</span>
|
||
<span class="n">t</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">sin</span><span class="p">(</span><span class="n">z</span><span class="p">)</span>
|
||
|
||
<span class="n">fig</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">()</span>
|
||
<span class="n">ax</span> <span class="o">=</span> <span class="n">fig</span><span class="o">.</span><span class="n">add_subplot</span><span class="p">(</span><span class="mi">111</span><span class="p">)</span>
|
||
<span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">z</span><span class="p">,</span> <span class="n">t</span><span class="p">)</span>
|
||
<span class="n">ax</span><span class="o">.</span><span class="n">set_ylim</span><span class="p">([</span><span class="o">-</span><span class="mf">1.0</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">])</span>
|
||
<span class="n">ax</span><span class="o">.</span><span class="n">set_xlim</span><span class="p">([</span><span class="o">-</span><span class="mi">2</span><span class="o">*</span><span class="n">mt</span><span class="o">.</span><span class="n">pi</span><span class="p">,</span><span class="mi">2</span><span class="o">*</span><span class="n">mt</span><span class="o">.</span><span class="n">pi</span><span class="p">])</span>
|
||
<span class="n">ax</span><span class="o">.</span><span class="n">grid</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>
|
||
<span class="n">ax</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s1">'z'</span><span class="p">)</span>
|
||
<span class="n">ax</span><span class="o">.</span><span class="n">set_title</span><span class="p">(</span><span class="s1">'sine function'</span><span class="p">)</span>
|
||
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
|
||
|
||
<span class="sd">"""Plots a graph of the squashing function used by a rectified linear</span>
|
||
<span class="sd">unit"""</span>
|
||
<span class="n">z</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mf">.1</span><span class="p">)</span>
|
||
<span class="n">zero</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">z</span><span class="p">))</span>
|
||
<span class="n">y</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">max</span><span class="p">([</span><span class="n">zero</span><span class="p">,</span> <span class="n">z</span><span class="p">],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
|
||
|
||
<span class="n">fig</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">()</span>
|
||
<span class="n">ax</span> <span class="o">=</span> <span class="n">fig</span><span class="o">.</span><span class="n">add_subplot</span><span class="p">(</span><span class="mi">111</span><span class="p">)</span>
|
||
<span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">z</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
|
||
<span class="n">ax</span><span class="o">.</span><span class="n">set_ylim</span><span class="p">([</span><span class="o">-</span><span class="mf">2.0</span><span class="p">,</span> <span class="mf">2.0</span><span class="p">])</span>
|
||
<span class="n">ax</span><span class="o">.</span><span class="n">set_xlim</span><span class="p">([</span><span class="o">-</span><span class="mf">2.0</span><span class="p">,</span> <span class="mf">2.0</span><span class="p">])</span>
|
||
<span class="n">ax</span><span class="o">.</span><span class="n">grid</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>
|
||
<span class="n">ax</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s1">'z'</span><span class="p">)</span>
|
||
<span class="n">ax</span><span class="o">.</span><span class="n">set_title</span><span class="p">(</span><span class="s1">'Rectified linear unit'</span><span class="p">)</span>
|
||
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="cell_output docutils container">
|
||
<img alt="_images/f595cb240e803bed7c09675e2c828acbc8a18aab73265f5a086bb9936d9ff30d.png" src="_images/f595cb240e803bed7c09675e2c828acbc8a18aab73265f5a086bb9936d9ff30d.png" />
|
||
<img alt="_images/26f4dc8f430dfce96e23f825488d4442c0f3e5d1d2561dba2bb589bbaabae1a5.png" src="_images/26f4dc8f430dfce96e23f825488d4442c0f3e5d1d2561dba2bb589bbaabae1a5.png" />
|
||
<img alt="_images/aefa13d1ceb5f6f14b5790760c064f2ae9c41cd2a1b1f7bc57ba470344031e03.png" src="_images/aefa13d1ceb5f6f14b5790760c064f2ae9c41cd2a1b1f7bc57ba470344031e03.png" />
|
||
<img alt="_images/08cf835c237586fe502420f1f28b4b705eb19d7b5d2ac8f7e08c087b1b39111b.png" src="_images/08cf835c237586fe502420f1f28b4b705eb19d7b5d2ac8f7e08c087b1b39111b.png" />
|
||
</div>
|
||
</div>
|
||
</section>
|
||
</section>
|
||
<section id="fine-tuning-neural-network-hyperparameters">
|
||
<h2>Fine-tuning neural network hyperparameters<a class="headerlink" href="#fine-tuning-neural-network-hyperparameters" title="Link to this heading">#</a></h2>
|
||
<p>The flexibility of neural networks is also one of their main
|
||
drawbacks: there are many hyperparameters to tweak. Not only can you
|
||
use any imaginable network topology (how neurons/nodes are
|
||
interconnected), but even in a simple FFNN you can change the number
|
||
of layers, the number of neurons per layer, the type of activation
|
||
function to use in each layer, the weight initialization logic, the
|
||
stochastic gradient optmized and much more. How do you know what
|
||
combination of hyperparameters is the best for your task?</p>
|
||
<ul class="simple">
|
||
<li><p>You can use grid search with cross-validation to find the right hyperparameters.</p></li>
|
||
</ul>
|
||
<p>However,since there are many hyperparameters to tune, and since
|
||
training a neural network on a large dataset takes a lot of time, you
|
||
will only be able to explore a tiny part of the hyperparameter space.</p>
|
||
<ul class="simple">
|
||
<li><p>You can use randomized search.</p></li>
|
||
<li><p>Or use tools like <a class="reference external" href="http://oscar.calldesk.ai/">Oscar</a>, which implements more complex algorithms to help you find a good set of hyperparameters quickly.</p></li>
|
||
</ul>
|
||
</section>
|
||
<section id="hidden-layers">
|
||
<h2>Hidden layers<a class="headerlink" href="#hidden-layers" title="Link to this heading">#</a></h2>
|
||
<p>For many problems you can start with just one or two hidden layers and
|
||
it will work just fine. For the MNIST data set you ca easily get a
|
||
high accuracy using just one hidden layer with a few hundred neurons.
|
||
You can reach for this data set above 98% accuracy using two hidden
|
||
layers with the same total amount of neurons, in roughly the same
|
||
amount of training time.</p>
|
||
<p>For more complex problems, you can gradually ramp up the number of
|
||
hidden layers, until you start overfitting the training set. Very
|
||
complex tasks, such as large image classification or speech
|
||
recognition, typically require networks with dozens of layers and they
|
||
need a huge amount of training data. However, you will rarely have to
|
||
train such networks from scratch: it is much more common to reuse
|
||
parts of a pretrained state-of-the-art network that performs a similar
|
||
task.</p>
|
||
</section>
|
||
<section id="vanishing-gradients">
|
||
<h2>Vanishing gradients<a class="headerlink" href="#vanishing-gradients" title="Link to this heading">#</a></h2>
|
||
<p>The Back propagation algorithm we derived above works by going from
|
||
the output layer to the input layer, propagating the error gradient on
|
||
the way. Once the algorithm has computed the gradient of the cost
|
||
function with regards to each parameter in the network, it uses these
|
||
gradients to update each parameter with a Gradient Descent (GD) step.</p>
|
||
<p>Unfortunately for us, the gradients often get smaller and smaller as
|
||
the algorithm progresses down to the first hidden layers. As a result,
|
||
the GD update leaves the lower layer connection weights virtually
|
||
unchanged, and training never converges to a good solution. This is
|
||
known in the literature as <strong>the vanishing gradients problem</strong>.</p>
|
||
</section>
|
||
<section id="exploding-gradients">
|
||
<h2>Exploding gradients<a class="headerlink" href="#exploding-gradients" title="Link to this heading">#</a></h2>
|
||
<p>In other cases, the opposite can happen, namely the the gradients can
|
||
grow bigger and bigger. The result is that many of the layers get
|
||
large updates of the weights the algorithm diverges. This is the
|
||
<strong>exploding gradients problem</strong>, which is mostly encountered in
|
||
recurrent neural networks. More generally, deep neural networks suffer
|
||
from unstable gradients, different layers may learn at widely
|
||
different speeds</p>
|
||
</section>
|
||
<section id="is-the-logistic-activation-function-sigmoid-our-choice">
|
||
<h2>Is the Logistic activation function (Sigmoid) our choice?<a class="headerlink" href="#is-the-logistic-activation-function-sigmoid-our-choice" title="Link to this heading">#</a></h2>
|
||
<p>Although this unfortunate behavior has been empirically observed for
|
||
quite a while (it was one of the reasons why deep neural networks were
|
||
mostly abandoned for a long time), it is only around 2010 that
|
||
significant progress was made in understanding it.</p>
|
||
<p>A paper titled <a class="reference external" href="http://proceedings.mlr.press/v9/glorot10a.html">Understanding the Difficulty of Training Deep
|
||
Feedforward Neural Networks by Xavier Glorot and Yoshua Bengio</a> found that
|
||
the problems with the popular logistic
|
||
sigmoid activation function and the weight initialization technique
|
||
that was most popular at the time, namely random initialization using
|
||
a normal distribution with a mean of 0 and a standard deviation of
|
||
1.</p>
|
||
</section>
|
||
<section id="logistic-function-as-the-root-of-problems">
|
||
<h2>Logistic function as the root of problems<a class="headerlink" href="#logistic-function-as-the-root-of-problems" title="Link to this heading">#</a></h2>
|
||
<p>They showed that with this activation function and this
|
||
initialization scheme, the variance of the outputs of each layer is
|
||
much greater than the variance of its inputs. Going forward in the
|
||
network, the variance keeps increasing after each layer until the
|
||
activation function saturates at the top layers. This is actually made
|
||
worse by the fact that the logistic function has a mean of 0.5, not 0
|
||
(the hyperbolic tangent function has a mean of 0 and behaves slightly
|
||
better than the logistic function in deep networks).</p>
|
||
</section>
|
||
<section id="the-derivative-of-the-logistic-funtion">
|
||
<h2>The derivative of the Logistic funtion<a class="headerlink" href="#the-derivative-of-the-logistic-funtion" title="Link to this heading">#</a></h2>
|
||
<p>Looking at the logistic activation function, when inputs become large
|
||
(negative or positive), the function saturates at 0 or 1, with a
|
||
derivative extremely close to 0. Thus when backpropagation kicks in,
|
||
it has virtually no gradient to propagate back through the network,
|
||
and what little gradient exists keeps getting diluted as
|
||
backpropagation progresses down through the top layers, so there is
|
||
really nothing left for the lower layers.</p>
|
||
<p>In their paper, Glorot and Bengio propose a way to significantly
|
||
alleviate this problem. We need the signal to flow properly in both
|
||
directions: in the forward direction when making predictions, and in
|
||
the reverse direction when backpropagating gradients. We don’t want
|
||
the signal to die out, nor do we want it to explode and saturate. For
|
||
the signal to flow properly, the authors argue that we need the
|
||
variance of the outputs of each layer to be equal to the variance of
|
||
its inputs, and we also need the gradients to have equal variance
|
||
before and after flowing through a layer in the reverse direction.</p>
|
||
</section>
|
||
<section id="insights-from-the-paper-by-glorot-and-bengio">
|
||
<h2>Insights from the paper by Glorot and Bengio<a class="headerlink" href="#insights-from-the-paper-by-glorot-and-bengio" title="Link to this heading">#</a></h2>
|
||
<p>One of the insights in the 2010 paper by Glorot and Bengio was that
|
||
the vanishing/exploding gradients problems were in part due to a poor
|
||
choice of activation function. Until then most people had assumed that
|
||
if Nature had chosen to use roughly sigmoid activation functions in
|
||
biological neurons, they must be an excellent choice. But it turns out
|
||
that other activation functions behave much better in deep neural
|
||
networks, in particular the ReLU activation function, mostly because
|
||
it does not saturate for positive values (and also because it is quite
|
||
fast to compute).</p>
|
||
</section>
|
||
<section id="the-relu-function-family">
|
||
<h2>The RELU function family<a class="headerlink" href="#the-relu-function-family" title="Link to this heading">#</a></h2>
|
||
<p>The ReLU activation function suffers from a problem known as the dying
|
||
ReLUs: during training, some neurons effectively die, meaning they
|
||
stop outputting anything other than 0.</p>
|
||
<p>In some cases, you may find that half of your network’s neurons are
|
||
dead, especially if you used a large learning rate. During training,
|
||
if a neuron’s weights get updated such that the weighted sum of the
|
||
neuron’s inputs is negative, it will start outputting 0. When this
|
||
happen, the neuron is unlikely to come back to life since the gradient
|
||
of the ReLU function is 0 when its input is negative.</p>
|
||
</section>
|
||
<section id="elu-function">
|
||
<h2>ELU function<a class="headerlink" href="#elu-function" title="Link to this heading">#</a></h2>
|
||
<p>To solve this problem, nowadays practitioners use a variant of the
|
||
ReLU function, such as the leaky ReLU discussed above or the so-called
|
||
exponential linear unit (ELU) function</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[\begin{split}
|
||
ELU(z) = \left\{\begin{array}{cc} \alpha\left( \exp{(z)}-1\right) & z < 0,\\ z & z \ge 0.\end{array}\right.
|
||
\end{split}\]</div>
|
||
</section>
|
||
<section id="which-activation-function-should-we-use">
|
||
<h2>Which activation function should we use?<a class="headerlink" href="#which-activation-function-should-we-use" title="Link to this heading">#</a></h2>
|
||
<p>In general it seems that the ELU activation function is better than
|
||
the leaky ReLU function (and its variants), which is better than
|
||
ReLU. ReLU performs better than <span class="math notranslate nohighlight">\(\tanh\)</span> which in turn performs better
|
||
than the logistic function.</p>
|
||
<p>If runtime performance is an issue, then you may opt for the leaky
|
||
ReLU function over the ELU function If you don’t want to tweak yet
|
||
another hyperparameter, you may just use the default <span class="math notranslate nohighlight">\(\alpha\)</span> of
|
||
<span class="math notranslate nohighlight">\(0.01\)</span> for the leaky ReLU, and <span class="math notranslate nohighlight">\(1\)</span> for ELU. If you have spare time and
|
||
computing power, you can use cross-validation or bootstrap to evaluate
|
||
other activation functions.</p>
|
||
</section>
|
||
<section id="more-on-activation-functions-output-layers">
|
||
<h2>More on activation functions, output layers<a class="headerlink" href="#more-on-activation-functions-output-layers" title="Link to this heading">#</a></h2>
|
||
<p>In most cases you can use the ReLU activation function in the hidden
|
||
layers (or one of its variants).</p>
|
||
<p>It is a bit faster to compute than other activation functions, and the
|
||
gradient descent optimization does in general not get stuck.</p>
|
||
<p><strong>For the output layer:</strong></p>
|
||
<ul class="simple">
|
||
<li><p>For classification the softmax activation function is generally a good choice for classification tasks (when the classes are mutually exclusive).</p></li>
|
||
<li><p>For regression tasks, you can simply use no activation function at all.</p></li>
|
||
</ul>
|
||
</section>
|
||
<section id="batch-normalization">
|
||
<h2>Batch Normalization<a class="headerlink" href="#batch-normalization" title="Link to this heading">#</a></h2>
|
||
<p>Batch Normalization aims to address the vanishing/exploding gradients
|
||
problems, and more generally the problem that the distribution of each
|
||
layer’s inputs changes during training, as the parameters of the
|
||
previous layers change.</p>
|
||
<p>The technique consists of adding an operation in the model just before
|
||
the activation function of each layer, simply zero-centering and
|
||
normalizing the inputs, then scaling and shifting the result using two
|
||
new parameters per layer (one for scaling, the other for shifting). In
|
||
other words, this operation lets the model learn the optimal scale and
|
||
mean of the inputs for each layer. In order to zero-center and
|
||
normalize the inputs, the algorithm needs to estimate the inputs’ mean
|
||
and standard deviation. It does so by evaluating the mean and standard
|
||
deviation of the inputs over the current mini-batch, from this the
|
||
name batch normalization.</p>
|
||
</section>
|
||
<section id="dropout">
|
||
<h2>Dropout<a class="headerlink" href="#dropout" title="Link to this heading">#</a></h2>
|
||
<p>It is a fairly simple algorithm: at every training step, every neuron
|
||
(including the input neurons but excluding the output neurons) has a
|
||
probability <span class="math notranslate nohighlight">\(p\)</span> of being temporarily dropped out, meaning it will be
|
||
entirely ignored during this training step, but it may be active
|
||
during the next step.</p>
|
||
<p>The hyperparameter <span class="math notranslate nohighlight">\(p\)</span> is called the dropout rate, and it is typically
|
||
set to 50%. After training, the neurons are not dropped anymore. It
|
||
is viewed as one of the most popular regularization techniques.</p>
|
||
</section>
|
||
<section id="gradient-clipping">
|
||
<h2>Gradient Clipping<a class="headerlink" href="#gradient-clipping" title="Link to this heading">#</a></h2>
|
||
<p>A popular technique to lessen the exploding gradients problem is to
|
||
simply clip the gradients during backpropagation so that they never
|
||
exceed some threshold (this is mostly useful for recurrent neural
|
||
networks).</p>
|
||
<p>This technique is called Gradient Clipping.</p>
|
||
<p>In general however, Batch
|
||
Normalization is preferred.</p>
|
||
</section>
|
||
<section id="a-top-down-perspective-on-neural-networks">
|
||
<h2>A top-down perspective on Neural networks<a class="headerlink" href="#a-top-down-perspective-on-neural-networks" title="Link to this heading">#</a></h2>
|
||
<p>The first thing we would like to do is divide the data into two or
|
||
three parts. A training set, a validation or dev (development) set,
|
||
and a test set. The test set is the data on which we want to make
|
||
predictions. The dev set is a subset of the training data we use to
|
||
check how well we are doing out-of-sample, after training the model on
|
||
the training dataset. We use the validation error as a proxy for the
|
||
test error in order to make tweaks to our model. It is crucial that we
|
||
do not use any of the test data to train the algorithm. This is a
|
||
cardinal sin in ML. Then:</p>
|
||
<ol class="arabic simple">
|
||
<li><p>Estimate optimal error rate</p></li>
|
||
<li><p>Minimize underfitting (bias) on training data set.</p></li>
|
||
<li><p>Make sure you are not overfitting.</p></li>
|
||
</ol>
|
||
</section>
|
||
<section id="more-top-down-perspectives">
|
||
<h2>More top-down perspectives<a class="headerlink" href="#more-top-down-perspectives" title="Link to this heading">#</a></h2>
|
||
<p>If the validation and test sets are drawn from the same distributions,
|
||
then a good performance on the validation set should lead to similarly
|
||
good performance on the test set.</p>
|
||
<p>However, sometimes
|
||
the training data and test data differ in subtle ways because, for
|
||
example, they are collected using slightly different methods, or
|
||
because it is cheaper to collect data in one way versus another. In
|
||
this case, there can be a mismatch between the training and test
|
||
data. This can lead to the neural network overfitting these small
|
||
differences between the test and training sets, and a poor performance
|
||
on the test set despite having a good performance on the validation
|
||
set. To rectify this, Andrew Ng suggests making two validation or dev
|
||
sets, one constructed from the training data and one constructed from
|
||
the test data. The difference between the performance of the algorithm
|
||
on these two validation sets quantifies the train-test mismatch. This
|
||
can serve as another important diagnostic when using DNNs for
|
||
supervised learning.</p>
|
||
</section>
|
||
<section id="limitations-of-supervised-learning-with-deep-networks">
|
||
<h2>Limitations of supervised learning with deep networks<a class="headerlink" href="#limitations-of-supervised-learning-with-deep-networks" title="Link to this heading">#</a></h2>
|
||
<p>Like all statistical methods, supervised learning using neural
|
||
networks has important limitations. This is especially important when
|
||
one seeks to apply these methods, especially to physics problems. Like
|
||
all tools, DNNs are not a universal solution. Often, the same or
|
||
better performance on a task can be achieved by using a few
|
||
hand-engineered features (or even a collection of random
|
||
features).</p>
|
||
</section>
|
||
<section id="limitations-of-nns">
|
||
<h2>Limitations of NNs<a class="headerlink" href="#limitations-of-nns" title="Link to this heading">#</a></h2>
|
||
<p>Here we list some of the important limitations of supervised neural network based models.</p>
|
||
<ul class="simple">
|
||
<li><p><strong>Need labeled data</strong>. All supervised learning methods, DNNs for supervised learning require labeled data. Often, labeled data is harder to acquire than unlabeled data (e.g. one must pay for human experts to label images).</p></li>
|
||
<li><p><strong>Supervised neural networks are extremely data intensive.</strong> DNNs are data hungry. They perform best when data is plentiful. This is doubly so for supervised methods where the data must also be labeled. The utility of DNNs is extremely limited if data is hard to acquire or the datasets are small (hundreds to a few thousand samples). In this case, the performance of other methods that utilize hand-engineered features can exceed that of DNNs.</p></li>
|
||
</ul>
|
||
</section>
|
||
<section id="homogeneous-data">
|
||
<h2>Homogeneous data<a class="headerlink" href="#homogeneous-data" title="Link to this heading">#</a></h2>
|
||
<ul class="simple">
|
||
<li><p><strong>Homogeneous data.</strong> Almost all DNNs deal with homogeneous data of one type. It is very hard to design architectures that mix and match data types (i.e. some continuous variables, some discrete variables, some time series). In applications beyond images, video, and language, this is often what is required. In contrast, ensemble models like random forests or gradient-boosted trees have no difficulty handling mixed data types.</p></li>
|
||
</ul>
|
||
</section>
|
||
<section id="more-limitations">
|
||
<h2>More limitations<a class="headerlink" href="#more-limitations" title="Link to this heading">#</a></h2>
|
||
<ul class="simple">
|
||
<li><p><strong>Many problems are not about prediction.</strong> In natural science we are often interested in learning something about the underlying distribution that generates the data. In this case, it is often difficult to cast these ideas in a supervised learning setting. While the problems are related, it is possible to make good predictions with a <em>wrong</em> model. The model might or might not be useful for understanding the underlying science.</p></li>
|
||
</ul>
|
||
<p>Some of these remarks are particular to DNNs, others are shared by all supervised learning methods. This motivates the use of unsupervised methods which in part circumvent these problems.</p>
|
||
</section>
|
||
</section>
|
||
|
||
<script type="text/x-thebe-config">
|
||
{
|
||
requestKernel: true,
|
||
binderOptions: {
|
||
repo: "binder-examples/jupyter-stacks-datascience",
|
||
ref: "master",
|
||
},
|
||
codeMirrorConfig: {
|
||
theme: "abcdef",
|
||
mode: "python"
|
||
},
|
||
kernelOptions: {
|
||
name: "python3",
|
||
path: "./."
|
||
},
|
||
predefinedOutput: true
|
||
}
|
||
</script>
|
||
<script>kernelName = 'python3'</script>
|
||
|
||
</article>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<footer class="prev-next-footer d-print-none">
|
||
|
||
<div class="prev-next-area">
|
||
<a class="left-prev"
|
||
href="exercisesweek41.html"
|
||
title="previous page">
|
||
<i class="fa-solid fa-angle-left"></i>
|
||
<div class="prev-next-info">
|
||
<p class="prev-next-subtitle">previous</p>
|
||
<p class="prev-next-title">Exercises week 41</p>
|
||
</div>
|
||
</a>
|
||
<a class="right-next"
|
||
href="exercisesweek42.html"
|
||
title="next page">
|
||
<div class="prev-next-info">
|
||
<p class="prev-next-subtitle">next</p>
|
||
<p class="prev-next-title">Exercises week 42</p>
|
||
</div>
|
||
<i class="fa-solid fa-angle-right"></i>
|
||
</a>
|
||
</div>
|
||
</footer>
|
||
|
||
</div>
|
||
|
||
|
||
|
||
<dialog id="pst-secondary-sidebar-modal"></dialog>
|
||
<div id="pst-secondary-sidebar" class="bd-sidebar-secondary bd-toc"><div class="sidebar-secondary-items sidebar-secondary__inner">
|
||
|
||
|
||
<div class="sidebar-secondary-item">
|
||
<div class="page-toc tocsection onthispage">
|
||
<i class="fa-solid fa-list"></i> Contents
|
||
</div>
|
||
<nav class="bd-toc-nav page-toc">
|
||
<ul class="visible nav section-nav flex-column">
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#plan-for-week-41-october-7-11">Plan for week 41, October 7-11</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#material-for-the-lecture-on-monday-october-7-2024">Material for the lecture on Monday October 7, 2024</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#material-for-the-active-learning-sessions-on-tuesday-and-wednesday">Material for the active learning sessions on Tuesday and Wednesday</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#lecture-monday-october-7">Lecture Monday October 7</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#introduction-to-neural-networks">Introduction to Neural networks</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#artificial-neurons">Artificial neurons</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#neural-network-types">Neural network types</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#feed-forward-neural-networks">Feed-forward neural networks</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#convolutional-neural-network">Convolutional Neural Network</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#recurrent-neural-networks">Recurrent neural networks</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#other-types-of-networks">Other types of networks</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#multilayer-perceptrons">Multilayer perceptrons</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#why-multilayer-perceptrons">Why multilayer perceptrons?</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#illustration-of-a-single-perceptron-model-and-a-multi-perceptron-model">Illustration of a single perceptron model and a multi-perceptron model</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#examples-of-xor-or-and-and-gates">Examples of XOR, OR and AND gates</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#does-logistic-regression-do-a-better-job">Does Logistic Regression do a better Job?</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#adding-neural-networks">Adding Neural Networks</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#mathematics-of-deep-learning">Mathematics of deep learning</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#reminder-on-books-with-hands-on-material-and-codes">Reminder on books with hands-on material and codes</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#reading-recommendations">Reading recommendations</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#mathematics-of-deep-learning-and-neural-networks">Mathematics of deep learning and neural networks</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#basics-of-an-nn">Basics of an NN</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#overarching-view-of-a-neural-network">Overarching view of a neural network</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#the-optimization-problem">The optimization problem</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#parameters-of-neural-networks">Parameters of neural networks</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#other-ingredients-of-a-neural-network">Other ingredients of a neural network</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#other-parameters">Other parameters</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#universal-approximation-theorem">Universal approximation theorem</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#some-parallels-from-real-analysis">Some parallels from real analysis</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#the-approximation-theorem-in-words">The approximation theorem in words</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#more-on-the-general-approximation-theorem">More on the general approximation theorem</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#class-of-functions-we-can-approximate">Class of functions we can approximate</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#setting-up-the-equations-for-a-neural-network">Setting up the equations for a neural network</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#layout-of-a-neural-network-with-three-hidden-layers">Layout of a neural network with three hidden layers</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#definitions">Definitions</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#inputs-to-the-activation-function">Inputs to the activation function</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#derivatives-and-the-chain-rule">Derivatives and the chain rule</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#derivative-of-the-cost-function">Derivative of the cost function</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#simpler-examples-first-and-automatic-differentiation">Simpler examples first, and automatic differentiation</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#reminder-on-the-chain-rule-and-gradients">Reminder on the chain rule and gradients</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#multivariable-functions">Multivariable functions</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#automatic-differentiation-through-examples">Automatic differentiation through examples</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#simple-example">Simple example</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#smarter-way-of-evaluating-the-above-function">Smarter way of evaluating the above function</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#reducing-the-number-of-operations">Reducing the number of operations</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#chain-rule-forward-and-reverse-modes">Chain rule, forward and reverse modes</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#forward-and-reverse-modes">Forward and reverse modes</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#more-complicated-function">More complicated function</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#counting-the-number-of-floating-point-operations">Counting the number of floating point operations</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#defining-intermediate-operations">Defining intermediate operations</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#new-expression-for-the-derivative">New expression for the derivative</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#final-derivatives">Final derivatives</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#in-general-not-this-simple">In general not this simple</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#automatic-differentiation">Automatic differentiation</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#chain-rule">Chain rule</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#first-network-example-simple-percepetron-with-one-input">First network example, simple percepetron with one input</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#layout-of-a-simple-neural-network-with-no-hidden-layer">Layout of a simple neural network with no hidden layer</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#optimizing-the-parameters">Optimizing the parameters</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#adding-a-hidden-layer">Adding a hidden layer</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#layout-of-a-simple-neural-network-with-one-hidden-layer">Layout of a simple neural network with one hidden layer</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#the-derivatives">The derivatives</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#important-observations">Important observations</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#the-training">The training</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#code-example">Code example</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#exercise-1-including-more-data">Exercise 1: Including more data</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#simple-neural-network-and-the-back-propagation-equations">Simple neural network and the back propagation equations</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#layout-of-a-simple-neural-network-with-two-input-nodes-one-hidden-layer-and-one-output-node">Layout of a simple neural network with two input nodes, one hidden layer and one output node</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#the-ouput-layer">The ouput layer</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#compact-expressions">Compact expressions</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#output-layer">Output layer</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#explicit-derivatives">Explicit derivatives</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#derivatives-of-the-hidden-layer">Derivatives of the hidden layer</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#final-expression">Final expression</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#completing-the-list">Completing the list</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#final-expressions-for-the-biases-of-the-hidden-layer">Final expressions for the biases of the hidden layer</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#gradient-expressions">Gradient expressions</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#exercise-2-extended-program">Exercise 2: Extended program</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#getting-serious-the-back-propagation-equations-for-a-neural-network">Getting serious, the back propagation equations for a neural network</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#analyzing-the-last-results">Analyzing the last results</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#more-considerations">More considerations</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#derivatives-in-terms-of-z-j-l">Derivatives in terms of <span class="math notranslate nohighlight">\(z_j^L\)</span></a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#bringing-it-together">Bringing it together</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#final-back-propagating-equation">Final back propagating equation</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#using-the-chain-rule-and-summing-over-all-k-entries">Using the chain rule and summing over all <span class="math notranslate nohighlight">\(k\)</span> entries</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#setting-up-the-back-propagation-algorithm">Setting up the back propagation algorithm</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#setting-up-the-back-propagation-algorithm-part-2">Setting up the back propagation algorithm, part 2</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#setting-up-the-back-propagation-algorithm-part-3">Setting up the Back propagation algorithm, part 3</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#updating-the-gradients">Updating the gradients</a><ul class="nav section-nav flex-column">
|
||
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#activation-functions">Activation functions</a></li>
|
||
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#activation-functions-logistic-and-hyperbolic-ones">Activation functions, Logistic and Hyperbolic ones</a></li>
|
||
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#relevance">Relevance</a></li>
|
||
</ul>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#fine-tuning-neural-network-hyperparameters">Fine-tuning neural network hyperparameters</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#hidden-layers">Hidden layers</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#vanishing-gradients">Vanishing gradients</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#exploding-gradients">Exploding gradients</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#is-the-logistic-activation-function-sigmoid-our-choice">Is the Logistic activation function (Sigmoid) our choice?</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#logistic-function-as-the-root-of-problems">Logistic function as the root of problems</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#the-derivative-of-the-logistic-funtion">The derivative of the Logistic funtion</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#insights-from-the-paper-by-glorot-and-bengio">Insights from the paper by Glorot and Bengio</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#the-relu-function-family">The RELU function family</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#elu-function">ELU function</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#which-activation-function-should-we-use">Which activation function should we use?</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#more-on-activation-functions-output-layers">More on activation functions, output layers</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#batch-normalization">Batch Normalization</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#dropout">Dropout</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#gradient-clipping">Gradient Clipping</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#a-top-down-perspective-on-neural-networks">A top-down perspective on Neural networks</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#more-top-down-perspectives">More top-down perspectives</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#limitations-of-supervised-learning-with-deep-networks">Limitations of supervised learning with deep networks</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#limitations-of-nns">Limitations of NNs</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#homogeneous-data">Homogeneous data</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#more-limitations">More limitations</a></li>
|
||
</ul>
|
||
</nav></div>
|
||
|
||
</div></div>
|
||
|
||
|
||
</div>
|
||
<footer class="bd-footer-content">
|
||
|
||
<div class="bd-footer-content__inner container">
|
||
|
||
<div class="footer-item">
|
||
|
||
<p class="component-author">
|
||
By Morten Hjorth-Jensen
|
||
</p>
|
||
|
||
</div>
|
||
|
||
<div class="footer-item">
|
||
|
||
|
||
<p class="copyright">
|
||
|
||
© Copyright 2023.
|
||
<br/>
|
||
|
||
</p>
|
||
|
||
</div>
|
||
|
||
<div class="footer-item">
|
||
|
||
</div>
|
||
|
||
<div class="footer-item">
|
||
|
||
</div>
|
||
|
||
</div>
|
||
</footer>
|
||
|
||
|
||
</main>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- Scripts loaded after <body> so the DOM is not blocked -->
|
||
<script defer src="_static/scripts/bootstrap.js?digest=26a4bc78f4c0ddb94549"></script>
|
||
<script defer src="_static/scripts/pydata-sphinx-theme.js?digest=26a4bc78f4c0ddb94549"></script>
|
||
|
||
<footer class="bd-footer">
|
||
</footer>
|
||
</body>
|
||
</html> |