예제 #1
0
def check_gradients(check_nn_lambda=0):
    # 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)
    rand_x_axis, rand_y_axis = np.where(
        np.random.rand(y.shape[0], y.shape[1]) > 0.5)
    y[rand_x_axis, rand_y_axis] = 0
    y_not_0_x_axis, y_not_0_y_axis = np.where(y == 0)

    r = np.ones(y.shape)
    r[y_not_0_x_axis, y_not_0_y_axis] = 0

    y[rand_x_axis, rand_y_axis] = 0

    # 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]
    num_movies = y.shape[0]
    num_features = theta_t.shape[1]

    numgrad = compute_numerical_gradient(
        cofi_cost_func,
        np.hstack((np.ravel(x, order='F'), np.ravel(theta, order='F'))), y, r,
        num_users, num_movies, num_features, check_nn_lambda)
    cost, grad = cofi_cost_func(
        np.hstack((np.ravel(x, order='F'), np.ravel(theta, order='F'))), y, r,
        num_users, num_movies, num_features, check_nn_lambda)
    disp([numgrad, grad])
    print('The above two columns you get should be very similar.\n'
          '(Left-Your Numerical Gradient, Right-Analytical Gradient)\n\n')

    diff = np.linalg.norm(numgrad - grad) / np.linalg.norm(numgrad + grad)
    print('If your cost function implementation is correct, then \n'
          'the relative difference will be small (less than 1e-9). \n\n'
          'Relative Difference: %s\n' % diff)
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

    def cost_func(p):
        return ccf.cofi_cost_func(p, Y, R, num_users, num_movies, num_features,
                                  lmd)

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

    cost, grad = ccf.cofi_cost_func(
        np.concatenate((x.flatten(), theta.flatten())), Y, R, num_users,
        num_movies, num_features, lmd)

    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))
예제 #3
0
def grad_func(p):
    return cofi_cost_func(p, Ynorm, R, num_users, num_movies, num_features, lmd)[1]
예제 #4
0
Theta = data['Theta']
num_users = data['num_users'].flatten()
num_movies = data['num_movies'].flatten()
num_features = data['num_features'].flatten()

# Reduce the data set size so that this runs faster
num_users = 4
num_movies = 5
num_features = 3
X = X[:num_movies, :num_features]
Theta = Theta[:num_users, :num_features]
Y = Y[:num_movies, :num_users]
R = R[:num_movies, :num_users]

# Evaluate cost function
J, _ = cofi_cost_func(np.r_[X.flatten(), Theta.flatten()],
                      Y, R, num_users, num_movies, num_features, 0)

print("Cost at loaded parameters: {} \n (this value should be about 22.22)"
      .format(J))

input('\nProgram paused. Press enter to continue.\n')

# ============== Part 3: Collaborative Filtering Gradient ==============
print('\nChecking Gradients (without regularization) ... ')

# Check gradients by running checkNNGradients
check_cost_function(0)

input('\nProgram paused. Press enter to continue.\n')

# ========= Part 4: Collaborative Filtering Cost Regularization ========
예제 #5
0
    X_reduce = X[np.arange(num_movies_reduce), :]
    X_reduce = X_reduce[:, np.arange(num_features_reduce)]

    Theta_reduce = Theta[np.arange(num_users_reduce), :]
    Theta_reduce = Theta_reduce[:, np.arange(num_features_reduce)]

    Y_reduce = Y[np.arange(num_movies_reduce), :]
    Y_reduce = Y_reduce[:, np.arange(num_users_reduce)]

    R_reduce = R[np.arange(num_movies_reduce), :]
    R_reduce = R_reduce[:, np.arange(num_users_reduce)]

    # Evaluate cost function
    J, Grad = cofi_cost_func(np.hstack((np.ravel(X_reduce, order='F'), np.ravel(Theta_reduce, order='F'))), Y_reduce,
                             R_reduce,
                             num_users_reduce, num_movies_reduce, num_features_reduce, 0)
    print('Cost at loaded parameters: %s \n(this value should be about 22.22)\n' % J)
    print('Program paused. Press enter to continue.\n')
    # pause_func()

    # ============== Part 3: Collaborative Filtering Gradient ==============
    print('\nChecking Gradients (without regularization) ... \n')
    check_gradients()
    print('Program paused. Press enter to continue.\n')
    # pause_func()

    # ========= Part 4: Collaborative Filtering Cost Regularization ========
    J_reg, Grad_reg = cofi_cost_func(
        np.hstack((np.ravel(X_reduce, order='F'), np.ravel(Theta_reduce, order='F'))),
        Y_reduce, R_reduce,
 def cost_func(p):
     return ccf.cofi_cost_func(p, Y, R, num_users, num_movies, num_features,
                               lmd)