LLM Learning Hub

workspace/llm-course/home

Matrices

A matrix is a 2D array of numbers with rows and columns, representing a linear transformation between vector spaces. Matrices map input vectors to output vectors via multiplication.

What is Matrices?

A matrix is a 2D array of numbers with rows and columns, representing a linear transformation between vector spaces. Matrices map input vectors to output vectors via multiplication.

A matrix is a 2D array of numbers with rows and columns, representing a linear transformation between vector spaces. Matrices map input vectors to output vectors via multiplication.

Where is it used?

Every `nn.Linear` layer stores a weight matrix; the QKV projection in attention uses three matrices; embedding tables are matrices of shape (vocab_size, d_model).

How to build it

Create a matrix with `torch.randn(4,3)`, multiply it by a vector with `torch.matmul(M, v)`, and verify the output shape matches the matrix's row count.

Code

A practical example:

example.pypython