01. PyTorch Workflow Fundamentals Timestand - 5:34:45sec #233
Answered
by
AlienSarlak
Gauravjake
asked this question in
Q&A
-
|
here are the code and I got the error that object has no attribute 'weights' from torch import nn
# Create lines repression model class
class LinearRegressionModel(nn.Module): # -> almost everything in Pytorch inherhits from nn.Module
def _init_(self):
super()._init_()
self.weights = nn.Parameter(torch.randn(1, # <- start with a random weight and try to adjust it to the ideal weight
requires_grad=True, # <- can this parameter be updated via gradient descent?
dtype=torch.float)) # <- Pytorch loves the datatype torch float32
self.bias = nn.Parameter(torch.randn(1, #<- start with a random bias and try to adjust it to the ideal bias
require_grad=True, # <- can this parameter be update via gradient descent?
dtype=torch.float)) # <- Python loves the datatype torch.float32
# Forward method to define the computation is the node:
def forward(self, x: torch.Tensor) -> torch.Tensor: # <- "x" is the input data
return self.weights * x + self.bias # this is the linear regression formulaError Can any please explain me what is going on here. I search this in stack overflow but still can fix my problem and doubt. |
Beta Was this translation helpful? Give feedback.
Answered by
AlienSarlak
Jan 11, 2023
Replies: 1 comment 1 reply
-
|
Hi, To me it seems you made a mistake in your LinearRegressionModel class: class LinearRegressionModel(nn.Module): # -> almost everything in Pytorch inherhits from nn.Module
def _init_(self):
super()._init_()Should be class LinearRegressionModel(nn.Module): # -> almost everything in Pytorch inherhits from nn.Module
def __init__(self): # <- notice the "__" on each side
super().__init__() # <- notice the "__" on each sideYou forgot using __ (double underscores). |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
mrdbourke
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
To me it seems you made a mistake in your LinearRegressionModel class:
Should be
You forgot using __ (double underscores).