神经网络问题:mat1和mat2形状不能相乘

probleme of neural network :mat1 and mat2 shapes cannot be multiplied

我像这样实现了一个简单的神经元网络:

import torch
from torch import nn

class Simple_NN(nn.Module):
  '''
    Multilayer Perceptron.
  '''
  def __init__(self, input_dim):
    super().__init__()
    self.input = input_dim
    #self.out = out_dim
    self.layer = nn.Linear(self.input, 1, bias=False)
      
  def getweights(self):
    return self.layer.weight
  def normalize(self):
    self.layer.weight.data /= self.layer.weight.data.sum()
    return self.layer.weight

  def forward(self, x, dim = 0):
    sort = torch.sort(x, dim, descending = True)[0]
    #top = torch.topk(x, 4, dim)
    sort = self.layer(sort)
    return sort

当我运行这段代码时:

outputs = torch.tensor([[1.9, 0.4, 1.3, 0.8, 0.2, 0.0],[1.7, 1.4, 0.3, 1.8, 1.2, 1.1]])
model = Simple_NN(input_dim = outputs.shape[0])
model.getweights()
model.normalize()

我得到以下结果:

Parameter containing:
tensor([[0.9772, 0.0228]], requires_grad=True)

但是,当我 运行 这一行时:

model(outputs, dim=0)

我收到这个错误:


<ipython-input-1-dd06de9bb6ad> in forward(self, x, dim)
     20     sort = torch.sort(x, dim, descending = True)[0]
     21     #top = torch.topk(x, 4, dim)
---> 22     sort = self.layer(sort)
     23     return sort

RuntimeError: mat1 and mat2 shapes cannot be multiplied (2x6 and 2x1)

我该如何解决这个问题?

由于您没有提供更多详细信息,这里有 2 种可能的解决方法:

  1. 如果batch_size=2input_dim应该是6,而不是2:

    model = Simple_NN(input_dim = outputs.shape[1])  # change [0] to [1]
    
  2. 如果batch_size=6,则outputs需要转置:

    model(outputs.t(), dim=0)  # add .t()
    

我认为您的情况的正确解决方案是第一个,但它们都有效。这取决于你真正想要什么。