in_feature 的值没有改变

问题描述 投票:0回答:1

我正在使用 resnet 进行多类分类制作深度学习模型,在其中我面临着过度拟合的问题。为了解决这个问题,我尝试减少 resnet 的层数,但我遇到了这个错误

运行时错误:mat1 和 mat2 形状无法相乘(65536x1 和 2048x1000)

我的代码是

class Flowers(nn.Module):
def __init__(self):
    super().__init__()
    self.classifier=nn.Sequential(
        nn.Flatten(),nn.Linear(in_features=1,out_features=102),
        )
def forward(self,x):
    x=self.classifier(x)
    return x
torch.manual_seed(42)
model = torch.hub.load("pytorch/vision", "resnet50", weights="IMAGENET1K_V2")
list(model.modules())
my_model = nn.Sequential(*list(model.children())[:12])
my_model.add_module('fc', Flowers())

如果有人能帮助我解决一般的过度拟合问题,那将是巨大的帮助。

deep-learning pytorch resnet
1个回答
0
投票

要删除最后一层而不干扰模型的推理,只需将全连接层替换为恒等函数即可,

nn.Identity
:

model.fc = nn.Identity()
© www.soinside.com 2019 - 2024. All rights reserved.