def test_forward():

    X = np.zeros((2,2))
    X[0,0] = 1
    test = CNN(vector_size=2)
    W1, dict = test.initialize_weights()
    _Ws,_Bs, CM1,B1,CM2,B2, FCL = dict['Ws'], dict['Ws_bias'], dict['CM1'], dict['B1'], dict['CM2'], dict['B2'], dict['FCL']
    z0, a0, z1, a1, z2, a2, z3, a3  = test.forward(X)
    print X, u'\n'
    print z0, u'\n'
    print a0, u'\n'
    for i in range(test.fmap1):
        print 'CM1',CM1[i], u'\n'
        print 'BM1',B1[i], u'\n'
        print 'z1',z1[i], u'\n'
        print 'a1',a1[i], u'\n'
        print u'\n\n'
    for i in range(test.fmap2):
        for k in range(test.fmap1):
            print 'CM2',CM2[i][k], u'\n'
        print 'B2',B2[i], u'\n'
        print 'z2',z2[i], u'\n'
        print 'a2',a2[i], u'\n'
        print u'\n\n'
    print 'z3',z3, u'\n'
    print 'a3',a3, u'\n'
Example #2
0
def test_forward():

    X = np.zeros((2,2))
    X[0,0] = 1
    test = CNN(vector_size=2)
    W1, _B1, CM1,B1,CM2,B2, FCL = test.initialize_weights()
    z0, a0, z1, a1, z2, a2, z3, a3  = test._forward(X,W1, _B1,CM1,B1,CM2,B2,FCL)
    print X, u'\n'
    print z0, u'\n'
    print a0, u'\n'
    for i in range(test.feature_maps_number_layer1):
        print 'CM1',CM1[i], u'\n'
        print 'BM1',B1[i], u'\n'
        print 'z1',z1[i], u'\n'
        print 'a1',a1[i], u'\n'
        print u'\n\n'
    for i in range(test.feature_maps_number_layer2):
        for k in range(test.feature_maps_number_layer1):
            print 'CM2',CM2[i][k], u'\n'
        print 'B2',B2[i], u'\n'
        print 'z2',z2[i], u'\n'
        print 'a2',a2[i], u'\n'
        print u'\n\n'
    print 'z3',z3, u'\n'
    print 'a3',a3, u'\n'
Example #3
0
def test_cost(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()
    print test.function(X, y, CM1,B1,CM2,B2, FCL)
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'
Example #5
0
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