liveBook · Manning

Chapter 2: Vectors, matrices, and tensors in machine learning

What you will learn in chapter 2 of Math and Architectures of Deep Learning

This chapter introduction frames machine learning as organized numerical computation and previews a machine-learning-focused study of vectors, matrices, eigenvalues, eigenvectors, and hyperellipses with accompanying executable notebooks.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This chapter introduction frames machine learning as organized numerical computation and previews a machine-learning-focused study of vectors, matrices, eigenvalues, eigenvectors, and hyperellipses with accompanying executable notebooks.

Key terms: vectors, matrices, number crunching, eigenvalues, eigenvectors, hyperellipse, Jupyter Notebook, Python, complete, fully functional code

2.1 Vectors and their role in machine learning

This section introduces vectors as ordered numerical representations of machine-learning inputs, outputs, parameters, and object features.

Key terms: vector, array, ordered sequence, input vector, output vector, model parameters, neural network layer, feature vector, document retrieval, natural language processing

2.1.1 The geometric view of vectors and its significance in machine learning

This section presents vectors as geometric points or positions, connecting coordinate representations to the geometric view of machine-learning models.

Key terms: vector, coordinate values, coordinate system, origin, array representation, coordinate axes, reference point, transform, n-dimensional space, input points

2.2 PyTorch code for vector manipulations

This section introduces PyTorch as the book’s practical deep-learning tool and explains how readers should engage with its Jupyter Notebook code.

Key terms: PyTorch, open source machine learning library, deep learning applications, Python, Jupyter Notebook, code snippet, theoretical material, Python basics, packages, code snippets

2.2.1 PyTorch code for the introduction to vectors

Demonstrates PyTorch operations for creating vectors and accessing, selecting, and slicing their elements.

Key terms: vectors, subvectors, slice and dice, PyTorch, Listing 2.1

2.3 Matrices and their role in machine learning

This section introduces matrices as rectangular arrays that compactly represent a dataset of many feature-vector input instances.

Key terms: matrix, rectangular array, rows, columns, row vectors, column vectors, training dataset, training input, input instance, matrix X

2.3.1 Matrix representation of digital images

This subsection explains how a grayscale digital image is stored as a matrix whose entries encode pixel brightness.

Key terms: matrix, digital image, pixel, brightness, normalized, 0 to 255, black, white, gray, I00

2.4 Python code: Introducing matrices, tensors, and images via PyTorch

This section presents tensors in PyTorch as multidimensional arrays and maps scalars, vectors, matrices, RGB images, and image batches to their tensor dimensions and shapes.

Key terms: tensor, multidimensional array, scalar, vector, matrix, RGB image, color channels, height, width, batch

2.5 Basic vector and matrix operations in machine learning

This section introduces basic vector and matrix operations through application-oriented examples in image processing, computer vision, and machine learning rather than as a comprehensive linear algebra review.

Key terms: vector operations, matrix operations, image processing, computer vision, machine learning, linear algebra

2.5.1 Matrix and vector transpose

This section defines matrix transposition as swapping rows and columns, including the row-vector form of a transposed column vector.

Key terms: matrix transposition, transpose, Aᵀ, row indices, column indices, column vector

2.5.2 Dot product of two vectors and its role in machine learning

This section defines the dot product and shows how it compactly expresses the weighted-sum output of a simple machine-learning model.

Key terms: dot product, corresponding elements, same dimensions, inner product, input vector, weight vector, bias, model output, weighted sum, y = x · w + b

2.5.3 Matrix multiplication and machine learning

This section shows how matrix multiplication compactly computes weighted model outputs across datasets, generalizes to matrix products, and relates products to dot products and transposes.

Key terms: matrix-vector multiplication, weight vector, bias, weighted sum, training dataset, matrix-matrix multiplication, m by p matrix, p by n matrix, row vector, column vector

2.5.4 Length of a vector (L2 norm): Model error

This section defines model error as squared output-target differences and shows that total training error is the squared L2 norm of the residual vector.

Key terms: squared error, target value, computed value, model output, ground truth, biased weighted sum model, weight vector, bias, target, output vector

2.5.5 Geometric intuitions for vector length

This subsection gives a geometric interpretation of vector length, introduces unit vectors as normalized directions, and relates error-vector length to machine-learning training.

Key terms: L2 norm, vector length, unit vector, length, direction, hat symbol, error vector, model output vector, target ground truth vector, training

2.5.6 Geometric intuitions for the dot product: Feature similarity

This section explains how the dot product measures directional agreement and similarity between feature vectors, including why normalization removes vector-length effects.

Key terms: feature vector, dot product, similarity, document retrieval, component, direction, cosine, angle, vector length, directional agreement

2.6 Orthogonality of vectors and its physical significance

This section uses the dot product to connect vector angle with physical effectiveness and defines orthogonal vectors as perpendicular, independent vectors with no shared component.

Key terms: dot product, angle, cosθ, force vector, displacement vector, orthogonal, perpendicular, feature vector

2.7 Python code: Basic vector and matrix operations via PyTorch

This section introduces a PyTorch notebook that illustrates the vector and matrix operations discussed earlier.

Key terms: PyTorch, Jupyter Notebook, vector and matrix operations

2.7.1 PyTorch code for a matrix transpose

2.7.2 PyTorch code for a dot product

This subsection shows the PyTorch dot-product operation for two vectors and restates its component-wise mathematical definition.

Key terms: dot product, vectors, components

2.7.3 PyTorch code for matrix vector multiplication

This subsection demonstrates that multiplying an m-by-n matrix by an n-element vector produces an m-element column vector in PyTorch.

Key terms: matrix-vector multiplication, m-by-n matrix, m-element column vector

2.7.4 PyTorch code for matrix-matrix multiplication

This subsection states the dimension rule and output shape for multiplying two matrices in PyTorch.

Key terms: matrix-matrix multiplication, m-by-p matrix, p-by-n matrix, m-by-n matrix

2.7.5 PyTorch code for the transpose of a matrix product

This subsection states and demonstrates the transpose rule for a valid matrix product in PyTorch.

Key terms: transpose, matrix product, reversed order, (AB)^T = B^T A^T

2.8 Multidimensional line and plane equations and machine learning

This section frames machine learning classification geometrically as separating feature-vector points with high-dimensional lines, planes, or more complex surfaces, and introduces the upcoming study of hyperlines and hyperplanes.

Key terms: classifier, feature vectors, multidimensional feature space, classes, input space, output space, transform, separator, line, plane

2.8.1 Multidimensional line equation

This section expresses a line between two vectors with a parameterized vector equation that works in any finite-dimensional space.

Key terms: multidimensional line equation, direction, α, weighted combination, line segment

2.8.2 Multidimensional planes and their role in machine learning

This section explains hyperplanes as multidimensional decision boundaries and connects their geometry to the weights and bias of a simple machine learning classifier.

Key terms: plane, hyperplane, feature space, class separator, transformation, input space, normal direction, normal vector, orthogonal, fixed point

2.9 Linear combinations, vector spans, basis vectors, and collinearity preservation

This introduction motivates studying how high-dimensional spaces can be represented using a small set of vectors.

Key terms: high-dimensional spaces, vectors

2.9.1 Linear dependence

This subsection defines linear dependence through nonzero scalar-weighted sums that equal zero, connecting it to collinearity and linear combinations.

Key terms: linear dependence, zero vector, scalar weights, collinearity, collinear vectors, multidimensional line equation, linear combination, weighted sum

2.9.2 Span of a set of vectors

This subsection defines the span of a vector set as all of its linear combinations and illustrates that two Cartesian axis vectors span a 2D plane.

Key terms: span, linear combination, set of vectors, Cartesian coordinate axes, X-axis, Y-axis, weighted sum

2.9.3 Vector spaces, basis vectors, and closure

This section defines vector spaces, bases, dimensional limits on independent vectors, and closure under linear combinations through algebraic and geometric examples.

Key terms: vector space, vector addition, scalar multiplication, linear combination, basis, basis vectors, span, Cartesian coordinate axes, orthogonal, linearly independent

2.10 Linear transforms: Geometric and algebraic interpretations

This section introduces matrix-based transforms as ways to map feature vectors into more useful spaces and identifies collinearity preservation as the defining property of linear transforms.

Key terms: feature vector, feature space, classifier, transform, matrix multiplication, R : ℝ2 → ℝ2, P : ℝ3 → ℝ2, rotation, projection, linear transform

2.10.1 Generic multidimensional definition of linear transforms

This section defines a linear transform as a function that preserves linear combinations of vectors.

Key terms: linear transform, linear combination, rotation matrix, projection matrix

2.10.2 All matrix-vector multiplications are linear transforms

This subsection proves that finite-dimensional matrix-vector multiplication is a linear transform and contrasts it with the nonlinear vector-length function.

Key terms: linear transform, linear mapping, matrix-vector multiplication, finite dimensions, matrix, infinite-dimensional vectors, geometric view, vector length, length of a vector, nonlinear mapping

2.11 Multidimensional arrays, multilinear transforms, and tensors

Introduces tensors as a concept commonly encountered in machine learning.

Key terms: tensor, machine learning, TensorFlow

2.11.1 Array view: Multidimensional arrays of numbers

This subsection introduces tensors as multidimensional numerical arrays, connecting scalars, vectors, matrices, color images, and neural-network layer data.

Key terms: tensor, multidimensional array, scalar, vector, matrix, color image, RGB, R, G, B

2.12 Linear systems and matrix inverse

This section introduces linear systems as compact matrix equations for estimating model parameters and relates their closed-form solution to matrix inverses.

Key terms: iterative process, closed-form solution, training data, machine parameters, linear system, matrix multiplication, weighted sum model, weights, bias, A x = b

2.12.1 Linear systems with zero or near-zero determinants,and ill-conditioned systems

This section explains why zero determinants make square linear systems singular and unsolvable, while near-zero determinants make them ill-conditioned and numerically unstable.

Key terms: singular matrix, zero determinant, linearly dependent rows or columns, matrix inverse, linear system, scalar multiple, linearly dependent, row vectors, near singularity, near-zero determinant

2.12.2 PyTorch code for inverse, determinant, and singularity testing of matrices

This subsection shows that PyTorch's linalg package computes matrix inverses and determinants, and uses a zero determinant to identify a singular, non-invertible matrix.

Key terms: PyTorch, linalg, matrix inverse, determinant, singular matrix, zero determinant, non-invertible, linear system of equations

2.12.3 Over- and under-determined linear systems in machine learning

This section relates nonsquare linear systems to machine learning training data and explains why noisy overdetermined systems require minimizing overall error.

Key terms: nonsquare matrix, training data instances, equations, unknowns, weights, iteratively, m × n, overdetermined system, underdetermined system, m > n

2.12.4 Moore Penrose pseudo-inverse of a matrix

This subsection derives the Moore–Penrose pseudo-inverse as a way to solve nonsquare linear systems by converting them through a transpose operation.

Key terms: pseudo-inverse, Moore Penrose pseudo-inverse, A+, transpose, AᵀA, overdetermined system, underdetermined system, error, ||Ax − b||, determinant

2.12.5 Pseudo-inverse of a matrix: A beautiful geometric intuition

This section derives the Moore-Penrose pseudo-inverse geometrically as a projection-based least-squares solution and applies it to fitting an overdetermined linear model.

Key terms: column space, linear span, least-squares solution, best fit, total error, projection, residual, orthogonal, normal equation, Moore-Penrose pseudo-inverse

2.12.6 PyTorch code to solve overdetermined systems

2.13 Eigenvalues and eigenvectors: Swiss Army knives of machine learning

This section introduces eigenvalues and eigenvectors as direction-preserving properties of square-matrix transforms, motivating their use for dimensionality reduction and interpreting them through 3D rotation.

Key terms: feature vectors, feature space, dimensionality reduction, eigenvalues, eigenvectors, square matrix, linear transformation, eigenvector, eigenvalue, A v = λv

2.13.1 Eigenvectors and linear independence

This subsection proves that eigenvectors associated with distinct eigenvalues of the same matrix must be linearly independent.

Key terms: eigenvector, eigenvalue, linearly independent, linearly dependent, matrix A

2.13.2 Symmetric matrices and orthogonal eigenvectors

This subsection proves that eigenvectors of a symmetric matrix associated with distinct eigenvalues are orthogonal.

Key terms: symmetric matrix, eigenvectors, eigenvalues, mutually orthogonal, distinct eigenvalues, matrix symmetry, A^T=A, transpose, eigenvalue-eigenvector pair, dot product

2.13.3 PyTorch code to compute eigenvectors and eigenvalues

2.14 Orthogonal (rotation) matrices and their eigenvalues and eigenvectors

This section introduces rotation, or orthogonal, matrices as intuitively observable transforms that also support machine learning tools.

Key terms: rotation transforms, rotation matrices, orthogonal matrices

2.14.1 Rotation matrices

This section introduces standard 2D and 3D rotation matrices and relates each 3D rotation’s unchanged coordinate axis to an eigenvector with eigenvalue 1.

Key terms: planar rotation, rotation matrix, origin, angle θ, rotation about the Z-axis, rotation about the X-axis, rotation about the Y-axis, Z coordinate, X coordinate, Y coordinate

2.14.2 Orthogonality of rotation matrices

This section defines orthogonal rotation matrices, shows that they preserve vector length, and explains why their transpose reverses the rotation angle.

Key terms: orthogonal matrix, rotation matrix, transpose, inverse, identity matrix, length-preserving, magnitude, orthogonality, L2 norm, negative angle

2.14.3 PyTorch code for orthogonality of rotation matrices

This subsection verifies in PyTorch that a rotation matrix is orthogonal by multiplying it by its transpose and checking for the identity matrix.

Key terms: orthogonality, rotation matrix, transpose, identity matrix

2.14.4 Eigenvalues and eigenvectors of a rotation matrix:Finding the axis of rotation

This subsection shows that a rotation matrix has eigenvalue 1 and that its associated eigenvector identifies the rotation axis.

Key terms: rotation matrix, eigenvalue, eigenvector, reflection, eigenvalue 1, axis of rotation

2.14.5 PyTorch code for eigenvalues and vectors of rotation matrices

2.15 Matrix diagonalization

This section introduces matrix diagonalization through a matrix’s linearly independent eigenvectors and shows why it supports solving systems without inversion and later machine-learning tools.

Key terms: diagonalization, linearly independent eigenvectors, eigenvector matrix, diagonal matrix, eigenvalues, S, symmetric matrix, orthogonal eigenvectors, orthogonal matrix, transpose

2.15.1 PyTorch code for matrix diagonalization

This subsection points readers to a PyTorch implementation of the matrix diagonalization mathematics introduced in section 2.15.

Key terms: PyTorch, matrix diagonalization, Jupyter Notebook, fully functional code

2.15.2 Solving linear systems without inversion via diagonalization

This section shows how diagonalizing a symmetric matrix solves a linear system through stable matrix-vector operations instead of directly inverting the matrix.

Key terms: matrix inversion, numerically unstable, diagonalization, square symmetric matrix, distinct eigenvalues, linear system, S, Λ, eigenvectors, orthogonal

2.15.3 PyTorch code for solving linear systems via diagonalization

This subsection sets up a linear system for solution via diagonalization by identifying the orthogonal eigenvector structure of its symmetric coefficient matrix.

Key terms: symmetric matrix, orthogonal eigenvectors, orthogonal matrix, eigenvectors, transpose, inverse, diagonalization

2.15.4 Matrix powers using diagonalization

This subsection shows that diagonalizing a matrix makes repeated matrix-power computations efficient.

Key terms: diagonalization, diagonalizable matrix, diagonal matrix, matrix power, S, D, precompute, O(m) operations, O(nm³) operations, naive computations

2.16 Spectral decomposition of a symmetric matrix

This section states the spectral resolution theorem: a square symmetric matrix can be decomposed in terms of its eigenvalues and eigenvectors.

Key terms: spectral decomposition, spectral resolution theorem, symmetric matrix, eigenvalues, eigenvectors

2.16.1 PyTorch code for the spectral decomposition of a matrix

2.17 An application relevant to machine learning: Finding the axes of a hyperellipse

This section expresses a hyperellipse as a quadratic matrix equation and shows that diagonalizing its symmetric matrix reveals the directions of its axes.

Key terms: ellipse, hyperellipse, position vector, diagonal matrix, axis aligned, centered at the origin, rotation, rotation matrix, RᵀΛR, symmetric matrix

2.17.1 PyTorch code for hyperellipses

This subsection shows that diagonalizing the symmetric matrix in a hyperellipse equation rotates the coordinate system so its axes align with the ellipse's principal axes.

Key terms: hyperellipse, ellipse equation, matrix, vector, cross-term, diagonalization, A = SΣS⁻¹, diagonal matrix, transformed coordinates, hyperellipse axes

2.18 Summary

This summary consolidates the vector, matrix, linear-system, and eigenvalue tools used to represent data and analyze machine-learning transformations.

Where this is used later

Concepts explained in this chapter

← Chapter 1: An overview of machine learning and deep learning Chapter 3: Classifiers and vector calculus →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.