Example #1
0
def check_nn_gradients(lmd):

    input_layer_size = 3
    hidden_layer_size = 5
    num_labels = 3
    m = 5

    # We generatesome 'random' test data
    theta1 = diw.debug_initialize_weights(hidden_layer_size, input_layer_size)
    theta2 = diw.debug_initialize_weights(num_labels, hidden_layer_size)

    # Reusing debugInitializeWeights to genete X
    X = diw.debug_initialize_weights(m, input_layer_size - 1)
    y = 1 + np.mod(np.arange(1, m + 1), num_labels)

    # Unroll parameters
    nn_params = np.concatenate([theta1.flatten(), theta2.flatten()])

    def cost_func(p):
        return ncf.nn_cost_function(p, input_layer_size, hidden_layer_size,
                                    num_labels, X, y, lmd)

    cost, grad = cost_func(nn_params)
    numgrad = cng.compute_numerial_gradient(cost_func, nn_params)

    print(np.c_[grad, numgrad])
def check_cost_function(lmd):

    # Create small problem
    x_t = np.random.rand(4, 3)
    theta_t = np.random.rand(5, 3)

    # Zap out most entries
    Y = np.dot(x_t, theta_t.T)  # 4x5
    Y[np.random.rand(Y.shape[0], Y.shape[1]) > 0.5] = 0
    R = np.zeros(Y.shape)
    R[Y != 0] = 1

    # Run Gradient Checking
    x = np.random.randn(x_t.shape[0], x_t.shape[1])
    theta = np.random.randn(theta_t.shape[0], theta_t.shape[1])
    num_users = Y.shape[1]  #5
    num_movies = Y.shape[0]  #4
    num_features = theta_t.shape[1]  #3

    print(x.shape, Y.shape, R.shape, theta.shape)

    def cost_func(p):
        cost_tmp = ccf.cofi_cost_function(p, Y, R, num_users, num_movies,
                                          num_features, lmd)
        gred_tmp = ccf.cofi_gred_function(p, Y, R, num_users, num_movies,
                                          num_features, lmd)
        return cost_tmp, gred_tmp

    numgrad = cng.compute_numerial_gradient(
        cost_func, np.concatenate((x.flatten(), theta.flatten())))

    cost, grad = cost_func(np.concatenate((x.flatten(), theta.flatten())))

    print(np.c_[numgrad, grad])
    print('The above two columns you get should be very similar.\n'
          '(Left-Your Numerical Gradient, Right-Analytical Gradient')

    diff = np.linalg.norm(numgrad - grad) / np.linalg.norm(numgrad + grad)
    print('If you backpropagation implementation is correct, then\n'
          'the relative difference will be small (less than 1e-9).\n'
          'Relative Difference: {:0.3e}'.format(diff))
def check_nn_gradients(lmd):
    # np.set_printoptions(precision=20)
    input_layer_size = 3
    hidden_layer_size = 5
    num_labels = 3
    m = 5
    # We generatesome 'random' test data
    theta1 = diw.randInitializeWeights(input_layer_size, hidden_layer_size)
    theta2 = diw.randInitializeWeights(hidden_layer_size, num_labels)

    # Reusing debugInitializeWeights to genete X
    X = diw.randInitializeWeights(input_layer_size - 1, m)
    X1 = np.hstack((np.ones((X.shape[0])).reshape(X.shape[0], 1), X))
    y = 1 + np.mod(np.arange(1, m + 1), num_labels)

    # Unroll parameters
    nn_params = np.concatenate([theta1.flatten(), theta2.flatten()])

    def cost_func(p):
        return ncf.ex3_nn(X, y, p, num_labels, X1, hidden_layer_size, lmd)

    cost, grad = cost_func(nn_params)
    numgrad = cng.compute_numerial_gradient(cost_func, nn_params)
    print(np.c_[grad, numgrad])