【问题标题】:AttributeError: 'Model' object has no attribute 'parameters'AttributeError:“模型”对象没有属性“参数”
【发布时间】:2021-05-26 22:27:58
【问题描述】:

我使用的是修改后的 Resnet18,在 Resnet 的末尾有我自己的池化功能。

这是我的代码:

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

现在,显然我不仅需要训练 resnet 部分,还需要训练 pool 部分。

但是,当我检查时:

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

它给出:

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

谁能帮忙?

【问题讨论】:

    标签: pytorch resnet


    【解决方案1】:

    你需要你Model继承torch.nn.Module

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

    【讨论】:

    • 哦,是的!正确的!你能告诉我如何分别打印 resnet 和 pool 的参数吗?
    • poolresnet 本身就是模块,因此您可以简单地使用 print(model.pool.parameters())resnet 同上)
    猜你喜欢
    • 2018-06-12
    • 2015-04-28
    • 2021-10-20
    • 2019-05-11
    • 2020-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-02
    相关资源
    最近更新 更多