liveBook · Manning

Computation graph

As explained in Deep Learning with R, Third Edition

A computation graph is a directed representation of operations and their data dependencies used to evaluate computations and derivatives.

A computation graph is a directed acyclic graph of operations and their data dependencies. It turns a calculation into a machine-readable structure: a program can evaluate the operations and work backward through them to compute derivatives. section 2.4.4

loss_value <- loss(
  y_true,
  softmax(matmul(relu(matmul(inputs, W1) + b1), W2) + b2)
)

This model calculation can be read as a graph: matrix multiplication, addition, ReLU, softmax, and loss each depend on earlier results. section 2.4.4

A forward pass computes values from inputs through the loss. A backward pass follows the dependencies in reverse, using the chain rule to combine derivatives and find how the loss changes with a weight. section 2.4.4

A one-operation graph

In PyTorch, squaring a tensor containing 3 creates a graph for that computation. Calling `result$backward()` finds the derivative at the input and stores 6 in `input_var$grad`. Calling it again without resetting `$grad` accumulates another 6; set `$grad` to `NULL` before a fresh calculation. section 3.4.1

one_training_step <- function(model, images_batch, labels_batch) {
  with(tf$GradientTape() %as% tape, {
    predictions <- model$call(images_batch)
    per_sample_losses <-
      op_sparse_categorical_crossentropy(labels_batch, predictions)
    average_loss <- op_mean(per_sample_losses)
  })
  gradients <- tape$gradient(average_loss, model$weights)
  update_weights(gradients, model$weights)
  average_loss
}

In a training step, TensorFlow’s `GradientTape` records the operations that produce the average loss. The tape then computes gradients with respect to the model’s weights for the weight update. section 2.5.2

The graph need not look the same in every API. TensorFlow exposes a recording scope through `GradientTape`; PyTorch records a one-time graph during computation and exposes backpropagation through a tensor’s `backward()` method. section 2.5.2 section 3.4.1

How Deep Learning with R, Third Edition covers it

How deep it goes
Defines the graph, traces forward and backward passes, and applies it through framework APIs rather than implementing backpropagation from scratch.
Examples
Uses a two-layer model, a scalar simplification, a TensorFlow training step, and a PyTorch square-and-backward calculation.
What it assumes
Builds from function composition and the chain rule, then simplifies a model graph to one scalar linear layer.
What you can do after
Apply it via TensorFlow GradientTape or PyTorch backward() to obtain gradients for model training.
How it connects
Connects tensor operations and the chain rule to backpropagation, automatic differentiation, and weight updates.

Also explained in

Compare how 7 books explain computation graph →

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.