2076 lines
130 KiB
HTML
2076 lines
130 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>
|
||
|
||
<!-- Loaded before other Sphinx assets -->
|
||
<link href="_static/styles/theme.css?digest=dfe6caa3a7d634c4db9b" rel="stylesheet" />
|
||
<link href="_static/styles/bootstrap.css?digest=dfe6caa3a7d634c4db9b" rel="stylesheet" />
|
||
<link href="_static/styles/pydata-sphinx-theme.css?digest=dfe6caa3a7d634c4db9b" rel="stylesheet" />
|
||
|
||
|
||
<link href="_static/vendor/fontawesome/6.5.2/css/all.min.css?digest=dfe6caa3a7d634c4db9b" rel="stylesheet" />
|
||
<link rel="preload" as="font" type="font/woff2" crossorigin href="_static/vendor/fontawesome/6.5.2/webfonts/fa-solid-900.woff2" />
|
||
<link rel="preload" as="font" type="font/woff2" crossorigin href="_static/vendor/fontawesome/6.5.2/webfonts/fa-brands-400.woff2" />
|
||
<link rel="preload" as="font" type="font/woff2" crossorigin href="_static/vendor/fontawesome/6.5.2/webfonts/fa-regular-400.woff2" />
|
||
|
||
<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=eba8b062" />
|
||
<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.8ecb98da25f57f5357bf6f572d296f466b2cfe2517ffebfabe82451661e28f02.css?v=6644e6bb" />
|
||
<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" />
|
||
|
||
<!-- Pre-loaded scripts that we'll load fully later -->
|
||
<link rel="preload" as="script" href="_static/scripts/bootstrap.js?digest=dfe6caa3a7d634c4db9b" />
|
||
<link rel="preload" as="script" href="_static/scripts/pydata-sphinx-theme.js?digest=dfe6caa3a7d634c4db9b" />
|
||
<script src="_static/vendor/fontawesome/6.5.2/js/all.min.js?digest=dfe6caa3a7d634c4db9b"></script>
|
||
|
||
<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 41" href="exercisesweek41.html" />
|
||
<link rel="prev" title="Week 40: Gradient descent methods (continued) and start Neural networks" href="week40.html" />
|
||
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
||
<meta name="docsearch:language" content="en"/>
|
||
</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>
|
||
|
||
|
||
<input type="checkbox"
|
||
class="sidebar-toggle"
|
||
id="pst-primary-sidebar-checkbox"/>
|
||
<label class="overlay overlay-primary" for="pst-primary-sidebar-checkbox"></label>
|
||
|
||
<input type="checkbox"
|
||
class="sidebar-toggle"
|
||
id="pst-secondary-sidebar-checkbox"/>
|
||
<label class="overlay overlay-secondary" for="pst-secondary-sidebar-checkbox"></label>
|
||
|
||
<div class="search-button__wrapper">
|
||
<div class="search-button__overlay"></div>
|
||
<div class="search-button__search-container">
|
||
<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"
|
||
id="search-input"
|
||
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></div>
|
||
</div>
|
||
|
||
<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">
|
||
|
||
|
||
|
||
<div 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"/>
|
||
<script>document.write(`<img src="_static/logo.png" class="logo__image only-dark" alt="Applied Data Analysis and Machine Learning - Home"/>`);</script>
|
||
|
||
|
||
</a></div>
|
||
<div class="sidebar-primary-item">
|
||
|
||
<script>
|
||
document.write(`
|
||
<button class="btn search-button-field search-button__button" 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>
|
||
`);
|
||
</script></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">Course setting</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 Gradient descent</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: Gradient descent 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: Statistical analysis, bias-variance tradeoff and resampling methods</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: Resampling methods and logistic regression</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 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="exercisesweek41.html">Exercises week 41</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="exercisesweek42.html">Exercises week 42</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>
|
||
|
||
</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 6 (midnight), 2025</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="project2.html">Project 2 on Machine Learning, deadline November 10 (Midnight)</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-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>
|
||
|
||
|
||
|
||
<script>
|
||
document.write(`
|
||
<button class="btn btn-sm nav-link pst-navbar-icon theme-switch-button" title="light/dark" aria-label="light/dark" data-bs-placement="bottom" data-bs-toggle="tooltip">
|
||
<i class="theme-switch fa-solid fa-sun fa-lg" data-mode="light"></i>
|
||
<i class="theme-switch fa-solid fa-moon fa-lg" data-mode="dark"></i>
|
||
<i class="theme-switch fa-solid fa-circle-half-stroke fa-lg" data-mode="auto"></i>
|
||
</button>
|
||
`);
|
||
</script>
|
||
|
||
|
||
<script>
|
||
document.write(`
|
||
<button class="btn btn-sm pst-navbar-icon search-button search-button__button" title="Search" aria-label="Search" data-bs-placement="bottom" data-bs-toggle="tooltip">
|
||
<i class="fa-solid fa-magnifying-glass fa-lg"></i>
|
||
</button>
|
||
`);
|
||
</script>
|
||
<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-6-10">Plan for week 41, October 6-10</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#material-for-the-lecture-on-monday-october-6-2025">Material for the lecture on Monday October 6, 2025</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#readings-and-videos">Readings and Videos:</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="#lab-sessions-on-tuesday-and-wednesday">Lab 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-6">Lecture Monday October 6</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="#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></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, Norway</p>
|
||
<p>Date: <strong>Week 41</strong></p>
|
||
<section id="plan-for-week-41-october-6-10">
|
||
<h2>Plan for week 41, October 6-10<a class="headerlink" href="#plan-for-week-41-october-6-10" title="Link to this heading">#</a></h2>
|
||
</section>
|
||
<section id="material-for-the-lecture-on-monday-october-6-2025">
|
||
<h2>Material for the lecture on Monday October 6, 2025<a class="headerlink" href="#material-for-the-lecture-on-monday-october-6-2025" 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, getting started</p></li>
|
||
</ol>
|
||
<!-- * Video of lecture notes at URL:"" -->
|
||
<!-- * Whiteboard notes at <https://github.com/CompPhysics/MachineLearning/blob/master/doc/HandWrittenNotes/2025/FYSSTKweek41.pdf> --></section>
|
||
<section id="readings-and-videos">
|
||
<h2>Readings and Videos:<a class="headerlink" href="#readings-and-videos" title="Link to this heading">#</a></h2>
|
||
<ol class="arabic simple">
|
||
<li><p>These lecture notes</p></li>
|
||
<li><p>For neural networks we recommend Goodfellow et al chapters 6 and 7.</p></li>
|
||
<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>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></li>
|
||
<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>
|
||
<li><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></li>
|
||
</ol>
|
||
</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>
|
||
<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>
|
||
</section>
|
||
<section id="lab-sessions-on-tuesday-and-wednesday">
|
||
<h2>Lab sessions on Tuesday and Wednesday<a class="headerlink" href="#lab-sessions-on-tuesday-and-wednesday" title="Link to this heading">#</a></h2>
|
||
<p>Aim: Getting started with coding neural network. The exercises this
|
||
week aim at setting up the feed-forward part of a neural network.</p>
|
||
</section>
|
||
<section id="lecture-monday-october-6">
|
||
<h2>Lecture Monday October 6<a class="headerlink" href="#lecture-monday-october-6" 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="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>. We will discuss these topics next week (week 42).</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>.
|
||
See also the video at <a class="reference external" href="https://www.youtube.com/watch?v=wG_nF1awSSY">https://www.youtube.com/watch?v=wG_nF1awSSY</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-none notranslate"><div class="highlight"><pre><span></span>from __future__ import division
|
||
from sympy import *
|
||
x = symbols('x')
|
||
expr = exp(x*x)
|
||
simplify(expr)
|
||
derivative = diff(expr,x)
|
||
print(python(expr))
|
||
print(python(derivative))
|
||
</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-none notranslate"><div class="highlight"><pre><span></span>from __future__ import division
|
||
from sympy import *
|
||
x = symbols('x')
|
||
expr = sqrt(x*x+exp(x*x))
|
||
simplify(expr)
|
||
derivative = diff(expr,x)
|
||
print(python(expr))
|
||
print(python(derivative))
|
||
</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-none notranslate"><div class="highlight"><pre><span></span>import numpy as np
|
||
# We use the Sigmoid function as activation function
|
||
def sigmoid(z):
|
||
return 1.0/(1.0+np.exp(-z))
|
||
|
||
def forwardpropagation(x):
|
||
# weighted sum of inputs to the hidden layer
|
||
z_1 = np.matmul(x, w_1) + b_1
|
||
# activation in the hidden layer
|
||
a_1 = sigmoid(z_1)
|
||
# weighted sum of inputs to the output layer
|
||
z_2 = np.matmul(a_1, w_2) + b_2
|
||
a_2 = z_2
|
||
return a_1, a_2
|
||
|
||
def backpropagation(x, y):
|
||
a_1, a_2 = forwardpropagation(x)
|
||
# parameter delta for the output layer, note that a_2=z_2 and its derivative wrt z_2 is just 1
|
||
delta_2 = a_2 - y
|
||
print(0.5*((a_2-y)**2))
|
||
# delta for the hidden layer
|
||
delta_1 = np.matmul(delta_2, w_2.T) * a_1 * (1 - a_1)
|
||
# gradients for the output layer
|
||
output_weights_gradient = np.matmul(a_1.T, delta_2)
|
||
output_bias_gradient = np.sum(delta_2, axis=0)
|
||
# gradient for the hidden layer
|
||
hidden_weights_gradient = np.matmul(x.T, delta_1)
|
||
hidden_bias_gradient = np.sum(delta_1, axis=0)
|
||
return output_weights_gradient, output_bias_gradient, hidden_weights_gradient, hidden_bias_gradient
|
||
|
||
|
||
# ensure the same random numbers appear every time
|
||
np.random.seed(0)
|
||
# Input variable
|
||
x = np.array([4.0],dtype=np.float64)
|
||
# Target values
|
||
y = 2*x+1.0
|
||
|
||
# Defining the neural network, only scalars here
|
||
n_inputs = x.shape
|
||
n_features = 1
|
||
n_hidden_neurons = 1
|
||
n_outputs = 1
|
||
|
||
# Initialize the network
|
||
# weights and bias in the hidden layer
|
||
w_1 = np.random.randn(n_features, n_hidden_neurons)
|
||
b_1 = np.zeros(n_hidden_neurons) + 0.01
|
||
|
||
# weights and bias in the output layer
|
||
w_2 = np.random.randn(n_hidden_neurons, n_outputs)
|
||
b_2 = np.zeros(n_outputs) + 0.01
|
||
|
||
eta = 0.1
|
||
for i in range(50):
|
||
# calculate gradients
|
||
derivW2, derivB2, derivW1, derivB1 = backpropagation(x, y)
|
||
# update weights and biases
|
||
w_2 -= eta * derivW2
|
||
b_2 -= eta * derivB2
|
||
w_1 -= eta * derivW1
|
||
b_1 -= eta * derivB1
|
||
</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>
|
||
</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="week40.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">Week 40: Gradient descent methods (continued) and start Neural networks</p>
|
||
</div>
|
||
</a>
|
||
<a class="right-next"
|
||
href="exercisesweek41.html"
|
||
title="next page">
|
||
<div class="prev-next-info">
|
||
<p class="prev-next-subtitle">next</p>
|
||
<p class="prev-next-title">Exercises week 41</p>
|
||
</div>
|
||
<i class="fa-solid fa-angle-right"></i>
|
||
</a>
|
||
</div>
|
||
</footer>
|
||
|
||
</div>
|
||
|
||
|
||
|
||
<div 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-6-10">Plan for week 41, October 6-10</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#material-for-the-lecture-on-monday-october-6-2025">Material for the lecture on Monday October 6, 2025</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#readings-and-videos">Readings and Videos:</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="#lab-sessions-on-tuesday-and-wednesday">Lab 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-6">Lecture Monday October 6</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="#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></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 src="_static/scripts/bootstrap.js?digest=dfe6caa3a7d634c4db9b"></script>
|
||
<script src="_static/scripts/pydata-sphinx-theme.js?digest=dfe6caa3a7d634c4db9b"></script>
|
||
|
||
<footer class="bd-footer">
|
||
</footer>
|
||
</body>
|
||
</html> |