AttributeError: 'Model' object has no attribute 'parameters'

I am using a modified Resnet18, with my own pooling function at the end of the Resnet.

Here is my code:

resnet = resnet18().cuda() #a modified resnet

class Model():
    def __init__(self, model, pool):
        self.model = model
        self.pool= pool #my own pool class which has trainable layers

    def forward(self, sample):
        output = self.model(sample)
        output = self.pool(output)
        output = F.normalize(output, p=2, dim=1)
        return output

Now, obviously I need to train not only the resnet part, but also the pool part.

But, when I check:

model = Model(model=resnet, pool= pool)
print(list(model.parameters()))

It gives:

AttributeError: 'Model' object has no attribute 'parameters'

Can anyone help?

1 Like

You would have to derive your custom Model from nn.Module as:

class Model(nn.Module):
    def __init__(self, model, pool):
        super().__init__()
        ...

to make sure all nn.Module methods and attributes are available.

2 Likes

Thanks! Yes, I understand it now