【问题标题】:RuntimeError: mat1 and mat2 shapes cannot be multiplied (2x720 and 784x10)RuntimeError:mat1 和 mat2 形状不能相乘(2x720 和 784x10)
【发布时间】:2022-11-24 23:21:08
【问题描述】:

有什么想法可以解决这个运行时错误吗?

我需要创建:

  1. 一个二维卷积层,带有 10 个大小为 5x5 的过滤器,步幅为 1,零填充,后跟 通过 ReLU 激活,然后是大小为 2x2 的 2d 最大池化操作。
  2. 一个二维卷积层,带有 20 个大小为 5x5、步幅为 1、零填充的滤波器,其后 通过 ReLU 激活,然后是大小为 2x2 的 2d 最大池化操作。
  3. 全连接层后跟 ReLU 激活。
    input_size = 1 * 28 * 28  # input spatial dimension of images
    hidden_size = 128         # width of hidden layer
    output_size = 10          # number of output neurons
    
    class CNN(torch.nn.Module):
        
        def __init__(self):
            
            super().__init__()
            self.flatten = torch.nn.Flatten(start_dim=1)
            # ------------------
            # Write your implementation here.
            self.conv1 = torch.nn.Conv2d(in_channels = 1, out_channels = 10, kernel_size = 5, stride = 1,
                          padding = 1, padding_mode = 'zeros')
            self.conv2 = torch.nn.Conv2d(in_channels = 10, out_channels = 20, kernel_size = 5, stride = 1,
                          padding = 1, padding_mode = 'zeros')
            self.fc = torch.nn.Linear(input_size, output_size)
            self.max_pool2d = torch.nn.MaxPool2d(kernel_size = 2)
            self.act = torch.nn.ReLU()
            self.log_softmax = torch.nn.LogSoftmax(dim = 1)
            # ------------------
        
        def forward(self, x):
            # Input image is of shape [batch_size, 1, 28, 28]
            # Need to flatten to [batch_size, 784] before feeding to fc1
    
            # ------------------
            # Write your implementation here.        
            x = self.conv1(x)
            x = self.act(x)
            x = self.max_pool2d(x)
            x = self.conv2(x)
            x = self.act(x)
            x = self.max_pool2d(x)
            x = self.flatten(x)
            # x = x.view(x.size(0), -1)
            x = self.act(self.fc(x))
            y_output = self.log_softmax(x)
            
            return y_output
            # ------------------
    
    model = CNN().to(DEVICE)
    
    # sanity check
    print(model)
    from torchsummary import summary
    summary(model, (1,32,32))
    

    因为我不知道如何解决这个错误,所以遇到了麻烦。

【问题讨论】:

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


    【解决方案1】:

    根据您提供的网络详细信息:

    我需要创建:

    1. 一个二维卷积层,具有 10 个大小为 5x5 的过滤器,步长为 1,零填充,然后是 ReLU 激活,然后是大小为 2x2 的二维最大池化操作。
    2. 一个二维卷积层,有 20 个大小为 5x5 的过滤器,步长为 1,零填充,然后是 ReLU 激活,然后是大小为 2x2 的二维最大池化操作。
    3. 全连接层后跟 ReLU 激活。

      以下模型应该可以完成这项工作:

      import torch
      import torch.nn as nn
      
      class ConvNet(nn.Module):
          def __init__(self):
              super(ConvNet, self).__init__()
              # assuming you have 1 input channels, replace 1 here with your choice of input channels
              self.conv1 = nn.Sequential(nn.Conv2d(1, 10, kernel_size=5, stride=1, padding=0),
                                              nn.ReLU(),
                                              nn.MaxPool2d(2))
              
              self.conv2 = nn.Sequential(nn.Conv2d(10, 20, kernel_size=5, stride=1, padding=0),
                                              nn.ReLU(),
                                              nn.MaxPool2d(2),
                                              nn.Flatten())
              
              #assuming input is 1 channels with height and width 32, and output of fc is 10
              self.linear = nn.Sequential(nn.Linear(500, 10),
                                          nn.ReLU())
      
          def forward(self, x):
              x = self.conv1(x)
              x = self.conv2(x)
              x = self.linear(x)
              return x
      

      上面的模型应该可以工作。

    【讨论】:

      猜你喜欢
      • 2021-07-20
      • 2021-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      • 2022-08-15
      相关资源
      最近更新 更多