Vectorization Explained: Batch Processing & Matrix Multiplication | Deep Learning From Scratch
Keduka
0:00 / 0:00
Vectorization Explained: Batch Processing & Matrix Multiplication | Deep Learning From Scratch
84 просмотра · 2 нед. назад
Keduka
33 подписчика
84 просмотра · 2 нед. назад
One loop or one matrix multiplication: the same answer, hundreds of times faster. See how a neural network handles a whole batch of samples in one step, worked by hand and checked in Python.
Welcome to Keduka AI School’s Deep Learning From Scratch. This lesson is about vectorization: how a neural network processes a whole batch of samples at once, with one matrix multiplication instead of a Python loop. It starts with vectors, matrices and their shapes, builds matrix multiplication from dot products by hand, stacks samples as the rows of a batch X, and follows that batch through a dense layer, Y = XW + b, with the shape rule and the broadcasting of the bias. A complete worked example is done by hand and checked in Python with NumPy, TensorFlow and PyTorch, and a benchmark shows where the speedup comes from.
Continue learning: https://keduka.com/
Watch the full playlist: • Deep Learning From Scratch
IN THIS LESSON
• Vectors and matrices, and why every shape lists the rows first: (4,) and (3, 3)
• What vectorization means: one operation on whole arrays instead of a loop over scalars; element-wise A * B
• Matrix multiplication, Cᵢⱼ = Σₖ AᵢₖBₖⱼ, one dot product per entry: A @ B = [[19, 22], [43, 50]], not A * B
• Batches: one row per sample, and one matrix that moves every sample at once
• One layer, one call: Y = XW + b on a (2, 3) batch, the same eight numbers in NumPy, TensorFlow and PyTorch
• The shape rule (m × n) · (n × p) → (m × p): (5, 10) @ (10, 8) gives (5, 8), and the error when the inner sizes differ
• Broadcasting the bias: align the shapes from the right; (2, 4) + (1, 4) gives (2, 4)
• A worked example by hand: Y = [[5.1, 6.2], [11.5, 14.2], [17.9, 22.2]], by a loop and by one call
• A small layer class with ReLU or sigmoid
• The loop against one call: the same multiply-adds, about 334 times faster in our run (it depends on your computer)
• Batch size and the cost of one sample; a dense layer and a convolution (im2col) as one matrix product
• Exercises, and the common mistakes, each explained
CHAPTERS
00:00 One at a time, or all at once
01:27 Vectors and matrices
02:38 What vectorization means
04:25 Matrix multiplication
06:31 Batches
08:13 One layer, one call
09:46 Shapes must line up
11:34 Broadcasting the bias
13:04 A worked example, by hand
15:20 A layer with an activation
16:32 The loop against one call
18:38 Batch size and cost
19:44 Every layer is a matrix product
21:17 Try it yourself
22:55 Common mistakes
23:59 Conclusion
TRY IT YOURSELF (pause and work it out first)
1. In the worked example, X has the rows (1, 2, 3, 4), (5, 6, 7, 8) and (9, 10, 11, 12); the columns of W are (0.1, 0.3, 0.5, 0.7) and (0.2, 0.4, 0.6, 0.8); and b = (0.1, 0.2). Compute the third row of Y = XW + b by hand.
2. A dense layer takes a batch X of shape (32, 784) and has 128 neurons. What shapes do W, b and Y have?
Answers: 1. 9 · 0.1 + 10 · 0.3 + 11 · 0.5 + 12 · 0.7 = 0.9 + 3.0 + 5.5 + 8.4 = 17.8, plus 0.1 gives 17.9; 9 · 0.2 + 10 · 0.4 + 11 · 0.6 + 12 · 0.8 = 1.8 + 4.0 + 6.6 + 9.6 = 22.0, plus 0.2 gives 22.2. So the third row of Y is (17.9, 22.2). 2. W is (784, 128), b is (1, 128) and Y is (32, 128): the inner sizes 784 and 784 match.
Subscribe to Keduka AI School for visual lessons that build your understanding of artificial intelligence, one idea at a time.
#Vectorization #MatrixMultiplication #MathForAI #DataScience #KedukaAISchool
#AI #MachineLearning #DeepLearning #NeuralNetworks #Transformers #ComputerVision #DeepLearningTutorial #AIEducation