Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Pytorch MNIST CNN输出错误=模型(图像)_Pytorch_Conv Neural Network - Fatal编程技术网

Pytorch MNIST CNN输出错误=模型(图像)

Pytorch MNIST CNN输出错误=模型(图像),pytorch,conv-neural-network,Pytorch,Conv Neural Network,我已尝试消除“未实施加薪”错误。它发生在行输出=模型(图像)处。 我已经转换了我的数据集,并通过数据加载器加载了它们。形状的打印输出立即位于主代码之前 列车形状和试验数据为火炬尺寸([300,1,28,28])和火炬尺寸([300,1,28,28])列车数据形状 class ConvNet(nn.Module): def __init__(self): super(ConvNet, self).__init__() self.layer1 = nn.Sequential(

我已尝试消除“未实施加薪”错误。它发生在行输出=模型(图像)处。 我已经转换了我的数据集,并通过数据加载器加载了它们。形状的打印输出立即位于主代码之前

列车形状和试验数据为火炬尺寸([300,1,28,28])和火炬尺寸([300,1,28,28])列车数据形状

class ConvNet(nn.Module):
  def __init__(self):
    super(ConvNet, self).__init__()
    self.layer1 = nn.Sequential(
        nn.Conv2d(1, 32, kernel_size=5, stride=1, padding=2),
        nn.ReLU(),
        nn.MaxPool2d(kernel_size=2, stride=2))
    self.layer2 = nn.Sequential(
        nn.Conv2d(32, 64, kernel_size=5, stride=1, padding=2),
        nn.ReLU(),
        nn.MaxPool2d(kernel_size=2, stride=2))
    self.drop_out = nn.Dropout()
    self.fc1 = nn.Linear(7 * 7 * 64, 1000)
    self.fc2 = nn.Linear(1000, 10)
def forward(self, x):
  out = self.layer1(x)
  out = self.layer2(out)
  out = out.reshape(out.size(0), -1)
  out = self.drop_out(out)
  out = self.fc1(out)
  out = self.fc2(out)
  print(f"output shape: {out.shape}")
  return out

device  = torch.device("cuda" if torch.cuda.is_available() else 'cpu')
print("Device used is {}".format(device))
model = ConvNet()
model.to(device) # send model to device/GPU for training 
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
num_epochs = 3

# Train the model
total_step = len(train_loader)
loss_list = []
acc_list = []
for epoch in range(num_epochs):
  for i, (images, labels) in enumerate(train_loader):
    #Run the forward pass
    # images = images.cuda()
    # label = labels.cuda()
    images = images.to(device, dtype =torch.float)
    label = label.to(device, dtype =torch.long)
    outputs = model(images)
    loss = criterion(outputs, labels)
    loss_list.append(loss.item())

    # Backprop and perform Adam optimisation
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

    # Track the accuracy
    total = labels.size(0)
    _, predicted = torch.max(outputs.data, 1)
    correct = (predicted == labels).sum().item()
    acc_list.append(correct / total)

    if (i + 1) % 100 == 0:
        print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}, Accuracy: {:.2f}%'
              .format(epoch + 1, num_epochs, i + 1, total_step, loss.item(),
                      (correct / total) * 100))

请显示完整的错误消息。我现在已解决此问题。def forward函数未缩进到ConvNet类下