liveBook · Manning

Tensor operation

As explained in Deep Learning with R, Third Edition

A tensor operation is a mathematical or computational transformation that takes one or more tensors as input and produces tensor values.

A tensor operation takes numeric tensors—such as vectors or matrices—and produces tensor values. Adding, multiplying, and transforming tensors are the small steps from which neural-network layers are built. A familiar example is a dense layer: it transforms an input matrix into a new representation. section 2.3

dense <- function(inputs, W, b) {
  torch$nn$relu(torch$matmul(inputs, W) + b)
}

This PyTorch function shows a dense layer as three operations in sequence: multiply the inputs by weights, add a bias, then apply relu. section 3.4.1

Read the dense-layer expression from the inside out:

  • matmul(inputs, W) combines the input tensor with the weight matrix W. section 2.3
  • Adding b incorporates a bias vector into the result. section 2.3
  • relu keeps positive values and replaces negative values with zero: relu(x) is max(x, 0). section 2.3

Tensor operations also have a geometric interpretation. Adding a vector moves a point; applying the same addition to many points translates an object without distorting it. A matrix product can rotate or scale points. Chaining operations lets a neural network make a much more complex transformation through simple steps. section 2.3.5 section 2.3.6

Figure 2.8 2D translation as a vector addition
figure

The figure makes vector addition concrete: every point of a 2D object moves by the same amount and in the same direction. section 2.3.5

During training, differentiable tensor operations have another useful property: gradients describe how changing tensor-valued weights changes the loss. Backpropagation combines gradients through the chain of operations, making it possible to update the weights. Frameworks such as PyTorch provide tensor operations and gradient computation; Keras provides higher-level layers and training tools. section 2.4.2 section 2.6 section 3.2

How Deep Learning with R, Third Edition covers it

How deep it goes
Explains dense-layer operations and geometric interpretations through code and figures, without formal mathematical derivations.
Examples
Uses a dense-layer expression, PyTorch tensor functions, and 2D translation, rotation, and scaling figures.
What it assumes
Starts from a neural-network example and explains the mathematics without assuming comfort with mathematical notation.
What you can do after
Apply it via PyTorch to assemble a dense-layer function and compute gradients with backward().
How it connects
Connects tensor operations to dense layers, geometric transformations, gradients, and backpropagation.

Also explained in

Compare how 3 books explain tensor operation →

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.