如何修改Pytorch中VGG模型的最后一层全连接层?
- 内容介绍
- 文章标签
- 相关推荐
本文共计219个文字,预计阅读时间需要1分钟。
在PyTorch中,VGG19模型没有直接的fc成员变量。相反,它使用一个名为(classifier)的Sequential模块。以下是修改最终全连接层的示例代码:

pythonimport torch.nn as nn

假设vgg19_model是已经加载的VGG19模型vgg19_model=...
创建新的全连接层,例如将输出改为10个类别new_fc=nn.Linear(vgg19_model.classifier[1].out_features, 10)
替换原有的全连接层vgg19_model.classifier[1]=new_fc
discuss.pytorch.org/t/how-to-modify-the-final-fc-layer-based-on-the-torch-model/766/12
That's because vgg19 doesn't have a fc member variable. Instead, it has a
(classifier): Sequential ( (0): Dropout (p = 0.5) (1): Linear (25088 -> 4096) (2): ReLU (inplace) (3): Dropout (p = 0.5) (4): Linear (4096 -> 4096) (5): ReLU (inplace) (6): Linear (4096 -> 100) )
To replace the last linear layer, a temporary solution would be
vgg19.classifier._modules['6'] = nn.Linear(4096, 8)
以上这篇Pytorch中的VGG实现修改最后一层FC就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。
本文共计219个文字,预计阅读时间需要1分钟。
在PyTorch中,VGG19模型没有直接的fc成员变量。相反,它使用一个名为(classifier)的Sequential模块。以下是修改最终全连接层的示例代码:

pythonimport torch.nn as nn

假设vgg19_model是已经加载的VGG19模型vgg19_model=...
创建新的全连接层,例如将输出改为10个类别new_fc=nn.Linear(vgg19_model.classifier[1].out_features, 10)
替换原有的全连接层vgg19_model.classifier[1]=new_fc
discuss.pytorch.org/t/how-to-modify-the-final-fc-layer-based-on-the-torch-model/766/12
That's because vgg19 doesn't have a fc member variable. Instead, it has a
(classifier): Sequential ( (0): Dropout (p = 0.5) (1): Linear (25088 -> 4096) (2): ReLU (inplace) (3): Dropout (p = 0.5) (4): Linear (4096 -> 4096) (5): ReLU (inplace) (6): Linear (4096 -> 100) )
To replace the last linear layer, a temporary solution would be
vgg19.classifier._modules['6'] = nn.Linear(4096, 8)
以上这篇Pytorch中的VGG实现修改最后一层FC就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。
