Matrix-vector multiplication applies a matrix to a compatible column vector and returns a vector. Each output element is the dot product of one matrix row with the input vector. This packages many weighted sums into one operation, making it useful for processing an entire training dataset or neural-network layer at once. section 2.5.3
For a data matrix X and weight vector w, a biased weighted-sum model can be written as y = Xw + b. If X has m rows and n columns, w has n elements and y has m elements; the bias is then added to the resulting vector. section 2.5.3 section 2.7.3
In a neural network, the same operation expresses a complete linear layer:
- Each row of W contains the weights feeding one neuron in the next layer. section 8.3.1
- Multiplying W by the previous layer's activation vector produces all neuron weighted sums together. section 8.3.1
- Adding a bias vector and applying an activation function completes the layer's forward computation. section 8.3.1
A PyTorch computation
With X shaped as 15 × 2 and w shaped as 2 × 1, torch.matmul(X, w) produces a 15 × 1 result. Adding b = 5.0 shifts every output: y = torch.matmul(X, w) + b. section 2.7.3
Figure 8.6 Linear layer outputting layer l from layer (l − 1). The weights belonging to row 1 of the weight matrix (coming from all the input neurons, layer (l − 1), which sum together to form output neuron 1) are shown in bold.
Trace how rows of a weight matrix collect incoming neuron activations to produce the outputs of the next layer. section 8.3.1
