RuntimeError: mat1 and mat2 shapes cannot be multiplied (1280x5 and 6400x4096)?
RuntimeError: mat1 and mat2 shapes cannot be multiplied (1280x5 and 6400x4096)?
使用以下代码定义Alexnet,我可以训练successfully.But当我想看到每一层的输出时,会报错'RuntimeError: mat1 and mat2 shapes cannot be multiplied (1280x5 and 6400x4096 )?'
class AlexNet(nn.Module):
def __init__(self):
super(AlexNet, self).__init__()
self.conv = nn.Sequential(
nn.Conv2d(1, 96, 11, 4),
nn.ReLU(),
nn.MaxPool2d(3, 2),
nn.Conv2d(96, 256, 5, 1, 2),
nn.ReLU(),
nn.MaxPool2d(3, 2),
nn.Conv2d(256, 384, 3, 1, 1),
nn.ReLU(),
nn.Conv2d(384, 384, 3, 1, 1),
nn.ReLU(),
nn.Conv2d(384, 256, 3, 1, 1),
nn.ReLU(),
nn.MaxPool2d(3, 2)
)
self.fc = nn.Sequential(
nn.Linear(256*5*5, 4096),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(4096, 4096),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(4096, 10)
)
def forward(self, img):
feature = self.conv(img)
output = self.fc(feature.view(img.shape[0], -1))
return output
X=torch.randn(1,1,224,224)
for name,layer in net.named_children():
X=layer(X)
print(name,X.shape)
你能帮帮我吗?
您忘记在 for 循环中展平 self.conv
的输出数组。你可以把它分成两个循环,一个用于卷积层,一个用于全连接层。
X = torch.randn(1, 1, 224, 224)
for name, layer in net.conv.named_children():
X = layer(X)
print(name, X.shape)
X = X.flatten() # or X = X.view(X.shape[0], -1)
for name, layer in net.fc.named_children():
X = layer(X)
print(name, X.shape)
使用以下代码定义Alexnet,我可以训练successfully.But当我想看到每一层的输出时,会报错'RuntimeError: mat1 and mat2 shapes cannot be multiplied (1280x5 and 6400x4096 )?'
class AlexNet(nn.Module):
def __init__(self):
super(AlexNet, self).__init__()
self.conv = nn.Sequential(
nn.Conv2d(1, 96, 11, 4),
nn.ReLU(),
nn.MaxPool2d(3, 2),
nn.Conv2d(96, 256, 5, 1, 2),
nn.ReLU(),
nn.MaxPool2d(3, 2),
nn.Conv2d(256, 384, 3, 1, 1),
nn.ReLU(),
nn.Conv2d(384, 384, 3, 1, 1),
nn.ReLU(),
nn.Conv2d(384, 256, 3, 1, 1),
nn.ReLU(),
nn.MaxPool2d(3, 2)
)
self.fc = nn.Sequential(
nn.Linear(256*5*5, 4096),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(4096, 4096),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(4096, 10)
)
def forward(self, img):
feature = self.conv(img)
output = self.fc(feature.view(img.shape[0], -1))
return output
X=torch.randn(1,1,224,224)
for name,layer in net.named_children():
X=layer(X)
print(name,X.shape)
你能帮帮我吗?
您忘记在 for 循环中展平 self.conv
的输出数组。你可以把它分成两个循环,一个用于卷积层,一个用于全连接层。
X = torch.randn(1, 1, 224, 224)
for name, layer in net.conv.named_children():
X = layer(X)
print(name, X.shape)
X = X.flatten() # or X = X.view(X.shape[0], -1)
for name, layer in net.fc.named_children():
X = layer(X)
print(name, X.shape)