You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

3_CNN_MNIST.py 3.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import torch
  2. import torch.nn as nn
  3. import torch.nn.functional as F
  4. from torch.autograd import Variable
  5. from torchvision import datasets, transforms
  6. # Training settings
  7. batch_size = 64
  8. # MNIST Dataset
  9. dataset_path = "../data/mnist"
  10. train_dataset = datasets.MNIST(root=dataset_path,
  11. train=True,
  12. transform=transforms.ToTensor(),
  13. download=True)
  14. test_dataset = datasets.MNIST(root=dataset_path,
  15. train=False,
  16. transform=transforms.ToTensor())
  17. # Data Loader (Input Pipeline)
  18. train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
  19. batch_size=batch_size,
  20. shuffle=True)
  21. test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
  22. batch_size=batch_size,
  23. shuffle=False)
  24. # Define the network
  25. class Net_CNN(nn.Module):
  26. def __init__(self):
  27. super(Net_CNN, self).__init__()
  28. self.conv1 = nn.Conv2d(1, 6, 5)
  29. self.conv2 = nn.Conv2d(6, 16, 5)
  30. self.fc1 = nn.Linear(16*4*4, 120)
  31. self.fc2 = nn.Linear(120, 84)
  32. self.fc3 = nn.Linear(84, 10)
  33. def forward(self, x):
  34. x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
  35. x = F.max_pool2d(F.relu(self.conv2(x)), (2, 2))
  36. x = x.view(x.size()[0], -1)
  37. x = F.relu(self.fc1(x))
  38. x = F.relu(self.fc2(x))
  39. x = self.fc3(x)
  40. return x
  41. # define optimizer & criterion
  42. model = Net_CNN()
  43. optim = torch.optim.Adam(model.parameters(), 0.01)
  44. criterion = nn.CrossEntropyLoss()
  45. # train the network
  46. for e in range(100):
  47. # train
  48. model.train()
  49. for batch_idx, (data, target) in enumerate(train_loader):
  50. data, target = Variable(data), Variable(target)
  51. out = model(data)
  52. loss = criterion(out, target)
  53. optim.zero_grad()
  54. loss.backward()
  55. optim.step()
  56. if batch_idx % 100 == 0:
  57. pred = out.data.max(1, keepdim=True)[1]
  58. c = float(pred.eq(target.data.view_as(pred)).cpu().sum() ) /out.size(0)
  59. print("epoch: %5d, loss: %f, acc: %f" %
  60. ( e +1, loss.data.item(), c))
  61. # test
  62. model.eval()
  63. test_loss = 0.0
  64. correct = 0.0
  65. for data, target in test_loader:
  66. data, target = Variable(data), Variable(target)
  67. output = model(data)
  68. # sum up batch loss
  69. test_loss += criterion(output, target).item()
  70. # get the index of the max
  71. pred = output.data.max(1, keepdim=True)[1]
  72. correct += float(pred.eq(target.data.view_as(pred)).cpu().sum())
  73. test_loss /= len(test_loader.dataset)
  74. print("\nTest set: Average loss: %.4f, Accuracy: %6d/%6d (%4.2f %%)\n" %
  75. (test_loss,
  76. correct, len(test_loader.dataset),
  77. 100.0*correct / len(test_loader.dataset)) )

机器学习越来越多应用到飞行器、机器人等领域,其目的是利用计算机实现类似人类的智能,从而实现装备的智能化与无人化。本课程旨在引导学生掌握机器学习的基本知识、典型方法与技术,通过具体的应用案例激发学生对该学科的兴趣,鼓励学生能够从人工智能的角度来分析、解决飞行器、机器人所面临的问题和挑战。本课程主要内容包括Python编程基础,机器学习模型,无监督学习、监督学习、深度学习基础知识与实现,并学习如何利用机器学习解决实际问题,从而全面提升自我的《综合能力》。