【问题标题】:Pytorch error mat1 and mat2 shapes cannot be multipliedPytorch 错误 mat1 和 mat2 形状不能相乘
【发布时间】:2022-07-15 14:54:17
【问题描述】:

我收到此错误。而我的输入图像的大小是 [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()

【问题讨论】:

    标签: python neural-network pytorch conv-neural-network training-data


    【解决方案1】:

    首先,您的线性层的特征不正确。功能内应该是您输入的最后一个暗淡。在这种情况下,应该是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 documenttorch.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
    

    【讨论】:

      【解决方案2】:

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

      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)。这个“内部”维度需要相同(左矩阵的列数 = 右矩阵的行数)。

      然后:---> out = torch.flatten(x,0) 将图像 [3072,2,2] 更改为 [3072*2,2] = [6144,2](不是这个 [16288]),

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

      【讨论】:

        猜你喜欢
        • 2021-09-09
        • 2022-08-22
        • 2021-05-25
        • 1970-01-01
        • 1970-01-01
        • 2021-09-23
        • 2023-01-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多