首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AttributeError:“模型”对象没有属性“参数”

AttributeError:“模型”对象没有属性“参数”
EN

Stack Overflow用户
提问于 2021-05-26 14:17:02
回答 1查看 2.6K关注 0票数 1

我使用的是一个经过修改的Resnet18,在Resnet的末尾具有自己的池函数。

这是我的代码:

代码语言:javascript
复制
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部分,还有池部分。

但是,当我检查:

代码语言:javascript
复制
model = Model(model=resnet, pool= pool)
print(list(model.parameters()))

它规定:

代码语言:javascript
复制
AttributeError: 'Model' object has no attribute 'parameters'

有人能帮忙吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-26 14:27:58

您需要Model继承torch.nn.Module

代码语言:javascript
复制
class Model(torch.nn.Module):
    def __init__(self, model, pool):
        super(Model, self).__init__()
        ...
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67706793

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档