2098 lines
114 KiB
HTML
2098 lines
114 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>14. Building a Feed Forward Neural Network — 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 = 'chapter10';</script>
|
||
<link rel="index" title="Index" href="genindex.html" />
|
||
<link rel="search" title="Search" href="search.html" />
|
||
<link rel="next" title="15. Solving Differential Equations with Deep Learning" href="chapter11.html" />
|
||
<link rel="prev" title="13. Neural networks" href="chapter9.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="current nav bd-sidenav">
|
||
<li class="toctree-l1"><a class="reference internal" href="chapter9.html">13. Neural networks</a></li>
|
||
<li class="toctree-l1 current active"><a class="current reference internal" href="#">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="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>
|
||
</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>
|
||
</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/chapter10.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>Building a Feed Forward Neural Network</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="#defining-the-cost-function">14.1. Defining the cost function</a><ul class="nav section-nav flex-column">
|
||
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#example-binary-classification-problem">14.1.1. Example: binary classification problem</a></li>
|
||
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#the-softmax-function">14.1.2. The Softmax function</a></li>
|
||
</ul>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#developing-a-code-for-doing-neural-networks-with-back-propagation">14.2. Developing a code for doing neural networks with back propagation</a><ul class="nav section-nav flex-column">
|
||
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#collect-and-pre-process-data">14.2.1. Collect and pre-process data</a></li>
|
||
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#train-and-test-datasets">14.2.2. Train and test datasets</a></li>
|
||
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#define-model-and-architecture">14.2.3. Define model and architecture</a></li>
|
||
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#layers">14.2.4. Layers</a></li>
|
||
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#feed-forward-pass">14.2.5. Feed-forward pass</a></li>
|
||
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#choose-cost-function-and-optimizer">14.2.6. Choose cost function and optimizer</a></li>
|
||
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#optimizing-the-cost-function">14.2.7. Optimizing the cost function</a></li>
|
||
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#regularization">14.2.8. Regularization</a></li>
|
||
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#matrix-multiplication">14.2.9. Matrix multiplication</a></li>
|
||
</ul>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#improving-performance">14.3. Improving performance</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#evaluate-model-performance-on-test-data">14.4. Evaluate model performance on test data</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#adjust-hyperparameters">14.5. Adjust hyperparameters</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#visualization">14.6. Visualization</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#scikit-learn-implementation">14.7. scikit-learn implementation</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#id1">14.8. Visualization</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#building-neural-networks-in-tensorflow-and-keras">14.9. Building neural networks in Tensorflow and Keras</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#the-breast-cancer-data-now-with-keras">14.10. The Breast Cancer Data, now with Keras</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#fine-tuning-neural-network-hyperparameters">14.11. Fine-tuning neural network hyperparameters</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#which-activation-function-should-i-use">14.12. Which activation function should I use?</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#the-relu-function-family">14.13. The RELU function family</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#batch-normalization">14.14. Batch Normalization</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#dropout">14.15. Dropout</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#gradient-clipping">14.16. Gradient Clipping</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#a-top-down-perspective-on-neural-networks">14.17. A top-down perspective on Neural networks</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#limitations-of-supervised-learning-with-deep-networks">14.18. Limitations of supervised learning with deep networks</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 chapter10.do.txt --no_mako --><section class="tex2jax_ignore mathjax_ignore" id="building-a-feed-forward-neural-network">
|
||
<h1><span class="section-number">14. </span>Building a Feed Forward Neural Network<a class="headerlink" href="#building-a-feed-forward-neural-network" title="Link to this heading">#</a></h1>
|
||
<p>We are now gong to develop an example based on the MNIST data
|
||
base. This is a classification problem and we need to use our
|
||
cross-entropy function we discussed in connection with logistic
|
||
regression. The cross-entropy defines our cost function for the
|
||
classificaton problems with neural networks.</p>
|
||
<p>In binary classification with two classes <span class="math notranslate nohighlight">\((0, 1)\)</span> we define the
|
||
logistic/sigmoid function as the probability that a particular input
|
||
is in class <span class="math notranslate nohighlight">\(0\)</span> or <span class="math notranslate nohighlight">\(1\)</span>. This is possible because the logistic
|
||
function takes any input from the real numbers and inputs a number
|
||
between 0 and 1, and can therefore be interpreted as a probability. It
|
||
also has other nice properties, such as a derivative that is simple to
|
||
calculate.</p>
|
||
<p>For an input <span class="math notranslate nohighlight">\(\boldsymbol{a}\)</span> from the hidden layer, the probability that the input <span class="math notranslate nohighlight">\(\boldsymbol{x}\)</span>
|
||
is in class 0 or 1 is just. We let <span class="math notranslate nohighlight">\(\theta\)</span> represent the unknown weights and biases to be adjusted by our equations). The variable <span class="math notranslate nohighlight">\(x\)</span>
|
||
represents our activation values <span class="math notranslate nohighlight">\(z\)</span>. We have</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
P(y = 0 \mid \hat{x}, \hat{\theta}) = \frac{1}{1 + \exp{(- \hat{x}})} ,
|
||
\]</div>
|
||
<p>and</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
P(y = 1 \mid \hat{x}, \hat{\theta}) = 1 - P(y = 0 \mid \hat{x}, \hat{\theta}) ,
|
||
\]</div>
|
||
<p>where <span class="math notranslate nohighlight">\(y \in \{0, 1\}\)</span> and <span class="math notranslate nohighlight">\(\hat{\theta}\)</span> represents the weights and biases
|
||
of our network.</p>
|
||
<section id="defining-the-cost-function">
|
||
<h2><span class="section-number">14.1. </span>Defining the cost function<a class="headerlink" href="#defining-the-cost-function" title="Link to this heading">#</a></h2>
|
||
<p>Our cost function is given as (see the Logistic regression lectures)</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\mathcal{C}(\hat{\theta}) = - \ln P(\mathcal{D} \mid \hat{\theta}) = - \sum_{i=1}^n
|
||
y_i \ln[P(y_i = 0)] + (1 - y_i) \ln [1 - P(y_i = 0)] = \sum_{i=1}^n \mathcal{L}_i(\hat{\theta}) .
|
||
\]</div>
|
||
<p>This last equality means that we can interpret our <em>cost</em> function as a sum over the <em>loss</em> function
|
||
for each point in the dataset <span class="math notranslate nohighlight">\(\mathcal{L}_i(\hat{\theta})\)</span>.<br />
|
||
The negative sign is just so that we can think about our algorithm as minimizing a positive number, rather
|
||
than maximizing a negative number.</p>
|
||
<p>In <em>multiclass</em> classification it is common to treat each integer label as a so called <em>one-hot</em> vector:</p>
|
||
<p><span class="math notranslate nohighlight">\(y = 5 \quad \rightarrow \quad \hat{y} = (0, 0, 0, 0, 0, 1, 0, 0, 0, 0) ,\)</span> and</p>
|
||
<p><span class="math notranslate nohighlight">\(y = 1 \quad \rightarrow \quad \hat{y} = (0, 1, 0, 0, 0, 0, 0, 0, 0, 0) ,\)</span></p>
|
||
<p>i.e. a binary bit string of length <span class="math notranslate nohighlight">\(C\)</span>, where <span class="math notranslate nohighlight">\(C = 10\)</span> is the number of classes in the MNIST dataset (numbers from <span class="math notranslate nohighlight">\(0\)</span> to <span class="math notranslate nohighlight">\(9\)</span>)..</p>
|
||
<p>If <span class="math notranslate nohighlight">\(\hat{x}_i\)</span> is the <span class="math notranslate nohighlight">\(i\)</span>-th input (image), <span class="math notranslate nohighlight">\(y_{ic}\)</span> refers to the <span class="math notranslate nohighlight">\(c\)</span>-th component of the <span class="math notranslate nohighlight">\(i\)</span>-th
|
||
output vector <span class="math notranslate nohighlight">\(\hat{y}_i\)</span>.<br />
|
||
The probability of <span class="math notranslate nohighlight">\(\hat{x}_i\)</span> being in class <span class="math notranslate nohighlight">\(c\)</span> will be given by the softmax function:</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
P(y_{ic} = 1 \mid \hat{x}_i, \hat{\theta}) = \frac{\exp{((\hat{a}_i^{hidden})^T \hat{w}_c)}}
|
||
{\sum_{c'=0}^{C-1} \exp{((\hat{a}_i^{hidden})^T \hat{w}_{c'})}} ,
|
||
\]</div>
|
||
<p>which reduces to the logistic function in the binary case.<br />
|
||
The likelihood of this <span class="math notranslate nohighlight">\(C\)</span>-class classifier
|
||
is now given as:</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
P(\mathcal{D} \mid \hat{\theta}) = \prod_{i=1}^n \prod_{c=0}^{C-1} [P(y_{ic} = 1)]^{y_{ic}} .
|
||
\]</div>
|
||
<p>Again we take the negative log-likelihood to define our cost function:</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\mathcal{C}(\hat{\theta}) = - \log{P(\mathcal{D} \mid \hat{\theta})}.
|
||
\]</div>
|
||
<p>See the logistic regression lectures for a full definition of the cost function.</p>
|
||
<p>The back propagation equations need now only a small change, namely the definition of a new cost function. We are thus ready to use the same equations as before!</p>
|
||
<section id="example-binary-classification-problem">
|
||
<h3><span class="section-number">14.1.1. </span>Example: binary classification problem<a class="headerlink" href="#example-binary-classification-problem" title="Link to this heading">#</a></h3>
|
||
<p>As an example of the above, relevant for project 2 as well, let us consider a binary class. As discussed in our logistic regression lectures, we defined a cost function in terms of the parameters <span class="math notranslate nohighlight">\(\beta\)</span> as</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\mathcal{C}(\hat{\beta}) = - \sum_{i=1}^n \left(y_i\log{p(y_i \vert x_i,\hat{\beta})}+(1-y_i)\log{1-p(y_i \vert x_i,\hat{\beta})}\right),
|
||
\]</div>
|
||
<p>where we had defined the logistic (sigmoid) function</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
p(y_i =1\vert x_i,\hat{\beta})=\frac{\exp{(\beta_0+\beta_1 x_i)}}{1+\exp{(\beta_0+\beta_1 x_i)}},
|
||
\]</div>
|
||
<p>and</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
p(y_i =0\vert x_i,\hat{\beta})=1-p(y_i =1\vert x_i,\hat{\beta}).
|
||
\]</div>
|
||
<p>The parameters <span class="math notranslate nohighlight">\(\hat{\beta}\)</span> were defined using a minimization method like gradient descent or Newton-Raphson’s method.</p>
|
||
<p>Now we replace <span class="math notranslate nohighlight">\(x_i\)</span> with the activation <span class="math notranslate nohighlight">\(z_i^l\)</span> for a given layer <span class="math notranslate nohighlight">\(l\)</span> and the outputs as <span class="math notranslate nohighlight">\(y_i=a_i^l=f(z_i^l)\)</span>, with <span class="math notranslate nohighlight">\(z_i^l\)</span> now being a function of the weights <span class="math notranslate nohighlight">\(w_{ij}^l\)</span> and biases <span class="math notranslate nohighlight">\(b_i^l\)</span>.
|
||
We have then</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
a_i^l = y_i = \frac{\exp{(z_i^l)}}{1+\exp{(z_i^l)}},
|
||
\]</div>
|
||
<p>with</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
z_i^l = \sum_{j}w_{ij}^l a_j^{l-1}+b_i^l,
|
||
\]</div>
|
||
<p>where the superscript <span class="math notranslate nohighlight">\(l-1\)</span> indicates that these are the outputs from layer <span class="math notranslate nohighlight">\(l-1\)</span>.
|
||
Our cost function at the final layer <span class="math notranslate nohighlight">\(l=L\)</span> is now</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\mathcal{C}(\hat{W}) = - \sum_{i=1}^n \left(t_i\log{a_i^L}+(1-t_i)\log{(1-a_i^L)}\right),
|
||
\]</div>
|
||
<p>where we have defined the targets <span class="math notranslate nohighlight">\(t_i\)</span>. The derivatives of the cost function with respect to the output <span class="math notranslate nohighlight">\(a_i^L\)</span> are then easily calculated and we get</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial \mathcal{C}(\hat{W})}{\partial a_i^L} = \frac{a_i^L-t_i}{a_i^L(1-a_i^L)}.
|
||
\]</div>
|
||
<p>In case we use another activation function than the logistic one, we need to evaluate other derivatives.</p>
|
||
</section>
|
||
<section id="the-softmax-function">
|
||
<h3><span class="section-number">14.1.2. </span>The Softmax function<a class="headerlink" href="#the-softmax-function" title="Link to this heading">#</a></h3>
|
||
<p>In case we employ the more general case given by the Softmax equation, we need to evaluate the derivative of the activation function with respect to the activation <span class="math notranslate nohighlight">\(z_i^l\)</span>, that is we need</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial f(z_i^l)}{\partial w_{jk}^l} =
|
||
\frac{\partial f(z_i^l)}{\partial z_j^l} \frac{\partial z_j^l}{\partial w_{jk}^l}= \frac{\partial f(z_i^l)}{\partial z_j^l}a_k^{l-1}.
|
||
\]</div>
|
||
<p>For the Softmax function we have</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
f(z_i^l) = \frac{\exp{(z_i^l)}}{\sum_{m=1}^K\exp{(z_m^l)}}.
|
||
\]</div>
|
||
<p>Its derivative with respect to <span class="math notranslate nohighlight">\(z_j^l\)</span> gives</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial f(z_i^l)}{\partial z_j^l}= f(z_i^l)\left(\delta_{ij}-f(z_j^l)\right),
|
||
\]</div>
|
||
<p>which in case of the simply binary model reduces to having <span class="math notranslate nohighlight">\(i=j\)</span>.</p>
|
||
</section>
|
||
</section>
|
||
<section id="developing-a-code-for-doing-neural-networks-with-back-propagation">
|
||
<h2><span class="section-number">14.2. </span>Developing a code for doing neural networks with back propagation<a class="headerlink" href="#developing-a-code-for-doing-neural-networks-with-back-propagation" title="Link to this heading">#</a></h2>
|
||
<p>One can identify a set of key steps when using neural networks to solve supervised learning problems:</p>
|
||
<ol class="arabic simple">
|
||
<li><p>Collect and pre-process data</p></li>
|
||
<li><p>Define model and architecture</p></li>
|
||
<li><p>Choose cost function and optimizer</p></li>
|
||
<li><p>Train the model</p></li>
|
||
<li><p>Evaluate model performance on test data</p></li>
|
||
<li><p>Adjust hyperparameters (if necessary, network architecture)</p></li>
|
||
</ol>
|
||
<section id="collect-and-pre-process-data">
|
||
<h3><span class="section-number">14.2.1. </span>Collect and pre-process data<a class="headerlink" href="#collect-and-pre-process-data" title="Link to this heading">#</a></h3>
|
||
<p>Here we will be using the MNIST dataset, which is readily available through the <strong>scikit-learn</strong>
|
||
package. You may also find it for example <a class="reference external" href="http://yann.lecun.com/exdb/mnist/">here</a>.<br />
|
||
The <em>MNIST</em> (Modified National Institute of Standards and Technology) database is a large database
|
||
of handwritten digits that is commonly used for training various image processing systems.<br />
|
||
The MNIST dataset consists of 70 000 images of size <span class="math notranslate nohighlight">\(28\times 28\)</span> pixels, each labeled from 0 to 9.<br />
|
||
The scikit-learn dataset we will use consists of a selection of 1797 images of size <span class="math notranslate nohighlight">\(8\times 8\)</span> collected and processed from this database.</p>
|
||
<p>To feed data into a feed-forward neural network we need to represent
|
||
the inputs as a design/feature matrix <span class="math notranslate nohighlight">\(X = (n_{inputs}, n_{features})\)</span>. Each
|
||
row represents an <em>input</em>, in this case a handwritten digit, and
|
||
each column represents a <em>feature</em>, in this case a pixel. The
|
||
correct answers, also known as <em>labels</em> or <em>targets</em> are
|
||
represented as a 1D array of integers
|
||
<span class="math notranslate nohighlight">\(Y = (n_{inputs}) = (5, 3, 1, 8,...)\)</span>.</p>
|
||
<p>As an example, say we want to build a neural network using supervised learning to predict Body-Mass Index (BMI) from
|
||
measurements of height (in m)<br />
|
||
and weight (in kg). If we have measurements of 5 people the design/feature matrix could be for example:</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[\begin{split} X = \begin{bmatrix}
|
||
1.85 & 81\\
|
||
1.71 & 65\\
|
||
1.95 & 103\\
|
||
1.55 & 42\\
|
||
1.63 & 56
|
||
\end{bmatrix} ,\end{split}\]</div>
|
||
<p>and the targets would be:</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[ Y = (23.7, 22.2, 27.1, 17.5, 21.1) \]</div>
|
||
<p>Since each input image is a 2D matrix, we need to flatten the image
|
||
(i.e. “unravel” the 2D matrix into a 1D array) to turn the data into a
|
||
design/feature matrix. This means we lose all spatial information in the
|
||
image, such as locality and translational invariance. More complicated
|
||
architectures such as Convolutional Neural Networks can take advantage
|
||
of such information, and are most commonly applied when analyzing
|
||
images.</p>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>%matplotlib inline
|
||
|
||
# import necessary packages
|
||
import numpy as np
|
||
import matplotlib.pyplot as plt
|
||
from sklearn import datasets
|
||
|
||
|
||
# ensure the same random numbers appear every time
|
||
np.random.seed(0)
|
||
|
||
# display images in notebook
|
||
%matplotlib inline
|
||
plt.rcParams['figure.figsize'] = (12,12)
|
||
|
||
|
||
# download MNIST dataset
|
||
digits = datasets.load_digits()
|
||
|
||
# define inputs and labels
|
||
inputs = digits.images
|
||
labels = digits.target
|
||
|
||
print("inputs = (n_inputs, pixel_width, pixel_height) = " + str(inputs.shape))
|
||
print("labels = (n_inputs) = " + str(labels.shape))
|
||
|
||
|
||
# flatten the image
|
||
# the value -1 means dimension is inferred from the remaining dimensions: 8x8 = 64
|
||
n_inputs = len(inputs)
|
||
inputs = inputs.reshape(n_inputs, -1)
|
||
print("X = (n_inputs, n_features) = " + str(inputs.shape))
|
||
|
||
|
||
# choose some random images to display
|
||
indices = np.arange(n_inputs)
|
||
random_indices = np.random.choice(indices, size=5)
|
||
|
||
for i, image in enumerate(digits.images[random_indices]):
|
||
plt.subplot(1, 5, i+1)
|
||
plt.axis('off')
|
||
plt.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest')
|
||
plt.title("Label: %d" % digits.target[random_indices[i]])
|
||
plt.show()
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
<section id="train-and-test-datasets">
|
||
<h3><span class="section-number">14.2.2. </span>Train and test datasets<a class="headerlink" href="#train-and-test-datasets" title="Link to this heading">#</a></h3>
|
||
<p>Performing analysis before partitioning the dataset is a major error, that can lead to incorrect conclusions.</p>
|
||
<p>We will reserve <span class="math notranslate nohighlight">\(80 \%\)</span> of our dataset for training and <span class="math notranslate nohighlight">\(20 \%\)</span> for testing.</p>
|
||
<p>It is important that the train and test datasets are drawn randomly from our dataset, to ensure
|
||
no bias in the sampling.<br />
|
||
Say you are taking measurements of weather data to predict the weather in the coming 5 days.
|
||
You don’t want to train your model on measurements taken from the hours 00.00 to 12.00, and then test it on data
|
||
collected from 12.00 to 24.00.</p>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>from sklearn.model_selection import train_test_split
|
||
|
||
# one-liner from scikit-learn library
|
||
train_size = 0.8
|
||
test_size = 1 - train_size
|
||
X_train, X_test, Y_train, Y_test = train_test_split(inputs, labels, train_size=train_size,
|
||
test_size=test_size)
|
||
|
||
# equivalently in numpy
|
||
def train_test_split_numpy(inputs, labels, train_size, test_size):
|
||
n_inputs = len(inputs)
|
||
inputs_shuffled = inputs.copy()
|
||
labels_shuffled = labels.copy()
|
||
|
||
np.random.shuffle(inputs_shuffled)
|
||
np.random.shuffle(labels_shuffled)
|
||
|
||
train_end = int(n_inputs*train_size)
|
||
X_train, X_test = inputs_shuffled[:train_end], inputs_shuffled[train_end:]
|
||
Y_train, Y_test = labels_shuffled[:train_end], labels_shuffled[train_end:]
|
||
|
||
return X_train, X_test, Y_train, Y_test
|
||
|
||
#X_train, X_test, Y_train, Y_test = train_test_split_numpy(inputs, labels, train_size, test_size)
|
||
|
||
print("Number of training images: " + str(len(X_train)))
|
||
print("Number of test images: " + str(len(X_test)))
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
<section id="define-model-and-architecture">
|
||
<h3><span class="section-number">14.2.3. </span>Define model and architecture<a class="headerlink" href="#define-model-and-architecture" title="Link to this heading">#</a></h3>
|
||
<p>Our simple feed-forward neural network will consist of an <em>input</em> layer, a single <em>hidden</em> layer and an <em>output</em> layer. The activation <span class="math notranslate nohighlight">\(y\)</span> of each neuron is a weighted sum of inputs, passed through an activation function. In case of the simple perceptron model we have</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[ z = \sum_{i=1}^n w_i a_i ,\]</div>
|
||
<div class="math notranslate nohighlight">
|
||
\[ y = f(z) ,\]</div>
|
||
<p>where <span class="math notranslate nohighlight">\(f\)</span> is the activation function, <span class="math notranslate nohighlight">\(a_i\)</span> represents input from neuron <span class="math notranslate nohighlight">\(i\)</span> in the preceding layer
|
||
and <span class="math notranslate nohighlight">\(w_i\)</span> is the weight to input <span class="math notranslate nohighlight">\(i\)</span>.<br />
|
||
The activation of the neurons in the input layer is just the features (e.g. a pixel value).</p>
|
||
<p>The simplest activation function for a neuron is the <em>Heaviside</em> function:</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[\begin{split} f(z) =
|
||
\begin{cases}
|
||
1, & z > 0\\
|
||
0, & \text{otherwise}
|
||
\end{cases}
|
||
\end{split}\]</div>
|
||
<p>A feed-forward neural network with this activation is known as a <em>perceptron</em>.<br />
|
||
For a binary classifier (i.e. two classes, 0 or 1, dog or not-dog) we can also use this in our output layer.<br />
|
||
This activation can be generalized to <span class="math notranslate nohighlight">\(k\)</span> classes (using e.g. the <em>one-against-all</em> strategy),
|
||
and we call these architectures <em>multiclass perceptrons</em>.</p>
|
||
<p>However, it is now common to use the terms Single Layer Perceptron (SLP) (1 hidden layer) and<br />
|
||
Multilayer Perceptron (MLP) (2 or more hidden layers) to refer to feed-forward neural networks with any activation function.</p>
|
||
<p>Typical choices for activation functions include the sigmoid function, hyperbolic tangent, and Rectified Linear Unit (ReLU).<br />
|
||
We will be using the sigmoid function <span class="math notranslate nohighlight">\(\sigma(x)\)</span>:</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[ f(x) = \sigma(x) = \frac{1}{1 + e^{-x}} ,\]</div>
|
||
<p>which is inspired by probability theory (see logistic regression) and was most commonly used until about 2011. See the discussion below concerning other activation functions.</p>
|
||
</section>
|
||
<section id="layers">
|
||
<h3><span class="section-number">14.2.4. </span>Layers<a class="headerlink" href="#layers" title="Link to this heading">#</a></h3>
|
||
<ul class="simple">
|
||
<li><p>Input</p></li>
|
||
</ul>
|
||
<p>Since each input image has 8x8 = 64 pixels or features, we have an input layer of 64 neurons.</p>
|
||
<ul class="simple">
|
||
<li><p>Hidden layer</p></li>
|
||
</ul>
|
||
<p>We will use 50 neurons in the hidden layer receiving input from the neurons in the input layer.<br />
|
||
Since each neuron in the hidden layer is connected to the 64 inputs we have 64x50 = 3200 weights to the hidden layer.</p>
|
||
<ul class="simple">
|
||
<li><p>Output</p></li>
|
||
</ul>
|
||
<p>If we were building a binary classifier, it would be sufficient with a single neuron in the output layer,
|
||
which could output 0 or 1 according to the Heaviside function. This would be an example of a <em>hard</em> classifier, meaning it outputs the class of the input directly. However, if we are dealing with noisy data it is often beneficial to use a <em>soft</em> classifier, which outputs the probability of being in class 0 or 1.</p>
|
||
<p>For a soft binary classifier, we could use a single neuron and interpret the output as either being the probability of being in class 0 or the probability of being in class 1. Alternatively we could use 2 neurons, and interpret each neuron as the probability of being in each class.</p>
|
||
<p>Since we are doing multiclass classification, with 10 categories, it is natural to use 10 neurons in the output layer. We number the neurons <span class="math notranslate nohighlight">\(j = 0,1,...,9\)</span>. The activation of each output neuron <span class="math notranslate nohighlight">\(j\)</span> will be according to the <em>softmax</em> function:</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[ P(\text{class $j$} \mid \text{input $\hat{a}$}) = \frac{\exp{(\hat{a}^T \hat{w}_j)}}
|
||
{\sum_{c=0}^{9} \exp{(\hat{a}^T \hat{w}_c)}} ,\]</div>
|
||
<p>i.e. each neuron <span class="math notranslate nohighlight">\(j\)</span> outputs the probability of being in class <span class="math notranslate nohighlight">\(j\)</span> given an input from the hidden layer <span class="math notranslate nohighlight">\(\hat{a}\)</span>, with <span class="math notranslate nohighlight">\(\hat{w}_j\)</span> the weights of neuron <span class="math notranslate nohighlight">\(j\)</span> to the inputs.<br />
|
||
The denominator is a normalization factor to ensure the outputs (probabilities) sum up to 1.<br />
|
||
The exponent is just the weighted sum of inputs as before:</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[ z_j = \sum_{i=1}^n w_ {ij} a_i+b_j.\]</div>
|
||
<p>Since each neuron in the output layer is connected to the 50 inputs from the hidden layer we have 50x10 = 500
|
||
weights to the output layer.</p>
|
||
<p>Typically weights are initialized with small values distributed around zero, drawn from a uniform
|
||
or normal distribution. Setting all weights to zero means all neurons give the same output, making the network useless.</p>
|
||
<p>Adding a bias value to the weighted sum of inputs allows the neural network to represent a greater range
|
||
of values. Without it, any input with the value 0 will be mapped to zero (before being passed through the activation). The bias unit has an output of 1, and a weight to each neuron <span class="math notranslate nohighlight">\(j\)</span>, <span class="math notranslate nohighlight">\(b_j\)</span>:</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[ z_j = \sum_{i=1}^n w_ {ij} a_i + b_j.\]</div>
|
||
<p>The bias weights <span class="math notranslate nohighlight">\(\hat{b}\)</span> are often initialized to zero, but a small value like <span class="math notranslate nohighlight">\(0.01\)</span> ensures all neurons have some output which can be backpropagated in the first training cycle.</p>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span># building our neural network
|
||
|
||
n_inputs, n_features = X_train.shape
|
||
n_hidden_neurons = 50
|
||
n_categories = 10
|
||
|
||
# we make the weights normally distributed using numpy.random.randn
|
||
|
||
# weights and bias in the hidden layer
|
||
hidden_weights = np.random.randn(n_features, n_hidden_neurons)
|
||
hidden_bias = np.zeros(n_hidden_neurons) + 0.01
|
||
|
||
# weights and bias in the output layer
|
||
output_weights = np.random.randn(n_hidden_neurons, n_categories)
|
||
output_bias = np.zeros(n_categories) + 0.01
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
<section id="feed-forward-pass">
|
||
<h3><span class="section-number">14.2.5. </span>Feed-forward pass<a class="headerlink" href="#feed-forward-pass" title="Link to this heading">#</a></h3>
|
||
<p>Denote <span class="math notranslate nohighlight">\(F\)</span> the number of features, <span class="math notranslate nohighlight">\(H\)</span> the number of hidden neurons and <span class="math notranslate nohighlight">\(C\)</span> the number of categories.<br />
|
||
For each input image we calculate a weighted sum of input features (pixel values) to each neuron <span class="math notranslate nohighlight">\(j\)</span> in the hidden layer <span class="math notranslate nohighlight">\(l\)</span>:</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[ z_{j}^{l} = \sum_{i=1}^{F} w_{ij}^{l} x_i + b_{j}^{l},\]</div>
|
||
<p>this is then passed through our activation function</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[ a_{j}^{l} = f(z_{j}^{l}) .\]</div>
|
||
<p>We calculate a weighted sum of inputs (activations in the hidden layer) to each neuron <span class="math notranslate nohighlight">\(j\)</span> in the output layer:</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[ z_{j}^{L} = \sum_{i=1}^{H} w_{ij}^{L} a_{i}^{l} + b_{j}^{L}.\]</div>
|
||
<p>Finally we calculate the output of neuron <span class="math notranslate nohighlight">\(j\)</span> in the output layer using the softmax function:</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[ a_{j}^{L} = \frac{\exp{(z_j^{L})}}
|
||
{\sum_{c=0}^{C-1} \exp{(z_c^{L})}} .\]</div>
|
||
<p>Since our data has the dimensions <span class="math notranslate nohighlight">\(X = (n_{inputs}, n_{features})\)</span> and our weights to the hidden
|
||
layer have the dimensions<br />
|
||
<span class="math notranslate nohighlight">\(W_{hidden} = (n_{features}, n_{hidden})\)</span>,
|
||
we can easily feed the network all our training data in one go by taking the matrix product</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[ X W^{h} = (n_{inputs}, n_{hidden}),\]</div>
|
||
<p>and obtain a matrix that holds the weighted sum of inputs to the hidden layer
|
||
for each input image and each hidden neuron.<br />
|
||
We also add the bias to obtain a matrix of weighted sums to the hidden layer <span class="math notranslate nohighlight">\(Z^{h}\)</span>:</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[ \hat{z}^{l} = \hat{X} \hat{W}^{l} + \hat{b}^{l} ,\]</div>
|
||
<p>meaning the same bias (1D array with size equal number of hidden neurons) is added to each input image.<br />
|
||
This is then passed through the activation:</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[ \hat{a}^{l} = f(\hat{z}^l) .\]</div>
|
||
<p>This is fed to the output layer:</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[ \hat{z}^{L} = \hat{a}^{L} \hat{W}^{L} + \hat{b}^{L} .\]</div>
|
||
<p>Finally we receive our output values for each image and each category by passing it through the softmax function:</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[ output = softmax (\hat{z}^{L}) = (n_{inputs}, n_{categories}) .\]</div>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span># setup the feed-forward pass, subscript h = hidden layer
|
||
|
||
def sigmoid(x):
|
||
return 1/(1 + np.exp(-x))
|
||
|
||
def feed_forward(X):
|
||
# weighted sum of inputs to the hidden layer
|
||
z_h = np.matmul(X, hidden_weights) + hidden_bias
|
||
# activation in the hidden layer
|
||
a_h = sigmoid(z_h)
|
||
|
||
# weighted sum of inputs to the output layer
|
||
z_o = np.matmul(a_h, output_weights) + output_bias
|
||
# softmax output
|
||
# axis 0 holds each input and axis 1 the probabilities of each category
|
||
exp_term = np.exp(z_o)
|
||
probabilities = exp_term / np.sum(exp_term, axis=1, keepdims=True)
|
||
|
||
return probabilities
|
||
|
||
probabilities = feed_forward(X_train)
|
||
print("probabilities = (n_inputs, n_categories) = " + str(probabilities.shape))
|
||
print("probability that image 0 is in category 0,1,2,...,9 = \n" + str(probabilities[0]))
|
||
print("probabilities sum up to: " + str(probabilities[0].sum()))
|
||
print()
|
||
|
||
# we obtain a prediction by taking the class with the highest likelihood
|
||
def predict(X):
|
||
probabilities = feed_forward(X)
|
||
return np.argmax(probabilities, axis=1)
|
||
|
||
predictions = predict(X_train)
|
||
print("predictions = (n_inputs) = " + str(predictions.shape))
|
||
print("prediction for image 0: " + str(predictions[0]))
|
||
print("correct label for image 0: " + str(Y_train[0]))
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
<section id="choose-cost-function-and-optimizer">
|
||
<h3><span class="section-number">14.2.6. </span>Choose cost function and optimizer<a class="headerlink" href="#choose-cost-function-and-optimizer" title="Link to this heading">#</a></h3>
|
||
<p>To measure how well our neural network is doing we need to introduce a cost function.<br />
|
||
We will call the function that gives the error of a single sample output the <em>loss</em> function, and the function
|
||
that gives the total error of our network across all samples the <em>cost</em> function.
|
||
A typical choice for multiclass classification is the <em>cross-entropy</em> loss, also known as the negative log likelihood.</p>
|
||
<p>In <em>multiclass</em> classification it is common to treat each integer label as a so called <em>one-hot</em> vector:</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[ y = 5 \quad \rightarrow \quad \hat{y} = (0, 0, 0, 0, 0, 1, 0, 0, 0, 0) ,\]</div>
|
||
<div class="math notranslate nohighlight">
|
||
\[ y = 1 \quad \rightarrow \quad \hat{y} = (0, 1, 0, 0, 0, 0, 0, 0, 0, 0) ,\]</div>
|
||
<p>i.e. a binary bit string of length <span class="math notranslate nohighlight">\(C\)</span>, where <span class="math notranslate nohighlight">\(C = 10\)</span> is the number of classes in the MNIST dataset.</p>
|
||
<p>Let <span class="math notranslate nohighlight">\(y_{ic}\)</span> denote the <span class="math notranslate nohighlight">\(c\)</span>-th component of the <span class="math notranslate nohighlight">\(i\)</span>-th one-hot vector.<br />
|
||
We define the cost function <span class="math notranslate nohighlight">\(\mathcal{C}\)</span> as a sum over the cross-entropy loss for each point <span class="math notranslate nohighlight">\(\hat{x}_i\)</span> in the dataset.</p>
|
||
<p>In the one-hot representation only one of the terms in the loss function is non-zero, namely the
|
||
probability of the correct category <span class="math notranslate nohighlight">\(c'\)</span><br />
|
||
(i.e. the category <span class="math notranslate nohighlight">\(c'\)</span> such that <span class="math notranslate nohighlight">\(y_{ic'} = 1\)</span>). This means that the cross entropy loss only punishes you for how wrong
|
||
you got the correct label. The probability of category <span class="math notranslate nohighlight">\(c\)</span> is given by the softmax function. The vector <span class="math notranslate nohighlight">\(\hat{\theta}\)</span> represents the parameters of our network, i.e. all the weights and biases.</p>
|
||
</section>
|
||
<section id="optimizing-the-cost-function">
|
||
<h3><span class="section-number">14.2.7. </span>Optimizing the cost function<a class="headerlink" href="#optimizing-the-cost-function" title="Link to this heading">#</a></h3>
|
||
<p>The network is trained by finding the weights and biases that minimize the cost function. One of the most widely used classes of methods is <em>gradient descent</em> and its generalizations. The idea behind gradient descent
|
||
is simply to adjust the weights in the direction where the gradient of the cost function is large and negative. This ensures we flow toward a <em>local</em> minimum of the cost function.<br />
|
||
Each parameter <span class="math notranslate nohighlight">\(\theta\)</span> is iteratively adjusted according to the rule</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[ \theta_{i+1} = \theta_i - \eta \nabla \mathcal{C}(\theta_i) ,\]</div>
|
||
<p>where <span class="math notranslate nohighlight">\(\eta\)</span> is known as the <em>learning rate</em>, which controls how big a step we take towards the minimum.<br />
|
||
This update can be repeated for any number of iterations, or until we are satisfied with the result.</p>
|
||
<p>A simple and effective improvement is a variant called <em>Batch Gradient Descent</em>.<br />
|
||
Instead of calculating the gradient on the whole dataset, we calculate an approximation of the gradient
|
||
on a subset of the data called a <em>minibatch</em>.<br />
|
||
If there are <span class="math notranslate nohighlight">\(N\)</span> data points and we have a minibatch size of <span class="math notranslate nohighlight">\(M\)</span>, the total number of batches
|
||
is <span class="math notranslate nohighlight">\(N/M\)</span>.<br />
|
||
We denote each minibatch <span class="math notranslate nohighlight">\(B_k\)</span>, with <span class="math notranslate nohighlight">\(k = 1, 2,...,N/M\)</span>. The gradient then becomes:</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[ \nabla \mathcal{C}(\theta) = \frac{1}{N} \sum_{i=1}^N \nabla \mathcal{L}_i(\theta) \quad \rightarrow \quad
|
||
\frac{1}{M} \sum_{i \in B_k} \nabla \mathcal{L}_i(\theta) ,\]</div>
|
||
<p>i.e. instead of averaging the loss over the entire dataset, we average over a minibatch.</p>
|
||
<p>This has two important benefits:</p>
|
||
<ol class="arabic simple">
|
||
<li><p>Introducing stochasticity decreases the chance that the algorithm becomes stuck in a local minima.</p></li>
|
||
<li><p>It significantly speeds up the calculation, since we do not have to use the entire dataset to calculate the gradient.</p></li>
|
||
</ol>
|
||
<p>The various optmization methods, with codes and algorithms, are discussed in our lectures on <a class="reference external" href="https://compphysics.github.io/MachineLearning/doc/pub/Splines/html/Splines-bs.html">Gradient descent approaches</a>.</p>
|
||
</section>
|
||
<section id="regularization">
|
||
<h3><span class="section-number">14.2.8. </span>Regularization<a class="headerlink" href="#regularization" title="Link to this heading">#</a></h3>
|
||
<p>It is common to add an extra term to the cost function, proportional
|
||
to the size of the weights. This is equivalent to constraining the
|
||
size of the weights, so that they do not grow out of control.
|
||
Constraining the size of the weights means that the weights cannot
|
||
grow arbitrarily large to fit the training data, and in this way
|
||
reduces <em>overfitting</em>.</p>
|
||
<p>We will measure the size of the weights using the so called <em>L2-norm</em>, meaning our cost function becomes:</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[ \mathcal{C}(\theta) = \frac{1}{N} \sum_{i=1}^N \mathcal{L}_i(\theta) \quad \rightarrow \quad
|
||
\frac{1}{N} \sum_{i=1}^N \mathcal{L}_i(\theta) + \lambda \lvert \lvert \hat{w} \rvert \rvert_2^2
|
||
= \frac{1}{N} \sum_{i=1}^N \mathcal{L}(\theta) + \lambda \sum_{ij} w_{ij}^2,\]</div>
|
||
<p>i.e. we sum up all the weights squared. The factor <span class="math notranslate nohighlight">\(\lambda\)</span> is known as a regularization parameter.</p>
|
||
<p>In order to train the model, we need to calculate the derivative of
|
||
the cost function with respect to every bias and weight in the
|
||
network. In total our network has <span class="math notranslate nohighlight">\((64 + 1)\times 50=3250\)</span> weights in
|
||
the hidden layer and <span class="math notranslate nohighlight">\((50 + 1)\times 10=510\)</span> weights to the output
|
||
layer (<span class="math notranslate nohighlight">\(+1\)</span> for the bias), and the gradient must be calculated for
|
||
every parameter. We use the <em>backpropagation</em> algorithm discussed
|
||
above. This is a clever use of the chain rule that allows us to
|
||
calculate the gradient efficently.</p>
|
||
</section>
|
||
<section id="matrix-multiplication">
|
||
<h3><span class="section-number">14.2.9. </span>Matrix multiplication<a class="headerlink" href="#matrix-multiplication" title="Link to this heading">#</a></h3>
|
||
<p>To more efficently train our network these equations are implemented using matrix operations.<br />
|
||
The error in the output layer is calculated simply as, with <span class="math notranslate nohighlight">\(\hat{t}\)</span> being our targets,</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[ \delta_L = \hat{t} - \hat{y} = (n_{inputs}, n_{categories}) .\]</div>
|
||
<p>The gradient for the output weights is calculated as</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[ \nabla W_{L} = \hat{a}^T \delta_L = (n_{hidden}, n_{categories}) ,\]</div>
|
||
<p>where <span class="math notranslate nohighlight">\(\hat{a} = (n_{inputs}, n_{hidden})\)</span>. This simply means that we are summing up the gradients for each input.<br />
|
||
Since we are going backwards we have to transpose the activation matrix.</p>
|
||
<p>The gradient with respect to the output bias is then</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[ \nabla \hat{b}_{L} = \sum_{i=1}^{n_{inputs}} \delta_L = (n_{categories}) .\]</div>
|
||
<p>The error in the hidden layer is</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[ \Delta_h = \delta_L W_{L}^T \circ f'(z_{h}) = \delta_L W_{L}^T \circ a_{h} \circ (1 - a_{h}) = (n_{inputs}, n_{hidden}) ,\]</div>
|
||
<p>where <span class="math notranslate nohighlight">\(f'(a_{h})\)</span> is the derivative of the activation in the hidden layer. The matrix products mean
|
||
that we are summing up the products for each neuron in the output layer. The symbol <span class="math notranslate nohighlight">\(\circ\)</span> denotes
|
||
the <em>Hadamard product</em>, meaning element-wise multiplication.</p>
|
||
<p>This again gives us the gradients in the hidden layer:</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[ \nabla W_{h} = X^T \delta_h = (n_{features}, n_{hidden}) ,\]</div>
|
||
<div class="math notranslate nohighlight">
|
||
\[ \nabla b_{h} = \sum_{i=1}^{n_{inputs}} \delta_h = (n_{hidden}) .\]</div>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span># to categorical turns our integer vector into a onehot representation
|
||
from sklearn.metrics import accuracy_score
|
||
|
||
# one-hot in numpy
|
||
def to_categorical_numpy(integer_vector):
|
||
n_inputs = len(integer_vector)
|
||
n_categories = np.max(integer_vector) + 1
|
||
onehot_vector = np.zeros((n_inputs, n_categories))
|
||
onehot_vector[range(n_inputs), integer_vector] = 1
|
||
|
||
return onehot_vector
|
||
|
||
#Y_train_onehot, Y_test_onehot = to_categorical(Y_train), to_categorical(Y_test)
|
||
Y_train_onehot, Y_test_onehot = to_categorical_numpy(Y_train), to_categorical_numpy(Y_test)
|
||
|
||
def feed_forward_train(X):
|
||
# weighted sum of inputs to the hidden layer
|
||
z_h = np.matmul(X, hidden_weights) + hidden_bias
|
||
# activation in the hidden layer
|
||
a_h = sigmoid(z_h)
|
||
|
||
# weighted sum of inputs to the output layer
|
||
z_o = np.matmul(a_h, output_weights) + output_bias
|
||
# softmax output
|
||
# axis 0 holds each input and axis 1 the probabilities of each category
|
||
exp_term = np.exp(z_o)
|
||
probabilities = exp_term / np.sum(exp_term, axis=1, keepdims=True)
|
||
|
||
# for backpropagation need activations in hidden and output layers
|
||
return a_h, probabilities
|
||
|
||
def backpropagation(X, Y):
|
||
a_h, probabilities = feed_forward_train(X)
|
||
|
||
# error in the output layer
|
||
error_output = probabilities - Y
|
||
# error in the hidden layer
|
||
error_hidden = np.matmul(error_output, output_weights.T) * a_h * (1 - a_h)
|
||
|
||
# gradients for the output layer
|
||
output_weights_gradient = np.matmul(a_h.T, error_output)
|
||
output_bias_gradient = np.sum(error_output, axis=0)
|
||
|
||
# gradient for the hidden layer
|
||
hidden_weights_gradient = np.matmul(X.T, error_hidden)
|
||
hidden_bias_gradient = np.sum(error_hidden, axis=0)
|
||
|
||
return output_weights_gradient, output_bias_gradient, hidden_weights_gradient, hidden_bias_gradient
|
||
|
||
print("Old accuracy on training data: " + str(accuracy_score(predict(X_train), Y_train)))
|
||
|
||
eta = 0.01
|
||
lmbd = 0.01
|
||
for i in range(1000):
|
||
# calculate gradients
|
||
dWo, dBo, dWh, dBh = backpropagation(X_train, Y_train_onehot)
|
||
|
||
# regularization term gradients
|
||
dWo += lmbd * output_weights
|
||
dWh += lmbd * hidden_weights
|
||
|
||
# update weights and biases
|
||
output_weights -= eta * dWo
|
||
output_bias -= eta * dBo
|
||
hidden_weights -= eta * dWh
|
||
hidden_bias -= eta * dBh
|
||
|
||
print("New accuracy on training data: " + str(accuracy_score(predict(X_train), Y_train)))
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
</section>
|
||
<section id="improving-performance">
|
||
<h2><span class="section-number">14.3. </span>Improving performance<a class="headerlink" href="#improving-performance" title="Link to this heading">#</a></h2>
|
||
<p>As we can see the network does not seem to be learning at all. It seems to be just guessing the label for each image.<br />
|
||
In order to obtain a network that does something useful, we will have to do a bit more work.</p>
|
||
<p>The choice of <em>hyperparameters</em> such as learning rate and regularization parameter is hugely influential for the performance of the network. Typically a <em>grid-search</em> is performed, wherein we test different hyperparameters separated by orders of magnitude. For example we could test the learning rates <span class="math notranslate nohighlight">\(\eta = 10^{-6}, 10^{-5},...,10^{-1}\)</span> with different regularization parameters <span class="math notranslate nohighlight">\(\lambda = 10^{-6},...,10^{-0}\)</span>.</p>
|
||
<p>Next, we haven’t implemented minibatching yet, which introduces stochasticity and is though to act as an important regularizer on the weights. We call a feed-forward + backward pass with a minibatch an <em>iteration</em>, and a full training period
|
||
going through the entire dataset (<span class="math notranslate nohighlight">\(n/M\)</span> batches) an <em>epoch</em>.</p>
|
||
<p>If this does not improve network performance, you may want to consider altering the network architecture, adding more neurons or hidden layers.<br />
|
||
Andrew Ng goes through some of these considerations in this <a class="reference external" href="https://youtu.be/F1ka6a13S9I">video</a>. You can find a summary of the video <a class="reference external" href="https://kevinzakka.github.io/2016/09/26/applying-deep-learning/">here</a>.</p>
|
||
<p>It is very natural to think of the network as an object, with specific instances of the network
|
||
being realizations of this object with different hyperparameters. An implementation using Python classes provides a clean structure and interface, and the full implementation of our neural network is given below.</p>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>class NeuralNetwork:
|
||
def __init__(
|
||
self,
|
||
X_data,
|
||
Y_data,
|
||
n_hidden_neurons=50,
|
||
n_categories=10,
|
||
epochs=10,
|
||
batch_size=100,
|
||
eta=0.1,
|
||
lmbd=0.0):
|
||
|
||
self.X_data_full = X_data
|
||
self.Y_data_full = Y_data
|
||
|
||
self.n_inputs = X_data.shape[0]
|
||
self.n_features = X_data.shape[1]
|
||
self.n_hidden_neurons = n_hidden_neurons
|
||
self.n_categories = n_categories
|
||
|
||
self.epochs = epochs
|
||
self.batch_size = batch_size
|
||
self.iterations = self.n_inputs // self.batch_size
|
||
self.eta = eta
|
||
self.lmbd = lmbd
|
||
|
||
self.create_biases_and_weights()
|
||
|
||
def create_biases_and_weights(self):
|
||
self.hidden_weights = np.random.randn(self.n_features, self.n_hidden_neurons)
|
||
self.hidden_bias = np.zeros(self.n_hidden_neurons) + 0.01
|
||
|
||
self.output_weights = np.random.randn(self.n_hidden_neurons, self.n_categories)
|
||
self.output_bias = np.zeros(self.n_categories) + 0.01
|
||
|
||
def feed_forward(self):
|
||
# feed-forward for training
|
||
self.z_h = np.matmul(self.X_data, self.hidden_weights) + self.hidden_bias
|
||
self.a_h = sigmoid(self.z_h)
|
||
|
||
self.z_o = np.matmul(self.a_h, self.output_weights) + self.output_bias
|
||
|
||
exp_term = np.exp(self.z_o)
|
||
self.probabilities = exp_term / np.sum(exp_term, axis=1, keepdims=True)
|
||
|
||
def feed_forward_out(self, X):
|
||
# feed-forward for output
|
||
z_h = np.matmul(X, self.hidden_weights) + self.hidden_bias
|
||
a_h = sigmoid(z_h)
|
||
|
||
z_o = np.matmul(a_h, self.output_weights) + self.output_bias
|
||
|
||
exp_term = np.exp(z_o)
|
||
probabilities = exp_term / np.sum(exp_term, axis=1, keepdims=True)
|
||
return probabilities
|
||
|
||
def backpropagation(self):
|
||
error_output = self.probabilities - self.Y_data
|
||
error_hidden = np.matmul(error_output, self.output_weights.T) * self.a_h * (1 - self.a_h)
|
||
|
||
self.output_weights_gradient = np.matmul(self.a_h.T, error_output)
|
||
self.output_bias_gradient = np.sum(error_output, axis=0)
|
||
|
||
self.hidden_weights_gradient = np.matmul(self.X_data.T, error_hidden)
|
||
self.hidden_bias_gradient = np.sum(error_hidden, axis=0)
|
||
|
||
if self.lmbd > 0.0:
|
||
self.output_weights_gradient += self.lmbd * self.output_weights
|
||
self.hidden_weights_gradient += self.lmbd * self.hidden_weights
|
||
|
||
self.output_weights -= self.eta * self.output_weights_gradient
|
||
self.output_bias -= self.eta * self.output_bias_gradient
|
||
self.hidden_weights -= self.eta * self.hidden_weights_gradient
|
||
self.hidden_bias -= self.eta * self.hidden_bias_gradient
|
||
|
||
def predict(self, X):
|
||
probabilities = self.feed_forward_out(X)
|
||
return np.argmax(probabilities, axis=1)
|
||
|
||
def predict_probabilities(self, X):
|
||
probabilities = self.feed_forward_out(X)
|
||
return probabilities
|
||
|
||
def train(self):
|
||
data_indices = np.arange(self.n_inputs)
|
||
|
||
for i in range(self.epochs):
|
||
for j in range(self.iterations):
|
||
# pick datapoints with replacement
|
||
chosen_datapoints = np.random.choice(
|
||
data_indices, size=self.batch_size, replace=False
|
||
)
|
||
|
||
# minibatch training data
|
||
self.X_data = self.X_data_full[chosen_datapoints]
|
||
self.Y_data = self.Y_data_full[chosen_datapoints]
|
||
|
||
self.feed_forward()
|
||
self.backpropagation()
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
<section id="evaluate-model-performance-on-test-data">
|
||
<h2><span class="section-number">14.4. </span>Evaluate model performance on test data<a class="headerlink" href="#evaluate-model-performance-on-test-data" title="Link to this heading">#</a></h2>
|
||
<p>To measure the performance of our network we evaluate how well it does it data it has never seen before, i.e. the test data.<br />
|
||
We measure the performance of the network using the <em>accuracy</em> score.<br />
|
||
The accuracy is as you would expect just the number of images correctly labeled divided by the total number of images. A perfect classifier will have an accuracy score of <span class="math notranslate nohighlight">\(1\)</span>.</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[ \text{Accuracy} = \frac{\sum_{i=1}^n I(\hat{y}_i = y_i)}{n} ,\]</div>
|
||
<p>where <span class="math notranslate nohighlight">\(I\)</span> is the indicator function, <span class="math notranslate nohighlight">\(1\)</span> if <span class="math notranslate nohighlight">\(\hat{y}_i = y_i\)</span> and <span class="math notranslate nohighlight">\(0\)</span> otherwise.</p>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>epochs = 100
|
||
batch_size = 100
|
||
|
||
dnn = NeuralNetwork(X_train, Y_train_onehot, eta=eta, lmbd=lmbd, epochs=epochs, batch_size=batch_size,
|
||
n_hidden_neurons=n_hidden_neurons, n_categories=n_categories)
|
||
dnn.train()
|
||
test_predict = dnn.predict(X_test)
|
||
|
||
# accuracy score from scikit library
|
||
print("Accuracy score on test set: ", accuracy_score(Y_test, test_predict))
|
||
|
||
# equivalent in numpy
|
||
def accuracy_score_numpy(Y_test, Y_pred):
|
||
return np.sum(Y_test == Y_pred) / len(Y_test)
|
||
|
||
#print("Accuracy score on test set: ", accuracy_score_numpy(Y_test, test_predict))
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
<section id="adjust-hyperparameters">
|
||
<h2><span class="section-number">14.5. </span>Adjust hyperparameters<a class="headerlink" href="#adjust-hyperparameters" title="Link to this heading">#</a></h2>
|
||
<p>We now perform a grid search to find the optimal hyperparameters for the network.<br />
|
||
Note that we are only using 1 layer with 50 neurons, and human performance is estimated to be around <span class="math notranslate nohighlight">\(98\%\)</span> (<span class="math notranslate nohighlight">\(2\%\)</span> error rate).</p>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>eta_vals = np.logspace(-5, 1, 7)
|
||
lmbd_vals = np.logspace(-5, 1, 7)
|
||
# store the models for later use
|
||
DNN_numpy = np.zeros((len(eta_vals), len(lmbd_vals)), dtype=object)
|
||
|
||
# grid search
|
||
for i, eta in enumerate(eta_vals):
|
||
for j, lmbd in enumerate(lmbd_vals):
|
||
dnn = NeuralNetwork(X_train, Y_train_onehot, eta=eta, lmbd=lmbd, epochs=epochs, batch_size=batch_size,
|
||
n_hidden_neurons=n_hidden_neurons, n_categories=n_categories)
|
||
dnn.train()
|
||
|
||
DNN_numpy[i][j] = dnn
|
||
|
||
test_predict = dnn.predict(X_test)
|
||
|
||
print("Learning rate = ", eta)
|
||
print("Lambda = ", lmbd)
|
||
print("Accuracy score on test set: ", accuracy_score(Y_test, test_predict))
|
||
print()
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
<section id="visualization">
|
||
<h2><span class="section-number">14.6. </span>Visualization<a class="headerlink" href="#visualization" title="Link to this heading">#</a></h2>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span># visual representation of grid search
|
||
# uses seaborn heatmap, you can also do this with matplotlib imshow
|
||
import seaborn as sns
|
||
|
||
sns.set()
|
||
|
||
train_accuracy = np.zeros((len(eta_vals), len(lmbd_vals)))
|
||
test_accuracy = np.zeros((len(eta_vals), len(lmbd_vals)))
|
||
|
||
for i in range(len(eta_vals)):
|
||
for j in range(len(lmbd_vals)):
|
||
dnn = DNN_numpy[i][j]
|
||
|
||
train_pred = dnn.predict(X_train)
|
||
test_pred = dnn.predict(X_test)
|
||
|
||
train_accuracy[i][j] = accuracy_score(Y_train, train_pred)
|
||
test_accuracy[i][j] = accuracy_score(Y_test, test_pred)
|
||
|
||
|
||
fig, ax = plt.subplots(figsize = (10, 10))
|
||
sns.heatmap(train_accuracy, annot=True, ax=ax, cmap="viridis")
|
||
ax.set_title("Training Accuracy")
|
||
ax.set_ylabel("$\eta$")
|
||
ax.set_xlabel("$\lambda$")
|
||
plt.show()
|
||
|
||
fig, ax = plt.subplots(figsize = (10, 10))
|
||
sns.heatmap(test_accuracy, annot=True, ax=ax, cmap="viridis")
|
||
ax.set_title("Test Accuracy")
|
||
ax.set_ylabel("$\eta$")
|
||
ax.set_xlabel("$\lambda$")
|
||
plt.show()
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
<section id="scikit-learn-implementation">
|
||
<h2><span class="section-number">14.7. </span>scikit-learn implementation<a class="headerlink" href="#scikit-learn-implementation" title="Link to this heading">#</a></h2>
|
||
<p><strong>scikit-learn</strong> focuses more
|
||
on traditional machine learning methods, such as regression,
|
||
clustering, decision trees, etc. As such, it has only two types of
|
||
neural networks: Multi Layer Perceptron outputting continuous values,
|
||
<em>MPLRegressor</em>, and Multi Layer Perceptron outputting labels,
|
||
<em>MLPClassifier</em>. We will see how simple it is to use these classes.</p>
|
||
<p><strong>scikit-learn</strong> implements a few improvements from our neural network,
|
||
such as early stopping, a varying learning rate, different
|
||
optimization methods, etc. We would therefore expect a better
|
||
performance overall.</p>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>from sklearn.neural_network import MLPClassifier
|
||
# store models for later use
|
||
DNN_scikit = np.zeros((len(eta_vals), len(lmbd_vals)), dtype=object)
|
||
|
||
for i, eta in enumerate(eta_vals):
|
||
for j, lmbd in enumerate(lmbd_vals):
|
||
dnn = MLPClassifier(hidden_layer_sizes=(n_hidden_neurons), activation='logistic',
|
||
alpha=lmbd, learning_rate_init=eta, max_iter=epochs)
|
||
dnn.fit(X_train, Y_train)
|
||
|
||
DNN_scikit[i][j] = dnn
|
||
|
||
print("Learning rate = ", eta)
|
||
print("Lambda = ", lmbd)
|
||
print("Accuracy score on test set: ", dnn.score(X_test, Y_test))
|
||
print()
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
<section id="id1">
|
||
<h2><span class="section-number">14.8. </span>Visualization<a class="headerlink" href="#id1" title="Link to this heading">#</a></h2>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span># optional
|
||
# visual representation of grid search
|
||
# uses seaborn heatmap, could probably do this in matplotlib
|
||
import seaborn as sns
|
||
|
||
sns.set()
|
||
|
||
train_accuracy = np.zeros((len(eta_vals), len(lmbd_vals)))
|
||
test_accuracy = np.zeros((len(eta_vals), len(lmbd_vals)))
|
||
|
||
for i in range(len(eta_vals)):
|
||
for j in range(len(lmbd_vals)):
|
||
dnn = DNN_scikit[i][j]
|
||
|
||
train_pred = dnn.predict(X_train)
|
||
test_pred = dnn.predict(X_test)
|
||
|
||
train_accuracy[i][j] = accuracy_score(Y_train, train_pred)
|
||
test_accuracy[i][j] = accuracy_score(Y_test, test_pred)
|
||
|
||
|
||
fig, ax = plt.subplots(figsize = (10, 10))
|
||
sns.heatmap(train_accuracy, annot=True, ax=ax, cmap="viridis")
|
||
ax.set_title("Training Accuracy")
|
||
ax.set_ylabel("$\eta$")
|
||
ax.set_xlabel("$\lambda$")
|
||
plt.show()
|
||
|
||
fig, ax = plt.subplots(figsize = (10, 10))
|
||
sns.heatmap(test_accuracy, annot=True, ax=ax, cmap="viridis")
|
||
ax.set_title("Test Accuracy")
|
||
ax.set_ylabel("$\eta$")
|
||
ax.set_xlabel("$\lambda$")
|
||
plt.show()
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
<section id="building-neural-networks-in-tensorflow-and-keras">
|
||
<h2><span class="section-number">14.9. </span>Building neural networks in Tensorflow and Keras<a class="headerlink" href="#building-neural-networks-in-tensorflow-and-keras" title="Link to this heading">#</a></h2>
|
||
<p>Now we want to build on the experience gained from our neural network implementation in NumPy and scikit-learn
|
||
and use it to construct a neural network in Tensorflow. Once we have constructed a neural network in NumPy
|
||
and Tensorflow, building one in Keras is really quite trivial, though the performance may suffer.</p>
|
||
<p>In our previous example we used only one hidden layer, and in this we will use two. From this it should be quite
|
||
clear how to build one using an arbitrary number of hidden layers, using data structures such as Python lists or
|
||
NumPy arrays.</p>
|
||
<p>Tensorflow is an open source library machine learning library
|
||
developed by the Google Brain team for internal use. It was released
|
||
under the Apache 2.0 open source license in November 9, 2015.</p>
|
||
<p>Tensorflow is a computational framework that allows you to construct
|
||
machine learning models at different levels of abstraction, from
|
||
high-level, object-oriented APIs like Keras, down to the C++ kernels
|
||
that Tensorflow is built upon. The higher levels of abstraction are
|
||
simpler to use, but less flexible, and our choice of implementation
|
||
should reflect the problems we are trying to solve.</p>
|
||
<p><a class="reference external" href="https://www.tensorflow.org/guide/graphs">Tensorflow uses</a> so-called graphs to represent your computation
|
||
in terms of the dependencies between individual operations, such that you first build a Tensorflow <em>graph</em>
|
||
to represent your model, and then create a Tensorflow <em>session</em> to run the graph.</p>
|
||
<p>In this guide we will analyze the same data as we did in our NumPy and
|
||
scikit-learn tutorial, gathered from the MNIST database of images. We
|
||
will give an introduction to the lower level Python Application
|
||
Program Interfaces (APIs), and see how we use them to build our graph.
|
||
Then we will build (effectively) the same graph in Keras, to see just
|
||
how simple solving a machine learning problem can be.</p>
|
||
<p>To install tensorflow on Unix/Linux systems, use pip as <strong>pip3 install tensorflow</strong>
|
||
and/or if you use <strong>anaconda</strong>, just write (or install from the graphical user interface)
|
||
(current release of CPU-only TensorFlow)</p>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>conda create -n tf tensorflow
|
||
conda activate tf
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<p>To install the current release of GPU TensorFlow</p>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>conda create -n tf-gpu tensorflow-gpu
|
||
conda activate tf-gpu
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<p>Keras is a high level <a class="reference external" href="https://en.wikipedia.org/wiki/Application_programming_interface">neural network</a>
|
||
that supports Tensorflow, CTNK and Theano as backends.<br />
|
||
If you have Anaconda installed you may run the following command</p>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>conda install keras
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<p>You can look up the <a class="reference external" href="https://keras.io/">instructions here</a> for more information.</p>
|
||
<p>We will to a large extent use <strong>keras</strong> in this course.</p>
|
||
<p>Let us look again at the MINST data set.</p>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span># import necessary packages
|
||
import numpy as np
|
||
import matplotlib.pyplot as plt
|
||
import tensorflow as tf
|
||
from sklearn import datasets
|
||
|
||
|
||
# ensure the same random numbers appear every time
|
||
np.random.seed(0)
|
||
|
||
# display images in notebook
|
||
%matplotlib inline
|
||
plt.rcParams['figure.figsize'] = (12,12)
|
||
|
||
|
||
# download MNIST dataset
|
||
digits = datasets.load_digits()
|
||
|
||
# define inputs and labels
|
||
inputs = digits.images
|
||
labels = digits.target
|
||
|
||
print("inputs = (n_inputs, pixel_width, pixel_height) = " + str(inputs.shape))
|
||
print("labels = (n_inputs) = " + str(labels.shape))
|
||
|
||
|
||
# flatten the image
|
||
# the value -1 means dimension is inferred from the remaining dimensions: 8x8 = 64
|
||
n_inputs = len(inputs)
|
||
inputs = inputs.reshape(n_inputs, -1)
|
||
print("X = (n_inputs, n_features) = " + str(inputs.shape))
|
||
|
||
|
||
# choose some random images to display
|
||
indices = np.arange(n_inputs)
|
||
random_indices = np.random.choice(indices, size=5)
|
||
|
||
for i, image in enumerate(digits.images[random_indices]):
|
||
plt.subplot(1, 5, i+1)
|
||
plt.axis('off')
|
||
plt.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest')
|
||
plt.title("Label: %d" % digits.target[random_indices[i]])
|
||
plt.show()
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>from tensorflow.keras.layers import Input
|
||
from tensorflow.keras.models import Sequential #This allows appending layers to existing models
|
||
from tensorflow.keras.layers import Dense #This allows defining the characteristics of a particular layer
|
||
from tensorflow.keras import optimizers #This allows using whichever optimiser we want (sgd,adam,RMSprop)
|
||
from tensorflow.keras import regularizers #This allows using whichever regularizer we want (l1,l2,l1_l2)
|
||
from tensorflow.keras.utils import to_categorical #This allows using categorical cross entropy as the cost function
|
||
|
||
from sklearn.model_selection import train_test_split
|
||
|
||
# one-hot representation of labels
|
||
labels = to_categorical(labels)
|
||
|
||
# split into train and test data
|
||
train_size = 0.8
|
||
test_size = 1 - train_size
|
||
X_train, X_test, Y_train, Y_test = train_test_split(inputs, labels, train_size=train_size,
|
||
test_size=test_size)
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>
|
||
epochs = 100
|
||
batch_size = 100
|
||
n_neurons_layer1 = 100
|
||
n_neurons_layer2 = 50
|
||
n_categories = 10
|
||
eta_vals = np.logspace(-5, 1, 7)
|
||
lmbd_vals = np.logspace(-5, 1, 7)
|
||
def create_neural_network_keras(n_neurons_layer1, n_neurons_layer2, n_categories, eta, lmbd):
|
||
model = Sequential()
|
||
model.add(Dense(n_neurons_layer1, activation='sigmoid', kernel_regularizer=regularizers.l2(lmbd)))
|
||
model.add(Dense(n_neurons_layer2, activation='sigmoid', kernel_regularizer=regularizers.l2(lmbd)))
|
||
model.add(Dense(n_categories, activation='softmax'))
|
||
|
||
sgd = optimizers.SGD(lr=eta)
|
||
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
|
||
|
||
return model
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>DNN_keras = np.zeros((len(eta_vals), len(lmbd_vals)), dtype=object)
|
||
|
||
for i, eta in enumerate(eta_vals):
|
||
for j, lmbd in enumerate(lmbd_vals):
|
||
DNN = create_neural_network_keras(n_neurons_layer1, n_neurons_layer2, n_categories,
|
||
eta=eta, lmbd=lmbd)
|
||
DNN.fit(X_train, Y_train, epochs=epochs, batch_size=batch_size, verbose=0)
|
||
scores = DNN.evaluate(X_test, Y_test)
|
||
|
||
DNN_keras[i][j] = DNN
|
||
|
||
print("Learning rate = ", eta)
|
||
print("Lambda = ", lmbd)
|
||
print("Test accuracy: %.3f" % scores[1])
|
||
print()
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span># optional
|
||
# visual representation of grid search
|
||
# uses seaborn heatmap, could probably do this in matplotlib
|
||
import seaborn as sns
|
||
|
||
sns.set()
|
||
|
||
train_accuracy = np.zeros((len(eta_vals), len(lmbd_vals)))
|
||
test_accuracy = np.zeros((len(eta_vals), len(lmbd_vals)))
|
||
|
||
for i in range(len(eta_vals)):
|
||
for j in range(len(lmbd_vals)):
|
||
DNN = DNN_keras[i][j]
|
||
|
||
train_accuracy[i][j] = DNN.evaluate(X_train, Y_train)[1]
|
||
test_accuracy[i][j] = DNN.evaluate(X_test, Y_test)[1]
|
||
|
||
|
||
fig, ax = plt.subplots(figsize = (10, 10))
|
||
sns.heatmap(train_accuracy, annot=True, ax=ax, cmap="viridis")
|
||
ax.set_title("Training Accuracy")
|
||
ax.set_ylabel("$\eta$")
|
||
ax.set_xlabel("$\lambda$")
|
||
plt.show()
|
||
|
||
fig, ax = plt.subplots(figsize = (10, 10))
|
||
sns.heatmap(test_accuracy, annot=True, ax=ax, cmap="viridis")
|
||
ax.set_title("Test Accuracy")
|
||
ax.set_ylabel("$\eta$")
|
||
ax.set_xlabel("$\lambda$")
|
||
plt.show()
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
<section id="the-breast-cancer-data-now-with-keras">
|
||
<h2><span class="section-number">14.10. </span>The Breast Cancer Data, now with Keras<a class="headerlink" href="#the-breast-cancer-data-now-with-keras" title="Link to this heading">#</a></h2>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>
|
||
import tensorflow as tf
|
||
from tensorflow.keras.layers import Input
|
||
from tensorflow.keras.models import Sequential #This allows appending layers to existing models
|
||
from tensorflow.keras.layers import Dense #This allows defining the characteristics of a particular layer
|
||
from tensorflow.keras import optimizers #This allows using whichever optimiser we want (sgd,adam,RMSprop)
|
||
from tensorflow.keras import regularizers #This allows using whichever regularizer we want (l1,l2,l1_l2)
|
||
from tensorflow.keras.utils import to_categorical #This allows using categorical cross entropy as the cost function
|
||
import numpy as np
|
||
import matplotlib.pyplot as plt
|
||
import seaborn as sns
|
||
from sklearn.model_selection import train_test_split as splitter
|
||
from sklearn.datasets import load_breast_cancer
|
||
import pickle
|
||
import os
|
||
|
||
|
||
"""Load breast cancer dataset"""
|
||
|
||
np.random.seed(0) #create same seed for random number every time
|
||
|
||
cancer=load_breast_cancer() #Download breast cancer dataset
|
||
|
||
inputs=cancer.data #Feature matrix of 569 rows (samples) and 30 columns (parameters)
|
||
outputs=cancer.target #Label array of 569 rows (0 for benign and 1 for malignant)
|
||
labels=cancer.feature_names[0:30]
|
||
|
||
print('The content of the breast cancer dataset is:') #Print information about the datasets
|
||
print(labels)
|
||
print('-------------------------')
|
||
print("inputs = " + str(inputs.shape))
|
||
print("outputs = " + str(outputs.shape))
|
||
print("labels = "+ str(labels.shape))
|
||
|
||
x=inputs #Reassign the Feature and Label matrices to other variables
|
||
y=outputs
|
||
|
||
#%%
|
||
|
||
# Visualisation of dataset (for correlation analysis)
|
||
|
||
plt.figure()
|
||
plt.scatter(x[:,0],x[:,2],s=40,c=y,cmap=plt.cm.Spectral)
|
||
plt.xlabel('Mean radius',fontweight='bold')
|
||
plt.ylabel('Mean perimeter',fontweight='bold')
|
||
plt.show()
|
||
|
||
plt.figure()
|
||
plt.scatter(x[:,5],x[:,6],s=40,c=y, cmap=plt.cm.Spectral)
|
||
plt.xlabel('Mean compactness',fontweight='bold')
|
||
plt.ylabel('Mean concavity',fontweight='bold')
|
||
plt.show()
|
||
|
||
|
||
plt.figure()
|
||
plt.scatter(x[:,0],x[:,1],s=40,c=y,cmap=plt.cm.Spectral)
|
||
plt.xlabel('Mean radius',fontweight='bold')
|
||
plt.ylabel('Mean texture',fontweight='bold')
|
||
plt.show()
|
||
|
||
plt.figure()
|
||
plt.scatter(x[:,2],x[:,1],s=40,c=y,cmap=plt.cm.Spectral)
|
||
plt.xlabel('Mean perimeter',fontweight='bold')
|
||
plt.ylabel('Mean compactness',fontweight='bold')
|
||
plt.show()
|
||
|
||
|
||
# Generate training and testing datasets
|
||
|
||
#Select features relevant to classification (texture,perimeter,compactness and symmetery)
|
||
#and add to input matrix
|
||
|
||
temp1=np.reshape(x[:,1],(len(x[:,1]),1))
|
||
temp2=np.reshape(x[:,2],(len(x[:,2]),1))
|
||
X=np.hstack((temp1,temp2))
|
||
temp=np.reshape(x[:,5],(len(x[:,5]),1))
|
||
X=np.hstack((X,temp))
|
||
temp=np.reshape(x[:,8],(len(x[:,8]),1))
|
||
X=np.hstack((X,temp))
|
||
|
||
X_train,X_test,y_train,y_test=splitter(X,y,test_size=0.1) #Split datasets into training and testing
|
||
|
||
y_train=to_categorical(y_train) #Convert labels to categorical when using categorical cross entropy
|
||
y_test=to_categorical(y_test)
|
||
|
||
del temp1,temp2,temp
|
||
|
||
# %%
|
||
|
||
# Define tunable parameters"
|
||
|
||
eta=np.logspace(-3,-1,3) #Define vector of learning rates (parameter to SGD optimiser)
|
||
lamda=0.01 #Define hyperparameter
|
||
n_layers=2 #Define number of hidden layers in the model
|
||
n_neuron=np.logspace(0,3,4,dtype=int) #Define number of neurons per layer
|
||
epochs=100 #Number of reiterations over the input data
|
||
batch_size=100 #Number of samples per gradient update
|
||
|
||
# %%
|
||
|
||
"""Define function to return Deep Neural Network model"""
|
||
|
||
def NN_model(inputsize,n_layers,n_neuron,eta,lamda):
|
||
model=Sequential()
|
||
for i in range(n_layers): #Run loop to add hidden layers to the model
|
||
if (i==0): #First layer requires input dimensions
|
||
model.add(Dense(n_neuron,activation='relu',kernel_regularizer=regularizers.l2(lamda),input_dim=inputsize))
|
||
else: #Subsequent layers are capable of automatic shape inferencing
|
||
model.add(Dense(n_neuron,activation='relu',kernel_regularizer=regularizers.l2(lamda)))
|
||
model.add(Dense(2,activation='softmax')) #2 outputs - ordered and disordered (softmax for prob)
|
||
sgd=optimizers.SGD(lr=eta)
|
||
model.compile(loss='categorical_crossentropy',optimizer=sgd,metrics=['accuracy'])
|
||
return model
|
||
|
||
|
||
Train_accuracy=np.zeros((len(n_neuron),len(eta))) #Define matrices to store accuracy scores as a function
|
||
Test_accuracy=np.zeros((len(n_neuron),len(eta))) #of learning rate and number of hidden neurons for
|
||
|
||
for i in range(len(n_neuron)): #run loops over hidden neurons and learning rates to calculate
|
||
for j in range(len(eta)): #accuracy scores
|
||
DNN_model=NN_model(X_train.shape[1],n_layers,n_neuron[i],eta[j],lamda)
|
||
DNN_model.fit(X_train,y_train,epochs=epochs,batch_size=batch_size,verbose=1)
|
||
Train_accuracy[i,j]=DNN_model.evaluate(X_train,y_train)[1]
|
||
Test_accuracy[i,j]=DNN_model.evaluate(X_test,y_test)[1]
|
||
|
||
|
||
def plot_data(x,y,data,title=None):
|
||
|
||
# plot results
|
||
fontsize=16
|
||
|
||
|
||
fig = plt.figure()
|
||
ax = fig.add_subplot(111)
|
||
cax = ax.matshow(data, interpolation='nearest', vmin=0, vmax=1)
|
||
|
||
cbar=fig.colorbar(cax)
|
||
cbar.ax.set_ylabel('accuracy (%)',rotation=90,fontsize=fontsize)
|
||
cbar.set_ticks([0,.2,.4,0.6,0.8,1.0])
|
||
cbar.set_ticklabels(['0%','20%','40%','60%','80%','100%'])
|
||
|
||
# put text on matrix elements
|
||
for i, x_val in enumerate(np.arange(len(x))):
|
||
for j, y_val in enumerate(np.arange(len(y))):
|
||
c = "${0:.1f}\\%$".format( 100*data[j,i])
|
||
ax.text(x_val, y_val, c, va='center', ha='center')
|
||
|
||
# convert axis vaues to to string labels
|
||
x=[str(i) for i in x]
|
||
y=[str(i) for i in y]
|
||
|
||
|
||
ax.set_xticklabels(['']+x)
|
||
ax.set_yticklabels(['']+y)
|
||
|
||
ax.set_xlabel('$\\mathrm{learning\\ rate}$',fontsize=fontsize)
|
||
ax.set_ylabel('$\\mathrm{hidden\\ neurons}$',fontsize=fontsize)
|
||
if title is not None:
|
||
ax.set_title(title)
|
||
|
||
plt.tight_layout()
|
||
|
||
plt.show()
|
||
|
||
plot_data(eta,n_neuron,Train_accuracy, 'training')
|
||
plot_data(eta,n_neuron,Test_accuracy, 'testing')
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
<section id="fine-tuning-neural-network-hyperparameters">
|
||
<h2><span class="section-number">14.11. </span>Fine-tuning neural network hyperparameters<a class="headerlink" href="#fine-tuning-neural-network-hyperparameters" title="Link to this heading">#</a></h2>
|
||
<p>The flexibility of neural networks is also one of their main
|
||
drawbacks: there are many hyperparameters to tweak. Not only can you
|
||
use any imaginable network topology (how neurons/nodes are interconnected),
|
||
but even in a simple FFNN you can change the number of layers, the
|
||
number of neurons per layer, the type of activation function to use in
|
||
each layer, the weight initialization logic, the stochastic gradient optmized and much more. How do you
|
||
know what combination of hyperparameters is the best for your task?</p>
|
||
<ul class="simple">
|
||
<li><p>You can use grid search with cross-validation to find the right hyperparameters.</p></li>
|
||
</ul>
|
||
<p>However,since there are many hyperparameters to tune, and since
|
||
training a neural network on a large dataset takes a lot of time, you
|
||
will only be able to explore a tiny part of the hyperparameter space.</p>
|
||
<ul class="simple">
|
||
<li><p>You can use randomized search.</p></li>
|
||
<li><p>Or use tools like <a class="reference external" href="http://oscar.calldesk.ai/">Oscar</a>, which implements more complex algorithms to help you find a good set of hyperparameters quickly.</p></li>
|
||
</ul>
|
||
<p>For many problems you can start with just one or two hidden layers and it will work just fine.
|
||
For the MNIST data set you ca easily get a high accuracy using just one hidden layer with a
|
||
few hundred neurons.
|
||
You can reach for this data set above 98% accuracy using two hidden layers with the same total amount of
|
||
neurons, in roughly the same amount of training time.</p>
|
||
<p>For more complex problems, you can gradually
|
||
ramp up the number of hidden layers, until you start overfitting the training set. Very complex tasks, such
|
||
as large image classification or speech recognition, typically require networks with dozens of layers
|
||
and they need a huge amount
|
||
of training data. However, you will rarely have to train such networks from scratch: it is much more
|
||
common to reuse parts of a pretrained state-of-the-art network that performs a similar task.</p>
|
||
</section>
|
||
<section id="which-activation-function-should-i-use">
|
||
<h2><span class="section-number">14.12. </span>Which activation function should I use?<a class="headerlink" href="#which-activation-function-should-i-use" title="Link to this heading">#</a></h2>
|
||
<p>The Back propagation algorithm we derived above works by going from
|
||
the output layer to the input layer, propagating the error gradient on
|
||
the way. Once the algorithm has computed the gradient of the cost
|
||
function with regards to each parameter in the network, it uses these
|
||
gradients to update each parameter with a Gradient Descent (GD) step.</p>
|
||
<p>Unfortunately for us, the gradients often get smaller and smaller as the
|
||
algorithm progresses down to the first hidden layers. As a result, the
|
||
GD update leaves the lower layer connection weights
|
||
virtually unchanged, and training never converges to a good
|
||
solution. This is known in the literature as
|
||
<strong>the vanishing gradients problem</strong>.</p>
|
||
<p>In other cases, the opposite can happen, namely the the gradients can grow bigger and
|
||
bigger. The result is that many of the layers get large updates of the
|
||
weights the
|
||
algorithm diverges. This is the <strong>exploding gradients problem</strong>, which is
|
||
mostly encountered in recurrent neural networks. More generally, deep
|
||
neural networks suffer from unstable gradients, different layers may
|
||
learn at widely different speeds</p>
|
||
<p>Although this unfortunate behavior has been empirically observed for
|
||
quite a while (it was one of the reasons why deep neural networks were
|
||
mostly abandoned for a long time), it is only around 2010 that
|
||
significant progress was made in understanding it.</p>
|
||
<p>A paper titled <a class="reference external" href="http://proceedings.mlr.press/v9/glorot10a.html">Understanding the Difficulty of Training Deep
|
||
Feedforward Neural Networks by Xavier Glorot and Yoshua Bengio</a> found that
|
||
the problems with the popular logistic
|
||
sigmoid activation function and the weight initialization technique
|
||
that was most popular at the time, namely random initialization using
|
||
a normal distribution with a mean of 0 and a standard deviation of
|
||
1.</p>
|
||
<p>They showed that with this activation function and this
|
||
initialization scheme, the variance of the outputs of each layer is
|
||
much greater than the variance of its inputs. Going forward in the
|
||
network, the variance keeps increasing after each layer until the
|
||
activation function saturates at the top layers. This is actually made
|
||
worse by the fact that the logistic function has a mean of 0.5, not 0
|
||
(the hyperbolic tangent function has a mean of 0 and behaves slightly
|
||
better than the logistic function in deep networks).</p>
|
||
<p>Looking at the logistic activation function, when inputs become large
|
||
(negative or positive), the function saturates at 0 or 1, with a
|
||
derivative extremely close to 0. Thus when backpropagation kicks in,
|
||
it has virtually no gradient to propagate back through the network,
|
||
and what little gradient exists keeps getting diluted as
|
||
backpropagation progresses down through the top layers, so there is
|
||
really nothing left for the lower layers.</p>
|
||
<p>In their paper, Glorot and Bengio propose a way to significantly
|
||
alleviate this problem. We need the signal to flow properly in both
|
||
directions: in the forward direction when making predictions, and in
|
||
the reverse direction when backpropagating gradients. We don’t want
|
||
the signal to die out, nor do we want it to explode and saturate. For
|
||
the signal to flow properly, the authors argue that we need the
|
||
variance of the outputs of each layer to be equal to the variance of
|
||
its inputs, and we also need the gradients to have equal variance
|
||
before and after flowing through a layer in the reverse direction.</p>
|
||
<p>One of the insights in the 2010 paper by Glorot and Bengio was that
|
||
the vanishing/exploding gradients problems were in part due to a poor
|
||
choice of activation function. Until then most people had assumed that
|
||
if Nature had chosen to use roughly sigmoid activation functions in
|
||
biological neurons, they must be an excellent choice. But it turns out
|
||
that other activation functions behave much better in deep neural
|
||
networks, in particular the ReLU activation function, mostly because
|
||
it does not saturate for positive values (and also because it is quite
|
||
fast to compute).</p>
|
||
</section>
|
||
<section id="the-relu-function-family">
|
||
<h2><span class="section-number">14.13. </span>The RELU function family<a class="headerlink" href="#the-relu-function-family" title="Link to this heading">#</a></h2>
|
||
<p>The ReLU activation function suffers from a problem known as the dying
|
||
ReLUs: during training, some neurons effectively die, meaning they
|
||
stop outputting anything other than 0.</p>
|
||
<p>In some cases, you may find that half of your network’s neurons are
|
||
dead, especially if you used a large learning rate. During training,
|
||
if a neuron’s weights get updated such that the weighted sum of the
|
||
neuron’s inputs is negative, it will start outputting 0. When this
|
||
happen, the neuron is unlikely to come back to life since the gradient
|
||
of the ReLU function is 0 when its input is negative.</p>
|
||
<p>To solve this problem, nowadays practitioners use a variant of the ReLU
|
||
function, such as the leaky ReLU discussed above or the so-called
|
||
exponential linear unit (ELU) function</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[\begin{split}
|
||
ELU(z) = \left\{\begin{array}{cc} \alpha\left( \exp{(z)}-1\right) & z < 0,\\ z & z \ge 0.\end{array}\right.
|
||
\end{split}\]</div>
|
||
<p>In general it seems that the ELU activation function is better than
|
||
the leaky ReLU function (and its variants), which is better than
|
||
ReLU. ReLU performs better than <span class="math notranslate nohighlight">\(\tanh\)</span> which in turn performs better
|
||
than the logistic function.</p>
|
||
<p>If runtime
|
||
performance is an issue, then you may opt for the leaky ReLU function over the
|
||
ELU function If you don’t
|
||
want to tweak yet another hyperparameter, you may just use the default
|
||
<span class="math notranslate nohighlight">\(\alpha\)</span> of <span class="math notranslate nohighlight">\(0.01\)</span> for the leaky ReLU, and <span class="math notranslate nohighlight">\(1\)</span> for ELU. If you have
|
||
spare time and computing power, you can use cross-validation or
|
||
bootstrap to evaluate other activation functions.</p>
|
||
<p>In most cases you can use the ReLU activation function in the hidden layers (or one of its variants).</p>
|
||
<p>It is a bit faster to compute than other activation functions, and the gradient descent optimization does in general not get stuck.</p>
|
||
<p><strong>For the output layer:</strong></p>
|
||
<ul class="simple">
|
||
<li><p>For classification the softmax activation function is generally a good choice for classification tasks (when the classes are mutually exclusive).</p></li>
|
||
<li><p>For regression tasks, you can simply use no activation function at all.</p></li>
|
||
</ul>
|
||
</section>
|
||
<section id="batch-normalization">
|
||
<h2><span class="section-number">14.14. </span>Batch Normalization<a class="headerlink" href="#batch-normalization" title="Link to this heading">#</a></h2>
|
||
<p>Batch Normalization
|
||
aims to address the vanishing/exploding gradients problems, and more generally the problem that the
|
||
distribution of each layer’s inputs changes during training, as the parameters of the previous layers change.</p>
|
||
<p>The technique consists of adding an operation in the model just before the activation function of each
|
||
layer, simply zero-centering and normalizing the inputs, then scaling and shifting the result using two new
|
||
parameters per layer (one for scaling, the other for shifting). In other words, this operation lets the model
|
||
learn the optimal scale and mean of the inputs for each layer.
|
||
In order to zero-center and normalize the inputs, the algorithm needs to estimate the inputs’ mean and
|
||
standard deviation. It does so by evaluating the mean and standard deviation of the inputs over the current
|
||
mini-batch, from this the name batch normalization.</p>
|
||
</section>
|
||
<section id="dropout">
|
||
<h2><span class="section-number">14.15. </span>Dropout<a class="headerlink" href="#dropout" title="Link to this heading">#</a></h2>
|
||
<p>It is a fairly simple algorithm: at every training step, every neuron (including the input neurons but
|
||
excluding the output neurons) has a probability <span class="math notranslate nohighlight">\(p\)</span> of being temporarily dropped out, meaning it will be
|
||
entirely ignored during this training step, but it may be active during the next step.</p>
|
||
<p>The
|
||
hyperparameter <span class="math notranslate nohighlight">\(p\)</span> is called the dropout rate, and it is typically set to 50%. After training, the neurons are not dropped anymore.
|
||
It is viewed as one of the most popular regularization techniques.</p>
|
||
</section>
|
||
<section id="gradient-clipping">
|
||
<h2><span class="section-number">14.16. </span>Gradient Clipping<a class="headerlink" href="#gradient-clipping" title="Link to this heading">#</a></h2>
|
||
<p>A popular technique to lessen the exploding gradients problem is to simply clip the gradients during
|
||
backpropagation so that they never exceed some threshold (this is mostly useful for recurrent neural
|
||
networks).</p>
|
||
<p>This technique is called Gradient Clipping.</p>
|
||
<p>In general however, Batch
|
||
Normalization is preferred.</p>
|
||
</section>
|
||
<section id="a-top-down-perspective-on-neural-networks">
|
||
<h2><span class="section-number">14.17. </span>A top-down perspective on Neural networks<a class="headerlink" href="#a-top-down-perspective-on-neural-networks" title="Link to this heading">#</a></h2>
|
||
<p>The first thing we would like to do is divide the data into two or three
|
||
parts. A training set, a validation or dev (development) set, and a
|
||
test set. The test set is the data on which we want to make
|
||
predictions. The dev set is a subset of the training data we use to
|
||
check how well we are doing out-of-sample, after training the model on
|
||
the training dataset. We use the validation error as a proxy for the
|
||
test error in order to make tweaks to our model. It is crucial that we
|
||
do not use any of the test data to train the algorithm. This is a
|
||
cardinal sin in ML. Then:</p>
|
||
<ul class="simple">
|
||
<li><p>Estimate optimal error rate</p></li>
|
||
<li><p>Minimize underfitting (bias) on training data set.</p></li>
|
||
<li><p>Make sure you are not overfitting.</p></li>
|
||
</ul>
|
||
<p>If the validation and test sets are drawn from the same distributions,
|
||
then a good performance on the validation set should lead to similarly
|
||
good performance on the test set.</p>
|
||
<p>However, sometimes
|
||
the training data and test data differ in subtle ways because, for
|
||
example, they are collected using slightly different methods, or
|
||
because it is cheaper to collect data in one way versus another. In
|
||
this case, there can be a mismatch between the training and test
|
||
data. This can lead to the neural network overfitting these small
|
||
differences between the test and training sets, and a poor performance
|
||
on the test set despite having a good performance on the validation
|
||
set. To rectify this, Andrew Ng suggests making two validation or dev
|
||
sets, one constructed from the training data and one constructed from
|
||
the test data. The difference between the performance of the algorithm
|
||
on these two validation sets quantifies the train-test mismatch. This
|
||
can serve as another important diagnostic when using DNNs for
|
||
supervised learning.</p>
|
||
</section>
|
||
<section id="limitations-of-supervised-learning-with-deep-networks">
|
||
<h2><span class="section-number">14.18. </span>Limitations of supervised learning with deep networks<a class="headerlink" href="#limitations-of-supervised-learning-with-deep-networks" title="Link to this heading">#</a></h2>
|
||
<p>Like all statistical methods, supervised learning using neural
|
||
networks has important limitations. This is especially important when
|
||
one seeks to apply these methods, especially to physics problems. Like
|
||
all tools, DNNs are not a universal solution. Often, the same or
|
||
better performance on a task can be achieved by using a few
|
||
hand-engineered features (or even a collection of random
|
||
features).</p>
|
||
<p>Here we list some of the important limitations of supervised neural network based models.</p>
|
||
<ul class="simple">
|
||
<li><p><strong>Need labeled data</strong>. All supervised learning methods, DNNs for supervised learning require labeled data. Often, labeled data is harder to acquire than unlabeled data (e.g. one must pay for human experts to label images).</p></li>
|
||
<li><p><strong>Supervised neural networks are extremely data intensive.</strong> DNNs are data hungry. They perform best when data is plentiful. This is doubly so for supervised methods where the data must also be labeled. The utility of DNNs is extremely limited if data is hard to acquire or the datasets are small (hundreds to a few thousand samples). In this case, the performance of other methods that utilize hand-engineered features can exceed that of DNNs.</p></li>
|
||
<li><p><strong>Homogeneous data.</strong> Almost all DNNs deal with homogeneous data of one type. It is very hard to design architectures that mix and match data types (i.e. some continuous variables, some discrete variables, some time series). In applications beyond images, video, and language, this is often what is required. In contrast, ensemble models like random forests or gradient-boosted trees have no difficulty handling mixed data types.</p></li>
|
||
<li><p><strong>Many problems are not about prediction.</strong> In natural science we are often interested in learning something about the underlying distribution that generates the data. In this case, it is often difficult to cast these ideas in a supervised learning setting. While the problems are related, it is possible to make good predictions with a <em>wrong</em> model. The model might or might not be useful for understanding the underlying science.</p></li>
|
||
</ul>
|
||
<p>Some of these remarks are particular to DNNs, others are shared by all supervised learning methods. This motivates the use of unsupervised methods which in part circumvent these problems.</p>
|
||
</section>
|
||
</section>
|
||
|
||
<script type="text/x-thebe-config">
|
||
{
|
||
requestKernel: true,
|
||
binderOptions: {
|
||
repo: "binder-examples/jupyter-stacks-datascience",
|
||
ref: "master",
|
||
},
|
||
codeMirrorConfig: {
|
||
theme: "abcdef",
|
||
mode: "python"
|
||
},
|
||
kernelOptions: {
|
||
name: "python3",
|
||
path: "./."
|
||
},
|
||
predefinedOutput: true
|
||
}
|
||
</script>
|
||
<script>kernelName = 'python3'</script>
|
||
|
||
</article>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<footer class="prev-next-footer d-print-none">
|
||
|
||
<div class="prev-next-area">
|
||
<a class="left-prev"
|
||
href="chapter9.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"><span class="section-number">13. </span>Neural networks</p>
|
||
</div>
|
||
</a>
|
||
<a class="right-next"
|
||
href="chapter11.html"
|
||
title="next page">
|
||
<div class="prev-next-info">
|
||
<p class="prev-next-subtitle">next</p>
|
||
<p class="prev-next-title"><span class="section-number">15. </span>Solving Differential Equations with Deep Learning</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="#defining-the-cost-function">14.1. Defining the cost function</a><ul class="nav section-nav flex-column">
|
||
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#example-binary-classification-problem">14.1.1. Example: binary classification problem</a></li>
|
||
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#the-softmax-function">14.1.2. The Softmax function</a></li>
|
||
</ul>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#developing-a-code-for-doing-neural-networks-with-back-propagation">14.2. Developing a code for doing neural networks with back propagation</a><ul class="nav section-nav flex-column">
|
||
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#collect-and-pre-process-data">14.2.1. Collect and pre-process data</a></li>
|
||
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#train-and-test-datasets">14.2.2. Train and test datasets</a></li>
|
||
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#define-model-and-architecture">14.2.3. Define model and architecture</a></li>
|
||
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#layers">14.2.4. Layers</a></li>
|
||
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#feed-forward-pass">14.2.5. Feed-forward pass</a></li>
|
||
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#choose-cost-function-and-optimizer">14.2.6. Choose cost function and optimizer</a></li>
|
||
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#optimizing-the-cost-function">14.2.7. Optimizing the cost function</a></li>
|
||
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#regularization">14.2.8. Regularization</a></li>
|
||
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#matrix-multiplication">14.2.9. Matrix multiplication</a></li>
|
||
</ul>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#improving-performance">14.3. Improving performance</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#evaluate-model-performance-on-test-data">14.4. Evaluate model performance on test data</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#adjust-hyperparameters">14.5. Adjust hyperparameters</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#visualization">14.6. Visualization</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#scikit-learn-implementation">14.7. scikit-learn implementation</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#id1">14.8. Visualization</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#building-neural-networks-in-tensorflow-and-keras">14.9. Building neural networks in Tensorflow and Keras</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#the-breast-cancer-data-now-with-keras">14.10. The Breast Cancer Data, now with Keras</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#fine-tuning-neural-network-hyperparameters">14.11. Fine-tuning neural network hyperparameters</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#which-activation-function-should-i-use">14.12. Which activation function should I use?</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#the-relu-function-family">14.13. The RELU function family</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#batch-normalization">14.14. Batch Normalization</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#dropout">14.15. Dropout</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#gradient-clipping">14.16. Gradient Clipping</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#a-top-down-perspective-on-neural-networks">14.17. A top-down perspective on Neural networks</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#limitations-of-supervised-learning-with-deep-networks">14.18. Limitations of supervised learning with deep networks</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> |