NotImplementedError: Module is missing the required "forward" function #97
-
I don't know why they show error in this code I tried lot of thing it didn't found it what is problem in this code |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
Beta Was this translation helpful? Give feedback.
-
Hi @Sarthakparmar-04, @Pouyaexe is right, the issue is with your indentation of the Notice the from torch import nn
# Build model
class BlobModel(nn.Module):
def __init__(self, input_features, output_features, hidden_units=8):
"""Initializes all required hyperparameters for a multi-class classification model.
Args:
input_features (int): Number of input features to the model.
out_features (int): Number of output features of the model
(how many classes there are).
hidden_units (int): Number of hidden units between layers, default 8.
"""
super().__init__()
self.linear_layer_stack = nn.Sequential(
nn.Linear(in_features=input_features, out_features=hidden_units),
# nn.ReLU(), # <- does our dataset require non-linear layers? (try uncommenting and see if the results change)
nn.Linear(in_features=hidden_units, out_features=hidden_units),
# nn.ReLU(), # <- does our dataset require non-linear layers? (try uncommenting and see if the results change)
nn.Linear(in_features=hidden_units, out_features=output_features), # how many classes are there?
)
def forward(self, x):
return self.linear_layer_stack(x)
# Create an instance of BlobModel and send it to the target device
model_4 = BlobModel(input_features=NUM_FEATURES,
output_features=NUM_CLASSES,
hidden_units=8).to(device)
model_4 Without the As @Pouyaexe said, you can turn on indentation guides in Google Colab by going to tools -> settings -> editor -> show indentation guides. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
Well, you are defining the forward function inside the def init function. You should remove the indentation before the def forward.
I recommend you to turn on the Indentation guides from tools>settings>editor.
Example of a correct implementation: