Video wird geladen...

Video konnte nicht geladen werden

Zur Startseite

➡️ Machine Learning from Scratch, part 37: softmax. Every neuron first adds up its inputs times their weights, then an activation function reshapes the result. Without activation functions, any number of layers would act like one single linear layer. Softmax is the special activation at the very end of...

0 Kommentare

Keine Kommentare verfügbar

Kommentare vom Original-Post werden hier angezeigt

Ähnliche Videos

➡️ Machine Learning from Scratch, part 38: cross entropy. Softmax (part 37) turns the raw scores of a network into one probability per class. Cross entropy is the loss function that scores how wrong those probabilities are, with one number. It looks only at the probability of the right class and takes minus its natural log. Right with 99 % costs 0.01. An unsure answer that gives the right class 40 % costs 0.92. If the network is 99 % sure of a wrong class, the right class gets 1 % and the cost jumps to 4.61. Training averages this cost over all examples and pushes the average down. Together with softmax, the push on every raw score is simply the predicted probability minus the truth, so the right class is pushed up, the wrong classes are pushed down, and a confident mistake gets the strongest push. With squared error through softmax, that push on a confident miss can be much weaker, which is one reason classifiers usually learn with cross entropy. Our own run, nothing downloaded. cross_entropy.py (16 lines of numpy) prints the three costs above, makes 300 points in three classes and trains the small network from part 37 (2 inputs, 4 hidden neurons, 3 output neurons) with cross entropy. The average loss falls from 1.01 to 0.13 in 1,000 steps. After training it gets 283 of the 300 points right, and the 17 it still gets wrong make up 73 % of the remaining loss. Honest notes: we train and measure on the same 300 points, so this is the training loss. On these easy points, the same network trained with squared error ends up about as accurate (284 of 300). The difference shows in the push on single confident mistakes: for our most confident miss at the start, 0.98 with cross entropy against 0.07 with squared error. The 3D surfaces show both losses over two raw scores of that real point, with the same 12 steps on each. #machinelearning #ai #crossentropy #neuralnetwork #deeplearning

MachineLearningToGo

11,400 Aufrufe • vor 1 Monat

➡️ Part 9 of learning ML code from scratch Six weights, one wrong answer, and not one of them knows it was at fault. Backpropagation is how they find out. Two questions guide the video: what even is backpropagation, and why do models need it? Step 1, the forward pass. Two numbers go into a tiny network, run through a hidden layer, and one number comes out. Ours says 0.700. We wanted 1.0, so square the difference and the loss is 0.09. That single number is how wrong the model is. Step 2, the question. Six weights produced that answer together. Which of them is to blame, and by how much? Step 3, the one idea. Change a single weight by a tiny amount and watch the error move. How much the error changes per unit of weight is the gradient of that weight. In the video you can see it happen: the weight wobbles and the error bar answers. Step 4, the shortcut. Every weight running into the same neuron starts from one shared number, the blame of that neuron. Multiply that blame by the value each weight carried and you have its gradient. One blame, computed once, and every weight into that neuron gets its own gradient from it. That is why backpropagation is fast enough to train anything at all. Step 5, backwards. The blame starts at the output and every layer hands its share to the layer in front of it, back through the same network the data came forward through. It goes layer by layer, and inside one layer every neuron is done together. Step 6, the update. The optimizer moves every weight against its own gradient, the whole weight matrix in one go, so no weight waits for its turn. All six gradients here are negative, so all six weights go up. After one step the answer reads 0.769. After sixty it reads 0.968 and the error is down to 0.001. The honest part: every gradient in the video was checked against the measured change of the error. Move that one weight, see what the error does, divide. Backprop and the measurement agree to nine decimal places, otherwise nothing would have rendered. #machinelearning #backpropagation #neuralnetworks #python #coding

MachineLearningToGo

212,200 Aufrufe • vor 1 Monat

➡️ Machine Learning from Scratch, part 47: the Hopfield network. In 1982 the physicist John Hopfield described a network that works as a memory, and 42 years later this work was part of the Nobel Prize in Physics. Each neuron is one pixel that is either on or off, and every neuron is connected to every other neuron, so 100 pixels need 4,950 connections. There are no layers: signals go back and forth until the network settles. Each neuron takes a weighted sum of its inputs and switches on if the sum is positive, off if it is negative. Storing a picture is simple: for every pair of pixels with the same colour the connection gets stronger, otherwise weaker. Every stored picture becomes a valley in the energy landscape, and no switch can ever raise the energy, so a noisy picture rolls downhill into the nearest stored memory. The code (hopfield.py, 16 lines of numpy, pictures in pics.txt) stores three 10 x 10 pictures, a heart, a house and a smiley, and flips 30 of the 100 pixels in each. After a single pass over all neurons all three come back exactly; the energy of the heart falls from -6.90 to -48.90, the energy of the stored heart. With 100 neurons and random patterns (10 flipped pixels, 20 runs) it recalls 5 patterns every time, 10 patterns in 94.5 % of the cases, 14 in 68 %, 20 in about one in four (24 %) and 30 in 1 %. Honest notes: the weights are kept as whole numbers (100 times the Hebb weight), so a sum of exactly zero switches the neuron on, as the rule says. The landscape in the video is a picture of the idea; the valley floors and the height of the rolling ball are the real energies from the run. #machinelearning #ai #coding #neuralnetworks #nobelprize

MachineLearningToGo

12,000 Aufrufe • vor 17 Tagen

➡️ Machine Learning from Scratch, part 35: supervised vs unsupervised learning. In supervised learning every training example comes with the right answer, a label. The model guesses, a loss function measures how wrong the guess was, and training makes that error smaller step by step. In unsupervised learning there are no labels. The model still has a goal, but it is measured on the data itself. k-means is the classic example: place a few group centers, give every point to its nearest center, add up the squared distances, move each center to the middle of its points and repeat. That total never gets larger, and after a few rounds the groups stop changing. Our own run, nothing downloaded. learn.py (16 lines of numpy) makes 300 points in 2D from 3 true groups and learns them both ways. With labels (a nearest-middle classifier trained on 200 points) it gets 100 of 100 new points right. Without labels, k-means brings the total squared distance down from 2927 to 741, and its groups match the true ones for 298 of 300 points. Honest notes: our three groups are made up and well separated on purpose, so both methods do very well; real data is messier. The 298 counts each found group as the true group most of its points belong to (here that is a clean one-to-one match). k-means starts from far-apart points, because a random start can get stuck in a worse grouping. The algorithm cannot know what a group means, so a person still has to name it. #machinelearning #ai #supervisedlearning #coding #python

MachineLearningToGo

26,000 Aufrufe • vor 1 Monat

➡️ Machine Learning from Scratch, part 31: the transformer. A transformer turns every token into a list of numbers, adds its position, and then lets every token look at the others. That step is called attention: each token has a query (what am I looking for), a key (what do I contain) and a value (what I pass on). The better a query matches a key, the more of that value flows in. Real transformers repeat this in many layers with several attention heads each. The design comes from the 2017 Google paper "Attention Is All You Need" by Ashish Vaswani and colleagues, first used for translation. A large language model is a transformer trained to predict the next token, again and again, on a huge amount of text. Our own run, nothing downloaded. attention.py is one self-attention layer in 16 lines of numpy. Every word gets a query, a key and a value of 8 numbers, starting random, trained from scratch with hand-written backpropagation on 192 made-up sentences like "the dog ate the carrot because it was hungry". The task: find out which pet "it" means. On 64 new test sentences "it" puts 99.95 percent of its attention on the right word on average (the lowest single sentence gets 99.9 percent; the program prints this as 100%). Before training it was 10 percent, spread evenly. The cat sentence from the video is one of the test sentences. Honest notes: our task is easy on purpose. Every sentence has exactly one pet, so "it" only has to learn to look for pets, and it learns that within the first pass. Real sentences with two candidates need more context, which is what many layers are for. Our small version has no positions, no scaling, no mask, one head, and only "it" asks a question. Every word of the test sentences also appears in training; the combinations are new. The 3D space in the video shows the real keys of our model, projected to three axes (two of them stretched so you can see them). The stacked layers are a sketch. #machinelearning #ai #transformer #python #llm

MachineLearningToGo

81,200 Aufrufe • vor 1 Monat

Dropout by hand ✍️ ~ 10 steps walkthrough below Dropout is the simplest trick in deep learning that actually works: during training you randomly switch neurons off, so the network cannot lean on any one of them. It is two lines of code and almost nobody has worked through what those lines do to the numbers. So I drew and calculated one entirely by hand. Goal: train one pass through a small network with two dropout layers, then run inference with dropout switched off. The network: Linear(2,4), ReLU, Dropout(0.5), Linear(4,3), ReLU, Dropout(0.33), Linear(3,2). = 1. Given = A training set of two examples, X1 and X2, and the weight matrices for all three linear layers. = 2. Draw the first random numbers = Let us draw 4 random numbers, one per neuron in the first hidden layer. Above 0.5 we keep (◯), below we drop (╳). Here that gives [◯, ╳, ◯, ╳]. = 3. Build the first dropout matrix = We turn that pattern into a diagonal matrix. The scaling factor is 1/(1-p) = 2, so a kept neuron gets 2 and a dropped one gets 0. Multiplying by it does both jobs at once: it deletes the 2nd and 4th neurons and doubles the two that survive. = 4. Draw the second random numbers = Let us do it again for the 3 neurons in the next layer, this time against p = 0.33. The result is [◯, ◯, ╳]. = 5. Build the second dropout matrix = We set the diagonal to 1.5 where kept and 0 where dropped. Only the 3rd neuron goes. = 6. Feed forward = Let us run the whole thing top to bottom: one matrix multiplication per layer, ReLU setting the negatives to zero, and the two dropout matrices doing their work in between. The outputs Y come out at the bottom. = 7. MSE loss gradients = We compare Y against the targets Y', subtract, and multiply each element by 2. That is the whole gradient of the mean squared error. = 8. Update the weights = Let us push those gradients back through the network and update the weights (marked in light red). = 9. Deactivate dropout = Training is over, so we set both dropout matrices to the identity. Every neuron is back, and nothing is scaled. = 10. Feed forward again = One more pass, this time on unseen data, to make the prediction. You have just trained and run a network with dropout by hand. ✍️ The outputs: Training outputs Y = [-6, 9; 13, 4] Loss gradients = [-4, 4; 6, -2] Inference outputs = [13, 13; 4, 3] 💾 Save this post! #AIbyHand #Dropout #DeepLearning #NeuralNetworks

Tom Yeh

14,442 Aufrufe • vor 4 Monaten

Backpropagation by hand ✍️ ~ 11 steps walkthrough below Backpropagation is the algorithm that actually trains a neural network, and it is where most people stop following along. It is not calculus you cannot do. It is matrix multiplication, working backward, one layer at a time. So I drew and calculated one entirely by hand. Goal: push the loss gradient back through a 3-layer network and land on a new value for every weight and bias. = 1. Given = A 3-layer perceptron, an input X, predictions Ypred = [0.5, 0.5, 0], and the truth Ytarget = [0, 1, 0]. = 2. Backprop gradient cells = Let us draw empty cells for every gradient we are about to compute. The shape of the answer comes first. = 3. Layer 3 softmax = We get dL/dz3 straight from Ypred minus Ytarget = [0.5, -0.5, 0]. No chain rule needed, and that shortcut is the whole reason softmax and cross-entropy are paired. = 4. Layer 3 weights and biases = Let us multiply dL/dz3 by [a2 | 1]. One multiplication gives the gradient for W3 and b3 together. = 5. Layer 2 activations = We multiply dL/dz3 by W3 to get dL/da2. The gradient moves back across a layer the same way the signal moved forward. = 6. Layer 2 ReLU = Let us pass it through the gate: keep the gradient where the activation was positive, zero it everywhere else. = 7. Layer 2 weights and biases = We multiply dL/dz2 by [a1 | 1]. The same figure as step 4, one layer up. = 8. Layer 1 activations = Let us multiply dL/dz2 by W2. = 9. Layer 1 ReLU = We apply the same gate again, now on a1. = 10. Layer 1 weights and biases = Let us multiply dL/dz1 by [x | 1], and every weight in the network now has a gradient. = 11. Update = We subtract, and the network has learned. In practice a learning rate scales this step. The gradients: dL/dz3 = [0.5, -0.5, 0] dL/da1 = [1, -2, 2, -1] dL/dz1 = [0, -2, 2, -1] The takeaway: matrix multiplication is all you need. Just like the forward pass, backpropagation is matrix multiplications end to end. You can do every one by hand, slowly and imperfectly, which is exactly why a GPU's ability to do them fast mattered so much to deep learning. 💾 Save this post!

Tom Yeh

964,224 Aufrufe • vor 4 Monaten

Self Attention by hand ✍️ ~ 9 steps walkthrough below Self-attention is what enables LLMs to understand context. How does it work? So I drew and calculated one entirely by hand. Goal: turn four 6D features into four 3D attention weighted features, filling in every cell yourself. = 1. Given = Four feature vectors, six dimensions each, one per position. = 2. Query, key, value = Let us multiply the features by WQ, WK and WV. Queries, keys and values all come out of the same four features, and that is what the word "self" is doing in self-attention. = 3. Prepare for MatMul = We copy the queries across the top and the transposed keys down the side. Lining the two up is half the work. = 4. MatMul = Let us multiply K transpose by Q. Every cell is the dot product of one key with one query, which we use as a matching score. That works because the dot product is the numerator of cosine similarity: it is how alike two vectors are, before anyone divides by their lengths. = 5. Scale = We divide by the square root of dk, the dimension of a key vector, here 3. Without it the scores grow with the dimension and a 64-wide head would swamp the softmax. To keep the page doable in pen, the drawing approximates dividing by root 3 with halving. = 6. e to the power = Let us raise e to the power of each score. This is the first half of softmax, and the drawing uses 3 in place of e, which is close enough to do in your head. = 7. Sum = We add up each column: 16, 6, 7 and 12. = 8. Normalize = Let us divide every cell by its column sum. That gives the attention weight matrix in yellow, and each of its four columns is now a probability distribution over the four positions. The decimals are nudged as they are rounded, so every column still sums to exactly 1. = 9. MatMul = We multiply the value vectors by those weights. Each output is a blend of all four values, mixed in the proportion the attention matrix just decided, and it goes to the position-wise feed forward network in the next layer: the FFN box at the bottom of the page. The outputs: Attention weights (A), by column = [.2, .6, 0, .2], [.2, .4, .2, .2], [.4, .2, 0, .4], [.1, .7, .1, .1] Attention weighted features (Z) = [8, 2, 6], [8, 4, 4], [16, 4, 2], [4, 2, 7] The takeaway: attention is a weighted average, and everything before step 9 exists to decide the weights. Compare every position with every other, turn the scores into one distribution per position, then blend. 💾 Save this post!

Tom Yeh

27,383 Aufrufe • vor 3 Monaten

➡️ Machine Learning from Scratch, part 48: the Boltzmann machine. In 1985 Hinton, David Ackley and Terrence Sejnowski published how a Boltzmann machine learns, starting from the Hopfield network of part 47. Like the Hopfield network it prefers pictures with low energy, but it also has hidden units next to the visible pixels, and its units switch on by chance: the sigmoid turns the weighted sum into a probability. So the machine never settles and visits low energy pictures more often. We use the restricted version, with connections only between the two layers. The code (boltzmann.py, 16 lines of numpy) trains a restricted Boltzmann machine with 9 visible and 8 hidden units on all 14 bars-and-stripes pictures of 3 x 3 pixels, with contrastive divergence (one dream step per round, 5,000 rounds). Then 1,000 dreams start from pure noise and switch back and forth 100 times: 855 of them became one of the 14 pictures, from 3 times for the empty picture to 142 times for one with two bars. Sources: Nobel Prize in Physics 2024, press release of 8 October 2024 (Hopfield and Hinton, "for foundational discoveries and inventions that enable machine learning with artificial neural networks"); D. H. Ackley, G. E. Hinton, T. J. Sejnowski, "A Learning Algorithm for Boltzmann Machines", Cognitive Science, 1985; G. E. Hinton, "Training Products of Experts by Minimizing Contrastive Divergence", Neural Computation, 2002. Honest notes: the pictures do not come up equally often, because one dream step per round is only a shortcut for the real learning rule. When two of the 14 pictures are left out of training, our small machine does not dream them up; new examples of the same kind come from large machines trained on many examples. #machinelearning #ai #boltzmannmachine #neuralnetworks #nobelprize

MachineLearningToGo

14,700 Aufrufe • vor 16 Tagen

Game theory says the only rational answer is 1. Ben Polak had the smartest students at Yale play it for real - and 1 lost every single year. The winning number was 9, and being right is exactly what got you beaten. The game is simple. Everyone picks a whole number from 1 to 100. Whoever lands closest to two-thirds of the class average wins the money. Work it backwards and the logic is airtight. If people guess at random the average is 50, so you should pick two-thirds of that, 33. But if everyone thinks that far, the average is 33, so you pick 22. Chase the reasoning to its end and every number collapses to 1 - the Nash equilibrium, the only choice that survives if everyone is rational, knows everyone is rational, and knows that they know, forever. So 1 is the "correct" answer. It also loses. The reason is a model of how deep people actually reason: g = 50 × (2/3)^k Start at 50, the random baseline. Each k is one round of "assume everyone else divides by two-thirds." Level 1 gives 33. Level 2 gives 22. Level 3 gives about 15. The Nash player sits at k = infinity and lands on 1. Real humans do not go to infinity. Polak's class averaged 13.33, which the model places at roughly level 3. Two-thirds of 13.33 is 8.9. Round it, and 9 wins. Anyone who played the "rational" 1 was several levels too deep and finished nowhere. This is not a one-off. The same game run on Yale classes came in at 18.5, then 21.5, then 23, then 13.33. Never close to 1. The crowd reasons two to three levels deep and no further, so the money sits near two-thirds of that, not at the equilibrium. It generalizes anywhere the payoff depends on the crowd rather than the theory. A market can stay "wrong" for years because the winning move is not fair value, it is two-thirds of the average guess at fair value. Keynes called it the beauty contest sixty years before anyone put a number on it. The equilibrium tells you where the game ends if everyone is perfect. The money is one step ahead of where the crowd actually stands. Model the players, not the theory.

Orion

177,819 Aufrufe • vor 3 Monaten

LSTM by hand ✍️ ~ 15 steps walkthrough below Since Hochreiter and Schmidhuber introduced them in 1997, LSTMs were the most effective way to handle long sequences, right up until the Transformer wave. They are a recurrent network: they read one input at a time and carry a memory forward. Lately recurrence is back in fashion (Mamba), because attention does not scale to hundreds of thousands of tokens. So I drew and calculated one entirely by hand. Goal: run an LSTM cell over a sequence of three inputs, filling in every gate and memory cell yourself. 1. Given Three inputs X1, X2, X3, and four weight matrices: forget, input, candidate, and output. 2. Initialize Let us set the previous hidden state h0 and the memory cell C0 to their start values. 3. Linear transform We multiply the four weight matrices by the stack of the current input, the previous hidden state, and a 1. 4. The gates Let us squash three of those results with sigmoid, giving the forget, input, and output gates, each between 0 and 1. 5. Update the memory We forget part of the old memory (C0 times the forget gate) and add the new (the candidate times the input gate). That is the new memory C1. 6. Candidate output Let us apply tanh to the new memory. 7. Update the hidden state We multiply that candidate by the output gate. The result is h1. 8. Process X2 Copy h1 and C1 forward, then repeat the whole cell: linear transform, the gates, update memory to C2, output gate to h2. 9. Process X3 Once more. Copy h2 and C2 forward, repeat, and read off h3, the final hidden state. Now you can show off to your friends that you calculated an LSTM by hand. ✍️😉 💾 Save this post! #AIbyHand #LSTM #DeepLearning

Tom Yeh

18,144 Aufrufe • vor 4 Monaten

CLIP by hand ✍️ ~ 13 steps walkthrough below CLIP, Contrastive Language-Image Pre-training, is OpenAI's answer to a question that sounds impossible: how do you put a sentence and a picture in the same space? CLIP shipped when OpenAI was still open, and those embeddings were shared far and wide. Almost every multimodal model you use today descends from them. How does it work? Goal: learn one shared embedding space for text and images. = 1. Given = A mini batch of three text-image pairs. OpenAI trained the original on 400 million. = 2. Text to vectors = Let us look up each word with word2vec. = 3. Image to vectors = We cut each image into two patches and flatten them. Now text and pixels are both just numbers. = 4. The other pairs = Repeat steps 2 and 3 for the rest of the batch. = 5. Encode = Let us push both sides through their encoders, a linear layer and a ReLU. In practice these are transformers, but the shape of the operation is the same. = 6. Mean pooling = We average across the columns, so each image and each sentence collapses to a single vector. = 7. Projection = The text vectors are 3D and the image vectors are 4D, so they cannot be compared at all. A linear layer projects both to 2D. That 2D space is the shared embedding space, and getting here is the whole point of the model. = 8. Prepare for matmul = Let us copy the text vectors down and the transposed image vectors across. = 9. MatMul = We multiply, which takes the dot product of every text vector with every image vector. Each cell is one estimate of how well a sentence matches a picture. = 10. Softmax, e to the power = Raise e to each cell. To keep it hand sized we approximate e with 3. = 11. Softmax, sum = Sum each row for image to text, each column for text to image. = 12. Softmax, normalize = Divide, and out come two similarity matrices, one per direction. = 13. Loss gradients = The targets are identity matrices: a pair that belongs together should score 1, every other cell 0. Subtract the target from the similarity and you have the gradients, in both directions. The takeaway: pairing a picture with a sentence comes down to a single dot product. Everything before step 9 is the work of getting them into one shared space, so that the dot product finally means something. 💾 Save this post!

Tom Yeh

20,929 Aufrufe • vor 3 Monaten