python - 如何在 PyTorch 中向预训练模型添加层?
问题描述
我想在layer 和after 之后添加layer normalization
功能。我希望我的 输出为 200,因此尝试不包含 fc 层而不是它 make new ,但它没有删除我正在使用的预训练模型附带的。AdaptiveAvgPool2d
L2 normalization
fc layer
fc layer
fc layer
fc layers
googlenet
我的代码:
class GoogleNet(nn.Module):
def __init__(self):
super(GoogleNet,self).__init__()
self.model = googlenet_pytorch.GoogLeNet.from_pretrained('googlenet')
self.fc = nn.Linear(1024,200, bias=False),
def forward(self, x):
batch_size ,_,_,_ =x.shape
x = self.model.extract_features(x)
x = self.model._avg_pooling(x)
x = F.layer_norm(x,x.size[1:],elementwise_affine=False)
x = self.fc(x)
x = F.normalize(x, p=2, dim=1)
return x
我得到的输出:
.....
.....
.....
(aux1): InceptionAux(
(conv): BasicConv2d(
(conv): Conv2d(512, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
(bn): BatchNorm2d(128, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)
)
(fc1): Linear(in_features=2048, out_features=1024, bias=True)
(fc2): Linear(in_features=1024, out_features=1000, bias=True)
)
(aux2): InceptionAux(
(conv): BasicConv2d(
(conv): Conv2d(528, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
(bn): BatchNorm2d(128, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)
)
(fc1): Linear(in_features=2048, out_features=1024, bias=True)
(fc2): Linear(in_features=1024, out_features=1000, bias=True)
)
(avgpool): AdaptiveAvgPool2d(output_size=(1, 1))
(dropout): Dropout(p=0.2, inplace=False)
(fc): Linear(in_features=1024, out_features=1000, bias=True)
)
)
我想要的输出:
......
......
......
(aux1): None
(aux2): None
(avgpool): AdaptiveAvgPool2d(output_size=(1, 1))
** layer normalization here**
(dropout): Dropout(p=0.2, inplace=False)
(fc): Linear(in_features=1024, out_features=200, bias=False)
**L2 normalization here**
)
如果有人需要知道这个问题的解决方案代码,在 iacob 的答案的帮助下我解决了它,我将它添加为answer。
解决方案
根据@iacob对代码进行更正后,我解决了这个问题。
正确的代码:
class GoogleNet(nn.Module):
def __init__(self):
super(GoogleNet,self).__init__()
self.model = googlenet_pytorch.GoogLeNet.from_pretrained('googlenet')
self.avgpool = nn.AdaptiveAvgPool2d((1,1))
self.layernorm = nn.LayerNorm(1024,elementwise_affine=True)
self._fc = nn.Linear(1024,200, bias=False)
def forward(self, x):
batch_size ,_,_,_ =x.shape
x = self.model.extract_features(x)
x = self.model.avgpool(x)
x = x.view(-1, 1024)
x = self.layernorm(x)
x = self._fc(x)
x = F.normalize(x, p=2, dim=1)
return x
在此之后,我创建了一个实例GoogleNet()
并将不需要的层转换为 Identity()。
dep = GoogleNet()
dep.model.aux1 = Identity()
dep.model.aux2 = Identity()
dep.model.avgpool = Identity()
dep.model.dropout = Identity()
dep.model.fc = Identity()
推荐阅读
- r - 使用 nls 跳过数据表计算中的错误
- python - 如何使用python将csv数据输出到终端
- android - 有没有办法检查我当前连接的 wifi 是否与以前连接的 wifi 不同?
- sh - 如何使用 sh 脚本对 crontab 作业进行哈希处理
- angular - Cannot properly set RequestOptionsArgs of POST, in service
- google-cloud-platform - 将近线存储转换为区域存储然后定期返回的成本是多少?
- mysql - MySQL查询列出不在另一个表中的用户
- caching - Drupal 8, Browser language detection fails after some time (anonymous users)
- jquery - How to automatically resize height of visjs Network component when window resized?
- rational-team-concert - 如何在 RTC 中“git stash”?