Im trying to build network which extract features by backbone network (ViT) and classify by linear layer.
So i get error when trying to predict/forward-pass model:
raise NotImplementedError(f"Module [{type(self).name}] is missing the required "forward" function")
NotImplementedError: Module [ModuleList] is missing the required “forward” function
class FTNet(nn.Module):
def __init__(self):
super(FTNet, self).__init__()
self.features = torch.nn.Sequential(*list(model_bb.children())[:-2])
self.linear = torch.nn.Linear(320, 10)
def forward(self, x):
x = self.features(x)
x = self.linear(x)
return x
It seems you are using an nn.ModuleList in your model and are trying to call it directly which won’t work as it’s acting as a list but properly registers trainable parameters:
modules = nn.ModuleList([
nn.Linear(10, 10),
nn.ReLU(),
nn.Linear(10, 10),
])
x = torch.randn(1, 10)
out = modules(x)
# NotImplementedError: Module [ModuleList] is missing the required "forward" function
You should iterate the modules instead:
out = x
for module in modules:
out = module(out)
or use nn.Sequential:
model = nn.Sequential(
nn.Linear(10, 10),
nn.ReLU(),
nn.Linear(10, 10),
)
x = torch.randn(1, 10)
out = model(x)