In the Workflow exercise solutions, the location of requires_grad is different to the lecutre notebook #582
-
In the 01_pytorch_workflow.ipynb in section 2. Build Model, the class initialization code has the In trying to figure out what the difference between these two were, I had a look at the So, questions:
from 01_pytorch_workflow.ipynb self.weights = nn.Parameter(torch.randn(1, # <- start with random weights (this will get adjusted as the model learns)
dtype=torch.float), # <- PyTorch loves float32 by default
requires_grad=True) # <- can we update this value with gradient descent?) from 01_pytorch_workflow_exercise_solutions.ipynb self.weight = nn.Parameter(data=torch.randn(1,
requires_grad=True,
dtype=torch.float
)) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi @wittyalias (good GitHub name too btw), Good question! As far as PyTorch is concerned, both of these are the same in terms of Your assumption is right that But also, even if we didn't set I set it explicitly in the videos to showcase an example. But you can also check the two above are the same via: import torch
from torch import nn
# Grad outside torch.randn()
grad_outside = nn.Parameter(torch.randn(1, # <- start with random weights (this will get adjusted as the model learns)
dtype=torch.float), # <- PyTorch loves float32 by default
requires_grad=True) # <- can we update this value with gradient descent?)
# Grad inside torch.randn()
grad_inside = nn.Parameter(data=torch.randn(1,
requires_grad=True,
dtype=torch.float))
assert grad_outside.requires_grad == grad_inside.requires_grad (the code above will pass the assertion) Or: grad_outside, grad_inside Output: (Parameter containing:
tensor([0.0031], requires_grad=True),
Parameter containing:
tensor([0.9672], requires_grad=True)) See a demo notebook here: https://colab.research.google.com/drive/1dge-FUOp9T06JGu92yNLEIPBgiMP5MFr?usp=sharing |
Beta Was this translation helpful? Give feedback.
Hi @wittyalias (good GitHub name too btw),
Good question!
As far as PyTorch is concerned, both of these are the same in terms of
requires_grad
.Your assumption is right that
nn.Parameter
takes therequires_grad
parameter of the tensor passed to it.But also, even if we didn't set
requires_grad=True
,nn.Parameter
has it set toTrue
by default, see: https://pytorch.org/docs/stable/generated/torch.nn.parameter.Parameter.htmlI set it explicitly in the videos to showcase an example.
But you can also check the two above are the same via: