Building makemore Part 3: Activations & Gradients, BatchNorm

Sdílet
Vložit
  • čas přidán 16. 06. 2024
  • We dive into some of the internals of MLPs with multiple layers and scrutinize the statistics of the forward pass activations, backward pass gradients, and some of the pitfalls when they are improperly scaled. We also look at the typical diagnostic tools and visualizations you'd want to use to understand the health of your deep network. We learn why training deep neural nets can be fragile and introduce the first modern innovation that made doing so much easier: Batch Normalization. Residual connections and the Adam optimizer remain notable todos for later video.
    Links:
    - makemore on github: github.com/karpathy/makemore
    - jupyter notebook I built in this video: github.com/karpathy/nn-zero-t...
    - collab notebook: colab.research.google.com/dri...
    - my website: karpathy.ai
    - my twitter: / karpathy
    - Discord channel: / discord
    Useful links:
    - "Kaiming init" paper: arxiv.org/abs/1502.01852
    - BatchNorm paper: arxiv.org/abs/1502.03167
    - Bengio et al. 2003 MLP language model paper (pdf): www.jmlr.org/papers/volume3/b...
    - Good paper illustrating some of the problems with batchnorm in practice: arxiv.org/abs/2105.07576
    Exercises:
    - E01: I did not get around to seeing what happens when you initialize all weights and biases to zero. Try this and train the neural net. You might think either that 1) the network trains just fine or 2) the network doesn't train at all, but actually it is 3) the network trains but only partially, and achieves a pretty bad final performance. Inspect the gradients and activations to figure out what is happening and why the network is only partially training, and what part is being trained exactly.
    - E02: BatchNorm, unlike other normalization layers like LayerNorm/GroupNorm etc. has the big advantage that after training, the batchnorm gamma/beta can be "folded into" the weights of the preceeding Linear layers, effectively erasing the need to forward it at test time. Set up a small 3-layer MLP with batchnorms, train the network, then "fold" the batchnorm gamma/beta into the preceeding Linear layer's W,b by creating a new W2, b2 and erasing the batch norm. Verify that this gives the same forward pass during inference. i.e. we see that the batchnorm is there just for stabilizing the training, and can be thrown out after training is done! pretty cool.
    Chapters:
    00:00:00 intro
    00:01:22 starter code
    00:04:19 fixing the initial loss
    00:12:59 fixing the saturated tanh
    00:27:53 calculating the init scale: “Kaiming init”
    00:40:40 batch normalization
    01:03:07 batch normalization: summary
    01:04:50 real example: resnet50 walkthrough
    01:14:10 summary of the lecture
    01:18:35 just kidding: part2: PyTorch-ifying the code
    01:26:51 viz #1: forward pass activations statistics
    01:30:54 viz #2: backward pass gradient statistics
    01:32:07 the fully linear case of no non-linearities
    01:36:15 viz #3: parameter activation and gradient statistics
    01:39:55 viz #4: update:data ratio over time
    01:46:04 bringing back batchnorm, looking at the visualizations
    01:51:34 summary of the lecture for real this time
  • Věda a technologie

Komentáře • 316

  • @mileseverett
    @mileseverett Před rokem +322

    Andrej, as a third year PhD student this video series has given me so much more understanding of the systems I take for granted. You're doing incredible work here!

  • @leopetrini
    @leopetrini Před rokem +256

    1:30:10 The 5/3 gain in the tanh comes for the average value of tanh^2(x) where x is distributed as a Gaussian, i.e.
    integrate (tanh x)^2*exp(-x^2/2)/sqrt(2*pi) from -inf to inf ~= 0.39
    The square root of this value is how much the tanh squeezes the variance of the incoming variable: 0.39 ** .5 ~= 0.63 ~= 3/5 (hence 5/3 is just an approximation of the exact gain).
    We then multiply by the gain to keep the output variance 1.

    • @peace-wink
      @peace-wink Před rokem +2

      Thank you : )

    • @MonkeyKong21
      @MonkeyKong21 Před rokem

      i hope they're using the actual value and just writing 5/3 in the docs as slang

    • @Abhishekkumar-qj6hb
      @Abhishekkumar-qj6hb Před rokem

      Hi leopetrini could you check my comment above n answer if possible

    • @Zoronoa01
      @Zoronoa01 Před 9 měsíci

      Thank you for the insight!

    • @lennixplayzpokemon1239
      @lennixplayzpokemon1239 Před 9 měsíci

      @leopetrini Can you explain how you calculated the integral?

  • @Erosis
    @Erosis Před rokem +147

    This has to be the best hands-on coding tutorial for these small yet super-important deep learning fundamentals online. Absolutely great job!

  • @parent5x
    @parent5x Před rokem +58

    Andrej you have a wonderful gift for educating others. I’m a self learner of NNs and it’s a painful process but you seriously help ease that suffering… much appreciated! Ty

    • @Umar-Ateeq
      @Umar-Ateeq Před 3 měsíci

      Great, I am also going through this same painful process. Can you suggest something that can help ease this pain?

    • @hetanshpatel8521
      @hetanshpatel8521 Před 2 měsíci

      If you want to learn the theory. Try Soheil feizi . He is professor at UMD. Amazing teacher. And the course content is just top notch.

  • @nkhuang1390
    @nkhuang1390 Před rokem +39

    Every time another Andrej Karpathy video drops, its like Christmas for me. This video series has helped me to develop genuine Intuition about how neural networks work. I hope you continue to put these out, its making a massive impact on making these "black box" technologies accessible to the anyone and everyone!

  • @ragibshahriyear3682
    @ragibshahriyear3682 Před měsícem +3

    Thank you Mr. Karpathy. I am in love with your teaching. Given how accomplished and experienced you are, you are still teaching with such grace. I dream about sitting in one of your classes and learning from you, and I hope to meet you one day. May you be blessed with good health. Lots of love and respect.

  • @khuongtranhoang9197
    @khuongtranhoang9197 Před rokem +12

    Turns out this should be the way to teach machine learning: a combination of theory reference and actual coding. Thank you Andrej!

    • @american-professor
      @american-professor Před 5 měsíci +1

      Exactly. My DL course back in 2015 had a ton of obscure math and no coding. I had no idea how to train NNs after that course. I'm rediscovering and learning a ton of stuff from this video alone, way more than from my course.

  • @chanep1
    @chanep1 Před 11 měsíci +5

    I like that not even the smallest detail is pulled out of thin air, everything is completely explained

  • @hungrydeal6154
    @hungrydeal6154 Před rokem +19

    To put BatchNorm into perspective, I am going through Geoffrey Hinton's 2012 lecture notes on bag of tricks for mini-batch descent, it was when AlexNet was first published. Hinton was saying there was no one best way for learning method/gradient descent with mini-batches. Well, here it is BatchNorm. Hinton: "Just think of how much better neural nets will work, once we've got this sorted out". We are living in that future :)

    • @amgad_hasan
      @amgad_hasan Před rokem

      What did he mean by "Hinton was saying there was no one best way for learning method/gradient descent with mini-batches"?
      Did he mean initializing them?

  • @CuriousAnonDev
    @CuriousAnonDev Před rokem +7

    I really enjoy learning and listening to people like Andrej who love what they do and aren't doing it just for money. Shared the channel with my friends ☺️
    Keep up the great work Andrej!

  • @project-hq
    @project-hq Před rokem +5

    This whole series is absolutely amazing. Thank you very much Andrej! Being able to code along with you, improving a system as my own knowledge improves is fantastic

  • @1knmd
    @1knmd Před rokem +5

    The quality of these lectures is out of the charts. This channel is a gold mine!. Andrej, thank you, thank you very much for these lectures.

  • @lkothari
    @lkothari Před rokem +6

    You're a great teacher Andrej. This has been by far the most interesting ML course/training I have come across. Keep up the good work!

  • @minhajulhoque2113
    @minhajulhoque2113 Před rokem +2

    The batch normalization explanation was amazing! Thank you for your hard work and concise and clear explanations.

  • @vitorzucher435
    @vitorzucher435 Před 5 měsíci +2

    You have some much depth in your knowledge,
    Yet, you manage to explain complex concepts with such and incredible didactics.
    This is someone who truly understands his field. Andrej, thank you so much and even more for the humility in how you do it so.
    You explain how libraries and languages like python and pytorch work and dive into the WHYs on why things are happening.
    This is absolute priceless.

  • @ptrckqnln
    @ptrckqnln Před rokem +5

    This is filling in a lot of gaps for me, thank you! I especially appreciate your insights about reading a network's behavior during training; they gave me a few epiphanies.

  • @hlinc2
    @hlinc2 Před rokem +13

    This series is definitely the clearest presentation of ML concepts I have seen, but the teaching generalizes so well. I’ll be using this step-by-step intuition-building approach for all complicated things from now on. Nice that the approach also gives a confidence that I can understand stuff with enough time. Truly appreciate your doing this.

  • @IchibanKanobee
    @IchibanKanobee Před 6 měsíci +4

    These video series are exceptional. The clarity and practicality of the tutorials are unmatched by any other course. Thank you for the invaluable help for all practitioners of deep learning!

  • @kimiochang
    @kimiochang Před rokem +1

    I completed this one today, and I just want to show Andrej my gratitude. Looking forward to the next one. Thank you very much, Andrej. Thank you!

  • @siddharth-gandhi
    @siddharth-gandhi Před rokem

    It's so nice to have you back on CZcams! Thanks for teaching me Rubik's Cube back in the day and thanks for teaching us deep learning now!

  • @enchanted_swiftie
    @enchanted_swiftie Před rokem +2

    So many small things, scrutinzers and how easily he has pointed them out one by one, step by step, problem to solution is just amazing. Love your work Andrej. You are amazing.

  • @thodorispaparrigopoulos8542
    @thodorispaparrigopoulos8542 Před 5 měsíci +13

    I cannot fathom that this video only has 4k likes... He is literally explaining stuff that no one else goes through, because they simply don't know them, but they are crucial!

  • @tsunamidestructor
    @tsunamidestructor Před rokem +37

    Thank you so much for this, Andrej! Your series single-handedly revitalized my love for deep learning! Please keep this series going :)

    • @eustin
      @eustin Před rokem +6

      it's done the same for me. i'm excitedly going through each video. it feels good to be back!

  • @scottsun345
    @scottsun345 Před rokem +2

    Thank you, Andrej, amazing content! As a beginner in deep learning and even in programing, I find most materials out there are either pure theories or pure API applications, and they rarely come this deep and detailed. Your videos cover not just the knowledge of this field, but also so many empirical insights that came from working on actual projects. Just fantastic! Please make more of these lessons!

  • @philipwoods6720
    @philipwoods6720 Před rokem +1

    These videos have been incredible. Thank you so much for taking the time to make them, and I look forward to all the future ones!!!

  • @mrmiyagi26
    @mrmiyagi26 Před rokem +1

    Thank you for the deep dive into batch normalization and diagnostic approaches! Really useful to see it explained from the paper with the code.

  • @eitanporat9892
    @eitanporat9892 Před rokem

    Thank you for delving deep into the nitty-gritty details. I appreciate the exercises!

  • @1997benjaminvh
    @1997benjaminvh Před rokem +2

    This is awsome!!
    Thank you so much for taking the time to do this Andrej.
    Please keep this going, I am learning so much from you.

  • @MLwithAmir
    @MLwithAmir Před rokem

    These videos are so useful, Andrej thank you so much. The parts when you wrap up the lecture, and then change your mind to add more content are my fav. 😄

  • @gonzaloalbornoz7279
    @gonzaloalbornoz7279 Před rokem +1

    You explain very simple things that I know and always give me a new perspective on it!. Your way of transmitting knowledge is incredible.

  • @yonastesh7830
    @yonastesh7830 Před rokem

    Andrej, thank you so much for your tutorials here. You've no idea how much your videos helped me. Please keep doing more videos.

  • @theusualcouple
    @theusualcouple Před 4 měsíci

    Thank you @Andrej for bringing this series. You are a great teacher, the way you have simplified such seemingly complex topics is valuable to all the students like me. 🙏

  • @fhools
    @fhools Před rokem

    These are some of the best lectures i've ever seen. I love the explaination in the first part about tanh saturation. Really trying to get the viewer to develop intuition.

  • @8eck
    @8eck Před rokem

    Thank you Andrej, i have finally found time to go through your lectures. I have learn and understood a lot more than before, thank you.

  • @rafaelsouza4575
    @rafaelsouza4575 Před rokem

    oh man, this is top-notch content! Not sure if there are other available contents on these topics with so much clearness about its inner gears with reproducible examples. Thank you so much! You're a DL hero.

  • @divelix2666
    @divelix2666 Před rokem

    Can't even explain how impactful this video for my understanding of nns... Thank you so much!

  • @cktse_jp
    @cktse_jp Před 7 měsíci

    Your choice of visualizations as diagnostic tool is super insightful. Thanks so much for sharing your experience.

  • @swarajnanda5990
    @swarajnanda5990 Před rokem +6

    My mind is totally blown at the detail I am getting. Feel like this is an ivy league level course, with the content so meticulously covered.

  • @rmajdodin
    @rmajdodin Před rokem +8

    1:33:30 The reason the gradients of the higer layers have a bigger deviation (in the absence of tanh layer), is that you can write the whole NN as a sum of products, and it is easy to see that each weight of Layer 0 appears in 1 term, of layer 1 in 30 terms, of layer 2 in 3000 terms and so on. Therefore a small change of a weight in higer layers changes the output more.

  • @ernietam6202
    @ernietam6202 Před 11 měsíci

    Really enjoy your classes. I learnt a lot of tips for training and feel comfortable now. Will continue finishing this series.

  • @juleswombat5309
    @juleswombat5309 Před rokem

    These lectures are an awesome gift to us mortals. Such a clear explanation on the principles of neural networks.
    I only need to be able to afford access to massive TPU cloud compute and huge corpus, but at least I can now gain insight and understand the principles of these technologies.

  • @vpamula1
    @vpamula1 Před 2 měsíci +4

    A lot of material packed in this video; I envision understanding the mechanics of these networks to take many years, even with some prior experience.

  • @american-professor
    @american-professor Před 5 měsíci +2

    You are such a gold mine of knowledge, it's insane. I wish you were my DL professor during my PhD.

  • @AbhishekSingh-ee2bo
    @AbhishekSingh-ee2bo Před rokem

    Thank you.. I am a self learner and your series has been a milestone for me.

  • @greatwall2003
    @greatwall2003 Před 2 měsíci

    Great material on the intricacies of how neural networks work. Until now, I hadn't paid attention to the distribution of values entering the activation layers, and as it turns out, this is an extremely important issue. Thanks!

  • @moalimus
    @moalimus Před rokem +4

    Thanks very much for this, please keep them coming, you are changing lives.

  • @AlexTang99
    @AlexTang99 Před měsícem

    This is the most amazing video on neural network mathematics knowledge I've ever seen; thank you very much, Andrej!

  • @vil9386
    @vil9386 Před 3 měsíci

    I don't think any other book or blog or videos cover what Andrej has covered. Awesome insights. THANK YOU Andrej.

  • @houbenbub
    @houbenbub Před rokem

    following your lectures is a delight! Thanks for taking the time to make them :)

  • @the-hanhpham8950
    @the-hanhpham8950 Před rokem

    Thank you so much for the time and effort put into the videos of this series. Appreciate it very much.

  • @AboutOliver
    @AboutOliver Před 6 měsíci +1

    You're an incredible teacher. You really have a gift. Thanks for these lectures!!!!

  • @styssine
    @styssine Před 4 měsíci

    This is a great lecture, especially the second half building intuition about diagnostics. Amazing stuff.

  • @sanjaybhatikar
    @sanjaybhatikar Před 5 měsíci +1

    I keep coming back to these videos again and again. Andrej is legend!

  • @PureArtMV
    @PureArtMV Před 6 měsíci +2

    What a gem. Thanks for the lectures, Andrej

  • @dengzhonghan5125
    @dengzhonghan5125 Před 11 měsíci

    Your lecture is so amazing. Please keep updating, thanks for sharing and educating.

  • @Lauren-qj6ti
    @Lauren-qj6ti Před 6 měsíci +1

    What an inspiration. Like others have alluded in the comments, I find Andrej's teaching so remarkably therapeutic.

  • @lucianovidal8721
    @lucianovidal8721 Před 5 měsíci

    The amount of useful information in this video is impressive. Thanks for such good content.

  • @dang242
    @dang242 Před 11 měsíci

    Thank you for explaining everything in such detail. It makes everything much more understandable

  • @mPajuhaan
    @mPajuhaan Před rokem

    Very interesting how you've described the concept of pre-tuning NN.

  • @user-og6xo6iw9h
    @user-og6xo6iw9h Před rokem +1

    love the short snippets about how to implement these tools in production.

  • @afbf6522
    @afbf6522 Před rokem +1

    Amazing explanation about the nitty gritty details of Deep Learning, the "dark arts" of the trade.

  • @ShouryanNikam
    @ShouryanNikam Před 6 měsíci

    Thanks for making this! It's such an honor to be learning from you!

  • @adamskrodzki6152
    @adamskrodzki6152 Před 4 měsíci

    Amazing, knowledge that is hell hard to find in other videos and also, you have AMAZING skill in clearly explaining complex stuff.

  • @ukaszlipniak2365
    @ukaszlipniak2365 Před rokem

    I'm learning a lot. Thanks for these lectures!

  • @yazanmaarouf48
    @yazanmaarouf48 Před 5 měsíci

    I have shot myself in the foot multiple times before these videos. Training big models are much more difficult than I initially anticipated. Time wasted sadly. But I have more confidence in myself thanks to these video. Thanks Andrej

  • @jeanchristophe15
    @jeanchristophe15 Před 3 měsíci

    Thank you so much for your clear and thoughtful explanation Andrej!

  • @kemalware4912
    @kemalware4912 Před rokem

    My life is much better now because of your videos.

  • @hole62
    @hole62 Před rokem

    As a former HTML nerd, I am forever indebted to the amount of precise calculations and their limits, as is expressed in this video…

  • @punto-y-coma7890
    @punto-y-coma7890 Před 3 měsíci

    Awesome explanation Andrej! Than you very much for sharing your knowledge.

  • @peterhojnos6705
    @peterhojnos6705 Před rokem +1

    Zdravím, díky, že si sa dal na tvorbu videí pre širšiu verejnosť!

  • @alexandermedina4950
    @alexandermedina4950 Před rokem

    Amazing video tutorial, thank you professor Andrej.

  • @sauloviedo2677
    @sauloviedo2677 Před rokem

    Finallyyy!!! I was nervious waiting for the new video! Thank you Andrej!!!

  • @GregorUrbanekmenticorpAG

    Andrej thanks very much for your wonderful, educational videos. they are here to stay. A suggestion: wouldn't it be nice to prop up the resolution and contrast of the screen capture to make it more easy on the spectators eyes?

  • @8eck
    @8eck Před rokem

    Wow, didn't knew that there is such content and from Karpathy himself. Thank you!

  • @robertcowher
    @robertcowher Před 2 měsíci

    If you decide to make more content, a video series like this with a focus on self-driving or RL for robotics would be awesome. Not that you don't have enough going on, but that's my wish-list item :) Thanks for putting an incredibly in-depth resource out here free on the internet.

  • @sarai3538
    @sarai3538 Před rokem

    Thanks for the great explanation of activation,gradient as well as histogram.

  • @riponsaha8320
    @riponsaha8320 Před rokem

    This tutorial is so engaging and insightful.

  • @tildarusso
    @tildarusso Před rokem

    Nice to be lectured again after watching the Stanford CS231 multiple times!

  • @JuliusSmith
    @JuliusSmith Před 5 měsíci

    Thanks for the fantastic download! You have changed my learning_rate in this area from 0.1 to something >1!

  • @ramboli4118
    @ramboli4118 Před rokem

    I finally understand when Statistics come into play in machine learning. It's when you introduce the randomized weights(matrices)!

  • @josephngari1659
    @josephngari1659 Před rokem

    I absolutely LOVE this series!!!!

  • @ianfukushima1316
    @ianfukushima1316 Před rokem

    Thank you! You made me happy with these lectures :)

  • @aureliencobb199
    @aureliencobb199 Před rokem

    Thank you for the lectures. I really appreciate the effort.

  • @thegimel
    @thegimel Před rokem

    Your videos are absolutely wonderful, hitting the key points in a simple and concise manner. Thank you!
    P.S. What should be the initialization of weights if there are say 2 or more consecutive Linear layers, and only then a ReLU activation layer?

  • @eduardtsuranov712
    @eduardtsuranov712 Před rokem

    incredibly cool videos, huge thanks, all the best!

  • @casvanmarcel
    @casvanmarcel Před rokem

    I love these videos! Thanks for making them!

  • @mikehoops
    @mikehoops Před rokem

    Thank you Andrej, fantastic tutorial

  • @ayogheswaran9270
    @ayogheswaran9270 Před rokem

    Thanks a lot Andrej!! Thank you for all your efforts❤.

  • @mcnica89
    @mcnica89 Před rokem +10

    I believe you can calculate the gain by doing Gain = 1/sqrt(E[ f(Z)^2 ]) where Z is a standard Gaussian (so that Gain*f(Z) will have unit variance when Z is a standard Gaussian). If you do this for tanh you ~=1.592 which I guess is close to 5/3?

  • @fetulhakabdurahman4720

    Your lectures are the best

  • @venkateshmunagala205
    @venkateshmunagala205 Před rokem

    Thanks Andrej :) . It gave me a better understanding on the concept

  • @smithwill9952
    @smithwill9952 Před rokem

    Better than study in University. Keep going A.K. Share your video for sure.

  • @billykotsos4642
    @billykotsos4642 Před rokem

    Dives straight in and kills the presentation…. Another banger…. Can make old papers fun to go through….

  • @karolduracz4008
    @karolduracz4008 Před rokem

    This is very helpful and interesting. Thank you.

  • @4mb127
    @4mb127 Před rokem

    Thanks so much for this series.

  • @TheMato1112
    @TheMato1112 Před rokem

    Dakujem ti Andrej, je to vazne na inej urovni :)

  • @JavArButt
    @JavArButt Před 10 měsíci

    He says 'Bye', but looking at the time, it seems too early [01:18:30]. Most people don't want lectures to be long, but I'm happy this one didn't end there.

  • @rmajdodin
    @rmajdodin Před rokem +1

    ​ @Andrej Karpathy Great video! A quick question: at 25:43 Why a U shape is better than a Cup shape for the histogram of h? Don't we want h to be have some normal distribution, like hpreact?

  • @starshipx1282
    @starshipx1282 Před rokem

    Love you Andrej❤❤. I hope you keep going with this

  • @aayushsmarten
    @aayushsmarten Před rokem +1

    Andrej's transformation between 15:14 and 15:16 was pretty quick 😉