• API
• FAQ
• Tools
• Archive
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
4.         #d  =   X.shape
5.
8.         for i in range(len(Ws)):
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):
31.                         for j in range(Ws[layer_index].shape):
32.                               print('  {},{} of {}, {}'.format(i,j,Ws[layer_index].shape,Ws[layer_index].shape))
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.