Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. #parameter setting
  2.  
  3. import numpy as np
  4. from mnist import load_mnist
  5.  
  6. np.random.seed(5)
  7.  
  8. (x_train, t_train), (x_test, t_test) = load_mnist(normalize=True ,one_hot_label=True)
  9. <br>
  10. /*mnist save <br><br> */
  11.  
  12.  
  13.  
  14. X = x_train[0:100] /* Test data */ <br>
  15. W = {} <br>
  16.  
  17. W['W1'] = np.random.randn(784, 30)
  18.  
  19. W['W2'] = np.random.randn(30, 20)
  20.  
  21. W['W3'] = np.random.randn(20, 10) /*set weight */
  22.  
  23.  
  24. S = {}
  25. A = {}
  26. Y = t_train[0:100]
  27.  
  28. dA ={}
  29. dS ={}
  30. dW ={}
  31.  
  32. ---
  33.  
  34. #function setting
  35.  
  36. ### def Softmax(ScoreMatrix):
  37.  
  38. if ScoreMatrix.ndim == 2:
  39. temp = ScoreMatrix
  40. temp = temp - np.max(temp, axis=1, keepdims= True)
  41. y_predict = np.exp(temp) / np.sum(np.exp(temp), axis=1, keepdims=True)
  42. return y_predict
  43. temp = ScoreMatrix - np.max(ScoreMatrix, axis=0)
  44. expX = np.exp(temp)
  45. y_predict = expX / np.sum(expX)
  46. return y_predict
  47.  
  48. ### def relu(Score):
  49.  
  50. A = np.maximum(0, Score)
  51. return A
  52.  
  53. ### def forward(X):
  54.  
  55. A['A0'] = X
  56. S['S1'] = np.dot(X, W['W1'])
  57. A['A1'] = relu(S['S1'])
  58. S['S2'] = np.dot(A['A1'], W['W2'])
  59. A['A2'] = relu(S['S2'])
  60. S['S3'] = np.dot(A['A2'], W['W3'])
  61. Y_predict = Softmax(S['S3'])
  62. Loss = -(np.sum(Y*np.log(Y_predict+1e-7))) / Y.shape[0]
  63. print(Loss)
  64. return Y_predict
  65.  
  66. ### def Backward(Y_predict):
  67.  
  68. mask ={}
  69. dS['S3'] = (Y_predict - Y) / Y.shape[0]
  70. dW['W3'] = np.dot(A['A2'].T, dS['S3'])
  71. dA['A2'] = np.dot(dS['S3'], W['W3'].T)
  72. mask['R2'] = (A['A2'] > 0)
  73. dS['S2'] = dA['A2']*mask['R2']
  74. dW['W2'] = np.dot(A['A1'].T, dS['S2'])
  75. dA['A1'] = np.dot(dS['S2'], W['W2'].T)
  76. mask['R1'] = (A['A1'] > 0)
  77. dS['S1'] = dA['A1'] * mask['R1']
  78. dW['W1'] = np.dot(A['A0'].T, dS['S1'])
  79.  
  80. ### def Gradient():
  81.  
  82. W['W1'] -= 0.1*dW['W1']
  83. W['W2'] -= 0.1*dW['W2']
  84. W['W3'] -= 0.1*dW['W3']
  85.  
  86.  
  87. ### def Optimizer():
  88.  
  89. for i in range(10000):
  90. Y_predict = forward(X)
  91. Backward(Y_predict)
  92. Gradient()
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99. Y_predict = forward(x_train[100:200])
  100. Y_predict.shape
  101.  
  102.  
  103. Last = (Y_predict - Y) / Y.shape[0]
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112. Optimizer() /* optimizer call
  113.  
  114.  
  115. y_predict =forward(x_train[0:100])<br>
  116. print(y_predict[10].shape) <br>
  117. count=0 <br>
  118. it is checked y_predict[i] checked [0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]
  119.  
  120. for i in range(100):
  121. if(np.array_equal(y_predict[i], np.ones(10)*0.1)):
  122. print(i)
  123. print(y_predict[i])
  124. count+=1
  125.  
  126. print('count ', count)
  127.  
  128. '''
  129.  
  130.  
  131. ## result is :
  132.  
  133. y_predict[i]
  134. 41
  135. [0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]
  136. 45
  137. [0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]
  138. 54
  139. [0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]
  140. 67
  141. [0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]
  142. 72
  143. [0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]
  144. 76
  145. [0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]
  146. 82
  147. [0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]
  148.  
  149. count 7
  150.  
  151. Why did this result come out?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement