目标:迁移vgg16,固定卷积池化层以及前两层全连接层,在分类层的最后一层前面再加两层全连接层
原来的代码:
class CNN(nn.Module):
def __init__(self, model):
super(CNN, self).__init__()
self.vgg_layer = nn.Sequential(
*list(model.children())[:-1],
*list(model.classifier.children())[:-1]
)
self.Linear_layer = nn.Sequential(
nn.Linear(4096, 4096),
nn.ReLU(True),
nn.Dropout(),
nn.Linear(4096, 4096),
nn.ReLU(True),
nn.Dropout(),
nn.Linear(4096, 10),
)
def forward(self, x):
x = self.vgg_layer(x)
x = x.view(x.size(0), -1)
x = self.Linear_layer(x)
return x
输出后出现错误,如题。
输出了网络,形式上好像没有什么错误,于是将sequential里面的分开,然后分别输出x,即
class CNN(nn.Module):
def __init__(self, model):
super(CNN, self).__init__()
self.vgg_layer1 = nn.Sequential(*list(model.children())[:-1])
self.vgg_layer2 = nn.Sequential(*list(model.classifier.children())[:-1])
self.Linear_layer = nn.Sequential(
nn.Linear(4096, 4096),
nn.ReLU(True),
nn.Dropout(),
nn.Linear(4096, 4096),
nn.ReLU(True),
nn.Dropout(),
nn.Linear(4096, 10),
)
ones = torch.Tensor(np.ones([10,10]))
self.xi_layer = nn.Linear(10, 1, bias=False)
self.xi_layer.weight=torch.nn.Parameter(ones)
def forward(self, x):
x = self.vgg_layer1(x)
print(x.shape)
x = self.vgg_layer2(x)
x = x.view(x.size(0), -1)
x = self.Linear_layer(x) #(1, 10)
x = self.xi_layer(x)
return x
输出的x本来应该是(255×7×7)最后输出发现是(2×255×7×7)维度明显不对,原来是少写了一行
x = self.vgg_layer1(x)
x = x.view(x.size(0), -1)
x = self.vgg_layer2(x)
x = x.view(x.size(0), -1)
再输出就能发现是正确的(255×7×7),问题解决!
注意:
1.出现维度问题的时候一定要记得输出每一层的输出或者维度,更便于发现问题;
2.全连接层后面不要忘了加x = x.view(x.size(0), -1)。