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_NN_FC_1.py 3.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import torch
  2. import torch.nn as nn
  3. import torch.nn.functional as F
  4. import torch.optim as optim
  5. from torch.autograd import Variable
  6. from torchvision import datasets, transforms
  7. # Training settings
  8. batch_size = 64
  9. # MNIST Dataset
  10. dataset_path = "../data/mnist"
  11. train_dataset = datasets.MNIST(root=dataset_path,
  12. train=True,
  13. transform=transforms.ToTensor(),
  14. download=True)
  15. test_dataset = datasets.MNIST(root=dataset_path,
  16. train=False,
  17. transform=transforms.ToTensor())
  18. # Data Loader (Input Pipeline)
  19. train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
  20. batch_size=batch_size,
  21. shuffle=True)
  22. test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
  23. batch_size=batch_size,
  24. shuffle=False)
  25. # define Network
  26. class NN_FC1(nn.Module):
  27. def __init__(self):
  28. super(NN_FC1, self).__init__()
  29. self.l1 = nn.Linear(784, 520)
  30. self.l2 = nn.Linear(520, 320)
  31. self.l3 = nn.Linear(320, 240)
  32. self.l4 = nn.Linear(240, 120)
  33. self.l5 = nn.Linear(120, 10)
  34. def forward(self, x):
  35. x = x.view(-1, 784) # Flatten the data (n, 1, 28, 28)-> (n, 784)
  36. x = F.relu(self.l1(x))
  37. x = F.relu(self.l2(x))
  38. x = F.relu(self.l3(x))
  39. x = F.relu(self.l4(x))
  40. return self.l5(x)
  41. # Define the network
  42. class NN_FC2(nn.Module):
  43. def __init__(self):
  44. super(NN_FC2, self).__init__()
  45. in_dim = 28*28
  46. n_hidden_1 = 300
  47. n_hidden_2 = 100
  48. out_dim = 10
  49. self.layer1 = nn.Linear(in_dim, n_hidden_1)
  50. self.layer2 = nn.Linear(n_hidden_1, n_hidden_2)
  51. self.layer3 = nn.Linear(n_hidden_2, out_dim)
  52. def forward(self, x):
  53. x = x.view(-1, 784)
  54. x = F.relu(self.layer1(x))
  55. x = F.relu(self.layer2(x))
  56. x = self.layer3(x)
  57. return x
  58. # create the NN object
  59. model = NN_FC2()
  60. criterion = nn.CrossEntropyLoss()
  61. optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.5)
  62. def train(epoch):
  63. model.train()
  64. for batch_idx, (data, target) in enumerate(train_loader):
  65. data, target = Variable(data), Variable(target)
  66. optimizer.zero_grad()
  67. output = model(data)
  68. loss = criterion(output, target)
  69. loss.backward()
  70. optimizer.step()
  71. if batch_idx % 100 == 0:
  72. print("Train epoch: %6d [%6d/%6d (%.0f %%)] \t Loss: %.6f" % (
  73. epoch, batch_idx * len(data), len(train_loader.dataset),
  74. 100. * batch_idx / len(train_loader), loss.data[0]) )
  75. def test():
  76. model.eval()
  77. test_loss = 0.0
  78. correct = 0.0
  79. for data, target in test_loader:
  80. data, target = Variable(data), Variable(target)
  81. output = model(data)
  82. # sum up batch loss
  83. test_loss += criterion(output, target).data[0]
  84. # get the index of the max
  85. pred = output.data.max(1, keepdim=True)[1]
  86. correct += float(pred.eq(target.data.view_as(pred)).cpu().sum())
  87. test_loss /= len(test_loader.dataset)
  88. print("\nTest set: Average loss: %.4f, Accuracy: %6d/%6d (%4.2f %%)\n" %
  89. (test_loss,
  90. correct, len(test_loader.dataset),
  91. 100.0*correct / len(test_loader.dataset)) )
  92. for epoch in range(1, 10):
  93. train(epoch)
  94. test()

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