liveBook · Manning

Tensor operation

As explained in Deep Learning with PyTorch, Second Edition

An operation provided by PyTorch that acts on tensor data, either as a torch-module function or as a tensor method.

A tensor operation is a PyTorch operation that acts on tensor data. Most operations are available both through the torch module and as methods on a tensor, with interchangeable behavior. section 3.7

Common tensor-operation families include:

  • Creation operations construct tensors, while indexing, slicing, joining, and mutation operations change their shape, stride, or content. section 3.7
  • Pointwise operations apply a function independently to each element; reduction operations compute aggregate values such as means, standard deviations, and norms. section 3.7
  • Comparison, spectral, linear-algebra, random-sampling, serialization, and parallelism operations support numerical predicates, transformations, computation, generation, storage, and execution control. section 3.7

Calling the same operation two ways

Create a tensor with `a = torch.ones(3, 2)`. Transpose it either with `torch.transpose(a, 0, 1)` or with `a.transpose(0, 1)`; both produce a tensor whose shape is `(2, 3)`. section 3.7

Recognizing in-place operations

Methods ending in an underscore modify the input tensor instead of creating a new output. For example, `a.zero_()` replaces every element of a tensor with zero, while methods without the suffix leave the source unchanged. section 3.8.2

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Defines operation families and demonstrates transpose and in-place behavior, but defers exhaustive API coverage to the documentation.
Examples
Demonstrates transpose through module and method calls, zeroing a tensor in place, and indexing and mutating a Python list.
What it assumes
Assumes familiarity with PyTorch tensors and their behavior while building practical awareness of tensor-operation categories.
What you can do after
Apply it via PyTorch by selecting tensor APIs, choosing functional or method syntax, and recognizing operations that mutate inputs.
How it connects
Connects tensor operations with tensor methods, indexing, slicing, in-place operations, data preparation, and linear algebra.

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