Python 使用 Pytorch 进行多层感知器深度学习

Multi Layer Perceptron Deep Learning in Python using Pytorch

我在 MLP 中执行代码的训练函数时出错。

这是错误:

mat1 和 mat2 形状不能相乘(128x10 和 48x10)

我的火车功能代码是这样的:

class net(nn.Module):
def __init__(self, input_dim2, hidden_dim2, output_dim2):
    super(net, self).__init__()
    self.input_dim2 = input_dim2
    self.fc1 = nn.Linear(input_dim2, hidden_dim2)
    self.relu = nn.ReLU()
    self.fc2 = nn.Linear(hidden_dim2, hidden_dim2)
    self.fc3 = nn.Linear(hidden_dim2, output_dim2) 
def forward(self, x):
  x = self.fc1(x)
  x = self.relu(x)
  x = self.fc2(x)
  x = self.relu(x)
  x = self.fc3(x) 
  x = F.softmax(self.fc3(x)) 
  
  return x



model = net(input_dim2, hidden_dim2, output_dim2) #create the network
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.RMSprop(model.parameters(), lr = learning_rate2)


def train(num_epochs2):
for i in range(num_epochs2):
  tmp_loss = []
  for (x,y) in train_loader:
    print(y.shape)
    print(x.shape)
    outputs = model(x) #forward pass
    print(outputs.shape)
    loss = criterion(outputs, y) #loss computation
    tmp_loss.append(loss.item()) #recording the loss
    optimizer.zero_grad() #all the accumulated gradient
    loss.backward()  #auto-differentiaton - accumulation of gradient
    optimizer.step() # a gradient step

  print("Loss at {}th epoch: {}".format(i, np.mean(tmp_loss)))  

我不知道我哪里错了。我的代码似乎工作正常。

此处,“init”函数中似乎缺少 relu 激活。或者在forward函数中多了一个relu激活。查看下面的代码并尝试找出多余的或缺少的内容。

def __init__(self, input_dim2, hidden_dim2, output_dim2):
    super(net, self).__init__()
    self.input_dim2 = input_dim2
    self.fc1 = nn.Linear(input_dim2, hidden_dim2)
    self.relu = nn.ReLU()
    self.fc2 = nn.Linear(hidden_dim2, hidden_dim2)
    self.fc3 = nn.Linear(hidden_dim2, output_dim2) 
def forward(self, x):
    x = self.fc1(x)
    x = self.relu(x)
    x = self.fc2(x)
    x = self.relu(x)
    x = self.fc3(x) 
    x = F.softmax(self.fc3(x)) 

return x

从有限的留言来看,我猜你错的地方是以下片段:

x = self.fc3(x) 
x = F.softmax(self.fc3(x))

尝试替换为:

x = self.fc3(x) 
x = F.softmax(x)

一个好的问题应该包括:错误回溯信息和可能重复错误的完整玩具示例!