AttributeError: 'Model' object has no attribute 'parameters'
AttributeError: 'Model' object has no attribute 'parameters'
我正在使用修改后的 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 部分,还需要训练池部分。
但是,当我检查时:
model = Model(model=resnet, pool= pool)
print(list(model.parameters()))
它给出:
AttributeError: 'Model' object has no attribute 'parameters'
有人能帮忙吗?
您需要 Model
才能继承 torch.nn.Module
:
class Model(torch.nn.Module):
def __init__(self, model, pool):
super(Model, self).__init__()
...
我正在使用修改后的 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 部分,还需要训练池部分。
但是,当我检查时:
model = Model(model=resnet, pool= pool)
print(list(model.parameters()))
它给出:
AttributeError: 'Model' object has no attribute 'parameters'
有人能帮忙吗?
您需要 Model
才能继承 torch.nn.Module
:
class Model(torch.nn.Module):
def __init__(self, model, pool):
super(Model, self).__init__()
...