4

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 Answer 1

3

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

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

Sign up to request clarification or add additional context in comments.

2 Comments

Oh yes! right! Can you tell me how to print the parameters separately for the resnet and pool?
pool and resnet are themselves modules, so you can simply print(model.pool.parameters()) (ditto for resnet)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.