Guest User

Untitled

a guest
Jul 20th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. # this implementation was given as assignment 3 of the course
  2. # B55.2 WT Ausgewählte Kapitel sozialer Webtechnologien at HTW Berlin
  3.  
  4.  
  5. # third party
  6. import numpy as np
  7. import matplotlib.pyplot as plt
  8.  
  9. # internal
  10. from deep_teaching_commons.data.fundamentals.mnist import Mnist
  11.  
  12. # create mnist loader from deep_teaching_commons
  13. mnist_loader = Mnist(data_dir='data')
  14.  
  15. # load all data, labels are one-hot-encoded, images are flatten and pixel squashed between [0,1]
  16. train_images, train_labels, test_images, test_labels = mnist_loader.get_all_data(
  17. one_hot_enc=True, normalized=True)
  18.  
  19. # shuffle training data
  20. shuffle_index = np.random.permutation(60000)
  21. train_images, train_labels = train_images[shuffle_index], train_labels[shuffle_index]
  22.  
  23.  
  24. def feed_forward(X, weights):
  25. """
  26. calculates the forward path of our neural network with RELU as activation function of every neuron
  27.  
  28. Args:
  29. X: input data of our neural network (in our cases - our images)
  30. weights: the learnable parametre of our network
  31.  
  32. Returns:
  33. a matrix which represents the forward path
  34. """
  35. a = [X]
  36. for w in weights:
  37. # the last item of our list is always the latest item that was calculated
  38. # which is why a[-1] is always called
  39. a.append(np.maximum(a[-1].dot(w), 0))
  40. return a
  41.  
  42.  
  43. def grads(X, Y, weights):
  44. """
  45. calculates the gradient of our network by using a algorithm called backpropagation
  46.  
  47. Args:
  48. X: input data of our neural network (in our cases - our images)
  49. Y: labels of our input data
  50. weights: the learnable parametre of our network
  51.  
  52. Returns:
  53. the gradient of our loss function
  54. """
  55. grads = np.empty_like(weights)
  56. a = feed_forward(X, weights)
  57.  
  58. # calculating the gradient
  59. delta = a[-1] - Y
  60. grads[-1] = a[-2].T.dot(delta)
  61. for i in range(len(a)-2, 0, -1):
  62. delta = (a[i] > 0) * delta.dot(weights[i].T)
  63. grads[i-1] = a[i-1].T.dot(delta)
  64. return grads / len(X)
  65.  
  66.  
  67. # To test out weather our implementation works, we are going to first initialize our neural network with
  68. # 3 layers with 784 input neurons, 200 hidden neurons and 10 output neurons.
  69. # The 784 input neurons stand for every pixel of one image (every image has a resolution of 28x28) and
  70. # the 10 output neurons stands for every possible numbber the image could stand for (every image could be 0-9).
  71. # We also set up variables for our train and test dataset
  72. trX, trY, teX, teY = train_images, train_labels, test_images, test_labels
  73. weights = [np.random.randn(
  74. *w) * 0.1 for w in [(784, 200), (200, 100), (100, 10)]]
  75.  
  76.  
  77. # After initializing our network we are going to train our network and then see how accurate it performs.
  78. # The number of epochs stands for the amount of times we are going to repeat this/repeat the training.
  79. #
  80. # In order to train our network/minimize our loss we use stochastical gradient descent method
  81. # which is the same as gradient descent but only uses just a part of the whole data
  82. # - a so called "mini-batch" - to calculate the gradient for each iteration.
  83. #
  84. # Gradient descent tries to minimize our loss function
  85. # by substracting our current weights with the gradient of our loss function.
  86. # so we have W_new = W_old - grad(L)*learning_rate
  87. num_epochs, batch_size, learn_rate = 10, 50, 0.1
  88. for i in range(num_epochs):
  89. for j in range(0, len(trX), batch_size):
  90. # creating a mini-batch with the size of batch_size
  91. X, Y = trX[j:j+batch_size], trY[j:j+batch_size]
  92. weights -= learn_rate * grads(X, Y, weights)
  93. prediction_test = np.argmax(feed_forward(teX, weights)[-1], axis=1)
  94.  
  95. # prints our accuracy on the test data after training
  96. print(i, np.mean(prediction_test == np.argmax(teY, axis=1)))
Add Comment
Please, Sign In to add comment