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






