Ledger Nano X - The secure hardware wallet
SHARE
TWEET

Untitled

a guest Apr 7th, 2020 129 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def compute_grads_num(X, Y, P, Ws, bs, lamda, h):
  2.         """ Converted from matlab code, borrowed from given code """
  3.         #no     =   Ws.shape[0]
  4.         #d  =   X.shape[0]
  5.        
  6.         grads_W = []
  7.         grads_b= []
  8.         for i in range(len(Ws)):
  9.                 grads_W.append( np.zeros(Ws[i].shape) )
  10.                 grads_b.append( np.zeros(bs[i].shape) )
  11.                 #grad_W = np.zeros(Ws.shape);
  12.                 #grad_b = np.zeros(bs.shape);
  13.  
  14.         c = compute_cost(X, Y, Ws, bs, lamda)
  15.  
  16.         #print('\n\nCheckinig dimensions in compute grads num:\nb grads ({},{})\nW grads ({},{})'.format(len(bs),[x.shape for x in bs],len(Ws),[x.shape for x in Ws]))
  17.         print('Starting numerical computation of gradient')
  18.         for layer_index in range(len(bs)):
  19.                 print('b layer {}'.format(layer_index))
  20.                 for i in range(len(bs[layer_index])):
  21.                         print(' cell {}/{}'.format(i,len(bs[layer_index])))
  22.                         b_try = np.array(bs,copy=True)
  23.                         b_try[layer_index][i] += h
  24.                         c2 = compute_cost(X, Y, Ws, b_try, lamda)
  25.                         grads_b[layer_index][i] = np.sum(c2-c) / h
  26.         print('numerical gradient of b calculated, starting W')
  27.                        
  28.         for layer_index in range(len(Ws)):
  29.                 print('W layer {}'.format(layer_index))
  30.                 for i in range(Ws[layer_index].shape[0]):
  31.                         for j in range(Ws[layer_index].shape[1]):
  32.                               print('  {},{} of {}, {}'.format(i,j,Ws[layer_index].shape[0],Ws[layer_index].shape[1]))
  33.                               W_try = np.array(Ws,copy=True)
  34.                               W_try[layer_index][i,j] += h
  35.                               c2 = compute_cost(X, Y, W_try, bs, lamda)
  36.                               grads_W[layer_index][i,j] = np.sum(c2-c) / h
  37.  
  38.         return [grads_W, grads_b]
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top