What's wrong with the following class or custom module:23456789101112131415161# Customize Linear Regression Class class LR(nn.Module): # Constructor def __init__(self, input_size, output_size): # Inherit from parent super(LR, self).__init__() linear = nn.Linear(input_size, output_size) # Prediction function def forward(self, x): out = self.linear(x) return out 1 point"super" is not needed"nn.Module" is not required"linear" should be self.linearThe code will run fine
Question
What's wrong with the following class or custom module:23456789101112131415161# Customize Linear Regression Class class LR(nn.Module): # Constructor def init(self, input_size, output_size): # Inherit from parent super(LR, self).init() linear = nn.Linear(input_size, output_size) # Prediction function def forward(self, x): out = self.linear(x) return out 1 point"super" is not needed"nn.Module" is not required"linear" should be self.linearThe code will run fine
Solution
The problem with the given class is that the "linear" should be "self.linear". In Python, "self" is used to represent the instance of the class and binds the attributes with the given arguments. By using "self", we can access the attributes of the class in python. So, the "linear" attribute should be defined as "self.linear" to be correctly associated with instances of the class.
Here is the corrected code:
# Customize Linear Regression Class
class LR(nn.Module):
# Constructor
def __init__(self, input_size, output_size):
# Inherit from parent
super(LR, self).__init__()
self.linear = nn.Linear(input_size, output_size)
# Prediction function
def forward(self, x):
out = self.linear(x)
return out
The use of "super" and "nn.Module" is correct. "super" is used to call a method from the parent class, in this case, nn.Module. "nn.Module" is the base class for all neural network modules in PyTorch, which this class is correctly inheriting from.
Similar Questions
What's wrong with the following class or custom module:23456789101112131415161# Customize Linear Regression Class class LR(nn.Module): # Constructor def __init__(self, input_size, output_size): # Inherit from parent super(LR, self).__init__() linear = nn.Linear(input_size, output_size) # Prediction function def forward(self, x): out = self.linear(x) return out 1 point"super" is not needed"nn.Module" is not required"linear" should be self.linearThe code will run fine
9143 : 9963 :: 6731 : ?Options5666136889649694
What is the number in the unit’s place of 999^22292582Options :9731
Which of the following is a bad Python variable name?1 point23spam_spamSPAM23Spam
What is the number in the unit’s place of 999^22292582
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.