liveBook · Manning

Tensor

As explained in Learn Generative AI with PyTorch

A multidimensional numerical array used in PyTorch to represent model inputs, outputs, and intermediate values.

A PyTorch tensor is a multidimensional array of numbers. It holds data in a form that deep learning models can use: image pixels, for example, must become numerical inputs before a model can classify them. Tensors resemble NumPy arrays, but PyTorch can also use them in GPU-accelerated training. section 2.1 section 2.2.1

Start with a Python list of heights. `torch.tensor(heights, dtype=torch.float64)` turns it into a tensor of 64-bit floating-point values. The `dtype` argument controls the numerical type: `float64` offers more precision than `float32` at a computational cost. PyTorch also provides constructors such as `torch.zeros(2, 3)` and `torch.ones(1, 4, 5)` when the desired shape is known. section 2.1.1

import torch
heights_tensor = torch.tensor(heights,      #1
           dtype=torch.float64)             #2

See the list-to-tensor conversion and its explicit `dtype`. section 2.1.1

Use square brackets to select values as you would with a Python list: `heights_tensor[2]` selects one height, while `heights_tensor[-5:]` selects the last five. Check `.shape` before combining tensors; incompatible shapes can cause errors. A 46-value height tensor has shape `(46,)`. Concatenating centimeter and foot measurements produces 92 values, which can be reshaped to `(2, 46)`—one row per unit. In that 2D tensor, `[1, -2]` selects the second row’s second-to-last value. section 2.1.2 section 2.1.3

Operations can act on selected values or along a dimension. `torch.median(heights_reshaped[0, :])` finds the median centimeter height. `torch.mean(heights_reshaped, dim=1)` returns one average for each row; `torch.max(..., dim=1)` returns both maximum values and their indices. Here, `dim` specifies which dimension the operation reduces. section 2.1.4

From image pixels to model inputs

For grayscale clothing images, `T.ToTensor()` converts image values from integers in the range 0–255 to floating-point tensors in the range 0–1. `T.Normalize([0.5], [0.5])` then moves them to the range −1 to 1. The resulting tensors are ready to enter an image-classification workflow. section 2.2.2 section 2.2.1

How Learn Generative AI with PyTorch covers it

How deep it goes
Works through creation, dtypes, indexing, shapes, reshaping, and reductions without developing tensor internals.
Examples
Uses presidential heights for tensor operations and Fashion MNIST clothing images for preprocessing.
What it assumes
Assumes foundational machine-learning and neural-network knowledge while introducing tensor creation through Python lists.
What you can do after
Apply it via PyTorch to create, inspect, reshape, and reduce tensors and prepare image inputs.
How it connects
Links tensors to NumPy arrays, GPU training, image preprocessing, and deep neural networks.

Also explained in

Compare how 7 books explain tensor →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Generative AI with PyTorch. The section links point to the parts of the book it draws on.