liveBook · Manning

TensorFlow

As explained in Deep Learning with Python, Third Edition

A Python-based open source machine learning platform with an ecosystem of components for training, deployment, and related workflows.

TensorFlow is a Python-based open source machine learning framework developed primarily by Google. It supplies low-level tools for tensor computation and gradient-based training, while its wider ecosystem supports workflows from research to production deployment. section 3.3 section 3.2

A linear classifier from scratch

Start with two classes of points in a 2D plane, stored as 2,000 input rows with corresponding 0-or-1 targets. A classifier computes prediction = tf.matmul(inputs, W) + b. Its trainable variables W and b define a line separating the classes. Training minimizes mean squared error; predictions above 0.5 can then be plotted to inspect the learned separation. section 3.3.2

The training step puts TensorFlow’s core operations together:

  • Use tensors and tensor operations to calculate predictions and loss. section 3.3.2
  • Use tf.GradientTape to obtain gradients of the loss with respect to W and b, then update those variables with assign_sub. section 3.3.2
  • Repeat the step in a training loop and watch the loss; the example runs 40 steps. section 3.3.2
Listing 3.20 The training-step function
learning_rate = 0.1

@tf.function(jit_compile=True)                                       #1
def training_step(inputs, targets, W, b):
    with tf.GradientTape() as tape:                                   #2
        predictions = model(inputs, W, b)                              #2
        loss = mean_squared_error(predictions, targets)                #2
    grad_loss_wrt_W, grad_loss_wrt_b = tape.gradient(loss, [W, b])    #3
    W.assign_sub(grad_loss_wrt_W * learning_rate)                     #4
    b.assign_sub(grad_loss_wrt_b * learning_rate)                      #4
    return loss

The training-step listing shows the gradient calculation and variable updates in one place. It also wraps the step in tf.function with XLA compilation enabled. section 3.3.2

TensorFlow can also serve as a Keras backend. In that pairing, TensorFlow provides lower-level tensors, operations, and gradients; Keras supplies high-level layers, models, losses, optimizers, metrics, and training-loop APIs. Use the lower-level approach when you need to see or control each training step. section 3.2 section 3.3.2

For broader work, TensorFlow offers tf.data for preprocessing, string and ragged tensors for specialized inputs, and deployment components including TF-Serving and TFLite. Graph mode and XLA compilation are performance strengths. The trade-offs are a sprawling API, occasional differences from NumPy’s numerical API, and less Hugging Face support for some recent generative AI models. section 3.3.3 section 3.3

How Deep Learning with Python, Third Edition covers it

How deep it goes
Works through a linear classifier’s variables, forward pass, loss, gradients, and updates; surveys platform components without implementing deployment.
Examples
Builds a classifier for two synthetic 2D point classes and plots its predictions and separating line.
What it assumes
Assumes familiarity with tensors, variables, tensor operations, and gradients before combining them in a complete training example.
What you can do after
Build it from scratch: train a linear classifier with TensorFlow variables, tf.GradientTape, and repeated gradient updates.
How it connects
Relates TensorFlow to Keras backends, automatic differentiation, PyTorch, JAX, and NumPy.

Also explained in

Compare how 4 books explain TensorFlow →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with Python, Third Edition. The section links point to the parts of the book it draws on.