liveBook · Manning

Array

As explained in Learn Generative AI with PyTorch

An array with one or more dimensions, used here as the general definition of a tensor.

An array holds values in one or more dimensions. In deep learning, inputs must first become arrays of numbers: image pixels can become floating-point values, while text can become sequences of integer vocabulary indexes. PyTorch calls its multidimensional numeric arrays tensors. They resemble NumPy arrays, but PyTorch tensors also support GPU-accelerated training. section 2.1.1 section 2.1

Start with one dimension

Store the heights of 46 U.S. presidents in a PyTorch tensor. Its shape is (46): one dimension containing 46 values. Square brackets select a value, while a slice such as `heights_tensor[-5:]` selects the last five. Use `dtype=torch.float64` when creating the tensor if that precision is wanted; it offers greater precision than the default `float32` at a computational cost. section 2.1.1 section 2.1.2 section 2.1.3

Shape determines how values are arranged and which operations fit. Convert the heights to feet, concatenate the two measurements, then reshape the 92 values into a (2, 46) array: one row per unit and one column per president. Now two indexes select one value, and `torch.mean(heights_reshaped, dim=1)` calculates a mean for each row. Check shapes before operating on tensors; mismatches cause errors. section 2.1.3 section 2.1.4

Listing 3.1 Creating training data to form an exponential growth shape
import torch
  
torch.manual_seed(0)                                #1
  
observations = 2048
  
train_data = torch.zeros((observations, 2))         #2
  
train_data[:,0]=50*torch.rand(observations)         #3
  
train_data[:,1]=1.08**train_data[:,0]               #4

This two-dimensional training array starts with 2,048 rows and two columns. Column indexing fills the first column with sampled x values and the second with values on an exponential growth curve. section 3.2.1

Arrays also carry less tabular inputs. For clothing-image classification, `ToTensor()` converts image values from integers between 0 and 255 to floats between 0 and 1; normalization then maps them to the range −1 to 1. For music generation, a piece can instead be represented as a four-dimensional object with shape (4, 2, 16, 84). The useful shape depends on what the model must process or produce. section 2.2.2 section 13.1

How Learn Generative AI with PyTorch covers it

How deep it goes
Works through creation, data types, shapes, indexing, reshaping, and dimension-wise operations without descending into tensor storage internals.
Examples
Uses presidential heights, two-column curve data, grayscale clothing images, and four-dimensional music pieces.
What it assumes
Assumes foundational machine learning and neural-network knowledge while teaching tensor creation from Python lists.
What you can do after
Apply it via PyTorch to create, index, reshape, and summarize numeric data before feeding it to models.
How it connects
Connects tensor shape and data types to image preprocessing, GPU training, and deep neural networks.

Also explained in

Compare how 40 books explain array →

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.