liveBook · Manning

Matrix-matrix multiplication

As explained in Inside Deep Learning: Math, Algorithms, Models

The binary operation that multiplies two matrices to produce a new matrix, written with the @ operator in Python and used here for performance comparison.

Matrix multiplication combines an n×m matrix X with an m×p matrix Y to produce an n×p matrix C. In PyTorch, the @ operator performs this operation. Matching the inner dimensions and checking tensor shapes helps prevent shape-related bugs. section 1.2.1

For GPU performance comparisons, benchmark enough work to account for the time needed to move data between the CPU and GPU. Small computations may run faster on the CPU. section 1.2.1

import timeit 
x = torch.rand(2**11, 2**11) 
time_cpu = timeit.timeit("x@x", globals=globals(), number=100)

Times 100 multiplications of a 2048×2048 matrix using Python’s timeit library. section 1.2.1

How Inside Deep Learning: Math, Algorithms, Models covers it

How deep it goes
Defines output dimensions and shows a timed PyTorch operation, without deriving the multiplication algorithm.
Examples
Benchmarks 100 multiplications of a 2048×2048 matrix with timeit.
What you can do after
Evaluate CPU and GPU runtimes by timing repeated matrix multiplications and considering data-transfer overhead.
How it connects
Connects matrix multiplication to tensor shapes, neural networks, PyTorch, and GPU acceleration.

Also explained in

Compare how 8 books explain matrix-matrix multiplication →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Inside Deep Learning: Math, Algorithms, Models. The section links point to the parts of the book it draws on.