Pytorch错误mat1和mat2形状不能相乘



我收到这个错误。而我的输入图像的大小是[3072,2,2],所以我用下面的代码将图像扁平化,然而,我收到了这个错误:

mat1 and mat2 shapes cannot be multiplied (6144x2 and 12288x512)

我代码:

class NeuralNet(nn.Module):
def __init__(self):
super(NeuralNet, self).__init__()
self.fc1 = nn.Linear(12288 ,512) 
self.relu = nn.ReLU()
self.fc2 = nn.Linear(512, 3)  

def forward(self, x):
out = torch.flatten(x,0)

out = self.fc1(x)
out = self.relu(out)
out = self.fc2(out)
return out
model = NeuralNet().to(device)
# Train the model
total_step = len(my_dataloader)
for epoch in range(5):
for i, (images, labels) in enumerate(my_dataloader):  
# Move tensors to the configured device
images = images.to(device)
print(type(images))
labels = labels.to(device)

# Forward pass
outputs = model(images)
loss = criterion(outputs, labels)

# Backward and optimize
optimizer.zero_grad()
loss.backward()
optimizer.step()

你知道发生了什么错误吗?

mat1 and mat2 shapes cannot be multiplied (6144x2 and 12288x512)

你不能将(m x n)矩阵与(p x n)相乘。

your error : (6144x2) * (12288x512)

需要(m x n)+(n x p)。这个"inner"维数需要相同(左矩阵的列数=右矩阵的行数)

Then:——>out = torch.flatten(x,0)将图片[3072,2,2]改为[3072 * 2 2] =[6144年2](不是这个[16288]),

和矩阵[6144,2]和[2512]形状可以相乘

首先,你的线性层的特征不正确。in-feature应该是输入的最后一个dim。在本例中,应该是nn.Linear(2,512)

class NeuralNet(nn.Module):
def __init__(self):
super(NeuralNet, self).__init__()
self.fc1 = nn.Linear(2 ,512) 
self.relu = nn.ReLU()
self.fc2 = nn.Linear(512, 3)  

def forward(self, x):
out = torch.flatten(x,0)

out = self.fc1(x)
out = self.relu(out)
out = self.fc2(out)
return out

基于PyTorch文档的torch.flatten(x,0)返回[3072*2,2]的形状如果你想有[12288]的形状作为你的线性特征,你应该使用torch.flatten(input, start_dim=0, end_dim=- 1)

class NeuralNet(nn.Module):
def __init__(self):
super(NeuralNet, self).__init__()
self.fc1 = nn.Linear(12288 ,512) 
self.relu = nn.ReLU()
self.fc2 = nn.Linear(512, 3)  

def forward(self, x):
out = torch.flatten(x)

out = self.fc1(x,start_dim=0, end_dim=- 1)
out = self.relu(out)
out = self.fc2(out)
return out

最新更新