【问题标题】:PyTorch MNIST Model RuntimeError: mat1 and mat2 shapes cannot be multiplied (3584x28 and 784x10)PyTorch MNIST 模型运行时错误:mat1 和 mat2 形状不能相乘(3584x28 和 784x10)
【发布时间】:2021-05-26 06:22:13
【问题描述】:

我正在尝试构建一个简单的 MNIST 模型,这就是我构建的 -

training_loader = DataLoader(training_dataset, 128, shuffle = True)
validation_loader = DataLoader(validation_dataset, 128)


class mnistmodel(nn.Module):
  def __init__(self):
    super().__init__()
    self.linear1 = nn.Linear(784, 10)
    self.linear2 = nn.Linear(10, 5)
    self.linear3 = nn.Linear(5, 10)  

  def forward(self, xb):
    xb.reshape(-1, 784)
    predicted = F.relu(self.linear1(xb))
    predicted.reshape(-1, 10)
    predicted = F.relu(self.linear2(predicted))
    predicted.reshape(-1, 5)
    predicted = self.linear3(predicted)
    return predicted
  
  def training_step(self, batch):
    images, labels = batch
    predicted = self(images)
    loss = F.cross_entropy(predicted, labels)
    return loss
  
  def validation_step(self, batch):
    images, labels = batch
    predicted = self(images)
    loss = F.cross_entropy(predicted, labels)
    _, preds = torch.max(predicted, dim=1)
    accuracy = torch.tensor(torch.sum(preds == labels).item() / len(preds))
    return {'validation_loss': loss, 'validation_accuracy': accuracy}

  def validation_epoch_end(self, outputs):
    batch_losses = [x['validation_loss'] for x in outputs]
    epoch_loss = torch.stack(batch_losses).mean()
    batch_accs = [x['validation_acc'] for x in outputs]
    epoch_acc = torch.stack(batch_accs).mean()
    return {'validation_loss': epoch_loss.item(), 'validation_accuracy': epoch_acc.item()}
    
def epoch_end(self, epoch, result):
    print(f"Epoch [{epoch}], val_loss: {result['validation_loss']}, val_acc: {result['validation_acc']}")
    
model = mnistmodel()


def fit_mnist(epochs, lr, model, training_loader, validation_loader, optimizer_function=torch.optim.SGD):
    optimizer = optimizer_function(model.parameters(), lr)
    history = []
    
    for epoch in range(epochs):
        
        for batch in training_loader:
            loss = model.training_step(batch)
            loss.backward()
            optimizer.step()
            optimizer.zero_grad()
        
        result = evaluate(model, validation_loader)
        model.epoch_end(epoch, result)
        history.append(result)


    return history

history1 = fit_mnist(5, 0.001, model, training_loader, validation_loader)

我收到以下错误 -

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-162-48e4fe0cc2d9> in <module>()
----> 1 history1 = fit_mnist(5, 0.001, model, training_loader, validation_loader)

6 frames
/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in linear(input, weight, bias)
   1751     if has_torch_function_variadic(input, weight):
   1752         return handle_torch_function(linear, (input, weight), input, weight, bias=bias)
-> 1753     return torch._C._nn.linear(input, weight, bias)
   1754 
   1755 

RuntimeError: mat1 and mat2 shapes cannot be multiplied (3584x28 and 784x10)

我是 pytorch 的新手,但据我所知,形状似乎很好,这里出了什么问题? 我已经手动检查过,尺寸似乎很好。我什至尝试了多种方法来删除添加重塑命令,但此错误仍然存​​在

【问题讨论】:

    标签: error-handling pytorch


    【解决方案1】:

    我发现了一个错误,我需要重新分配我错过的重塑并花费其余时间进行调试。

    【讨论】:

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