【问题标题】:mat1 dim 1 must match mat2 dim 0 - PyTorchmat1 dim 1 必须匹配 mat2 dim 0 - PyTorch
【发布时间】:2020-12-02 15:56:46
【问题描述】:

我是 PyTorch 的新手,我不断收到错误 mat1 dim1 must match mat1 dim0

这是我的网络代码

class Net(Module):
    def __init__(self):
        super(Net, self).__init__()
        
        self.cnn_layers = Sequential(
            Conv1d(4,4,kernel_size=2, stride=1, padding=1),
            BatchNorm1d(4),
            ReLU(inplace=True),
            MaxPool1d(kernel_size=2,stride=1),
        )
        
        self.linear_layers = Sequential(
            Linear(8267*4,2)
        )
        
    def forward(self, x):
        print(x.shape)
        x = self.cnn_layers(x)
        print(x.shape)
        x = self.linear_layers(x)
        return x

以及打印语句的位置:

torch.Size([8267, 4, 1])
torch.Size([8267, 4, 1])

有什么帮助/建议吗?

【问题讨论】:

    标签: python pytorch conv-neural-network


    【解决方案1】:

    假设8267 是您的批量大小。你的 CNN 的输出是8267x4x1。所以你首先需要将dim=1dim=2 扁平化为一个维度来得到一个形状8267x4。然后下一层(密集)将需要4 神经元。

    self.cnn_layers = Sequential(
        Conv1d(4, 4, kernel_size=2, stride=1, padding=1),
        BatchNorm1d(4),
        ReLU(inplace=True),
        MaxPool1d(kernel_size=2, stride=1))
        
    self.linear_layers = Sequential(
        Flatten(),
        Linear(4, 2))
    

    【讨论】:

      猜你喜欢
      • 2023-03-25
      • 1970-01-01
      • 2021-08-02
      • 2021-04-09
      • 1970-01-01
      • 2021-02-28
      • 1970-01-01
      • 2021-10-12
      • 2018-10-05
      相关资源
      最近更新 更多