liveBook · Manning

Chapter 3: Introduction to TensorFlow, PyTorch, JAX, and Keras

What you will learn in chapter 3 of Deep Learning with Python, Third Edition

This chapter introduction previews TensorFlow, PyTorch, and JAX alongside Keras, and points toward practical deep learning applications.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This chapter introduction previews TensorFlow, PyTorch, and JAX alongside Keras, and points toward practical deep learning applications.

3.1 A brief history of deep learning frameworks

This section traces how deep learning frameworks developed and explains why their shared capabilities and today’s ecosystem matter to practitioners.

Key terms: automatic differentiation, tensor computations, distributed computation, CPUs, GPUs, CUDA, Theano, autodifferentiation, GPU computation, Keras

3.2 How these frameworks relate to each other

This section distinguishes Keras’s high-level role from the lower-level roles of TensorFlow, PyTorch, and JAX, and explains why Keras needs a backend.

Key terms: high-level framework, low-level APIs, tensors, backpropagation, layers, loss function, backend engine, Keras backends, NumPy, gradients

3.3 Introduction to TensorFlow

Introduces TensorFlow as a widely used machine learning framework and a broader ecosystem spanning research and production.

Key terms: TensorFlow, TFX, TF-Serving, TF Optimization Toolkit, TFLite, MediaPipe

3.3.1 First steps with TensorFlow

This section introduces TensorFlow tensor creation, mutable variables, automatic gradients, and function compilation.

Key terms: tf.ones, tf.zeros, tf.constant, tf.random, TensorFlow tensors, tf.Variable, assign, assign_add, assign_sub, GradientTape

3.3.2 An end-to-end example: A linear classifier in pure TensorFlow

The section builds a linear classifier for two separable classes of 2D points and shows how its learned predictions define a separating line.

Key terms: negative_samples, positive_samples, inputs, targets, linear classifier, affine transformation, W, b, loss function, batch training

3.3.3 What makes the TensorFlow approach unique

This section compares TensorFlow’s strengths and limitations to help readers judge when it may be a good framework choice.

Key terms: graph mode, XLA compilation, string tensors, ragged tensors, tf.data, production deployment, sprawling API, NumPy API, Hugging Face, pretrained models

3.4 Introduction to PyTorch

Introduces PyTorch’s place in the machine-learning ecosystem and compares its API with TensorFlow, JAX, and Keras.

Key terms: PyTorch API, layers, optimizers, Keras workflows, PyTorch backend, PyTorch, Meta, torchvision, torchaudio, Hugging Face

3.4.1 First steps with PyTorch

This section introduces PyTorch tensors and parameters, then explains how backward() computes gradients and why those gradients sometimes need resetting.

Key terms: torch, tensor, normal, torch.rand, torch.nn.parameter.Parameter, torch.Tensor, trainable state, computation graph, tape, tensor.backward()

3.4.2 An end-to-end example: A linear classifier in pure PyTorch

This section builds a linear classifier in PyTorch, first with manual gradient updates and then with a Module and optimizer, before introducing optional compilation.

Key terms: loss.backward(), tensor.grad, torch.no_grad(), W, b, torch.nn.Module, Parameters, __init__(), forward(), __call__()

3.4.3 What makes the PyTorch approach unique

This section weighs PyTorch’s debugging and model-availability strengths against its API inconsistencies and performance drawbacks.

Key terms: eagerly, compilation, debugging, Hugging Face, first-class support, pretrained model, PyTorch API, NumPy, axis, dim

3.5 Introduction to JAX

Introduces JAX and explains how its stateless approach supports large-scale machine learning computation.

Key terms: stateless, automatic parallelization, distributed computation, synchronization, JAX, differentiable computation, TPUs

3.5.1 First steps with JAX

This subsection previews the JAX concepts that will be covered next.

3.5.2 Tensors in JAX

This section introduces JAX tensors through its NumPy-compatible array API and the `jax.numpy` namespace.

Key terms: NumPy API, jax.numpy, jnp

3.5.3 Random number generation in JAX

This section explains how JAX’s stateless design affects random numbers and array updates, then shows how to compute gradients and compile functions.

Key terms: PRNG, key, jax.random.split(), statelessness, tensor assignment, at()/set() API, in-place modification, jax.grad(), compute_loss(), grad_fn

3.5.4 An end-to-end example: A linear classifier in pure JAX

This section shows how to build a JAX linear-classifier training loop using stateless functions and automatic gradient computation.

Key terms: stateless, state, W and b, training_step(), learning_rate, compute_loss(), scalar, jax.value_and_grad(), gradients

3.5.5 What makes the JAX approach unique

This section explains JAX’s functional, stateless approach and weighs its performance advantages against its development costs.

Key terms: functional, stateless philosophy, compile, NumPy, TPUs, XLA, metaprogramming, compilation, pure eager execution, low-level training loops

3.6 Introduction to Keras

This section introduces Keras as a Python deep learning API and explains why it offers multiple ways to build and train models.

Key terms: Keras, deep learning API, workflows, high-level, low-level, usability, flexibility

3.6.1 First steps with Keras

This section explains how Keras uses interchangeable backend frameworks and how to choose one before importing Keras.

Key terms: Keras, backend frameworks, JAX, TensorFlow, PyTorch, default backend, KERAS_BACKEND, ~/.keras/keras.json, import keras

3.6.2 Layers: The building blocks of deep learning

This section explains Keras layers as stateful data-processing building blocks and shows how they infer input shapes and create weights when first called.

Key terms: layer, weights, forward pass, dense layers, Dense, recurrent layers, LSTM, Conv1D, Conv2D, layer compatibility

3.6.3 From layers to models

This section explains how Keras models organize layers into networks and why a model’s topology determines what it can learn.

Key terms: Model class, Sequential models, graph of layers, network topology, hypothesis space, weight tensors, architecture, linearly separable

3.6.4 The “compile” step: Configuring the learning process

This section explains the choices needed to configure Keras training and how to supply them to compile().

Key terms: loss function, objective function, optimizer, metrics, stochastic gradient descent (SGD), compile(), loss, object instances, learning_rate

3.6.5 Picking a loss function

This section explains why a loss must reflect the intended task and gives standard loss choices for common classification problems.

Key terms: loss function, objective, shortcut, unintended side effects, binary crossentropy, categorical crossentropy, two-class classification, many-class classification, loss functions

3.6.6 Understanding the fit method

This section explains what Keras fit() does, its main training arguments, and the per-epoch results it returns.

Key terms: fit, inputs and targets, epochs, batch size, mini-batch gradient descent, History object, history, loss, metrics, per-epoch values

3.6.7 Monitoring loss and metrics on validation data

The section explains why validation data must be kept separate from training data and how Keras reports loss and metrics on it during or after training.

Key terms: training data, general, memorizing, validation data, validation_data, fit(), validation loss, training loss, evaluate(), validation metrics

3.6.8 Inference: Using a model after training

This section explains how to use a trained Keras model for inference and why predict() is preferable to calling the model directly on a large input set.

Key terms: inference, __call__(), predict(), small batches, NumPy array, TensorFlow Dataset objects

3.7 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 2: The mathematical building blocks of neural networks Chapter 4: Classification and regression →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.