当前位置:   article > 正文

利用knn svm cnn 逻辑回归 mlp rnn等方法实现mnist数据集分类(pytorch实现及源码解析)_pytorchcnn+svm

pytorchcnn+svm

电脑 配置:

  1. python3.6*
  2. *Pytorch 1.2.0*
  3. *torchvision 0.4.0

想学习机器学习和深度学习的同学,首先找个比较经典的案例和经典的方法自己动手试一试,分析这些方法的思想和每一行代码是一个快速入门的小技巧,今天我们谈论怎么用一些比较经典的方法实现经典数据集MNIST的识别分类问题。

废话不多说,直接上代码!!!

可爱小熊动图小表情 - 可爱动图小表情

1.svm实现

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # arguments define
  4. import argparse
  5. # load torch
  6. import torchvision
  7. # other utilities
  8. # import matplotlib.pyplot as plt
  9. from sklearn import svm
  10. from sklearn.metrics import confusion_matrix
  11. #%% Load the training data
  12. def MNIST_DATASET_TRAIN(downloads, train_amount):
  13.    # Load dataset
  14.    training_data = torchvision.datasets.MNIST(
  15.              root = './mnist/',
  16.              train = True,
  17.              transform = torchvision.transforms.ToTensor(),
  18.              download = downloads
  19.             )
  20.    
  21.    #Convert Training data to numpy
  22.    train_data = training_data.train_data.numpy()[:train_amount]
  23.    train_label = training_data.train_labels.numpy()[:train_amount]
  24.  
  25.    # Print training data size
  26.    print('Training data size: ',train_data.shape)
  27.    print('Training data label size:',train_label.shape)  
  28.    # plt.imshow(train_data[0])
  29.    # plt.show()
  30.    
  31.    train_data = train_data/255.0
  32.    
  33.    return train_data, train_label
  34. #%% Load the test data
  35. def MNIST_DATASET_TEST(downloads, test_amount):
  36.    # Load dataset
  37.    testing_data = torchvision.datasets.MNIST(
  38.              root = './mnist/',
  39.              train = False,
  40.              transform = torchvision.transforms.ToTensor(),
  41.              download = downloads
  42.             )
  43.    
  44.    # Convert Testing data to numpy
  45.    test_data = testing_data.test_data.numpy()[:test_amount]
  46.    test_label = testing_data.test_labels.numpy()[:test_amount]
  47.    
  48.    # Print training data size
  49.    print('test data size: ',test_data.shape)
  50.    print('test data label size:',test_label.shape)  
  51.    # plt.imshow(test_data[0])
  52.    # plt.show()
  53.    
  54.    test_data = test_data/255.0
  55.    
  56.    return test_data, test_label
  57. #%% Main function for MNIST dataset    
  58. if __name__=='__main__':
  59.    # Training Arguments Settings
  60.    parser = argparse.ArgumentParser(description='Saak')
  61.    parser.add_argument('--download_MNIST', default=True, metavar='DL',
  62.                        help='Download MNIST (default: True)')
  63.    parser.add_argument('--train_amount', type=int, default=60000,
  64.                        help='Amount of training samples')
  65.    parser.add_argument('--test_amount', type=int, default=2000,
  66.                        help='Amount of testing samples')
  67.    args = parser.parse_args()
  68.    
  69.    # Print Arguments
  70.    print('\n----------Argument Values-----------')
  71.    for name, value in vars(args).items():
  72.        print('%s: %s' % (str(name), str(value)))
  73.    print('------------------------------------\n')
  74.    
  75.    
  76.    # Load Training Data & Testing Data
  77.    train_data, train_label = MNIST_DATASET_TRAIN(args.download_MNIST, args.train_amount)
  78.    test_data, test_label = MNIST_DATASET_TEST(args.download_MNIST, args.test_amount)
  79.    training_features = train_data.reshape(args.train_amount,-1)
  80.    test_features = test_data.reshape(args.test_amount,-1)
  81.    # Training SVM
  82.    print('------Training and testing SVM------')
  83.    clf = svm.SVC(C=5, gamma=0.05,max_iter=10)
  84.    clf.fit(training_features, train_label)
  85.    
  86.    
  87.    #Test on test data
  88.    test_result = clf.predict(test_features)
  89.    precision = sum(test_result == test_label)/test_label.shape[0]
  90.    print('Test precision: ', precision)
  91.    
  92.    #Test on Training data
  93.    train_result = clf.predict(training_features)
  94.    precision = sum(train_result == train_label)/train_label.shape[0]
  95.    print('Training precision: ', precision)
  96.    
  97.    
  98.    #Show the confusion matrix
  99.    matrix = confusion_matrix(test_label, test_result)

2.cnn实现

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

3.knn实现

  1. from __future__ import print_function
  2. import os
  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 time
  9. # import matplotlib.pyplot as plt
  10. localtime = time.asctime( time.localtime(time.time()) )
  11. print("本地时间为 :", localtime)
  12. # torch.manual_seed(1)   # reproducible
  13. # Hyper Parameters
  14. EPOCH = 1               # train the training data n times, to save time, we just train 1 epoch
  15. BATCH_SIZE = 50
  16. LR = 0.001              # learning rate
  17. DOWNLOAD_MNIST = False
  18. # Mnist digits dataset
  19. if not(os.path.exists('./mnist/')) or not os.listdir('./mnist/'):
  20.    # not mnist dir or mnist is empyt dir
  21.    DOWNLOAD_MNIST = True
  22. train_data = torchvision.datasets.MNIST(
  23.    root='./mnist/',
  24.    train=True,                                     # this is training data
  25.    transform=torchvision.transforms.ToTensor(),    # Converts a PIL.Image or numpy.ndarray to
  26.                                                    # torch.FloatTensor of shape (C x H x W) and normalize in the range [0.0, 1.0]
  27.    download=DOWNLOAD_MNIST,
  28. )
  29. # plot one example
  30. print(train_data.train_data.size())                 # (60000, 28, 28)
  31. print(train_data.train_labels.size())               # (60000)
  32. # plt.imshow(train_data.train_data[0].numpy(), cmap='gray')
  33. # plt.title('%i' % train_data.train_labels[0])
  34. # plt.show()
  35. # pick 2000 samples to speed up testing
  36. train_data = torchvision.datasets.MNIST(root='./mnist/', train=True)
  37. train_x = torch.unsqueeze(train_data.train_data, dim=1).type(torch.FloatTensor)[:60000]/255.   # shape from (2000, 28, 28) to (2000, 1, 28, 28), value in range(0,1)
  38. train_y = train_data.train_labels[:60000]
  39. # pick 2000 samples to speed up testing
  40. test_data = torchvision.datasets.MNIST(root='./mnist/', train=False)
  41. 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)
  42. test_y = test_data.test_labels[:2000]
  43. print(train_x.size(),train_y.size(),test_x.size(),test_y.size())
  44. train_x = train_x.view(-1,28*28)
  45. test_x = test_x.view(-1,28*28)
  46. # K-Nearest Neighbor Classification
  47. from sklearn.model_selection import train_test_split
  48. from sklearn.neighbors import KNeighborsClassifier
  49. from sklearn.metrics import classification_report
  50. from sklearn import datasets
  51. from skimage import exposure
  52. # import matplotlib.pyplot as plt
  53. import numpy as np
  54. # import imutils
  55. # import cv2
  56. # load the MNIST digits dataset
  57. # mnist = datasets.load_digits()
  58. # print(len(np.array(mnist.data)[0]))
  59. # Training and testing split,
  60. # 75% for training and 25% for testing
  61. # print(len(mnist))
  62. # (trainData, testData, trainLabels, testLabels) = train_test_split(np.array(mnist.data), mnist.target, test_size=0.25, random_state=42)
  63. # take 10% of the training data and use that for validation
  64. # (trainData, valData, trainLabels, valLabels) = train_test_split(trainData, trainLabels, test_size=0.1, random_state=84)
  65. trainData = np.array(train_x)
  66. testData = np.array(test_x)
  67. trainLabels = np.array(train_y)
  68. testLabels = np.array(test_y)
  69. valData = testData
  70. valLabels = testLabels
  71. # Checking sizes of each data split
  72. print("training data points: {}".format(len(trainLabels)))
  73. print("validation data points: {}".format(len(valLabels)))
  74. print("testing data points: {}".format(len(testLabels)))
  75. # initialize the values of k for our k-Nearest Neighbor classifier along with the
  76. # list of accuracies for each value of k
  77. kVals = range(1, 30, 2)
  78. accuracies = []
  79. # loop over kVals
  80. for k in range(1, 30, 2):
  81.    # train the classifier with the current value of `k`
  82.    model = KNeighborsClassifier(n_neighbors=k)
  83.    model.fit(trainData, trainLabels)
  84.    # evaluate the model and print the accuracies list
  85.    score = model.score(valData, valLabels)
  86.    print("k=%d, accuracy=%.2f%%" % (k, score * 100))
  87.    accuracies.append(score)
  88.    localtime = time.asctime( time.localtime(time.time()) )
  89.    print("本地时间为 :", localtime)
  90. # largest accuracy
  91. # np.argmax returns the indices of the maximum values along an axis
  92. i = np.argmax(accuracies)
  93. print("k=%d achieved highest accuracy of %.2f%% on validation data" % (kVals[i],
  94.    accuracies[i] * 100))
  95. # Now that I know the best value of k, re-train the classifier
  96. model = KNeighborsClassifier(n_neighbors=kVals[i])
  97. model.fit(trainData, trainLabels)
  98. # Predict labels for the test set
  99. predictions = model.predict(testData)
  100. # Evaluate performance of model for each of the digits
  101. print("EVALUATION ON TESTING DATA")
  102. print(classification_report(testLabels, predictions))
  103. # some indices are classified correctly 100% of the time (precision = 1)
  104. # high accuracy (98%)
  105. # check predictions against images
  106. # loop over a few random digits
  107. image = testData
  108. j = 0
  109. for i in np.random.randint(0, high=len(testLabels), size=(24,)):
  110.        # np.random.randint(low, high=None, size=None, dtype='l')
  111.    prediction = model.predict(image)[i]
  112.    image0 = image[i].reshape((8, 8)).astype("uint8")
  113.    image0 = exposure.rescale_intensity(image0, out_range=(0, 255))
  114.    # plt.subplot(4,6,j+1)
  115.    # plt.title(str(prediction))
  116.    # plt.imshow(image0,cmap='gray')
  117.    # plt.axis('off')
  118.        # convert the image for a 64-dim array to an 8 x 8 image compatible with OpenCV,
  119.        # then resize it to 32 x 32 pixels for better visualization
  120.        #image0 = imutils.resize(image[0], width=32, inter=cv2.INTER_CUBIC)
  121.    j = j+1
  122.    # show the prediction
  123.    # print("I think that digit is: {}".format(prediction))
  124.    # print('image0 is ',image0)
  125.    # cv2.imshow("Image", image0)
  126.    # cv2.waitKey(0) # press enter to view each one!
  127. # plt.show()

4.逻辑回归实现

  1. # library
  2. # standard library
  3. import os
  4. # third-party library
  5. import torch
  6. import torch.nn as nn
  7. import torch.utils.data as Data
  8. import torchvision
  9. # import matplotlib.pyplot as plt
  10. # torch.manual_seed(1)   # reproducible
  11. # Hyper Parameters
  12. EPOCH = 1               # train the training data n times, to save time, we just train 1 epoch
  13. BATCH_SIZE = 50
  14. LR = 0.001              # learning rate
  15. DOWNLOAD_MNIST = False
  16. # Mnist digits dataset
  17. if not(os.path.exists('./mnist/')) or not os.listdir('./mnist/'):
  18.    # not mnist dir or mnist is empyt dir
  19.    DOWNLOAD_MNIST = True
  20. train_data = torchvision.datasets.MNIST(
  21.    root='./mnist/',
  22.    train=True,                                     # this is training data
  23.    transform=torchvision.transforms.ToTensor(),    # Converts a PIL.Image or numpy.ndarray to
  24.                                                    # torch.FloatTensor of shape (C x H x W) and normalize in the range [0.0, 1.0]
  25.    download=DOWNLOAD_MNIST,
  26. )
  27. # plot one example
  28. print(train_data.train_data.size())                 # (60000, 28, 28)
  29. print(train_data.train_labels.size())               # (60000)
  30. # plt.imshow(train_data.train_data[0].numpy(), cmap='gray')
  31. # plt.title('%i' % train_data.train_labels[0])
  32. # plt.show()
  33. # Data Loader for easy mini-batch return in training, the image batch shape will be (50, 1, 28, 28)
  34. train_loader = Data.DataLoader(dataset=train_data, batch_size=BATCH_SIZE, shuffle=True)
  35. # pick 2000 samples to speed up testing
  36. test_data = torchvision.datasets.MNIST(root='./mnist/', train=False)
  37. 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)
  38. test_y = test_data.test_labels[:2000]
  39. class logisticRg(nn.Module):
  40.    def __init__(self):
  41.        super(logisticRg, self).__init__()
  42.        self.lr = nn.Sequential(
  43.            nn.Linear(28*28,10)
  44.       )
  45.    def forward(self, x):
  46.        output = self.lr(x)
  47.        return output, x    # return x for visualization
  48. lor = logisticRg()
  49. print(lor)  # net architecture
  50. optimizer = torch.optim.Adam(lor.parameters(), lr=LR)   # optimize all logistic parameters
  51. loss_func = nn.CrossEntropyLoss()                       # the target label is not one-hotted
  52. # following function (plot_with_labels) is for visualization, can be ignored if not interested
  53. # from matplotlib import cm
  54. # try: from sklearn.manifold import TSNE; HAS_SK = True
  55. # except: HAS_SK = False; print('Please install sklearn for layer visualization')
  56. # def plot_with_labels(lowDWeights, labels):
  57. #     plt.cla()
  58. #     X, Y = lowDWeights[:, 0], lowDWeights[:, 1]
  59. #     for x, y, s in zip(X, Y, labels):
  60. #         c = cm.rainbow(int(255 * s / 9)); plt.text(x, y, s, backgroundcolor=c, fontsize=9)
  61. #     plt.xlim(X.min(), X.max()); plt.ylim(Y.min(), Y.max()); plt.title('Visualize last layer'); plt.show(); plt.pause(0.01)
  62. # plt.ion()
  63. # training and testing
  64. for epoch in range(EPOCH):
  65.    for step, (b_x, b_y) in enumerate(train_loader):   # gives batch data, normalize x when iterate train_loader
  66.        # print(b_x.size())
  67.        b_x = b_x.view(-1, 28*28)
  68.        # print(b_x.size())
  69.        output = lor(b_x)[0]               # logistic output
  70.        loss = loss_func(output, b_y)   # cross entropy loss
  71.        optimizer.zero_grad()           # clear gradients for this training step
  72.        loss.backward()                 # backpropagation, compute gradients
  73.        optimizer.step()                # apply gradients
  74.        if step % 50 == 0:
  75.            test_output, last_layer = lor(test_x.view(-1,28*28))
  76.            pred_y = torch.max(test_output, 1)[1].data.numpy()
  77.            accuracy = float((pred_y == test_y.data.numpy()).astype(int).sum()) / float(test_y.size(0))
  78.            print('Epoch: ', epoch, '| train loss: %.4f' % loss.data.numpy(), '| test accuracy: %.2f' % accuracy)
  79.            # if HAS_SK:
  80.            #     # Visualization of trained flatten layer (T-SNE)
  81.            #     tsne = TSNE(perplexity=30, n_components=2, init='pca', n_iter=5000)
  82.            #     plot_only = 500
  83.            #     low_dim_embs = tsne.fit_transform(last_layer.data.numpy()[:plot_only, :])
  84.            #     labels = test_y.numpy()[:plot_only]
  85.            #     plot_with_labels(low_dim_embs, labels)
  86. # plt.ioff()
  87. # print 10 predictions from test data
  88. test_output, _ = lor(test_x[:10].view(-1,28*28))
  89. pred_y = torch.max(test_output, 1)[1].data.numpy()
  90. print(pred_y, 'prediction number')
  91. print(test_y[:10].numpy(), 'real number')

5.mlp实现

  1. # library
  2. # standard library
  3. import os
  4. # third-party library
  5. import torch
  6. import torch.nn as nn
  7. import torch.utils.data as Data
  8. import torchvision
  9. # import matplotlib.pyplot as plt
  10. # torch.manual_seed(1)   # reproducible
  11. # Hyper Parameters
  12. EPOCH = 1               # train the training data n times, to save time, we just train 1 epoch
  13. BATCH_SIZE = 50
  14. LR = 0.001              # learning rate
  15. DOWNLOAD_MNIST = False
  16. # Mnist digits dataset
  17. if not(os.path.exists('./mnist/')) or not os.listdir('./mnist/'):
  18.    # not mnist dir or mnist is empyt dir
  19.    DOWNLOAD_MNIST = True
  20. train_data = torchvision.datasets.MNIST(
  21.    root='./mnist/',
  22.    train=True,                                     # this is training data
  23.    transform=torchvision.transforms.ToTensor(),    # Converts a PIL.Image or numpy.ndarray to
  24.                                                    # torch.FloatTensor of shape (C x H x W) and normalize in the range [0.0, 1.0]
  25.    download=DOWNLOAD_MNIST,
  26. )
  27. # plot one example
  28. print(train_data.train_data.size())                 # (60000, 28, 28)
  29. print(train_data.train_labels.size())               # (60000)
  30. # plt.imshow(train_data.train_data[0].numpy(), cmap='gray')
  31. # plt.title('%i' % train_data.train_labels[0])
  32. # plt.show()
  33. # Data Loader for easy mini-batch return in training, the image batch shape will be (50, 1, 28, 28)
  34. train_loader = Data.DataLoader(dataset=train_data, batch_size=BATCH_SIZE, shuffle=True)
  35. # pick 2000 samples to speed up testing
  36. test_data = torchvision.datasets.MNIST(root='./mnist/', train=False)
  37. 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)
  38. test_y = test_data.test_labels[:2000]
  39. class MLP(nn.Module):
  40.    def __init__(self):
  41.        super(MLP, self).__init__()
  42.        self.mlp = nn.Sequential(
  43.            nn.Linear(28*28,28*28),
  44.            nn.Linear(28*28,10)
  45.       )
  46.    def forward(self, x):
  47.        output = self.mlp(x)
  48.        return output, x    # return x for visualization
  49. mlp = MLP()
  50. print(mlp)  # net architecture
  51. optimizer = torch.optim.Adam(mlp.parameters(), lr=LR)   # optimize all logistic parameters
  52. loss_func = nn.CrossEntropyLoss()                       # the target label is not one-hotted
  53. # following function (plot_with_labels) is for visualization, can be ignored if not interested
  54. # from matplotlib import cm
  55. # try: from sklearn.manifold import TSNE; HAS_SK = True
  56. # except: HAS_SK = False; print('Please install sklearn for layer visualization')
  57. # def plot_with_labels(lowDWeights, labels):
  58. #     plt.cla()
  59. #     X, Y = lowDWeights[:, 0], lowDWeights[:, 1]
  60. #     for x, y, s in zip(X, Y, labels):
  61. #         c = cm.rainbow(int(255 * s / 9)); plt.text(x, y, s, backgroundcolor=c, fontsize=9)
  62. #     plt.xlim(X.min(), X.max()); plt.ylim(Y.min(), Y.max()); plt.title('Visualize last layer'); plt.show(); plt.pause(0.01)
  63. # plt.ion()
  64. # training and testing
  65. for epoch in range(EPOCH):
  66.    for step, (b_x, b_y) in enumerate(train_loader):   # gives batch data, normalize x when iterate train_loader
  67.        # print(b_x.size())
  68.        b_x = b_x.view(-1, 28*28)
  69.        # print(b_x.size())
  70.        output = mlp(b_x)[0]               # logistic output
  71.        loss = loss_func(output, b_y)   # cross entropy loss
  72.        optimizer.zero_grad()           # clear gradients for this training step
  73.        loss.backward()                 # backpropagation, compute gradients
  74.        optimizer.step()                # apply gradients
  75.        if step % 50 == 0:
  76.            test_output, last_layer = mlp(test_x.view(-1,28*28))
  77.            pred_y = torch.max(test_output, 1)[1].data.numpy()
  78.            accuracy = float((pred_y == test_y.data.numpy()).astype(int).sum()) / float(test_y.size(0))
  79.            print('Epoch: ', epoch, '| train loss: %.4f' % loss.data.numpy(), '| test accuracy: %.2f' % accuracy)
  80.            # if HAS_SK:
  81.            #     # Visualization of trained flatten layer (T-SNE)
  82.            #     tsne = TSNE(perplexity=30, n_components=2, init='pca', n_iter=5000)
  83.            #     plot_only = 500
  84.            #     low_dim_embs = tsne.fit_transform(last_layer.data.numpy()[:plot_only, :])
  85.            #     labels = test_y.numpy()[:plot_only]
  86.            #     plot_with_labels(low_dim_embs, labels)
  87. # plt.ioff()
  88. # print 10 predictions from test data
  89. test_output, _ = mlp(test_x[:10].view(-1,28*28))
  90. pred_y = torch.max(test_output, 1)[1].data.numpy()
  91. print(pred_y, 'prediction number')
  92. print(test_y[:10].numpy(), 'real number')

6.rnn实现

  1. import torch
  2. from torch import nn
  3. import torchvision.datasets as dsets
  4. import torchvision.transforms as transforms
  5. # import matplotlib.pyplot as plt
  6. # torch.manual_seed(1)   # reproducible
  7. # Hyper Parameters
  8. EPOCH = 1               # train the training data n times, to save time, we just train 1 epoch
  9. BATCH_SIZE = 64
  10. TIME_STEP = 28          # rnn time step / image height
  11. INPUT_SIZE = 28         # rnn input size / image width
  12. LR = 0.01               # learning rate
  13. DOWNLOAD_MNIST = True   # set to True if haven't download the data
  14. # Mnist digital dataset
  15. train_data = dsets.MNIST(
  16.    root='./mnist/',
  17.    train=True,                         # this is training data
  18.    transform=transforms.ToTensor(),    # Converts a PIL.Image or numpy.ndarray to
  19.                                        # torch.FloatTensor of shape (C x H x W) and normalize in the range [0.0, 1.0]
  20.    download=DOWNLOAD_MNIST,            # download it if you don't have it
  21. )
  22. # plot one example
  23. print(train_data.train_data.size())     # (60000, 28, 28)
  24. print(train_data.train_labels.size())   # (60000)
  25. # plt.imshow(train_data.train_data[0].numpy(), cmap='gray')
  26. # plt.title('%i' % train_data.train_labels[0])
  27. # plt.show()
  28. # Data Loader for easy mini-batch return in training
  29. train_loader = torch.utils.data.DataLoader(dataset=train_data, batch_size=BATCH_SIZE, shuffle=True)
  30. # convert test data into Variable, pick 2000 samples to speed up testing
  31. test_data = dsets.MNIST(root='./mnist/', train=False, transform=transforms.ToTensor())
  32. test_x = test_data.test_data.type(torch.FloatTensor)[:2000]/255.   # shape (2000, 28, 28) value in range(0,1)
  33. test_y = test_data.test_labels.numpy()[:2000]    # covert to numpy array
  34. class RNN(nn.Module):
  35.    def __init__(self):
  36.        super(RNN, self).__init__()
  37.        self.rnn = nn.LSTM(         # if use nn.RNN(), it hardly learns
  38.            input_size=INPUT_SIZE,
  39.            hidden_size=64,         # rnn hidden unit
  40.            num_layers=1,           # number of rnn layer
  41.            batch_first=True,       # input & output will has batch size as 1s dimension. e.g. (batch, time_step, input_size)
  42.       )
  43.        self.out = nn.Linear(64, 10)
  44.    def forward(self, x):
  45.        # x shape (batch, time_step, input_size)
  46.        # r_out shape (batch, time_step, output_size)
  47.        # h_n shape (n_layers, batch, hidden_size)
  48.        # h_c shape (n_layers, batch, hidden_size)
  49.        r_out, (h_n, h_c) = self.rnn(x, None)   # None represents zero initial hidden state
  50.        # choose r_out at the last time step
  51.        out = self.out(r_out[:, -1, :])
  52.        return out
  53. rnn = RNN()
  54. print(rnn)
  55. optimizer = torch.optim.Adam(rnn.parameters(), lr=LR)   # optimize all cnn parameters
  56. loss_func = nn.CrossEntropyLoss()                       # the target label is not one-hotted
  57. # training and testing
  58. for epoch in range(EPOCH):
  59.    for step, (b_x, b_y) in enumerate(train_loader):        # gives batch data
  60.        b_x = b_x.view(-1, 28, 28)              # reshape x to (batch, time_step, input_size)
  61.        output = rnn(b_x)                               # rnn output
  62.        loss = loss_func(output, b_y)                   # cross entropy loss
  63.        optimizer.zero_grad()                           # clear gradients for this training step
  64.        loss.backward()                                 # backpropagation, compute gradients
  65.        optimizer.step()                                # apply gradients
  66.        if step % 50 == 0:
  67.            test_output = rnn(test_x)                   # (samples, time_step, input_size)
  68.            pred_y = torch.max(test_output, 1)[1].data.numpy()
  69.            accuracy = float((pred_y == test_y).astype(int).sum()) / float(test_y.size)
  70.            print('Epoch: ', epoch, '| train loss: %.4f' % loss.data.numpy(), '| test accuracy: %.2f' % accuracy)
  71. # print 10 predictions from test data
  72. test_output = rnn(test_x[:10].view(-1, 28, 28))
  73. pred_y = torch.max(test_output, 1)[1].data.numpy()
  74. print(pred_y, 'prediction number')
  75. print(test_y[:10], 'real number')
也可以自己添加可视化效果,我这里用matplotlib工具实现的效果如示:

大家可以自己动手试一试!!

蓝小卷鼠动图表情包 - 蓝粉小卷鼠动图表情包_小卷鼠表情

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/372462
推荐阅读
相关标签
  

闽ICP备14008679号