code: import os
  import torch
  import torch.nn as nn
  import torch.utils.data as Data
  import torchvision
  
  
  EPOCH = 1
  BATCH_SIZE = 50
  LR = 0.001
  DOWNLOAD_MNIST = False
  
  
  if not(os.path.exists('./mnist/')) or not os.listdir('./mnist/'):
      DOWNLOAD_MNIST = True
  
  
  train_data = torchvision.datasets.MNIST(
      root='./mnist/',
      train=True,
      transform=torchvision.transforms.ToTensor(),
      download=DOWNLOAD_MNIST,
  )
  
  
  print(train_data.data.size())
  print(train_data.targets.size())
  
  
  train_loader = Data.DataLoader(dataset=train_data, batch_size=BATCH_SIZE, shuffle=True) # batch shape:(50,1,28,28)
  
  
  test_data = torchvision.datasets.MNIST(root='./mnist/', train=False)
  test_x = torch.unsqueeze(test_data.data, dim=1).type(torch.FloatTensor)[:2000]/255. # shape:(2000,1,28,28)
  test_y = test_data.targets[:2000]
  
  
  class CNN(nn.Module):
      def __init__(self):
          super(CNN, self).__init__()
          self.conv1 = nn.Sequential(
              nn.Conv2d(1, 16, 5, 1, 2),
              nn.ReLU(),
              nn.MaxPool2d(kernel_size=2),
          )
          self.conv2 = nn.Sequential(
              nn.Conv2d(16, 32, 5, 1, 2),
              nn.ReLU(),
              nn.MaxPool2d(2),
          )
          self.out = nn.Linear(32*7*7, 10)
  
      def forward(self, x):
          x = self.conv1(x)
          x = self.conv2(x)
          x = x.view(x.size(0), -1)
          out = self.out(x)
          return out, x
  
  
  cnn = CNN()
  print(cnn)
  optimizer = torch.optim.Adam(cnn.parameters(), lr=LR)
  loss_func = nn.CrossEntropyLoss()
  
  
  for epoch in range(EPOCH):
      for step, (b_x, b_y) in enumerate(train_loader):
          output = cnn(b_x)[0]
          loss = loss_func(output, b_y)
          optimizer.zero_grad()
          loss.backward()
          optimizer.step()
  
          if step % 50 == 0:
              test_output, last_layer = cnn(test_x)
              pred_y = torch.max(test_output, 1)[1].data.numpy()
              accuracy = float((pred_y == test_y.data.numpy()).astype(int).sum())/float(test_y.size(0))
              print('Epoch: ', epoch, '| Training Loss: %.4f' % loss.data.numpy(), '| Test Accuracy: %.2f' % accuracy)
  
  
  test_output, _ = cnn(test_x[:20])
  pred_y = torch.max(test_output, 1)[1].data.numpy()
  print(pred_y, 'Prediction Number')
  print(test_y[:20].numpy(), 'Real Number')
apis: torch.unsqueeze torch.utils.data.DataLoader torch.nn.MaxPool2d torch.nn.Linear torch.nn.CrossEntropyLoss torch.nn.Conv2d torch.max torch.nn.ReLU