Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

pytorch

resnet

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?

like image 288
Khabbab Zakaria Avatar asked Oct 23 '25 05:10

Khabbab Zakaria


1 Answers

You need you Model to inherit torch.nn.Module:

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

like image 141
KonstantinosKokos Avatar answered Oct 25 '25 19:10

KonstantinosKokos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!