Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.09 KB | None | 0 0
  1. W1 = np.random.randn(H, X.shape[1])
  2.     b1 = np.random.randn(H)
  3.  
  4.     W2 = np.random.randn(C, H)
  5.     b2 = np.random.randn(C)
  6.  
  7.     Y = data.create_onehot_matrix(Y_)
  8.  
  9.     for i in range(param_niter):
  10.         s1 = np.dot(X, W1.T) + b1
  11.         h1 = np.array(s1)
  12.         h1[h1<0] = 0
  13.  
  14.         s2 = np.dot(s1, W2.T) + b2
  15.  
  16.         probs = data.matrix_stable_softmax(s2)
  17.         positions = np.where(Y == 1)
  18.         logprobs = np.log(probs[positions])
  19.  
  20.         loss = -np.mean(logprobs)  
  21.  
  22.         if i % 10 == 0:
  23.             print("iteration {}: loss {}".format(i, loss))
  24.  
  25.         Gs2 = probs - Y
  26.  
  27.         grad_W2 = np.dot(Gs2.T, h1)
  28.         grad_b2 = np.sum(Gs2, axis=0)
  29.  
  30.         Gh1 = np.dot(Gs2, W2)
  31.  
  32.         tmp = np.array(s1)
  33.         tmp[tmp<=0] = 0
  34.         tmp = np.count_nonzero(tmp, axis=0)
  35.         diag = np.diag(tmp)
  36.         Gs1 = np.dot(Gh1, diag)
  37.  
  38.         grad_W1 = np.dot(Gs1.T, X)
  39.         grad_b1 = np.sum(Gs1, axis=0)
  40.  
  41.         W2 -= param_delta * grad_W2
  42.         b2 -= param_delta * grad_b2
  43.         W1 -= param_delta * grad_W1
  44.         b1 -= param_delta * grad_b1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement