Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow, 3rd Edition

Book description

Through a recent series of breakthroughs, deep learning has boosted the entire field of machine learning. Now, even programmers who know close to nothing about this technology can use simple, efficient tools to implement programs capable of learning from data. This bestselling book uses concrete examples, minimal theory, and production-ready Python frameworks (Scikit-Learn, Keras, and TensorFlow) to help you gain an intuitive understanding of the concepts and tools for building intelligent systems.

With this updated third edition, author Aurélien Géron explores a range of techniques, starting with simple linear regression and progressing to deep neural networks. Numerous code examples and exercises throughout the book help you apply what you've learned. Programming experience is all you need to get started.

  • Use Scikit-learn to track an example ML project end to end
  • Explore several models, including support vector machines, decision trees, random forests, and ensemble methods
  • Exploit unsupervised learning techniques such as dimensionality reduction, clustering, and anomaly detection
  • Dive into neural net architectures, including convolutional nets, recurrent nets, generative adversarial networks, autoencoders, diffusion models, and transformers
  • Use TensorFlow and Keras to build and train neural nets for computer vision, natural language processing, generative models, and deep reinforcement learning

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. The Machine Learning Tsunami
    2. Machine Learning in Your Projects
    3. Objective and Approach
    4. Code Examples
    5. Prerequisites
    6. Roadmap
    7. Changes Between the First and the Second Edition
    8. Changes Between the Second and the Third Edition
    9. Other Resources
    10. Conventions Used in This Book
    11. O’Reilly Online Learning
    12. How to Contact Us
    13. Acknowledgments
  2. I. The Fundamentals of Machine Learning
  3. 1. The Machine Learning Landscape
    1. What Is Machine Learning?
    2. Why Use Machine Learning?
    3. Examples of Applications
    4. Types of Machine Learning Systems
      1. Training Supervision
      2. Batch Versus Online Learning
      3. Instance-Based Versus Model-Based Learning
    5. Main Challenges of Machine Learning
      1. Insufficient Quantity of Training Data
      2. Nonrepresentative Training Data
      3. Poor-Quality Data
      4. Irrelevant Features
      5. Overfitting the Training Data
      6. Underfitting the Training Data
      7. Stepping Back
    6. Testing and Validating
      1. Hyperparameter Tuning and Model Selection
      2. Data Mismatch
    7. Exercises
  4. 2. End-to-End Machine Learning Project
    1. Working with Real Data
    2. Look at the Big Picture
      1. Frame the Problem
      2. Select a Performance Measure
      3. Check the Assumptions
    3. Get the Data
      1. Running the Code Examples Using Google Colab
      2. Saving Your Code Changes and Your Data
      3. The Power and Danger of Interactivity
      4. Book Code Versus Notebook Code
      5. Download the Data
      6. Take a Quick Look at the Data Structure
      7. Create a Test Set
    4. Explore and Visualize the Data to Gain Insights
      1. Visualizing Geographical Data
      2. Look for Correlations
      3. Experiment with Attribute Combinations
    5. Prepare the Data for Machine Learning Algorithms
      1. Clean the Data
      2. Handling Text and Categorical Attributes
      3. Feature Scaling and Transformation
      4. Custom Transformers
      5. Transformation Pipelines
    6. Select and Train a Model
      1. Train and Evaluate on the Training Set
      2. Better Evaluation Using Cross-Validation
    7. Fine-Tune Your Model
      1. Grid Search
      2. Randomized Search
      3. Ensemble Methods
      4. Analyzing the Best Models and Their Errors
      5. Evaluate Your System on the Test Set
    8. Launch, Monitor, and Maintain Your System
    9. Try It Out!
    10. Exercises
  5. 3. Classification
    1. MNIST
    2. Training a Binary Classifier
    3. Performance Measures
      1. Measuring Accuracy Using Cross-Validation
      2. Confusion Matrices
      3. Precision and Recall
      4. The Precision/Recall Trade-off
      5. The ROC Curve
    4. Multiclass Classification
    5. Error Analysis
    6. Multilabel Classification
    7. Multioutput Classification
    8. Exercises
  6. 4. Training Models
    1. Linear Regression
      1. The Normal Equation
      2. Computational Complexity
    2. Gradient Descent
      1. Batch Gradient Descent
      2. Stochastic Gradient Descent
      3. Mini-Batch Gradient Descent
    3. Polynomial Regression
    4. Learning Curves
    5. Regularized Linear Models
      1. Ridge Regression
      2. Lasso Regression
      3. Elastic Net Regression
      4. Early Stopping
    6. Logistic Regression
      1. Estimating Probabilities
      2. Training and Cost Function
      3. Decision Boundaries
      4. Softmax Regression
    7. Exercises
  7. 5. Support Vector Machines
    1. Linear SVM Classification
      1. Soft Margin Classification
    2. Nonlinear SVM Classification
      1. Polynomial Kernel
      2. Similarity Features
      3. Gaussian RBF Kernel
      4. SVM Classes and Computational Complexity
    3. SVM Regression
    4. Under the Hood of Linear SVM Classifiers
    5. The Dual Problem
      1. Kernelized SVMs
    6. Exercises
  8. 6. Decision Trees
    1. Training and Visualizing a Decision Tree
    2. Making Predictions
    3. Estimating Class Probabilities
    4. The CART Training Algorithm
    5. Computational Complexity
    6. Gini Impurity or Entropy?
    7. Regularization Hyperparameters
    8. Regression
    9. Sensitivity to Axis Orientation
    10. Decision Trees Have a High Variance
    11. Exercises
  9. 7. Ensemble Learning and Random Forests
    1. Voting Classifiers
    2. Bagging and Pasting
      1. Bagging and Pasting in Scikit-Learn
      2. Out-of-Bag Evaluation
      3. Random Patches and Random Subspaces
    3. Random Forests
      1. Extra-Trees
      2. Feature Importance
    4. Boosting
      1. AdaBoost
      2. Gradient Boosting
      3. Histogram-Based Gradient Boosting
    5. Stacking
    6. Exercises
  10. 8. Dimensionality Reduction
    1. The Curse of Dimensionality
    2. Main Approaches for Dimensionality Reduction
      1. Projection
      2. Manifold Learning
    3. PCA
      1. Preserving the Variance
      2. Principal Components
      3. Projecting Down to d Dimensions
      4. Using Scikit-Learn
      5. Explained Variance Ratio
      6. Choosing the Right Number of Dimensions
      7. PCA for Compression
      8. Randomized PCA
      9. Incremental PCA
    4. Random Projection
    5. LLE
    6. Other Dimensionality Reduction Techniques
    7. Exercises
  11. 9. Unsupervised Learning Techniques
    1. Clustering Algorithms: k-means and DBSCAN
      1. k-means
      2. Limits of k-means
      3. Using Clustering for Image Segmentation
      4. Using Clustering for Semi-Supervised Learning
      5. DBSCAN
      6. Other Clustering Algorithms
    2. Gaussian Mixtures
      1. Using Gaussian Mixtures for Anomaly Detection
      2. Selecting the Number of Clusters
      3. Bayesian Gaussian Mixture Models
      4. Other Algorithms for Anomaly and Novelty Detection
    3. Exercises
  12. II. Neural Networks and Deep Learning
  13. 10. Introduction to Artificial Neural Networks with Keras
    1. From Biological to Artificial Neurons
      1. Biological Neurons
      2. Logical Computations with Neurons
      3. The Perceptron
      4. The Multilayer Perceptron and Backpropagation
      5. Regression MLPs
      6. Classification MLPs
    2. Implementing MLPs with Keras
      1. Building an Image Classifier Using the Sequential API
      2. Building a Regression MLP Using the Sequential API
      3. Building Complex Models Using the Functional API
      4. Using the Subclassing API to Build Dynamic Models
      5. Saving and Restoring a Model
      6. Using Callbacks
      7. Using TensorBoard for Visualization
    3. Fine-Tuning Neural Network Hyperparameters
      1. Number of Hidden Layers
      2. Number of Neurons per Hidden Layer
      3. Learning Rate, Batch Size, and Other Hyperparameters
    4. Exercises
  14. 11. Training Deep Neural Networks
    1. The Vanishing/Exploding Gradients Problems
      1. Glorot and He Initialization
      2. Better Activation Functions
      3. Batch Normalization
      4. Gradient Clipping
    2. Reusing Pretrained Layers
      1. Transfer Learning with Keras
      2. Unsupervised Pretraining
      3. Pretraining on an Auxiliary Task
    3. Faster Optimizers
      1. Momentum
      2. Nesterov Accelerated Gradient
      3. AdaGrad
      4. RMSProp
      5. Adam
      6. AdaMax
      7. Nadam
      8. AdamW
    4. Learning Rate Scheduling
    5. Avoiding Overfitting Through Regularization
      1. ℓ1 and ℓ2 Regularization
      2. Dropout
      3. Monte Carlo (MC) Dropout
      4. Max-Norm Regularization
    6. Summary and Practical Guidelines
    7. Exercises
  15. 12. Custom Models and Training with TensorFlow
    1. A Quick Tour of TensorFlow
    2. Using TensorFlow like NumPy
      1. Tensors and Operations
      2. Tensors and NumPy
      3. Type Conversions
      4. Variables
      5. Other Data Structures
    3. Customizing Models and Training Algorithms
      1. Custom Loss Functions
      2. Saving and Loading Models That Contain Custom Components
      3. Custom Activation Functions, Initializers, Regularizers, and Constraints
      4. Custom Metrics
      5. Custom Layers
      6. Custom Models
      7. Losses and Metrics Based on Model Internals
      8. Computing Gradients Using Autodiff
      9. Custom Training Loops
    4. TensorFlow Functions and Graphs
      1. AutoGraph and Tracing
      2. TF Function Rules
    5. Exercises
  16. 13. Loading and Preprocessing Data with TensorFlow
    1. The tf.data API
      1. Chaining Transformations
      2. Shuffling the Data
      3. Interleaving Lines from Multiple Files
      4. Preprocessing the Data
      5. Putting Everything Together
      6. Prefetching
      7. Using the Dataset with Keras
    2. The TFRecord Format
      1. Compressed TFRecord Files
      2. A Brief Introduction to Protocol Buffers
      3. TensorFlow Protobufs
      4. Loading and Parsing Examples
      5. Handling Lists of Lists Using the SequenceExample Protobuf
    3. Keras Preprocessing Layers
      1. The Normalization Layer
      2. The Discretization Layer
      3. The CategoryEncoding Layer
      4. The StringLookup Layer
      5. The Hashing Layer
      6. Encoding Categorical Features Using Embeddings
      7. Text Preprocessing
      8. Using Pretrained Language Model Components
      9. Image Preprocessing Layers
    4. The TensorFlow Datasets Project
    5. Exercises
  17. 14. Deep Computer Vision Using Convolutional Neural Networks
    1. The Architecture of the Visual Cortex
    2. Convolutional Layers
      1. Filters
      2. Stacking Multiple Feature Maps
      3. Implementing Convolutional Layers with Keras
      4. Memory Requirements
    3. Pooling Layers
    4. Implementing Pooling Layers with Keras
    5. CNN Architectures
      1. LeNet-5
      2. AlexNet
      3. GoogLeNet
      4. VGGNet
      5. ResNet
      6. Xception
      7. SENet
      8. Other Noteworthy Architectures
      9. Choosing the Right CNN Architecture
    6. Implementing a ResNet-34 CNN Using Keras
    7. Using Pretrained Models from Keras
    8. Pretrained Models for Transfer Learning
    9. Classification and Localization
    10. Object Detection
      1. Fully Convolutional Networks
      2. You Only Look Once
    11. Object Tracking
    12. Semantic Segmentation
    13. Exercises
  18. 15. Processing Sequences Using RNNs and CNNs
    1. Recurrent Neurons and Layers
      1. Memory Cells
      2. Input and Output Sequences
    2. Training RNNs
    3. Forecasting a Time Series
      1. The ARMA Model Family
      2. Preparing the Data for Machine Learning Models
      3. Forecasting Using a Linear Model
      4. Forecasting Using a Simple RNN
      5. Forecasting Using a Deep RNN
      6. Forecasting Multivariate Time Series
      7. Forecasting Several Time Steps Ahead
      8. Forecasting Using a Sequence-to-Sequence Model
    4. Handling Long Sequences
      1. Fighting the Unstable Gradients Problem
      2. Tackling the Short-Term Memory Problem
    5. Exercises
  19. 16. Natural Language Processing with RNNs and Attention
    1. Generating Shakespearean Text Using a Character RNN
      1. Creating the Training Dataset
      2. Building and Training the Char-RNN Model
      3. Generating Fake Shakespearean Text
      4. Stateful RNN
    2. Sentiment Analysis
      1. Masking
      2. Reusing Pretrained Embeddings and Language Models
    3. An Encoder–Decoder Network for Neural Machine Translation
      1. Bidirectional RNNs
      2. Beam Search
    4. Attention Mechanisms
      1. Attention Is All You Need: The Original Transformer Architecture
    5. An Avalanche of Transformer Models
    6. Vision Transformers
    7. Hugging Face’s Transformers Library
    8. Exercises
  20. 17. Autoencoders, GANs, and Diffusion Models
    1. Efficient Data Representations
    2. Performing PCA with an Undercomplete Linear Autoencoder
    3. Stacked Autoencoders
      1. Implementing a Stacked Autoencoder Using Keras
      2. Visualizing the Reconstructions
      3. Visualizing the Fashion MNIST Dataset
      4. Unsupervised Pretraining Using Stacked Autoencoders
      5. Tying Weights
      6. Training One Autoencoder at a Time
    4. Convolutional Autoencoders
    5. Denoising Autoencoders
    6. Sparse Autoencoders
    7. Variational Autoencoders
    8. Generating Fashion MNIST Images
    9. Generative Adversarial Networks
      1. The Difficulties of Training GANs
      2. Deep Convolutional GANs
      3. Progressive Growing of GANs
      4. StyleGANs
    10. Diffusion Models
    11. Exercises
  21. 18. Reinforcement Learning
    1. Learning to Optimize Rewards
    2. Policy Search
    3. Introduction to OpenAI Gym
    4. Neural Network Policies
    5. Evaluating Actions: The Credit Assignment Problem
    6. Policy Gradients
    7. Markov Decision Processes
    8. Temporal Difference Learning
    9. Q-Learning
      1. Exploration Policies
      2. Approximate Q-Learning and Deep Q-Learning
    10. Implementing Deep Q-Learning
    11. Deep Q-Learning Variants
      1. Fixed Q-value Targets
      2. Double DQN
      3. Prioritized Experience Replay
      4. Dueling DQN
    12. Overview of Some Popular RL Algorithms
    13. Exercises
  22. 19. Training and Deploying TensorFlow Models at Scale
    1. Serving a TensorFlow Model
      1. Using TensorFlow Serving
      2. Creating a Prediction Service on Vertex AI
      3. Running Batch Prediction Jobs on Vertex AI
    2. Deploying a Model to a Mobile or Embedded Device
    3. Running a Model in a Web Page
    4. Using GPUs to Speed Up Computations
      1. Getting Your Own GPU
      2. Managing the GPU RAM
      3. Placing Operations and Variables on Devices
      4. Parallel Execution Across Multiple Devices
    5. Training Models Across Multiple Devices
      1. Model Parallelism
      2. Data Parallelism
      3. Training at Scale Using the Distribution Strategies API
      4. Training a Model on a TensorFlow Cluster
      5. Running Large Training Jobs on Vertex AI
      6. Hyperparameter Tuning on Vertex AI
    6. Exercises
    7. Thank You!
  23. A. Machine Learning Project Checklist
    1. Frame the Problem and Look at the Big Picture
    2. Get the Data
    3. Explore the Data
    4. Prepare the Data
    5. Shortlist Promising Models
    6. Fine-Tune the System
    7. Present Your Solution
    8. Launch!
  24. B. Autodiff
    1. Manual Differentiation
    2. Finite Difference Approximation
    3. Forward-Mode Autodiff
    4. Reverse-Mode Autodiff
  25. C. Special Data Structures
    1. Strings
    2. Ragged Tensors
    3. Sparse Tensors
    4. Tensor Arrays
    5. Sets
    6. Queues
  26. D. TensorFlow Graphs
    1. TF Functions and Concrete Functions
    2. Exploring Function Definitions and Graphs
    3. A Closer Look at Tracing
    4. Using AutoGraph to Capture Control Flow
    5. Handling Variables and Other Resources in TF Functions
    6. Using TF Functions with Keras (or Not)
  27. Index
  28. About the Author

Product information

  • Title: Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow, 3rd Edition
  • Author(s): Aurélien Géron
  • Release date: October 2022
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781098125974