Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. batch_size = 256
  2. # train_dataset = train_dataset[0:data_size]
  3. # train_labels = train_labels[0:data_size]
  4.  
  5. # num_examples = len(train_dataset) # training set size
  6. nn_input_dim = 784 # input layer dimensionality
  7. nn_output_dim = 10 # output layer dimensionality
  8.  
  9.  
  10. # Helper function to evaluate the total loss on the dataset
  11. def calculate_loss(model):
  12. W1, b1, W2, b2 = model['W1'], model['b1'], model['W2'], model['b2']
  13.  
  14. batch_train_dataset, batch_train_labels = sample_training_data(batch_size)
  15.  
  16. # Forward propagation to calculate our predictions
  17. z1 = batch_train_dataset.dot(W1) + b1
  18. a1 = z1 * (z1 > 0) # Implemenatation of ReLU
  19. # a1 = np.tanh(z1)
  20. z2 = a1.dot(W2) + b2
  21. exp_scores = np.exp(z2)
  22. probs = exp_scores / np.sum(exp_scores, axis=1, keepdims=True)
  23.  
  24. # Calculating the loss
  25. corect_logprobs = -np.log(probs[range(batch_size), np.nonzero(batch_train_labels)[(0)][0].astype('int64')])
  26. data_loss = np.sum(corect_logprobs)
  27.  
  28. # Add regulatization term to loss (optional)
  29. data_loss += reg_lambda/2 * (np.sum(np.square(W1)) + np.sum(np.square(W2)))
  30. return 1./batch_size * data_loss
  31.  
  32. def sample_training_data(batch_size):
  33. import random
  34. random_indexes = random.sample(range(len(train_dataset)), batch_size)
  35.  
  36. batch_train_dataset = train_dataset[random_indexes]
  37. batch_train_labels = train_labels[random_indexes]
  38.  
  39. return batch_train_dataset, batch_train_labels
  40.  
  41. def build_model(nn_hdim, batch_size, num_passes=10000, print_loss=False):
  42. """
  43. This function learns parameters for the neural network and returns the model.
  44. - nn_hdim: Number of nodes in the hidden layer
  45. - num_passes: Number of passes through the training data for gradient descent
  46. - print_loss: If True, print the loss every 1000 iterations
  47. """
  48. # Initialize the parameters to random values. We need to learn these.
  49. np.random.seed(0)
  50. W1 = np.random.randn(nn_input_dim, nn_hdim) / np.sqrt(nn_input_dim)
  51. b1 = np.zeros((1, nn_hdim))
  52. W2 = np.random.randn(nn_hdim, nn_output_dim) / np.sqrt(nn_hdim)
  53. b2 = np.zeros((1, nn_output_dim))
  54.  
  55. # This is what we return at the end
  56. model = {}
  57.  
  58. # Gradient descent. For each batch...
  59. for i in range(0, num_passes):
  60. batch_train_dataset, batch_train_labels = sample_training_data(batch_size)
  61. #
  62. # Forward propagation
  63. #
  64. z1 = some_train_dataset.dot(W1) + b1
  65. a1 = z1 * (z1 > 0) # Implemenatation of ReLU
  66. # a1 = np.tanh(z1)
  67. z2 = a1.dot(W2) + b2
  68. exp_scores = np.exp(z2)
  69. probs = exp_scores / np.sum(exp_scores, axis=1, keepdims=True)
  70.  
  71. #
  72. # Backpropagation
  73. #
  74. delta3 = (probs - some_train_labels)
  75.  
  76. db2 = np.sum(delta3, axis=0, keepdims=True) # tr set 여러개인 것을 고려해야함
  77. dW2 = (a1.T).dot(delta3)
  78. dW2 += reg_lambda * W2
  79.  
  80. W2 += -epsilon * dW2
  81. b2 += -epsilon * db2
  82.  
  83.  
  84. delta2 = delta3.dot(W2.T) * (z1>0)
  85.  
  86. db1 = np.sum(delta2, axis=0, keepdims=True)
  87. dW1 = (some_train_dataset.T).dot(delta2)
  88. dW1 += reg_lambda * W1
  89.  
  90. W1 += -epsilon * dW1
  91. b1 += -epsilon * db1
  92.  
  93. # Assign new parameters to the model
  94. model = { 'W1': W1, 'b1': b1, 'W2': W2, 'b2': b2}
  95.  
  96. # Optionally print the loss.
  97. # This is expensive because it uses the whole dataset, so we don't want to do it too often.
  98. # if print_loss and i % 1000 == 0:
  99. # print("Loss after iteration %i: %f" %(i, calculate_loss(model)))
  100. # if print_loss and i % 1000 == 0:
  101. print("Loss after iteration %i: %f" %(i, calculate_loss(model)))
  102.  
  103. return model
  104.  
  105.  
  106. # In[23]:
  107.  
  108.  
  109. model = build_model(10, batch_size, num_passes=20000, print_loss=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement