® D E E P L E A R N I N G C R A S H C O U R S E A H A N D S - O N , P R O J E C T - B A S E D I N T R O D U C T I O N T O A R T I F I C I A L I N T E L L I G E N C E G . V O L P E , B . M I D T V E D T , J . P I N E D A , H . K . M O B E R G , H . B A C H I M A N C H I , J . B . P E R E I R A , & C . M A N Z O
(This page has no text content)
DEEP LEARNING CRASH COURSE
(This page has no text content)
DEEP LEARNING CRASH COURSE AHands-On, Project-Based Introduction to Artificial Intelligence by Giovanni Volpe, Benjamin Midtvedt, Jesús Pineda, Henrik Klein Moberg, Harshith Bachimanchi, Joana B. Pereira, and Carlo Manzo ® San Francisco
DEEP LEARNING CRASH COURSE. Copyright © 2026 by Giovanni Volpe, Benjamin Midtvedt, Jesús Pineda, Henrik Klein Moberg, Harshith Bachimanchi, Joana B. Pereira, and Carlo Manzo. All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. First printing 29 28 27 26 25 1 2 3 4 5 ISBN-13: 978-1-7185-0392-2 (print) ISBN-13: 978-1-7185-0393-9 (ebook) ® Published by No Starch Press®, Inc. 245 8th Street, San Francisco, CA 94103 phone: +1.415.863.9900 www.nostarch.com; info@nostarch.com Publisher: William Pollock Managing Editor: Jill Franklin Production Manager: Sabrina Plomitallo-González Production Editor: Miles Bond Developmental Editors: Annie Choi and Ryan Frankel Cover Illustrator: Garry Booth Interior Design: Octopod Studios Technical Reviewer: Wylie Ahmed Copyeditor: Sharon Wilkey Proofreader: Audrey Doyle Library of Congress Control Number: 2025027767 For customer service inquiries, please contact info@nostarch.com. For information on distribution, bulk sales, corporate sales, or translations: sales@nostarch.com. For permission to translate this work: rights@nostarch.com. To report counterfeit copies or piracy: counterfeit@nostarch.com. The authorized representative in the EU for product safety and compliance is EU Compliance Partner, Pärnu mnt. 139b-14, 11317 Tallinn, Estonia, hello@eucompliancepartner.com, +3375690241. No Starch Press and the No Starch Press iron logo are registered trademarks of No Starch Press, Inc. Other product and company namesmentioned hereinmay be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been taken in the preparation of this work, neither the authors nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it. [E]
About the Authors Giovanni Volpe is a professor in the physics department of the University of Gothenburg, Sweden, and head of the Soft Matter Lab. His research interests include deep learning, brain connectivity, statistical mechanics, and soft matter. He has authored more than 200 articles and reviews on these topics and has co-authored two books. He has also developed several software packages for microscopy (DeepTrack), deep learning (Deeplay), and brain connectivity (BRAPH). Benjamin Midtvedt earned his PhD at the University of Gothenburg with a focus on enhancing microscopy through deep learning. His research has centered on the development of accessible and practical AI optimized to the needs of the user. He has also been the lead developer of several Python- based open source deep learning frameworks, including DeepTrack and Deeplay. Jesús Pineda earned his PhD in physics at the University of Gothenburg with a focus on the intersection of deep learning and computer vision. He has co-authored several articles in high-impact journals on the application of deep learning to glean insights from microscopy data. He is also a core developer of the deep learning software packages DeepTrack and Deeplay. Henrik Klein Moberg received his PhD from Chalmers University of Tech- nology, Sweden, where he specialized in the integration of AI with the phys- ical sciences. His research focused on applying deep learning techniques to nanofluidic microscopy and nanophotonics, aiming to enhance the preci- sion and efficiency of these technologies. He has also organized and spoken at numerous conferences related to AI and scientific data analysis. Harshith Bachimanchi completed his PhD at the University of Gothenburg on the integration of holographic microscopy and deep learning to better understand marine microorganisms. He has applied deep learning tech- niques and experimental optics to track both biological and synthetic par- ticles, enhancing our understanding of these complex systems. He has also developed simulations demonstrating the applications of deep learning in microscopy. Joana B. Pereira is an associate professor at Karolinska Institute, Sweden, where she focuses on investigating new biomarkers for neurodegenerative disorders, in particular Alzheimer’s disease. Since 2020, she has organized an annual interdisciplinary conference called Emerging Topics in Artificial Intelligence in San Diego, California. She is the scientific coordinator for NeurotechEU and winner of the 2021 de Leon Prize for best neuroimaging article on Alzheimer’s disease.
Carlo Manzo is an associate professor at the University of Vic, Spain, where he leads the Quantitative Bioimaging Lab. His research is dedicated to the analysis of biophysical processes, merging deep learning techniques with state-of-the-art imaging technologies to achieve single-molecule sensitivity. He is the founder of the Anomalous Diffusion Challenge and winner of the 2017 E. Pérez Payá Prize from the Sociedad de Biofísica de España. About the Technical Reviewer Wylie Ahmed is a CNRS researcher at the Laboratoire de Physique Théorique and the Centre de Biologie Intégrative at the University of Toulouse, France, where he leads the Soft, Living, and Active Matter Lab. His research focuses on biological physics, statistical mechanics, and nonequilibrium systems, us- ing a combined experimental and theoretical approach. In 2023, he received the Irwin Oppenheim Award from the American Physical Society.
BRIEF CONTENTS Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Chapter 1: Building and Training Your First Neural Network . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Chapter 2: Capturing Trends and Recognizing Patterns with Dense Neural Networks . . . . . . 51 Chapter 3: Processing Images with Convolutional Neural Networks . . . . . . . . . . . . . . . . . . . . . . 95 Chapter 4: Enhancing, Generating, and Analyzing Data with Autoencoders . . . . . . . . . . . . . . 167 Chapter 5: Segmenting and Analyzing Images with U-Nets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 Chapter 6: Training Neural Networks with Self-Supervised Learning . . . . . . . . . . . . . . . . . . . . . 251 Chapter 7: Processing Time Series and Language with Recurrent Neural Networks . . . . . . . . 279 Chapter 8: Processing Language and Classifying Images with Attention and Transformers . . 325 Chapter 9: Creating and Transforming Images with Generative Adversarial Networks . . . . . 375 Chapter 10: Implementing Generative AI with Diffusion Models . . . . . . . . . . . . . . . . . . . . . . . . . 425 Chapter 11: Modeling Molecules and Complex Systems with Graph Neural Networks . . . . . 475 Chapter 12: Continuously Improving Performance with Active Learning . . . . . . . . . . . . . . . . . . . 529 Chapter 13: Mastering Decision-Making with Deep Reinforcement Learning. . . . . . . . . . . . . . . 557 Chapter 14: Predicting Chaos with Reservoir Computing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 601 Conclusion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623 Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
(This page has no text content)
CONTENTS IN DETA IL ACKNOWLEDGMENTS xix INTRODUCTION xxi Who Should Read This Book? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii This Book’s Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii Setting Up Your Learning Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiv Obtaining the Data and Code Used in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv What’s in This Book? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxviii Time to Start Your Journey . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxviii 1 BUILDING AND TRAINING YOUR FIRST NEURAL NETWORK 1 Classifying Data with a Single Neuron . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Classifying 1D Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Classifying 2D Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Adding a Bias as Another Dimension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Using Weight Regularization to Stabilize the Training . . . . . . . . . . . . . . . . . . . . . . 18 Using Different Activation Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Classifying Data with a Two-Layer Neural Network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Implementing a Dense Neural Network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Training with Error Backpropagation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Attempting an Impossible Classification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Classifying Data with a Three-Layer Neural Network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Project 1A: Classifying Handwritten Digits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Using the MNIST Digit Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Implementing the Neural Network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Training and Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Making Improvements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Performing Failure Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Seminal Works and Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 2 CAPTURING TRENDS AND RECOGNIZING PATTERNS WITH DENSE NEURAL NETWORKS 51 Regressing Data with a Single Neuron . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Regressing 1D Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Regressing 2D Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Regressing Data with a Two-Layer Neural Network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Implementing a Dense Neural Network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Training with Error Backpropagation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Training Using Batches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Plotting Predictions vs. Ground Truth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Training with Mini-Batches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Tracking Training Metrics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Randomizing the Batches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Dividing Data into Multiple Datasets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Splitting the Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Training and Validating the Neural Network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Project 2A: Emulating a Physical System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Loading the Theoretical and Geometrical–Optics Optical Forces . . . . . . . . . . . . . 80 Loading and Preparing the Training Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Implementing the Neural Network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Implementing Data Loaders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Training the Neural Network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Evaluating Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Seminal Works and Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 3 PROCESSING IMAGES WITH CONVOLUTIONAL NEURAL NETWORKS 95 Understanding Convolutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Convolving 1D Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Convolving 2D Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 Using Convolutions in a Neural Network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 Implementing Neural Networks in PyTorch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 Defining Convolutional Layers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Adding ReLU Activation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 Adding Pooling Layers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 Using Upsampling Layers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 Transforming Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 Using Dense Layers to Classify Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 Project 3A: Classifying Malaria-Infected Blood Smears . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 Loading the Malaria Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Classifying with Dense Neural Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Classifying with Convolutional Neural Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 Checking the Values of the Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 Visualizing Activations of Convolutional Layers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 Visualizing Heatmaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 Project 3B: Localizing Microscopic Particles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 Loading the Videos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 Manually Annotating the Videos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 Implementing a Convolutional Neural Network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 x Contents in Detail
Training with Annotated Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 Simulating the Training Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 Training with Simulated Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 Project 3C: Creating DeepDreams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 Loading an Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 Loading a Pretrained Neural Network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 Implementing the DeepDreams Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Using Multiple Layers at Once . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 Project 3D: Transferring the Style of Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 Loading the Content and Style Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 Loading a Pretrained Neural Network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 Implementing Style Transfer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 Creating an Image in Gaudí’s Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 Seminal Works and Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 4 ENHANCING, GENERATING, AND ANALYZING DATA WITH AUTOENCODERS 167 Understanding Encoder-Decoders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 Implementing a Denoising Encoder-Decoder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 Generating the Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 Creating a Simulated Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 Defining and Training the Encoder-Decoder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 Denoising the Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 Checking for Absence of Mode Collapse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 Checking Generalization Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 Retraining . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 Project 4A: Generating Images with Variational Autoencoders . . . . . . . . . . . . . . . . . . . . . . . 179 Understanding Variational Autoencoders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 Loading the MNIST Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 Training the Variational Autoencoder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 Generating Images with the Decoder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 Clustering Images with the Encoder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 Project 4B: Morphing Images with Wasserstein Autoencoders . . . . . . . . . . . . . . . . . . . . . . . 188 Understanding Wasserstein Autoencoders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 Loading the Fashion-MNIST Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 Training the Wasserstein Autoencoder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 Reconstructing the Fashion-MNIST Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 Creating New Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 Morphing Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 Project 4C: Detecting Anomalies in ECG Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 Understanding Anomaly Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 Loading the ECG Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 Defining and Training the Autoencoder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 Testing with Normal and Anomalous ECGs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 Detecting Anomalies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 Contents in Detail xi
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 Seminal Works and Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 5 SEGMENTING AND ANALYZING IMAGES WITH U-NETS 209 Introducing U-Nets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 Understanding Semantic Segmentation with U-Nets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 Segmenting Images of Biological Tissues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 Loading the Segmented Tissue Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 Creating the Data Pipelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 Defining and Training the U-Net . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 Plotting the Training Metrics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 Preventing Overfitting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220 Evaluating the Trained U-Net . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 Project 5A: Detecting Quantum Dots in Fluorescence Images . . . . . . . . . . . . . . . . . . . . . . . . 224 Loading the Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224 Simulating Quantum Dots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225 Defining and Training the U-Net . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 Evaluating the Trained U-Net . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 Project 5B: Counting Cells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234 Loading the Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234 Creating a Pipeline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 Simulating the Cell Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237 Implementing and Training the U-Net . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 Testing the Trained U-Net . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 Seminal Works and Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 6 TRAINING NEURAL NETWORKS WITH SELF-SUPERVISED LEARNING 251 Understanding Self-Supervised Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 Self-Supervised Contrastive Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 Self-Supervised Non-Contrastive Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253 Self-Supervised Geometric Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254 Determining the Position of a Particle in an Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 Creating the Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 Learning from Translations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 Learning from Flipping the Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262 Improving Performance with LodeSTAR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 Project 6A: Localizing Mouse Stem Cells with LodeSTAR . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268 Using the Cell Tracking Challenge Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268 Preparing the Training Crop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271 Creating the Training Pipeline and Data Loader . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272 Training the Neural Network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 Evaluating Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274 xii Contents in Detail
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 Seminal Works and Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278 7 PROCESSING TIME SERIES AND LANGUAGE WITH RECURRENT NEURAL NETWORKS 279 Understanding Recurrent Neural Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 Using a Comb Filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280 Understanding a Simple Recurrent Neural Network . . . . . . . . . . . . . . . . . . . . . . . . . 281 Predicting Temperature with Recurrent Neural Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282 Loading the Jena Climate Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282 Preprocessing the Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284 Implementing a Commonsense Benchmark . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 Determining the Computational Device . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 Predicting with a Simple Recurrent Neural Network . . . . . . . . . . . . . . . . . . . . . . . . . 288 Stacking Multiple Recurrent Layers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293 Using Gated Recurrent Units . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294 Using Long Short-Term Memory Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296 Project 7A: Translating with Recurrent Neural Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299 Preparing the Bilingual Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299 Defining the Sequence-to-Sequence Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308 Loading Pretrained Embeddings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316 Training the Sequence-to-Sequence Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318 Testing the Model Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319 Evaluating the Model with the BLEU Score . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322 Seminal Works and Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323 8 PROCESSING LANGUAGE AND CLASSIFYING IMAGES WITH ATTENTION AND TRANSFORMERS 325 Understanding Attention . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326 Implementing Dot-Product Attention . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326 Visualizing Attention . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331 Making the Attention Mechanism Trainable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333 Implementing Other Attention Mechanisms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335 Project 8A: Using Attention to Improve Language Translation. . . . . . . . . . . . . . . . . . . . . . . . 336 Incorporating Attention . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337 Training and Testing the Seq2Seq Model with Attention . . . . . . . . . . . . . . . . . . . . . 339 Interpreting the Attention Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340 Project 8B: Performing Sentiment Analysis with a Transformer . . . . . . . . . . . . . . . . . . . . . . . 342 Breaking Down Multi-Head Attention . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342 Understanding the Transformer Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344 Loading the IMDb Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345 Preprocessing the Reviews . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347 Contents in Detail xiii
Defining the Data Loaders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349 Building an Encoder-Only Transformer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350 Training the Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356 Evaluating the Trained Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356 Project 8C: Classifying Images with a Vision Transformer . . . . . . . . . . . . . . . . . . . . . . . . . . . 358 Using the CIFAR-10 Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358 Data Preprocessing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360 Building the ViT Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361 Training and Evaluating the ViT Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363 Improving the ViT Model with CutMix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364 Using a Pretrained ViT Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372 Seminal Works and Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373 9 CREATING AND TRANSFORMING IMAGES WITH GENERATIVE ADVERSARIAL NETWORKS 375 Understanding GANs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376 Discriminating Between Real and Fake Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377 Generating Realistic Fake Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378 Training a GAN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379 Generating Digits with a GAN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379 Loading the MNIST Dataset with PyTorch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380 Defining the Generator and Discriminator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381 Training the GAN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382 Plotting the Intermediate Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386 Plotting the Training Losses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388 Project 9A: Generating Digits with a Conditional GAN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389 Defining the Conditional Generator and Discriminator . . . . . . . . . . . . . . . . . . . . . . 390 Training the Conditional GAN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392 Plotting the Generated Digits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393 Project 9B: Virtually Staining a Biological Tissue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394 Downloading the Human Motor Neurons Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . 394 Creating a Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395 Instantiating the Generator and Discriminator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400 Compiling the Conditional GAN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402 Training the Conditional GAN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403 Evaluating the Trained Conditional GAN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408 Project 9C: Converting Between Holographic and Bright-Field Microscopy Images . . . . . . 409 Understanding CycleGANs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410 Using the Holo2Bright Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411 Instantiating the CycleGAN Generators and Discriminators . . . . . . . . . . . . . . . . . . 414 Training the CycleGAN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416 Evaluating the Trained CycleGAN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423 Seminal Works and Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423 xiv Contents in Detail
10 IMPLEMENTING GENERATIVE AI WITH DIFFUSION MODELS 425 Understanding Diffusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426 Breaking Down Denoising Diffusion Probabilistic Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427 Modeling the Forward Diffusion Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428 Deriving the Fast Forward Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429 Modeling the Reverse Diffusion Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430 Generating Digits with a Diffusion Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432 Loading the MNIST Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432 Implementing the Forward Diffusion Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433 Applying the Reverse Diffusion Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435 Defining the Positional Encoding Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436 Instantiating the Attention U-Net . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438 Training the Diffusion Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439 Sampling Images from Fixed Noise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444 Project 10A: Generating Bespoke Digits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446 Guiding the Diffusion Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446 Defining the Conditional Attention U-Net . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447 Training the Conditional Diffusion Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448 Plotting the Intermediate Training Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448 Project 10B: Generating Images from Text Prompts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450 Providing a Conditional Text Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450 Defining the Conditional Attention U-Net . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457 Training the Conditional Diffusion Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458 Plotting the Intermediate Training Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459 Training with the CLIP Tokenizer and CLIP Text Encoder . . . . . . . . . . . . . . . . . . . . . 460 Project 10C: Generating Super-Resolution Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463 Downloading the BioSR Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463 Managing the Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464 Preprocessing the Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465 Creating the Training and Test Datasets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465 Adapting the Diffusion Process for Super-Resolution . . . . . . . . . . . . . . . . . . . . . . . . . 466 Defining the Conditional Attention U-Net . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467 Training the Conditional Diffusion Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471 Seminal Works and Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471 11 MODELING MOLECULES AND COMPLEX SYSTEMS WITH GRAPH NEURAL NETWORKS 475 Understanding Graph Convolutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476 Predicting Molecular Properties with Graph Convolutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479 Implementing a Graph Convolution Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480 Representing a Molecule as a Graph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482 Using the ZINC Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483 Applying a Graph Convolutional Network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486 Contents in Detail xv
Training the Graph Convolutional Network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489 Evaluating the Graph Convolutional Network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491 Predicting Molecular Properties with Message Passing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491 Implementing a Message-Passing Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492 Implementing a Message-Passing Network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495 Training and Evaluating the Message-Passing Network . . . . . . . . . . . . . . . . . . . . . 496 Project 11A: Simulating Complex Physical Phenomena . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497 Working with the SAND Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497 Building a Graph Network–Based Simulator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500 Building the Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502 Training the Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507 Testing the Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508 Simulating the System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509 Project 11B: Identifying Cell Trajectories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511 Exploring the Cell-Tracking Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512 Creating a Graph from Segmented Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514 Building a Training Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519 Making MAGIK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521 Evaluating Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 522 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 526 Seminal Works and Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527 12 CONTINUOUSLY IMPROVING PERFORMANCE WITH ACTIVE LEARNING 529 Understanding Active Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 530 Performing Binary Classification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 531 Creating a Dataset with Two Groups of Data Points . . . . . . . . . . . . . . . . . . . . . . . . 531 Classifying the Data Points with a Logistic Regression . . . . . . . . . . . . . . . . . . . . . . . 532 Implementing the Active Learning Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 533 Comparing Random and Uncertainty Sampling Strategies . . . . . . . . . . . . . . . . . . . 538 Performing Multiclass Classification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539 Creating a Dataset with Three Groups of Data Points . . . . . . . . . . . . . . . . . . . . . . . 540 Implementing the Active Learning Process with Multiple Classes . . . . . . . . . . . . . . 541 Comparing Sampling Strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545 Project 12A: Classifying MNIST Digits with Active Learning . . . . . . . . . . . . . . . . . . . . . . . . . 547 Training a Baseline Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547 Implementing Multiple Active Learning Strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . 548 Comparing the Performance of the Active Learning Strategies . . . . . . . . . . . . . . . . 553 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 555 Seminal Works and Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 556 xvi Contents in Detail
13 MASTERING DECISION-MAKING WITH DEEP REINFORCEMENT LEARNING 557 Understanding Reinforcement Learning and Q-Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 558 Implementing Tetris . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 561 Constructing a Simplified Tetris . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 561 Playing Tetris with the Command Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568 Playing Tetris with a Pygame Graphical Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . 569 Making an Agent Play Tetris with Q-Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573 Adapting the Actions of Tetris for Q-Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573 Implementing the Q-Learning Agent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 574 Training the Q-Learning Agent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581 Training with a Random Tile Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 584 Making an Agent Play Tetris with Deep Q-Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585 Understanding Deep Q-Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 586 Implementing the Deep Q-Learning Agent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587 Training the Deep Q-Learning Agent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 598 Seminal Works and Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 598 14 PREDICTING CHAOS WITH RESERVOIR COMPUTING 601 Introducing Reservoir Computing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 601 Defining the Lorenz System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 602 Numerically Integrating the Lorenz System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603 Visualizing Time Evolution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 604 Visualizing the Lorenz Attractor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 606 Demonstrating the Butterfly Effect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607 Implementing a Reservoir Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 610 Setting Up the Reservoir . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 610 Preparing the Training and Validation Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 612 Training the Reservoir Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613 Evaluating the Performance of the Reservoir Computer . . . . . . . . . . . . . . . . . . . . . . 616 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 619 Seminal Works and Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 620 CONCLUSION 623 INDEX 625 Contents in Detail xvii
(This page has no text content)
Comments 0
Loading comments...
Reply to Comment
Edit Comment