PyTorch Tutorial 03 - Gradient Calculation With Autograd

Поділитися
Вставка
  • Опубліковано 19 тра 2024
  • New Tutorial series about Deep Learning with PyTorch!
    ⭐ Check out Tabnine, the FREE AI-powered code completion tool I use to help me code faster: www.tabnine.com/?... *
    In this part we learn how to calculate gradients using the autograd package in PyTorch.
    This tutorial contains the following topics:
    - requires_grad attribute for Tensors
    - Computational graph
    - Backpropagation (brief explanation)
    - How to stop autograd from tracking history
    - How to zero (empty) gradients
    Part 03: Gradient Calculation With Autograd
    📚 Get my FREE NumPy Handbook:
    www.python-engineer.com/numpy...
    📓 Notebooks available on Patreon:
    / patrickloeber
    ⭐ Join Our Discord : / discord
    If you enjoyed this video, please subscribe to the channel!
    Official website:
    pytorch.org/
    Part 01:
    • PyTorch Tutorial 01 - ...
    You can find me here:
    Website: www.python-engineer.com
    Twitter: / patloeber
    GitHub: github.com/patrickloeber
    #Python #DeepLearning #Pytorch
    ----------------------------------------------------------------------------------------------------------
    * This is a sponsored link. By clicking on it you will not have any additional costs, instead you will support me and my project. Thank you so much for the support! 🙏

КОМЕНТАРІ • 96

  • @simonbernard4216
    @simonbernard4216 3 роки тому +21

    I appreciate that you followed the official tutorial from the Pytorch documentation. Also your comments make things much clearer

    • @patloeber
      @patloeber  3 роки тому +1

      Thanks! Glad you like it

  • @GurpreetSingh-si2gh
    @GurpreetSingh-si2gh Рік тому +2

    I appreciate your efforts, you readily explained every detail. Thanks very much.
    🙏

  • @DanielWeikert
    @DanielWeikert 4 роки тому +38

    This is great work. I really like the detailed explanation using simple practical examples. Please continue doing this exploring the various options in pytorch. Happy holidays

    • @patloeber
      @patloeber  4 роки тому +3

      Thank you! Glad you like it. I have more videos planned for the next few days :) Happy holidays!

  • @kevinw6237
    @kevinw6237 Рік тому

    This video is very clear in explaining things. Thank you so much sir! Keep up the good work pls!

  • @barbaraulitsky8292
    @barbaraulitsky8292 6 місяців тому +2

    This video is super helpful! Explanations are very understandable. Thank you so much!!🤩👍🙏

  • @shazinhotech2155
    @shazinhotech2155 Рік тому +2

    what an amazing teacher you are

  • @user-tz4ms1ki3l
    @user-tz4ms1ki3l 2 роки тому

    This tutorial is more than great! Thank you!

  • @marcoantonioarmentaarmenta1350
    @marcoantonioarmentaarmenta1350 4 роки тому +1

    This video is extremely useful. Thank you!

  • @nishaqiao828
    @nishaqiao828 3 роки тому +3

    Thank you! Your video helps a lot to my undergraduate final project!

    • @patloeber
      @patloeber  3 роки тому

      that's nice to hear :)

  • @haroldsu1696
    @haroldsu1696 Рік тому

    Great work Sir, and Thank you!

  • @henrygory
    @henrygory 11 місяців тому

    Excellent video. Thanks!

  • @user-vm3jn7ih8j
    @user-vm3jn7ih8j 3 роки тому +5

    This tutorial is brilliant. It is super friendly to people who are new to Pytorch!

  • @wiktormigaszewski8684
    @wiktormigaszewski8684 4 роки тому +1

    very nice, congrats!

  • @warrior_1309
    @warrior_1309 2 роки тому

    Great Work Sir.

  • @exxzxxe
    @exxzxxe 3 роки тому +2

    Very well done. An excellent video!

  • @foodsscenes5891
    @foodsscenes5891 3 роки тому

    Quite love these courses! Great thanks

  • @mihaidumitrescu1325
    @mihaidumitrescu1325 Місяць тому

    Fantastic work!
    slight recommendation of how to improve this: if u use the same naming scheme in the Jacobian and in the code (l vs z), we can follow easier the chain rule!

  • @alexandredamiao1365
    @alexandredamiao1365 3 роки тому

    Great tutorial! Thanks for this material!

  • @soumyajitsarkar2372
    @soumyajitsarkar2372 3 роки тому +3

    Really amazing and very well explained ! Thank You .
    Btw what IDE are you using ? looks so cool and handy , love the output option below .

  • @yishu4244
    @yishu4244 3 роки тому

    This video helps me greatly. I like your language speed since English is not my mother tongue. Thank you a lot.

    • @patloeber
      @patloeber  3 роки тому

      Nice, glad you like it

  • @maithilijoshi796
    @maithilijoshi796 11 місяців тому

    thankyou for your efforts

  • @gordonxie7173
    @gordonxie7173 3 роки тому

    Great work!

  • @sanjaykrish8719
    @sanjaykrish8719 3 роки тому

    All in one pytorch.. yeahhh.. fantastic.. thanks a ton🎉🎊🎊

  • @andywang4189
    @andywang4189 3 місяці тому

    Very clear, thank you very much

  • @DiegoAndresAlvarezMarin
    @DiegoAndresAlvarezMarin 2 роки тому +2

    Thank you very much for your tutorial. However, I did not plenty understand some details, like the variable v or the use of the optimizer.

  • @charithas90
    @charithas90 3 роки тому

    very nice tutorial. thank you!

  • @dfdiasbr
    @dfdiasbr 2 місяці тому

    awesome! Thank you

  • @samirelamrany5323
    @samirelamrany5323 Рік тому

    great work thank you

  • @bhomiktakhar8226
    @bhomiktakhar8226 2 роки тому

    Just on to my 4 lec in pytorch series. Don't know if it's a complete series on pytorch ,but definitely whatever is there it's depicted nicely.

    • @patloeber
      @patloeber  2 роки тому

      nice, yes it's a complete beginner series

  • @Murmur1131
    @Murmur1131 2 роки тому +1

    @ 8:16 do you have a simple real life "example" why we have to use the v for the right size and why it wouldn't work without it? I know silly question, but I don't really grasp the concept behind it..

  • @alejandrosaenz4551
    @alejandrosaenz4551 4 роки тому +2

    I just discoverd your channel and it's really good! One question, I dont totally understand why we should use .detach() or no_grad() when updating weights... we are creating a new graph or something like that? what "prevent to tracking the gradient" exactly means? Hope you can help me with that. Keep the good job (:

    • @patloeber
      @patloeber  4 роки тому +11

      You should use this for example after the training when you evaluate your model. Because otherwise for each operation you do with your tensor, PyTorch will calculate the gradients. This is time consuming and expensive, so we don't want this anymore after the training because we no longer need backpropagation. It will reduce memory usage and speed up computations.

  • @xkaline
    @xkaline 3 роки тому

    Great PyTorch Tutorial videos! May I know what is the VS Code extension you use to autocomplete the PyTorch line?

    • @patloeber
      @patloeber  3 роки тому

      In this video it was only the built-in autocompletion through the official Python extension

  • @anurajms
    @anurajms 9 місяців тому

    thank you

  • @milk_steak_the_original
    @milk_steak_the_original 2 роки тому

    When do you learn the why of things ? why am I making a gradient ? when would I use it ? I feel like these things are often explained in DS videos/classes

  • @Muhammad_Abdullah_Sultan
    @Muhammad_Abdullah_Sultan 23 дні тому

    Amazing

  • @popamaji
    @popamaji 4 роки тому +3

    interesting thing that I realized is that eventhough after set z=z.mean which changes grad_fn of z from mulbackward to meanbackward(so z doesnt have mulbackward anymore), it is still able to track gradient

    • @patloeber
      @patloeber  4 роки тому +2

      if requires_grad=true, any operation that we do with z tracks the gradient

  • @jorgemercadog
    @jorgemercadog 3 роки тому

    Thank you!!

  • @iposipos9342
    @iposipos9342 4 роки тому +2

    Thanks for the video. for the tensor v, are the values of any importance or its only the size that is of importance?

    • @patloeber
      @patloeber  4 роки тому +2

      Hi, good question! Yes this can be a bit confusion, so I provided some links from the pytorch forum which might help:
      discuss.pytorch.org/t/how-to-use-torch-autograd-backward-when-variables-are-non-scalar/4191/4
      discuss.pytorch.org/t/clarification-using-backward-on-non-scalars/1059

    • @sebimoe
      @sebimoe 4 роки тому +1

      @@patloeber I can't find anyone describe how to use it there, are you able to give a quick summary? Help much be appreciated as I can't find anything, including values 0.1, 1, 0.001

    • @jesuispac
      @jesuispac 4 роки тому +1

      @@sebimoe Apparently he doesn't know either....Typical

    • @priyanka5516
      @priyanka5516 3 роки тому

      In my understanding, only the size will matter. So you can write v=torch.randn(3). Then pass v in backward function.

    • @dataaholic
      @dataaholic 3 роки тому

      Did anyone find a good explanation for v ? I'm also bit confused here
      @ 8:00 what is the difference between z.backward() vs z.backward(v)?
      z.backward() ==> is calculating dz/dx
      is z.backward(v) ==> is calculating dz/dv ?

  • @taku8751
    @taku8751 4 роки тому +1

    Really good tutorial, but I wanna know what the app which you use to draw is.

    • @patloeber
      @patloeber  4 роки тому

      I used an Ipad for this (some random notes app)

  • @dataaholic
    @dataaholic 3 роки тому +11

    @ 8:00 what is the difference between z.backward() vs z.backward(v)?
    z.backward() ==> is calculating dz/dx
    is z.backward(v) ==> is calculating dz/dv ?

    • @alanzhu7538
      @alanzhu7538 2 роки тому +5

      Also, how do we decide the value for v?

    • @gautame
      @gautame 2 роки тому +1

      z.backward(v) calculates dz/dx*v

    • @peixinwu4631
      @peixinwu4631 2 роки тому

      @@alanzhu7538 You got any idea now?😂

    • @peixinwu4631
      @peixinwu4631 2 роки тому

      @@gautame You mean v * (dz/dx)? Or dz/(dx * v)?

    • @faseehahmed6164
      @faseehahmed6164 2 роки тому

      @@peixinwu4631 v * (dz/dx)

  • @danielpietschmann5294
    @danielpietschmann5294 3 роки тому +7

    why can you just specify v as [0.1, 1.0, 0.001], why not some other numbers?

    • @crivatz
      @crivatz Рік тому

      Did you find out?

    • @minister1005
      @minister1005 10 місяців тому

      It doesn't matter. You just need to have the same shape

  • @consistentthoughts826
    @consistentthoughts826 3 роки тому +1

    @5:46 in the image it is J^T . V

  • @yuyangxi3359
    @yuyangxi3359 3 роки тому

    pretty good

  • @andrei642
    @andrei642 2 роки тому +2

    I had a german professor who also pronounced "value" as "ualue". Can you explain why do you sometimes pronounce it that way? I am very intrigued.
    P.S. you have the best pytorch series on youtube

  • @mahdiamrollahi8456
    @mahdiamrollahi8456 Рік тому

    If i want to play with it, suppose that we have y=3x, and we should get the answer of 3 for the gradient. So, how can i do that in pytorch?

  • @13579Josiah
    @13579Josiah 2 роки тому

    At 9:30, isn’t another way to do x.volatile = False ?

  • @cypherecon5989
    @cypherecon5989 5 днів тому

    I don't get why you need the 3 methods presented at 8:06 for preventing the gradient. Can one not simply put requires_grad=False in the x tensor?

  • @my_opiniondemocracy6584
    @my_opiniondemocracy6584 Рік тому +4

    how did you decide values in vector v?

    • @luanmartins8068
      @luanmartins8068 12 днів тому

      The gradient in 5:46 is the multiplication of the jacobian matrix and a directional vector. When the function which you want to calculate the gradient has a one dimensional output, there is no need to determine a direction for derivative since it is unique, that's why for the mean gradient it accepts no arguments. On other hand, see that for a multidimensional output as x+2 = y (y is a multidimensional output, a vector), you have to specify what direction you want to take your gradient. That is where the v vector enters. He arbitrarily choose components just to show that the function requires an vector to define the directional derivative, but usually for statistical learning it is chosen the direction where the gradient is steepest

  • @vanamaliis7185
    @vanamaliis7185 Рік тому

    Getting this error,
    RuntimeError: Trying to backward through the graph a second time (or directly access saved tensors after they have already been freed). Saved intermediate values of the graph are freed when you call .backward() or autograd.grad(). Specify retain_graph=True if you need to backward through the graph a second time or if you need to access saved tensors after calling backward.

  • @oleholgerson3416
    @oleholgerson3416 4 роки тому +1

    That’s good, ja? :)

  • @user-ph3qi5to4r
    @user-ph3qi5to4r 6 місяців тому

    This is so f**king useful, thank you sooo much!!

  • @user-ss7bm2id5c
    @user-ss7bm2id5c 3 роки тому

    I cant understand,why must gradient equal +gradient (gradient=+gradient) in each epoch.Where Can I find consistent mathematic formula?Can you explain me once?

    • @patloeber
      @patloeber  3 роки тому

      I'm not exactly sure what you mean. Can you point me to the time in the video where I show this?

  • @my_opiniondemocracy6584
    @my_opiniondemocracy6584 Рік тому

    in the end...it became heavy

  • @saurabhkumar-wj1nz
    @saurabhkumar-wj1nz 3 роки тому

    Hey, could you explain what it means by tracking the gradient. I mean why its a issue??

    • @patloeber
      @patloeber  3 роки тому

      Tracking each operation is necessary for the backpropagation. But it is expensive, so after the training we should disable it

  • @chq7759
    @chq7759 3 роки тому +2

    When I run the following code, I encountered an error. Could you help me? Thank you very much!
    weights = torch.ones(4, requires_grad=True)
    optimizer = torch.optim.SGD(weights, lr=0.01)
    The error is
    Traceback (most recent call last):
    File "D:/1pytorch-tutorial/my_try/learning-PythonEngineer/learning.py", line 113, in
    optimizer = torch.optim.SGD(weights, lr=0.01)
    File "D:\Anaconda3\lib\site-packages\torch\optim\sgd.py", line 68, in __init__
    super(SGD, self).__init__(params, defaults)
    File "D:\Anaconda3\lib\site-packages\torch\optim\optimizer.py", line 39, in __init__
    torch.typename(params))
    TypeError: params argument given to the optimizer should be an iterable of Tensors or dicts, but got torch.FloatTensor

  • @filosofiadetalhista
    @filosofiadetalhista 11 місяців тому

    I'm not sure how you managed to be unclear on the third video of the series.
    What you said about gradients, .backwards(), .step(), and .zero_grad() were not clear at all.

  • @mittalmayankcool
    @mittalmayankcool Рік тому

    Too many advertisements

  • @suecheng3755
    @suecheng3755 Рік тому

    Great work!