莫烦pytorch学习笔记(八)——卷积神经网络

莫烦视频网址

  1 import os
  2 
  3 # third-party library
  4 import torch
  5 import torch.nn as nn
  6 import torch.utils.data as Data
  7 import torchvision
  8 import matplotlib.pyplot as plt
  9 
 10 # torch.manual_seed(1)    # reproducible
 11 
 12 # Hyper Parameters
 13 EPOCH = 1               # train the training data n times, to save time, we just train 1 epoch
 14 BATCH_SIZE = 50
 15 LR = 0.001              # learning rate
 16 DOWNLOAD_MNIST = False
 17 
 18 
 19 # Mnist digits dataset
 20 if not(os.path.exists('./mnist/')) or not os.listdir('./mnist/'):
 21     # not mnist dir or mnist is empyt dir
 22     DOWNLOAD_MNIST = True
 23 
 24 train_data = torchvision.datasets.MNIST(
 25     root='./mnist/',
 26     train=True,                                     # this is training data
 27     transform=torchvision.transforms.ToTensor(),    # 把数据压缩到0到1之间的numpy数据
 28                                                     # torch.FloatTensor of shape (C x H x W) and normalize in the range [0.0, 1.0]
 29     download=DOWNLOAD_MNIST,
 30 )
 31 
 32 # plot one example
 33 print(train_data.train_data.size())                 # (60000, 28, 28)
 34 print(train_data.train_labels.size())               # (60000)
 35 plt.imshow(train_data.train_data[0].numpy(), cmap='gray')
 36 plt.title('%i' % train_data.train_labels[0])
 37 plt.show()
 38 
 39 # Data Loader for easy mini-batch return in training, the image batch shape will be (50, 1, 28, 28)
 40 train_loader = Data.DataLoader(dataset=train_data, batch_size=BATCH_SIZE, shuffle=True)
 41 
 42 # pick 2000 samples to speed up testing
 43 test_data = torchvision.datasets.MNIST(root='./mnist/', train=False)
 44 test_x = torch.unsqueeze(test_data.test_data, dim=1).type(torch.FloatTensor)[:2000]/255.   # shape from (2000, 28, 28) to (2000, 1, 28, 28), value in range(0,1)
 45 test_y = test_data.test_labels[:2000]
 46 
 47 
 48 class CNN(nn.Module):
 49     def __init__(self):
 50         super(CNN, self).__init__()
 51         self.conv1 = nn.Sequential(         # input shape (1, 28, 28)
 52             nn.Conv2d(
 53                 in_channels=1,              # input height
 54                 out_channels=16,            # n_filters
 55                 kernel_size=5,              # filter size
 56                 stride=1,                   # filter movement/step
 57                 padding=2,                  # if want same width and length of this image after Conv2d, padding=(kernel_size-1)/2 if stride=1
 58             ),                              # output shape (16, 28, 28)
 59             nn.ReLU(),                      # activation
 60             nn.MaxPool2d(kernel_size=2),    # choose max value in 2x2 area, output shape (16, 14, 14)
 61         )
 62         self.conv2 = nn.Sequential(         # input shape (16, 14, 14)
 63             nn.Conv2d(16, 32, 5, 1, 2),     # output shape (32, 14, 14)
 64             nn.ReLU(),                      # activation
 65             nn.MaxPool2d(2),                # output shape (32, 7, 7)
 66         )
 67         self.out = nn.Linear(32 * 7 * 7, 10)   # fully connected layer, output 10 classes
 68 
 69     def forward(self, x):
 70         x = self.conv1(x)
 71         x = self.conv2(x)
 72         x = x.view(x.size(0), -1)           # flatten the output of conv2 to (batch_size, 32 * 7 * 7)
 73         output = self.out(x)
 74         return output, x    # return x for visualization
 75 
 76 
 77 cnn = CNN()
 78 print(cnn)  # net architecture
 79 
 80 optimizer = torch.optim.Adam(cnn.parameters(), lr=LR)   # optimize all cnn parameters
 81 loss_func = nn.CrossEntropyLoss()                       # the target label is not one-hotted
 82 
 83 # following function (plot_with_labels) is for visualization, can be ignored if not interested
 84 from matplotlib import cm
 85 try: from sklearn.manifold import TSNE; HAS_SK = True
 86 except: HAS_SK = False; print('Please install sklearn for layer visualization')
 87 def plot_with_labels(lowDWeights, labels):
 88     plt.cla()
 89     X, Y = lowDWeights[:, 0], lowDWeights[:, 1]
 90     for x, y, s in zip(X, Y, labels):
 91         c = cm.rainbow(int(255 * s / 9)); plt.text(x, y, s, backgroundcolor=c, fontsize=9)
 92     plt.xlim(X.min(), X.max()); plt.ylim(Y.min(), Y.max()); plt.title('Visualize last layer'); plt.show(); plt.pause(0.01)
 93 
 94 plt.ion()
 95 # training and testing
 96 for epoch in range(EPOCH):
 97     for step, (b_x, b_y) in enumerate(train_loader):   # gives batch data, normalize x when iterate train_loader
 98 
 99         output = cnn(b_x)[0]               # cnn output
100         loss = loss_func(output, b_y)   # cross entropy loss
101         optimizer.zero_grad()           # clear gradients for this training step
102         loss.backward()                 # backpropagation, compute gradients
103         optimizer.step()                # apply gradients
104 
105         if step % 50 == 0:
106             test_output, last_layer = cnn(test_x)
107             pred_y = torch.max(test_output, 1)[1].data.numpy()
108             accuracy = float((pred_y == test_y.data.numpy()).astype(int).sum()) / float(test_y.size(0))
109             print('Epoch: ', epoch, '| train loss: %.4f' % loss.data.numpy(), '| test accuracy: %.2f' % accuracy)
110             if HAS_SK:
111                 # Visualization of trained flatten layer (T-SNE)
112                 tsne = TSNE(perplexity=30, n_components=2, init='pca', n_iter=5000)
113                 plot_only = 500
114                 low_dim_embs = tsne.fit_transform(last_layer.data.numpy()[:plot_only, :])
115                 labels = test_y.numpy()[:plot_only]
116                 plot_with_labels(low_dim_embs, labels)
117 plt.ioff()
118 
119 # print 10 predictions from test data
120 test_output, _ = cnn(test_x[:10])
121 pred_y = torch.max(test_output, 1)[1].data.numpy()
122 print(pred_y, 'prediction number')
123 print(test_y[:10].numpy(), 'real number')

猜你喜欢

转载自www.cnblogs.com/henuliulei/p/11400012.html