def test_function_prime(self): X = np.random.rand(4,2) y = np.array([0,1,0,0,0,0,0,0,0,0,0,0,0,0,0]) test = CNN(vector_size=4) CM1,B1,CM2,B2, FCL = test.initialize_weights() d1fm, d2, d3 = test.function_prime(X, y) for i in range(test.fmap1): print d1fm[i], u'\n' print d2, u'\n' print d3, u'\n'
def test_gradient_check(e): X = np.random.rand(4,2) y = np.array([0,1,0,0,0,0,0,0,0,0,0,0,0,0,0]) test = CNN(vector_size=4) W1,_B1, CM1,B1,CM2,B2,FCL = test.initialize_weights() dW1, d_B1, dCM1, dB1, dCM2, dB2, dFCL = test.function_prime(X, y,W1, _B1, CM1,B1,CM2,B2,FCL) # Through FCL for i in range(test.num_labels): for j in range(test.kmax*test.feature_maps_number_layer2*test.folding_width + 1): FCL[i,j] += e Jpos = test.function(X,y,W1, _B1,CM1,B1,CM2,B2,FCL) FCL[i,j] -= 2*e Jneg = test.function(X,y,W1, _B1,CM1,B1,CM2,B2,FCL) J = (Jpos - Jneg)/(2*e) backprop = dFCL[i,j] if (J-backprop)/(J+backprop)>e: print (J-backprop)/(J+backprop) print J, backprop FCL[i,j] += e # #Through CL2 for i in range(test.feature_maps_number_layer2): for j in range(test.feature_maps_number_layer1): for n in range(test.vector_size): for p in range(test.second_kernel_size): CM2[i][j][n,p] += e Jpos = test.function(X,y,W1, _B1,CM1,B1,CM2,B2,FCL) CM2[i][j][n,p] -= 2*e Jneg = test.function(X,y,W1, _B1,CM1,B1,CM2,B2,FCL) J = (Jpos - Jneg)/(2*e) backprop = dCM2[i][j][n,p] if (J-backprop)/(J+backprop)>e: print (J-backprop)/(J+backprop) print J, backprop CM2[i][j][n,p] += e for i in range(test.feature_maps_number_layer2): for j in range(test.folding_width): B2[i][j] += e Jpos = test.function(X,y,W1, _B1,CM1,B1,CM2,B2,FCL) B2[i][j] -= 2*e Jneg = test.function(X,y,W1, _B1,CM1,B1,CM2,B2,FCL) J = (Jpos - Jneg)/(2*e) backprop = dB2[i][j] if (J-backprop)/(J+backprop)>e: print (J-backprop)/(J+backprop) print J, backprop B2[i][j] += e # Through CL1 for i in range(test.feature_maps_number_layer1): for j in range(test.vector_size): B1[i][j] += e Jpos = test.function(X,y,W1, _B1,CM1,B1,CM2,B2,FCL) B1[i][j] -= 2*e Jneg = test.function(X,y,W1, _B1,CM1,B1,CM2,B2,FCL) J = (Jpos - Jneg)/(2*e) backprop = dB1[i][j] if (J-backprop)/(J+backprop)>e: print (J-backprop)/(J+backprop) print J, backprop B1[i][j] += e for i in range(test.feature_maps_number_layer1): for u in range(test.vector_size): for v in range(test.first_kernel_size): CM1[i][u,v] += e Jpos = test.function(X,y,W1, _B1,CM1,B1,CM2,B2,FCL) CM1[i][u,v] -= 2*e Jneg = test.function(X,y,W1, _B1,CM1,B1,CM2,B2,FCL) J = (Jpos - Jneg)/(2*e) backprop = dCM1[i][u,v] if (J-backprop)/(J+backprop)>e: print (J-backprop)/(J+backprop) print J, backprop CM1[i][u,v] += e # Through W2VTransf for i in range(test.vector_size): for j in range(X.shape[1]): W1[i,j] += e Jpos = test.function(X,y,W1, _B1,CM1,B1,CM2,B2,FCL) W1[i,j] -= 2*e Jneg = test.function(X,y,W1, _B1,CM1,B1,CM2,B2,FCL) J = (Jpos - Jneg)/(2*e) backprop = dW1[i,j] if (J-backprop)/(J+backprop)>e: print (J-backprop)/(J+backprop) W1[i,j] += e # Through W2VTransf for i in range(test.vector_size): _B1[i] += e Jpos = test.function(X,y,W1, _B1,CM1,B1,CM2,B2,FCL) _B1[i] -= 2*e Jneg = test.function(X,y,W1, _B1,CM1,B1,CM2,B2,FCL) J = (Jpos - Jneg)/(2*e) backprop = d_B1[i] if (J-backprop)/(J+backprop)>e: print (J-backprop)/(J+backprop) _B1[i] += e