Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.77 KB | None | 0 0
  1. def compute_grads(X,Y,P,Ws,bs,lambd):
  2.         #dl_dWs = [np.zeros( (Wi.shape) ) for Wi in W]
  3.         #dl_dbs = [np.zeros( (bi.hape) ) for bi in b]
  4.  
  5.         W1 = Ws[0]
  6.         b1 = bs[0]
  7.         W2 = Ws[1]
  8.         b2 = bs[1]
  9.         # Forward pass
  10.         H_batch = np.maximum( W1@X + b1 ,np.zeros( shape=(W1.shape[0],X.shape[1]) ))
  11.         P_batch = softmax( W2 @ H_batch + b2)
  12.         # Backwards pass
  13.         G_batch = - (Y-P_batch)
  14.  
  15.         dl_dw2 = (1/X.shape[0]) * G_batch @ H_batch.T
  16.         dl_db2 = (1/X.shape[0]) * G_batch @ np.ones( (X.shape[1],1))
  17.  
  18.         G_batch = W2.T @ G_batch
  19.         G_batch = G_batch * np.where(H_batch > 0, 1, 0)
  20.  
  21.         dl_dw1 = (1/X.shape[0]) * G_batch @ X.T
  22.         dl_db1 = (1/X.shape[0]) * G_batch @ np.ones( (X.shape[1],1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement