liveBook · Manning

TensorFlow

As explained in Deep Learning with R, Third Edition

TensorFlow is an open-source platform and software ecosystem for numerical computation and machine learning.

TensorFlow is a Python-based, open-source machine learning framework. It handles tensor computation and automatic differentiation, giving you the lower-level tools needed to train models without writing gradient calculations by hand. Keras can use TensorFlow as its backend, providing higher-level APIs for layers, losses, optimizers, and training. section 3.3 section 3.2

A classifier from tensor operations

Start with two groups of points in a 2D plane. A linear classifier predicts with `tf$matmul(inputs, W) + b`, where `W` and `b` are trainable variables. Compare predictions with zero-or-one targets using mean squared error. Each training step computes gradients of that loss and subtracts a fraction of each gradient from the corresponding variable. After training, a threshold of 0.5 assigns a class; the learned boundary can be plotted as a line. section 3.3.2

learning_rate <- 0.1

training_step <- tf_function(  #1
  jit_compile = TRUE,
  \(inputs, targets, W, b) {
    with(tf$GradientTape() %as% tape, {
      predictions <- model(inputs, W, b)  #2
      loss <- mean_squared_error(targets, predictions) 
    })
    grad_loss_wrt <- tape$gradient(loss, list(W = W, b = b))  #3



    W$assign_sub(grad_loss_wrt$W * learning_rate)  #4
    b$assign_sub(grad_loss_wrt$b * learning_rate)  #4
    loss #4
  }
)

The training-step listing shows the central TensorFlow pattern: run the forward pass and loss inside `tf$GradientTape()`, retrieve gradients for `W` and `b`, then update both variables. Here `tf_function(jit_compile = TRUE, ...)` wraps the step. section 3.3.2

Choose the level of control you need. Keras handles a standard model-training loop; a custom TensorFlow training step lets you explicitly calculate the loss, obtain gradients with `GradientTape`, and apply them with an optimizer. TensorFlow also supports graph mode and XLA compilation, `tf.data` preprocessing, string and ragged tensors, and an ecosystem for deployment. The trade-offs include a sprawling API and less support on Hugging Face than some alternatives. section 3.7 section 7.4.2 section 3.3.3

How Deep Learning with R, Third Edition covers it

How deep it goes
Works through a classifier’s forward pass, loss, gradients, and updates, then shows a custom Keras training step without deriving automatic differentiation.
Examples
Uses a synthetic two-class linear classifier and a Keras model’s custom training step on MNIST.
What it assumes
Builds on tensors, variables, tensor operations, gradients, and an initial encounter with Keras rather than assuming TensorFlow experience.
What you can do after
Build it from scratch: train a simple TensorFlow classifier, or apply it as the backend for a custom Keras training step.
How it connects
Connects TensorFlow to Keras backends, automatic differentiation, tensors, gradient descent, XLA compilation, and data preprocessing.

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 R, Third Edition. The section links point to the parts of the book it draws on.