Math for AI course · Lesson 34 of 180

Linear Mappings Preserve the Combinations AI Models Need

Test whether a transformation preserves sums and scaling, name its input and output spaces, and separate a neural layer from its activation.

Share this article

A neural network repeatedly turns one representation into another. To follow what a layer does, it helps to ask two questions: what kinds of vectors can go in, and what kinds can come out? A linear mapping answers these questions while preserving the ways vectors combine. This makes it possible to reason about matrix weights as transformations between spaces, then identify exactly where a network introduces nonlinear behavior.

By the end, you will be able to test a mapping for linearity, state its input and output spaces, and tell the matrix transformation in a neural layer apart from its activation.

Learning outcome: Test linearity and describe the input and output spaces of a mapping.

This lesson builds on the vector-space rules from Lesson 027. Section 2.7 of Mathematics for Machine Learning places linear mappings at the point where vector combinations begin to connect to coordinates and matrices. The calculations, neural-layer example, visual, and assessments here are original.

A transformation should respect how its inputs combine

Vectors can be added and scaled. For two real vector spaces VV and WW, a mapping T:V→WT:V\to W is linear when it respects both operations:

Here u,v∈Vu,v\in V and a,b∈Ra,b\in\mathbb{R}. The result is in WW, and the coefficients on the right scale the corresponding outputs.

You can test this one combined rule, or split it into two checks:

T(u+v)=T(u)+T(v),T(au)=aT(u).T(u+v)=T(u)+T(v), \qquad T(au)=aT(u).

The first says that adding two inputs before the transformation gives the same result as transforming each input and then adding. The second says that scaling an input before the transformation scales its output by the same amount. Both must hold for every valid input and every real scalar, including negative ones.

A linear map carries a sum into the same sum of imagesFor T(x₁, x₂) = (2x₁ + x₂, −x₁ + x₂), the image of e₁ + e₂ is the sum of the images of e₁ and e₂.

Input space ℝ²

Input space ℝ²Three arrows begin at the origin: e₁ = (1, 0), e₂ = (0, 1), and e₁ + e₂ = (1, 1).e₁e₂e₁ + e₂x₁x₂
  • e₁ = (1, 0)
  • e₂ = (0, 1)
  • e₁ + e₂ = (1, 1)

Output space ℝ²

Output space ℝ²The corresponding arrows are T(e₁) = (2, −1), T(e₂) = (1, 1), and T(e₁ + e₂) = (3, 0). The third output equals the sum of the first two.T(e₁)T(e₂)T(e₁ + e₂)y₁y₂
  • T(e₁) = (2, −1)
  • T(e₂) = (1, 1)
  • T(e₁ + e₂) = (3, 0)

Combination check: T(e₁ + e₂) = T(e₁) + T(e₂)

The three arrows show one concrete addition check. Here, T(e1+e2)=(3,0)T(e_1+e_2)=(3,0), and T(e1)+T(e2)=(2,−1)+(1,1)=(3,0)T(e_1)+T(e_2)=(2,-1)+(1,1)=(3,0). The visual offers a geometric reading; the formula is the general promise that must hold for all vectors and scalars, not only for these two basis vectors.

A matrix tells you which spaces the map connects

Let AA be an m×nm\times n matrix, meaning it has mm rows and nn columns. It multiplies an nn-coordinate column vector and produces an mm-coordinate column vector:

T:Rn→Rm,T(x)=Ax.T: \mathbb{R}^n\to\mathbb{R}^m, \qquad T(x)=Ax.

The domain is the set of inputs allowed by the mapping; the codomain is the output space it promises to land in. For this matrix rule, inputs are in Rn\mathbb{R}^n and outputs are in Rm\mathbb{R}^m. The dimensions need not be equal: a map can expand a vector into more coordinates or compress it into fewer.

Matrix multiplication is linear because it distributes over addition and scalar multiplication. For any vectors u,vu,v of length nn and scalars a,ba,b,

T(au+bv)=A(au+bv)=aAu+bAv=aT(u)+bT(v).T(au+bv)=A(au+bv)=aAu+bAv=aT(u)+bT(v).

For a small numerical example, use

A bias shifts the origin; an activation bends the rule

In machine-learning code, “linear layer” is familiar shorthand. In mathematics, the details determine whether the operation is linear. PyTorch’s torch.nn.Linear computes xAT+bxA^\mathsf{T}+b for row-oriented batches and includes the bias bb by default. With a nonzero bias, the rule is affine: it preserves combinations whose coefficients sum to one, but generally does not preserve arbitrary scaling or sums. The PyTorch documentation calls this an affine transformation and lets you disable the bias with bias=False (torch.nn.Linear documentation). It still preserves a weighted pair when the weights add to one:

F(λu+(1−λ)v)=λAu+(1−λ)Av+b=λ(Au+b)+(1−λ)(Av+b).F(\lambda u+(1-\lambda)v) =\lambda Au+(1-\lambda)Av+b =\lambda(Au+b)+(1-\lambda)(Av+b).

The last equality works because λb+(1−λ)b=b\lambda b+(1-\lambda)b=b. Ordinary vector addition uses coefficients 11 and 11, which do not add to one, so the bias does not cancel from F(u+v)F(u+v).

The zero vector is a quick diagnostic. Every linear map must send zero to zero, because T(0)=T(0⋅x)=0⋅T(x)=0T(0)=T(0\cdot x)=0\cdot T(x)=0. For an affine rule F(x)=Ax+bF(x)=Ax+b,

F(0)=A0+b=b.F(0)=A0+b=b.

If b≠0b\ne0, then FF cannot be linear. The bias does not make the layer incorrect; it gives the model the freedom to shift its output even when the input is zero.

Neural networks add another operation after the weight transformation. A common pattern is h=ReLU⁡(Ax+b)h=\operatorname{ReLU}(Ax+b), where ReLU replaces each negative coordinate with zero. It is nonlinear. For example, let r(t)=max⁡(0,t)r(t)=\max(0,t). Then r(−1+1)=r(0)=0r(-1+1)=r(0)=0, while r(−1)+r(1)=0+1=1r(-1)+r(1)=0+1=1. The addition test fails. PyTorch documents torch.nn.ReLU as this elementwise maximum operation (ReLU documentation).

This difference explains why networks combine these steps. If two layers were only linear maps, their composition would still be one linear map: B(Ax)=(BA)xB(Ax)=(BA)x. More layers would change the matrix, but not create a nonlinear input-output rule. An activation interrupts that collapse and lets the network represent more varied relationships. The weight matrix mixes input features; the activation changes the rule applied to the resulting coordinates.

Inspect the transformation in code

The same calculation can be checked with NumPy. Column vectors make the matrix-on-the-left convention match the derivation above:

import numpy as np

A = np.array([[2, 1],
              [-1, 1]])
u = np.array([1, 0])
v = np.array([0, 1])

def T(x):
    return A @ x
print(T(u + v))              # [3 0]
print(T(u) + T(v))           # [3 0]
print(T(-2 * u))             # [-4  2]
print(-2 * T(u))             # [-4  2]

For a batch in PyTorch, vectors are commonly stored as rows, so the library’s weight matrix is transposed in the displayed formula. The batch dimension stays the same while the final feature dimension changes from in_features to out_features:

import torch
from torch import nn

x = torch.tensor([[1.0, 0.0], [0.0, 1.0]])  # shape: (2 examples, 2 features)
linear_part = nn.Linear(2, 3, bias=False)
activation = nn.ReLU()

pre_activation = linear_part(x)  # shape: (2, 3); a linear map
hidden = activation(pre_activation)  # shape: (2, 3); nonlinear elementwise step

Here the linear map is from each feature vector in R2\mathbb{R}^2 to one in R3\mathbb{R}^3. This instance omits bias so its first operation is genuinely linear. With the default bias, PyTorch describes that operation as affine. The activation preserves the output shape but fails the vector-combination test. Shape agreement alone cannot tell you whether an operation is linear; the algebraic rule does.

A linear map can still lose information

Linearity does not say that a map is reversible, length-preserving, or even useful for every downstream task. The zero matrix maps every input to the zero vector and still satisfies both linearity rules. A linear map from R3\mathbb{R}^3 to R2\mathbb{R}^2 cannot be one-to-one: at least one direction of input information must disappear. To determine whether information is preserved, you need other ideas such as independence, rank, and invertibility.

When you inspect a neural block, write down the space before and after each operation, then test the rule each operation applies. A matrix multiplication transforms combinations predictably; a bias shifts the origin; an activation changes how combinations behave. That is the distinction to carry into the next lesson, where linear maps get concrete matrix representations.

Check your understanding

Question 1

State the two properties that a mapping must satisfy to be linear. What does a vector-addition check leave untested?

Show the step-by-step solution

A linear mapping must preserve addition and scalar multiplication: T(u+v)=T(u)+T(v)T(u+v)=T(u)+T(v) and T(cu)=cT(u)T(cu)=cT(u) for every valid u,vu,v and every real scalar cc. The combined rule follows by applying both properties: T(au+bv)=T(au)+T(bv)=aT(u)+bT(v)T(au+bv)=T(au)+T(bv)=aT(u)+bT(v). An addition check covers only sums of inputs; to establish the definition directly, also check scaling by every real number.

Question 2

For T(x₁, x₂) = (2x₁ + x₂, −x₁ + x₂), calculate T(3, 2). Show both output coordinates and name the input and output spaces.

Show the step-by-step solution

The matrix has two columns, so it accepts vectors in R2\mathbb{R}^2. It has two rows, so its output is in R2\mathbb{R}^2. Multiply each row by the input:

T(32)=(2⋅3+1⋅2−1⋅3+1⋅2)=(8−1).T\begin{pmatrix}3\\2\end{pmatrix} =\begin{pmatrix}2\cdot3+1\cdot2\\-1\cdot3+1\cdot2\end{pmatrix} =\begin{pmatrix}8\\-1\end{pmatrix}.

Thus this is a map T:R2→R2T:\mathbb{R}^2\to\mathbb{R}^2 and the output is (8,−1)(8,-1).

Question 3

Using the lesson's matrix, verify T((1, 0) + (0, 1)) = T(1, 0) + T(0, 1).

Show the step-by-step solution

Let u=(1,0)u=(1,0) and v=(0,1)v=(0,1). The worked example gives T(u)=(2,−1)T(u)=(2,-1) and T(v)=(1,1)T(v)=(1,1), so their sum is (3,0)(3,0). The input sum is u+v=(1,1)u+v=(1,1), and matrix multiplication gives T(1,1)=(3,0)T(1,1)=(3,0). Both sides match, verifying this particular addition example. This one example supports the rule but does not prove it for every vector; the distribution of matrix multiplication provides the general proof.

Question 4

For the same matrix and u = (1, 0), compute T(−2u) and compare it with −2T(u). What does this check test?

Show the step-by-step solution

First, T(u)=(2,−1)T(u)=(2,-1), so −2T(u)=(−4,2)-2T(u)=(-4,2). Also, −2u=(−2,0)-2u=(-2,0) and

T(−2u)=(21−11)(−20)=(−42).T(-2u)=\begin{pmatrix}2&1\\-1&1\end{pmatrix} \begin{pmatrix}-2\\0\end{pmatrix} =\begin{pmatrix}-4\\2\end{pmatrix}.

The results agree. This checks scalar preservation for a negative scalar, which matters because linearity requires the rule for every real scalar.

Question 5

A proposed layer is F(x) = Ax + (0, 2). Can F be linear? Use the zero-vector test and explain what the added vector does.

Show the step-by-step solution

For a linear map, T(0)T(0) must equal zero: scaling any input by 00 must scale its output to zero. Here, F(0)=A0+(0,2)=(0,2)≠(0,0)F(0)=A0+(0,2)=(0,2)\ne(0,0). Therefore FF is not linear. The added vector shifts every output by (0,2)(0,2), making this an affine map.

Question 6

A PyTorch block is `nn.Linear(4, 8)` followed by `nn.ReLU()`. Give the input and output feature spaces, and classify each operation.

Show the step-by-step solution

Each input example has four features, so it is a vector in R4\mathbb{R}^4. The linear layer produces eight coordinates, an element of R8\mathbb{R}^8. PyTorch enables bias by default, so nn.Linear(4, 8) is generally affine rather than mathematically linear. nn.ReLU() keeps the eight-coordinate shape but applies t↦max⁡(0,t)t\mapsto\max(0,t) to each coordinate, a nonlinear operation. With bias=False, the first operation is a linear map R4→R8\mathbb{R}^4\to\mathbb{R}^8.

Question 7

Show that ReLU fails to preserve addition using the inputs −1 and 1. Does its failure change the output dimension?

Show the step-by-step solution

ReLU maps tt to max⁡(0,t)\max(0,t). If it preserved addition, applying it to the sum would match adding the separately transformed values. But ReLU⁡(−1+1)=ReLU⁡(0)=0\operatorname{ReLU}(-1+1)=\operatorname{ReLU}(0)=0, while ReLU⁡(−1)+ReLU⁡(1)=0+1=1\operatorname{ReLU}(-1)+\operatorname{ReLU}(1)=0+1=1. Since the two values differ, ReLU is nonlinear. It still returns one number for each input number, so its output shape matches its input shape even though its rule is nonlinear.

Question 8

Is every linear mapping reversible or length-preserving? Give a linear counterexample for one of those claims.

Show the step-by-step solution

No. The zero map T(x)=0T(x)=0 is linear because T(u+v)=0=0+0T(u+v)=0=0+0 and T(cu)=0=cT(u)T(cu)=0=cT(u), but it maps every input to the same output and cannot be reversed. Length preservation also is not guaranteed: S(x1,x2)=(2x1,x2)S(x_1,x_2)=(2x_1,x_2) is linear, yet S(1,0)=(2,0)S(1,0)=(2,0) has length 22 instead of 11. Linearity describes how combinations behave, not whether lengths or information stay unchanged.

Sources

  1. Mathematics for Machine Learning book PDF, Section 2.7
  2. PyTorch documentation: torch.nn.Linear
  3. PyTorch documentation: torch.nn.ReLU