Guest User

Untitled

a guest
Aug 18th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. #Numpy for matrix math and matplotlib for plotting loss
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4.  
  5.  
  6. def forward(x, w1, w2):
  7.  
  8. #BS, D_in * D_in, H = BS, H
  9. hidden_raw = np.matmul(x, w1)
  10.  
  11. #BS, H = BS, H
  12. hidden = np.maximum(hidden_raw, 0)
  13.  
  14. #BS, H * H, D_out = BS, D_out
  15. yhat = np.matmul(hidden, w2)
  16.  
  17. #yhat for loss and prediction. hidden for backprop
  18. return yhat, hidden
  19.  
  20.  
  21. def backward(loss, hidden, x, y, yhat):
  22.  
  23. #BS, D_out = BS, D_out
  24. grad_to_yhat = 2 * (yhat - y)
  25.  
  26. #H, BS * BS, D_out = H, D_out
  27. grad_w2 = np.matmul(hidden.T, grad_to_yhat)
  28.  
  29. #BS, 10 * 10, H = BS, H
  30. grad_hidden = np.matmul(grad_to_yhat, w2.T)
  31.  
  32. #D_in, BS * BS, H = D_in, H
  33. grad_w1 = np.matmul(x.T, grad_hidden)
  34.  
  35. return grad_w1, grad_w2
  36.  
  37.  
  38. # N is batch size; D_in is input dimension;
  39. # H is hidden dimension; D_out is output dimension.
  40. N, D_in, H, D_out = 64, 1000, 100, 10
  41.  
  42. # Create random input and output data
  43. x = np.random.randn(N, D_in)
  44. y = np.random.randn(N, D_out)
  45.  
  46. #Randomly initialize network weights
  47. w1 = np.random.randn(D_in, H)
  48. w2 = np.random.randn(H, D_out)
  49.  
  50. #Track losses
  51. losses = []
  52.  
  53. #Set a constant learning rate
  54. learning_rate = .00001
  55.  
  56. #Perform full-batch optimization steps
  57. for t in range(500):
  58.  
  59. #Forward propagate through the network
  60. yhat, hidden = forward(x, w1, w2)
  61.  
  62. #Calculate our loss matrix. Sample by y_dimension
  63. loss_matrix = np.square(y - yhat)
  64.  
  65. #Backpropagate and calculate gradients
  66. grad_w1, grad_w2 = backward(loss_matrix, hidden, x, y, yhat)
  67.  
  68. #Update the weights by a small step in the direction of the gradient
  69. w1 = w1 - grad_w1 * learning_rate
  70. w2 = w2 - grad_w2 * learning_rate
  71.  
  72. # norm of the loss vector for each sample. Take the mean between samples
  73. loss_rms = np.sqrt(np.square(loss_matrix).sum(1)).mean()
  74. losses.append(loss_rms)
  75.  
  76. print(losses)
  77.  
  78. #Visualize our losses over time, starting after the initial training
  79. plt.plot(losses)
  80. plt.title('Loss for simple model\napproaches ' + str(losses[-1])[:5])
  81. plt.savefig('model_1.jpg')
  82. plt.show()
Add Comment
Please, Sign In to add comment