コード例 #1
0
def checkCostFunction(cofiCostFunc, 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)
    Y[np.random.rand(*Y.shape) > 0.5] = 0
    R = np.zeros(Y.shape)
    R[Y != 0] = 1

    # Run Gradient Checking
    X = np.random.randn(*X_t.shape)
    Theta = np.random.randn(*Theta_t.shape)
    num_movies, num_users = Y.shape
    num_features = Theta_t.shape[1]

    params = np.concatenate([X.ravel(), Theta.ravel()])
    numgrad = computeNumericalGradient(
        lambda x: cofiCostFunc(x, Y, R, num_users, num_movies, num_features,
                               lambda_), params)

    cost, grad = cofiCostFunc(params, Y, R, num_users, num_movies,
                              num_features, lambda_)

    print(np.stack([numgrad, grad], axis=1))
    print('\nThe above two columns you get should be very similar.'
          '(Left-Your Numerical Gradient, Right-Analytical Gradient)')

    diff = np.linalg.norm(numgrad - grad) / np.linalg.norm(numgrad + grad)
    print('If your cost function implementation is correct, then '
          'the relative difference will be small (less than 1e-9).')
    print('\nRelative Difference: %g' % diff)
コード例 #2
0
def checkCostFunction(lamda=0):
    # Create small problem
    X_t = np.random.rand(4, 3)
    Theta_t = np.random.rand(5, 3)

    # Zap out most entries
    Y = X_t.dot(Theta_t.T)
    Y[np.where(np.random.random_sample(Y.shape) > 0.5)] = 0
    R = np.zeros(Y.shape)
    R[np.where(Y != 0)] = 1

    # Run Gradient Checking
    X = np.random.random_sample(X_t.shape)
    Theta = np.random.random_sample(Theta_t.shape)
    num_users = Y.shape[1]
    num_movies = Y.shape[0]
    num_features = Theta_t.shape[1]

    # params = np.hstack((X.T.flatten(), Theta.T.flatten()))
    costFunc = lambda X, Theta: cofiCostFunc(X, Theta, Y, R, lamda)
    costFunc_w = lambda X, Theta: costFunc(X, Theta)[0]
    numgrad = computeNumericalGradient(costFunc_w, X, Theta)

    cost, grad = cofiCostFunc(X, Theta, Y, R, lamda)

    print(grad)
    print(numgrad)
コード例 #3
0
def checkCostFunction(Lambda=0):
    """Creates a collaborative filering problem
    to check your cost function and gradients, it will output the
    analytical gradients produced by your code and the numerical gradients
    (computed using computeNumericalGradient). These two gradient
    computations should result in very similar values.
    """

    ## Create small problem
    X_t = np.random.rand(4, 3)
    Theta_t = np.random.rand(5, 3)

    # Zap out most entries
    Y = X_t.dot(Theta_t.T)
    Y[np.where(np.random.random_sample(Y.shape) > 0.5, True, False)] = 0
    R = np.zeros(Y.shape)
    R[np.where(Y != 0, True, False)] = 1

    ## Run Gradient Checking
    X = np.random.random_sample(X_t.shape)
    Theta = np.random.random_sample(Theta_t.shape)
    num_users = Y.shape[1]
    num_movies = Y.shape[0]
    num_features = Theta_t.shape[1]

    # Unroll parameters
    params = np.hstack((X.T.flatten(), Theta.T.flatten()))

    costFunc = lambda t: cofiCostFunc(t, Y, R, num_users, num_movies,
                                      num_features, Lambda)

    def costFunc_w(t):
        Jgrad = costFunc(t)
        return Jgrad

    numgrad = computeNumericalGradient(costFunc_w, params)

    cost, grad = cofiCostFunc(params, Y, R, num_users, num_movies,
                              num_features, Lambda)

    print np.column_stack((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 backpropagation implementation is correct, then\n ' \
          'the relative difference will be small (less than 1e-9). \n' \
          '\nRelative Difference: %g\n' % diff
コード例 #4
0
def checkCostFunction(Lambda=0):
    """Creates a collaborative filering problem
    to check your cost function and gradients, it will output the
    analytical gradients produced by your code and the numerical gradients
    (computed using computeNumericalGradient). These two gradient
    computations should result in very similar values.
    """

    ## Create small problem
    X_t = np.random.rand(4, 3)
    Theta_t = np.random.rand(5, 3)

    # Zap out most entries
    Y = X_t.dot(Theta_t.T)
    Y[np.where(np.random.random_sample(Y.shape) > 0.5, True, False)] = 0
    R = np.zeros(Y.shape)
    R[np.where(Y != 0, True, False)] = 1

    ## Run Gradient Checking
    X = np.random.random_sample(X_t.shape)
    Theta = np.random.random_sample(Theta_t.shape)
    num_users = Y.shape[1]
    num_movies = Y.shape[0]
    num_features = Theta_t.shape[1]

   # Unroll parameters
    params = np.hstack((X.T.flatten(), Theta.T.flatten()))

    costFunc = lambda t: cofiCostFunc(t, Y, R, num_users, num_movies, num_features, Lambda)

    def costFunc_w(t):
        Jgrad = costFunc(t)
        return Jgrad

    numgrad = computeNumericalGradient(costFunc_w, params)

    cost, grad = cofiCostFunc(params, Y, R, num_users, num_movies, num_features, Lambda)


    print np.column_stack((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 backpropagation implementation is correct, then\n ' \
          'the relative difference will be small (less than 1e-9). \n' \
          '\nRelative Difference: %g\n' % diff
コード例 #5
0
def output(partId):
    # Random Test Cases
    n_u = 3
    n_m = 4
    n = 5
    X = np.sin(np.arange(1, 1 + n_m * n)).reshape(n_m, n, order='F')
    Theta = np.cos(np.arange(1, 1 + n_u * n)).reshape(n_u, n, order='F')
    Y = np.sin(np.arange(1, 1 + 2 * n_m * n_u, 2)).reshape(n_m, n_u, order='F')
    R = Y > 0.5
    pval = np.concatenate([abs(Y.ravel('F')), [0.001], [1]])
    Y = Y * R
    yval = np.concatenate([R.ravel('F'), [1], [0]])
    params = np.concatenate([X.ravel(), Theta.ravel()])
    if partId == '1':
        mu, sigma2 = estimateGaussian(X)
        out = formatter('%0.5f ', mu.ravel())
        out += formatter('%0.5f ', sigma2.ravel())
    elif partId == '2':
        bestEpsilon, bestF1 = selectThreshold(yval, pval)
        out = formatter('%0.5f ', bestEpsilon.ravel())
        out += formatter('%0.5f ', bestF1.ravel())
    elif partId == '3':
        J, _ = cofiCostFunc(params, Y, R, n_u, n_m, n, 0)
        out = formatter('%0.5f ', J.ravel())
    elif partId == '4':
        J, grad = cofiCostFunc(params, Y, R, n_u, n_m, n, 0)
        X_grad = grad[:n_m * n].reshape(n_m, n)
        Theta_grad = grad[n_m * n:].reshape(n_u, n)
        out = formatter(
            '%0.5f ',
            np.concatenate([X_grad.ravel('F'),
                            Theta_grad.ravel('F')]))
    elif partId == '5':
        J, _ = cofiCostFunc(params, Y, R, n_u, n_m, n, 1.5)
        out = formatter('%0.5f ', J.ravel())
    elif partId == '6':
        J, grad = cofiCostFunc(params, Y, R, n_u, n_m, n, 1.5)
        X_grad = grad[:n_m * n].reshape(n_m, n)
        Theta_grad = grad[n_m * n:].reshape(n_u, n)
        out = formatter(
            '%0.5f ',
            np.concatenate([X_grad.ravel('F'),
                            Theta_grad.ravel('F')]))
    return out
コード例 #6
0
ファイル: checkCostFunction.py プロジェクト: grixxy/ml_python
def checkCostFunction(lambda_ = 0):
#CHECKCOSTFUNCTION Creates a collaborative filering problem
#to check your cost function and gradients
#   CHECKCOSTFUNCTION(lambda) Creates a collaborative filering problem
#   to check your cost function and gradients, it will output the
#   analytical gradients produced by your code and the numerical gradients
#   (computed using computeNumericalGradient). These two gradient
#   computations should result in very similar values.


#Create small problem
    X_t = np.random.rand(4, 3)
    Theta_t = np.random.rand(5, 3)

# Zap out most entries
    Y = X_t.dot(Theta_t.T)
    y_shape = Y.shape
    rand_mask = np.random.rand(y_shape[0],y_shape[1])
    mask = rand_mask>0.5

    Y[mask] = 0
    R = np.zeros(Y.shape)
    R[Y != 0] = 1

# Run Gradient Checking
    X = np.random.rand(X_t.shape[0],X_t.shape[1])
    Theta = np.random.rand(Theta_t.shape[0],Theta_t.shape[1])
    num_users = Y.shape[1]
    num_movies = Y.shape[0]
    num_features = Theta_t.shape[1]

#Cost func reference

    # Unroll parameters
    params = np.hstack((np.hstack((X.flatten(),Theta.flatten()))))

    # Short hand for cost function
    #cofiCostFunc(params, Y, R, num_users, num_movies, num_features, lambda_)
    costFunc = functools.partial(cofiCostFunc, Y = Y, R = R, num_users = num_users, num_movies = num_movies, num_features = num_features, lambda_ = 0)


    numgrad = computeNumericalGradient(costFunc, params)


#numgrad = computeNumericalGradient( ...
#                @(t) cofiCostFunc(t, Y, R, num_users, num_movies, ...
#                                num_features, lambda), [X(:); Theta(:)]);


    cost, grad = cofiCostFunc(params,  Y, R, num_users, num_movies, num_features, 0)

    print (np.vstack((numgrad, grad)).flatten('F').reshape(-1,2))
    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 backpropagation implementation is correct, then \nthe relative difference will be small (less than 1e-9). \n\nRelative Difference: n', diff)
コード例 #7
0
def checkCostFunction(lambda_=0):
    #CHECKCOSTFUNCTION Creates a collaborative filering problem
    #to check your cost function and gradients
    #   CHECKCOSTFUNCTION(lambda_) Creates a collaborative filering problem
    #   to check your cost function and gradients, it will output the
    #   analytical gradients produced by your code and the numerical gradients
    #   (computed using computeNumericalGradient). These two gradient
    #   computations should result in very similar values.

    ## Create small problem
    X_t = random.rand(4, 3)
    Theta_t = random.rand(5, 3)

    # Zap out most entries
    Y = dot(X_t, Theta_t.T)
    Y[random.rand(*shape(Y)) > 0.5] = 0
    R = where(Y == 0, 0, 1)

    ## Run Gradient Checking
    X = random.randn(*shape(X_t))
    Theta = random.randn(*shape(Theta_t))
    num_users = size(Y, 1)
    num_movies = size(Y, 0)
    num_features = size(Theta_t, 1)

    numgrad = computeNumericalGradient(
        lambda t: cofiCostFunc(t, Y, R, num_users, num_movies, num_features,
                               lambda_),
        hstack((X.ravel('F'), Theta.ravel('F'))))

    cost, grad = cofiCostFunc(hstack((X.ravel('F'), Theta.ravel('F'))), Y, R,
                              num_users, num_movies, num_features, lambda_)

    print column_stack((numgrad, grad))

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

    diff = linalg.norm(numgrad - grad) / linalg.norm(numgrad + grad)
    print 'If your backpropagation implementation is correct, then'
    print 'the relative difference will be small (less than 1e-9).'
    print '\nRelative Difference: %g' % diff
コード例 #8
0
def checkCostFunction(_lambda=None):

    if _lambda == None:
        _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)
    Y[np.where(np.random.rand(Y.shape[0], Y.shape[1]) > 0.5)] = 0
    R = np.zeros(Y.shape)
    R[np.where(Y != 0)] = 1
    R = R.astype(int)

    ## 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]

    func = lambda t: cofiCostFunc(t, Y, R, num_users, num_movies, num_features,
                                  _lambda)
    numgrad = computeNumericalGradient(func,
                                       np.append(X.flatten(), Theta.flatten()))

    cost, grad = cofiCostFunc(
        np.append(X.flatten(), Theta.flatten()), \
        Y, R, num_users, num_movies, num_features, _lambda
    )

    print(numgrad)
    print(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 backpropagation implementation is correct, then \n \
        the relative difference will be small (less than 1e-9). \n \
        \nRelative Difference: %g\n' % diff)
コード例 #9
0
def checkCostFunction(lambda_=0):
    #CHECKCOSTFUNCTION Creates a collaborative filering problem
    #to check your cost function and gradients
    #   CHECKCOSTFUNCTION(lambda_) Creates a collaborative filering problem
    #   to check your cost function and gradients, it will output the
    #   analytical gradients produced by your code and the numerical gradients
    #   (computed using computeNumericalGradient). These two gradient
    #   computations should result in very similar values.

    ## Create small problem
    X_t = random.rand(4, 3)
    Theta_t = random.rand(5, 3)

    # Zap out most entries
    Y = dot(X_t, Theta_t.T)
    Y[random.rand(*shape(Y)) > 0.5] = 0
    R = where(Y == 0, 0, 1)

    ## Run Gradient Checking
    X = random.randn(*shape(X_t))
    Theta = random.randn(*shape(Theta_t))
    num_users = size(Y, 1)
    num_movies = size(Y, 0)
    num_features = size(Theta_t, 1)

    numgrad = computeNumericalGradient(
        lambda t: cofiCostFunc(t, Y, R, num_users, num_movies, num_features, lambda_),
        hstack((X.ravel('F'), Theta.ravel('F'))))

    cost, grad = cofiCostFunc(hstack((X.ravel('F'), Theta.ravel('F'))), Y, R,
                              num_users, num_movies, num_features, lambda_)

    print column_stack((numgrad, grad))

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

    diff = linalg.norm(numgrad-grad) / linalg.norm(numgrad+grad)
    print 'If your backpropagation implementation is correct, then'
    print 'the relative difference will be small (less than 1e-9).'
    print '\nRelative Difference: %g' % diff
コード例 #10
0
def checkCostFunction(lambda_var=0):
    #CHECKCOSTFUNCTION Creates a collaborative filering problem
    #to check your cost function and gradients
    #   CHECKCOSTFUNCTION(lambda_var) Creates a collaborative filering problem
    #   to check your cost function and gradients, it will output the
    #   analytical gradients produced by your code and the numerical gradients
    #   (computed using computeNumericalGradient). These two gradient
    #   computations should result in very similar values.

    # Set lambda_var
    # if not lambda_var or not 'lambda_var' in locals():
    #     lambda_var = 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)
    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]
    num_movies = Y.shape[0]
    num_features = Theta_t.shape[1]

    params = np.concatenate(
        (X.reshape(X.size, order='F'), Theta.reshape(Theta.size, order='F')))

    # Short hand for cost function
    def costFunc(p):
        return ccf.cofiCostFunc(p, Y, R, num_users, num_movies, num_features,
                                lambda_var)

    numgrad = cng.computeNumericalGradient(costFunc, params)

    cost, grad = ccf.cofiCostFunc(params, Y, R, num_users, num_movies,
                                  num_features, lambda_var)

    print(np.column_stack((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 your backpropagation implementation is correct, then \n' \
             'the relative difference will be small (less than 1e-9). ' \
             '\nRelative Difference: {:e}'.format(diff))
コード例 #11
0
def checkCostFunction(lambda_value=0):
    #CHECKCOSTFUNCTION Creates a collaborative filering problem 
    #to check your cost function and gradients
    #   CHECKCOSTFUNCTION(lambda) Creates a collaborative filering problem 
    #   to check your cost function and gradients, it will output the 
    #   analytical gradients produced by your code and the numerical gradients 
    #   (computed using computeNumericalGradient). These two gradient 
    #   computations should result in very similar values.

    ## 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)
    Y[np.random.rand(*Y.shape) > 0.5] = 0
    R = np.zeros(Y.shape)
    R[Y != 0] = 1

    ## Run Gradient Checking
    X = np.random.randn(*X_t.shape)
    Theta = np.random.randn(*Theta_t.shape)
    num_movies, num_users = Y.shape
    num_features = Theta_t.shape[1]

    numgrad = computeNumericalGradient(
        lambda x: cofiCostFunc(x, Y, R, num_users, num_movies, num_features, lambda_value), np.concatenate([X.ravel(), Theta.ravel()]))

    cost, grad = cofiCostFunc(np.concatenate([X.ravel(), Theta.ravel()]), Y, R, num_users, num_movies, num_features, lambda_value)

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

    diff = np.linalg.norm(numgrad - grad) / np.linalg.norm(numgrad + grad)
    print('If your cost function implementation is correct, then \nthe relative difference will be small (less than 1e-9).\nRelative Difference: %g' % diff)

    #end
コード例 #12
0
def checkCostFunction(lambda_var=0):
    #CHECKCOSTFUNCTION Creates a collaborative filering problem 
    #to check your cost function and gradients
    #   CHECKCOSTFUNCTION(lambda_var) Creates a collaborative filering problem 
    #   to check your cost function and gradients, it will output the 
    #   analytical gradients produced by your code and the numerical gradients 
    #   (computed using computeNumericalGradient). These two gradient 
    #   computations should result in very similar values.

    # Set lambda_var
    # if not lambda_var or not 'lambda_var' in locals():
    #     lambda_var = 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)
    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]
    num_movies = Y.shape[0]
    num_features = Theta_t.shape[1]

    params = np.concatenate((X.reshape(X.size, order='F'), Theta.reshape(Theta.size, order='F')))

    # Short hand for cost function
    def costFunc(p):
        return ccf.cofiCostFunc(p, Y, R, num_users, num_movies, num_features, lambda_var)

    numgrad = cng.computeNumericalGradient(costFunc, params)

    cost, grad = ccf.cofiCostFunc(params, Y, R, num_users, num_movies, num_features, lambda_var)


    print(np.column_stack((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 your backpropagation implementation is correct, then \n' \
             'the relative difference will be small (less than 1e-9). ' \
             '\nRelative Difference: {:e}'.format(diff))
コード例 #13
0
def checkCostFunction(params, Y, myR, nu, nm, nf, l=0.):
    costFunc = lambda w: cofiCostFunc(w, Y, myR, nu, nm, nf, l)

    J, grad = costFunc(params)
    # 计算数值梯度
    numgrad = computeNumericalGradient(costFunc, params)

    # Evaluate the norm of the difference between two solutions.
    # If you have a correct implementation, and assuming you used EPSILON = 0.0001
    # in computeNumericalGradient.m, then diff below should be less than 1e-9
    diff = np.linalg.norm(numgrad - grad) / np.linalg.norm(numgrad + grad)
    print(
        'If your cost function implementation is correct,\n'
        'the relative difference will be smaller than 10e-9 (assume epsilon=0.0001).\n'
        'lambda={}, Relative Difference: {}'.format(l, diff))
def checkCostFunction(reg_lambda=0):
    """ Creates a collaborative filtering problem
        to check your cost function and gradients
        checkCostFunction(lambda) Creates a collaborative filtering problem
        to check your cost function and gradients, it will output the
        analytical gradients produced by your code and the numerical gradients
        (computed using computeNumericalGradient). These two gradient
        computations should result in very similar values."""

    # Create small problem
    X_t = np.random.rand(4, 3)
    Theta_t = np.random.rand(5, 3)

    # Zap out most entries
    Y = X_t.dot(Theta_t.T)
    rand_data = np.random.randn(*Y.shape)
    Y[np.where(rand_data > 0.5)] = 0
    R = np.zeros(Y.shape)
    R[np.where(Y != 0)] = 1

    # Run Gradient Checking
    X = np.random.randn(*X_t.shape)
    Theta = np.random.randn(*Theta_t.shape)
    num_movies, num_users = Y.shape
    num_features = Theta_t.shape[1]

    # build params
    params = np.r_[X.flatten(), Theta.flatten()].reshape(-1, 1)

    costFunc = lambda t: cofiCostFunc(t, Y, R, num_users, num_movies, num_features, reg_lambda)

    numgrad = computeNumericalGradient(costFunc, params)

    cost, grad = costFunc(params)

    # make sure both grad have the same shape
    grad = grad.reshape(numgrad.shape)
    print(np.c_[numgrad.ravel(), grad.ravel()])
    print('The above two columns you get should be very similar. '
          '(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 \nRelative Difference: \n', diff)
コード例 #15
0
def checkCostFunction(*xlambda):
    if len(xlambda) == 0:
        xlambda = 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)
    Y[np.where(np.random.rand(Y.shape[0], Y.shape[1]) > 0.5)] = 0
    R = np.zeros(np.shape(Y))
    R[np.where(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]
    num_movies = Y.shape[0]
    num_features = Theta_t.shape[1]

    # cost function
    def cost_func(p):
        return cCF.cofiCostFunc(p, Y, R, num_users, num_movies, num_features,
                                xlambda)

    nn_params = np.r_[(X.ravel().reshape(num_movies * num_features, 1),
                       Theta.ravel().reshape(num_users * num_features, 1))]
    numgrad = cNG.computeNumericalGradient(cost_func, nn_params)

    cost, grad = cCF.cofiCostFunc(nn_params, Y, R, num_users, num_movies,
                                  num_features, xlambda)

    # Visually examine the two gradient computations.  The two columns you get should be very similar.
    print(np.c_[numgrad, grad])
    print(
        'The above two columns you get should be very similar.\n(Left: Numerical Gradient\tRight: Analytical Gradient)'
    )

    diff = np.linalg.norm(numgrad - grad) / np.linalg.norm(numgrad + grad)
    print('''If your cost function implementation is correct, then
         the relative difference will be small (less than 1e-9).
         Relative Difference: %.16f);''' % diff)
def optimizeParams(initial_parameters, Y, R, num_users, num_movies,
                   num_features, alpha, num_iters, Lambda):
    X = initial_parameters[:num_movies * num_features].reshape(
        num_movies, num_features)
    Theta = initial_parameters[num_movies * num_features:].reshape(
        num_users, num_features)
    J_history = np.zeros((num_iters, 2))

    for i in range(num_iters):
        params = np.append(X.flatten(), Theta.flatten())
        J, grad, reg_J, reg_grad = cofiCostFunc(params, Y, R, num_users,
                                                num_movies, num_features,
                                                Lambda)
        X_grad = reg_grad[:num_movies * num_features].reshape(
            num_movies, num_features)
        Theta_grad = reg_grad[num_movies * num_features:].reshape(
            num_users, num_features)
        X = X - (alpha * X_grad)
        Theta = Theta - (alpha * Theta_grad)
        J_history[i][0] = reg_J
        J_history[i][1] = i

    paramsFinal = np.append(X.flatten(), Theta.flatten())
    return paramsFinal, J_history
コード例 #17
0
def checkCostFunction(lambda_=0):
    #CHECKCOSTFUNCTION Creates a collaborative filering problem
    #to check your cost function and gradients
    #   CHECKCOSTFUNCTION(lambda) Creates a collaborative filering problem
    #   to check your cost function and gradients, it will output the
    #   analytical gradients produced by your code and the numerical gradients
    #   (computed using computeNumericalGradient). These two gradient
    #   computations should result in very similar values.

    #Create small problem
    X_t = np.random.rand(4, 3)
    Theta_t = np.random.rand(5, 3)

    # Zap out most entries
    Y = X_t.dot(Theta_t.T)
    y_shape = Y.shape
    rand_mask = np.random.rand(y_shape[0], y_shape[1])
    mask = rand_mask > 0.5

    Y[mask] = 0
    R = np.zeros(Y.shape)
    R[Y != 0] = 1

    # Run Gradient Checking
    X = np.random.rand(X_t.shape[0], X_t.shape[1])
    Theta = np.random.rand(Theta_t.shape[0], Theta_t.shape[1])
    num_users = Y.shape[1]
    num_movies = Y.shape[0]
    num_features = Theta_t.shape[1]

    #Cost func reference

    # Unroll parameters
    params = np.hstack((np.hstack((X.flatten(), Theta.flatten()))))

    # Short hand for cost function
    #cofiCostFunc(params, Y, R, num_users, num_movies, num_features, lambda_)
    costFunc = functools.partial(cofiCostFunc,
                                 Y=Y,
                                 R=R,
                                 num_users=num_users,
                                 num_movies=num_movies,
                                 num_features=num_features,
                                 lambda_=0)

    numgrad = computeNumericalGradient(costFunc, params)

    #numgrad = computeNumericalGradient( ...
    #                @(t) cofiCostFunc(t, Y, R, num_users, num_movies, ...
    #                                num_features, lambda), [X(:); Theta(:)]);

    cost, grad = cofiCostFunc(params, Y, R, num_users, num_movies,
                              num_features, 0)

    print(np.vstack((numgrad, grad)).flatten('F').reshape(-1, 2))
    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 backpropagation implementation is correct, then \nthe relative difference will be small (less than 1e-9). \n\nRelative Difference: n',
        diff)
コード例 #18
0
def costFunc(initial_parameters):
    return ccf.cofiCostFunc(initial_parameters, Y, R, num_users, num_movies, num_features, lambda_var)
X = mat_contents.get('X')
Theta = mat_contents.get('Theta')

#  Reduce the data set size so that this runs faster
num_users = 4
num_movies = 5
num_features = 3

X = X[0:num_movies, 0:num_features]
Theta = Theta[0:num_users, 0:num_features]
Y = Y[0:num_movies, 0:num_users]
R = R[0:num_movies, 0:num_users]

#  Evaluate cost function
params = np.r_[X.flatten(), Theta.flatten()].reshape(-1, 1)
J, grad = cofiCostFunc(params, Y, R, num_users, num_movies, num_features, 0)

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

print('\nProgram paused. Press enter to continue.\n')
pause()
"""
## Part 3: Collaborative Filtering Gradient
  Once your cost function matches up with ours, you should now implement 
  the collaborative filtering gradient function. Specifically, you should 
  complete the code in cofiCostFunc.m to return the grad argument.
"""

print('\nChecking Gradients (without regularization) ... \n')
コード例 #20
0
 def costFunc(p):
     return ccf.cofiCostFunc(p, Y, R, num_users, num_movies, num_features, lambda_var)
コード例 #21
0
 def costFunc(p):
     return ccf.cofiCostFunc(p, Y, R, num_users, num_movies, num_features,
                             lambda_var)
コード例 #22
0
ファイル: ex8_cofi.py プロジェクト: junwon1994/Coursera-ML
n_users = np.asscalar(data['num_users'])
n_movies = np.asscalar(data['num_movies'])
n_features = np.asscalar(data['num_features'])

# Reduce the data set size so that this runs faster
n_users = 4
n_movies = 5
n_features = 3
X = X[:n_movies, :n_features]
Theta = Theta[:n_users, :n_features]
Y = Y[:n_movies, :n_users]
R = R[:n_movies, :n_users]

# Evaluate cost function
J, _ = cofiCostFunc(np.r_[X.ravel(order='F'),
                          Theta.ravel(order='F')], Y, R, n_users, n_movies,
                    n_features, 0)

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

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

#  ============== Part 3: Collaborative Filtering Gradient ==============
#  Once your cost function matches up with ours, you should now implement
#  the collaborative filtering gradient function. Specifically, you should
#  complete the code in cofiCostFunc.m to return the grad argument.
#
print('\nChecking Gradients (without regularization) ... ')

# Check gradients by running checkNNGradients
コード例 #23
0
X = mat_contents.get('X')
Theta = mat_contents.get('Theta')

#  Reduce the data set size so that this runs faster
num_users = 4
num_movies = 5
num_features = 3

X = X[0:num_movies, 0:num_features]
Theta = Theta[0:num_users, 0:num_features]
Y = Y[0:num_movies, 0:num_users]
R = R[0:num_movies, 0:num_users]

#  Evaluate cost function
params = np.r_[X.flatten(), Theta.flatten()].reshape(-1, 1)
J, grad = cofiCostFunc(params, Y, R, num_users, num_movies, num_features, 0)

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

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

"""
## Part 3: Collaborative Filtering Gradient
  Once your cost function matches up with ours, you should now implement 
  the collaborative filtering gradient function. Specifically, you should 
  complete the code in cofiCostFunc.m to return the grad argument.
"""

print('\nChecking Gradients (without regularization) ... \n')
コード例 #24
0
#  cofiCostFunc.m to return J.

#  Load pre-trained weights (X, Theta, num_users, num_movies, num_features)
data = sio.loadmat('ex8_movieParams.mat')
X = data['X']
Theta = data['Theta']

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

#  Evaluate cost function
J, _ = cofiCostFunc(np.append(X.flatten(), Theta.flatten()), Y, R, num_users, num_movies, num_features, 0)
           
print("Cost at loaded parameters: %f \n(this value should be about 22.22)\n"%J)

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

## ============== Part 3: Collaborative Filtering Gradient ==============
#  Once your cost function matches up with ours, you should now implement 
#  the collaborative filtering gradient function. Specifically, you should 
#  complete the code in cofiCostFunc.m to return the grad argument.
#  
print('\nChecking Gradients (without regularization) ... \n')

#  Check gradients by running checkNNGradients
checkCostFunction()
コード例 #25
0
#  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]

# unroll the parameters into a single vector params
params = []
params.extend((list(X.flatten(order='F')) + list(Theta.flatten(order='F'))))

#  Evaluate cost function
J, _ = cofiCostFunc(params, Y, R, num_users, num_movies, num_features, 0)

print('Cost at loaded parameters: %f '
      '\n(this value should be about 22.22)\n' % (J))

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

# ============== Part 3: Collaborative Filtering Gradient ==============
#  Once your cost function matches up with ours, you should now implement
#  the collaborative filtering gradient function. Specifically, you should
#  complete the code in cofiCostFunc.m to return the grad argument.

print('\nChecking Gradients (without regularization) ... \n')

#  Check gradients by running checkNNGradients
checkCostFunction()
コード例 #26
0
ファイル: ex8_cofi.py プロジェクト: jy2881/AndrewNg-ML-python
#  Load pre-trained weights (X, Theta, num_users, num_movies, num_features)
ex8_movieParams = sco.loadmat('ex8_movieParams.mat')
X,Theta,num_users,num_movies,num_features = ex8_movieParams["X"],ex8_movieParams["Theta"],ex8_movieParams["num_users"],\
                                            ex8_movieParams["num_movies"],ex8_movieParams["num_features"]
#  Reduce the data set size so that this runs faster
num_users, num_movies, num_features = 4, 5, 3
X = X[0:num_movies, 0:num_features]
Theta = Theta[0:num_users, 0:num_features]
Y = Y[0:num_movies, 0:num_users]
R = R[0:num_movies, 0:num_users]

#  Evaluate cost function
nn_params = np.r_[(X.ravel().reshape(num_movies * num_features, 1),
                   Theta.ravel().reshape(num_users * num_features, 1))]
J, grad = cCF.cofiCostFunc(nn_params, Y, R, num_users, num_movies,
                           num_features, 0)
print('Cost at loaded parameters: %f\n(this value should be about 22.22)\n' %
      J)
input('\nProgram paused. Press enter to continue.\n')

## ============== Part 3: Collaborative Filtering Gradient ==============
#  Once your cost function matches up with ours, you should now implement the collaborative filtering gradient function.
#  Specifically, you should complete the code in cofiCostFunc.m to return the grad argument.

print('\nChecking Gradients (without regularization) ... \n')
#  Check gradients by running checkNNGradients
checkCostFunction.checkCostFunction(1.5)

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

## ========= Part 4: Collaborative Filtering Cost Regularization ========
コード例 #27
0
ファイル: ex8_cofi.py プロジェクト: Horizontal8/python_assign
def gradFunc(initial_parameters):
    return cofiCostFunc(initial_parameters, Y, R, num_users, num_movies,
                        num_features, lambda_var)[1]
コード例 #28
0
ファイル: ex8_cofi.py プロジェクト: DXist/py-coursera
#  cofiCostFunc.m to return J.

#  Load pre-trained weights (X, Theta, num_users, num_movies, num_features)
ex8_movieParams = loadmat('ex8_movieParams.mat')
X = ex8_movieParams['X']
Theta = ex8_movieParams['Theta']

#  Reduce the data set size so that this runs faster
num_users, num_movies, num_features = 4, 5, 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, _ = cofiCostFunc(serialize(X, Theta), Y, R, num_users, num_movies, num_features, 0.)

print 'Cost at loaded parameters: %f ' % J
print '(this value should be about 22.22)'

print '\nProgram paused. Press enter to continue.'
raw_input()


## ============== Part 3: Collaborative Filtering Gradient ==============
#  Once your cost function matches up with ours, you should now implement
#  the collaborative filtering gradient function. Specifically, you should
#  complete the code in cofiCostFunc.m to return the grad argument.
#
print '\nChecking Gradients (without regularization) ... '
コード例 #29
0
ファイル: ex8_cofi.py プロジェクト: grixxy/ml_python
#  Reduce the data set size so that this runs faster
num_users = 4
num_movies = 5
num_features = 3
X = X[0:num_movies, 0:num_features]
Theta = Theta[0:num_users, 0:num_features]
Y = Y[0:num_movies, 0:num_users]
R = R[0:num_movies, 0:num_users]




#  Evaluate cost function

J,grad = cofiCostFunc(np.hstack((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)\n', J)


# ============== Part 3: Collaborative Filtering Gradient ==============
#  Once your cost function matches up with ours, you should now implement
#  the collaborative filtering gradient function. Specifically, you should
#  complete the code in cofiCostFunc.m to return the grad argument.

print('\nChecking Gradients (without regularization) ... \n')

#  Check gradients by running checkNNGradients
checkCostFunction(0)

コード例 #30
0
Theta = mat['Theta']
num_users = int(mat['num_users'])
num_movies = int(mat['num_movies'])
num_features = int(mat['num_features'])

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

# Evaluate cost function
J, grad = cofiCostFunc(unrollParams([X, Theta]), Y, R, num_users, num_movies,
                       num_features, 0)
print('Cost at loaded parameters: %f ' % J,
      '\n(this value should be about 22.22)')

J, grad = cofiCostFunc(unrollParams([X, Theta]), Y, R, num_users, num_movies,
                       num_features, 1.5)
print('Cost at loaded parameters(lambad={}):{}'.format(1.5, J),
      '\n(this value should be about 31.34)')
""" 
============== Part 3: Collaborative Filtering Gradient ==============
 Once your cost function matches up with ours, you should now implement 
 the collaborative filtering gradient function. Specifically, you should 
 complete the code in cofiCostFunc.m to return the grad argument.
 """
print('\nChecking Gradients ... ')
コード例 #31
0
# Load pre-trained weights (X, Theta, num_users, num_movies, num_features)
data = loadmat('ex8_movieParams.mat')
X = data['X']
Theta = data['Theta']
num_users = data['num_users'][0][0]
num_movies = data['num_movies'][0][0]
num_features = data['num_features'][0][0]

# 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, _ = cofiCostFunc(X, Theta, Y, R, 0)
print('Cost at loaded parameters: %f' % J)
print('(this value should be about 22.22)\n')


# ============== Part 3: Collaborative Filtering Gradient ==============
# Once your cost function matches up with ours, you should now implement
# the collaborative filtering gradient function. Specifically, you should
# complete the code in cofiCostFunc.py to return the grad argument.
print('Checking Gradients (without regularization) ...')

checkCostFunction()
コード例 #32
0
ファイル: ex8_cofi.py プロジェクト: cyberteucer/CSML
Theta = data['Theta']
num_users = data['num_users']
num_movies = data['num_movies']
num_features = data['num_features']

#  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, grad = cofiCostFunc(np.hstack((X.T.flatten(), Theta.T.flatten())), Y, R,
                       num_users, num_movies, num_features, 0)

print 'Cost at loaded parameters: %f \n(this value should be about 22.22)' % J

raw_input("Program paused. Press Enter to continue...")

## ============== Part 3: Collaborative Filtering Gradient ==============
#  Once your cost function matches up with ours, you should now implement
#  the collaborative filtering gradient function. Specifically, you should
#  complete the code in cofiCostFunc.m to return the grad argument.
#
print 'Checking Gradients (without regularization) ...'

#  Check gradients by running checkNNGradients
checkCostFunction()
X = data2['X']
Theta = data2['Theta']
num_users = data2['num_users']
num_movies = data2['num_movies']
num_features = data2['num_features']

num_users, num_movies, num_features = 4, 5, 3
X_test = X[:num_movies, :num_features]
Theta_test = Theta[:num_users, :num_features]
Y_test = Y[:num_movies, :num_users]
R_test = R[:num_movies, :num_users]
params = np.append(X_test.flatten(), Theta_test.flatten())

#2.2.1,2.2.2
lam = 0
J, grad, reg_J, reg_grad = cofiCostFunc(params, Y_test, R_test, num_users,
                                        num_movies, num_features, 10)
#print(J, grad, reg_J, reg_grad)

#2.3
with open('movie_ids.txt', encoding='ISO-8859-1') as fid:
    movies = fid.readlines()

movieList = []
for movie in movies:
    movieList.append(movie.strip())

my_ratings = np.zeros((1682, 1))
my_ratings[0] = 4
my_ratings[97] = 2
my_ratings[6] = 3
my_ratings[11] = 5
コード例 #34
0
 def cost_func(p):
     return cCF.cofiCostFunc(p, Y, R, num_users, num_movies, num_features,
                             xlambda)
コード例 #35
0
def ex8_cofi():
    ## Machine Learning Online Class
    #  Exercise 8 | Anomaly Detection and Collaborative Filtering
    #
    #  Instructions
    #  ------------
    #
    #  This file contains code that helps you get started on the
    #  exercise. You will need to complete the following functions:
    #
    #     estimateGaussian.m
    #     selectThreshold.m
    #     cofiCostFunc.m
    #
    #  For this exercise, you will not need to change any code in this file,
    #  or any other files other than those mentioned above.
    #

    ## =============== Part 1: Loading movie ratings dataset ================
    #  You will start by loading the movie ratings dataset to understand the
    #  structure of the data.
    #
    print('Loading movie ratings dataset.\n')

    #  Load data
    mat = scipy.io.loadmat('ex8_movies.mat')
    Y = mat['Y']
    R = mat['R']

    #  Y is a 1682x943 matrix, containing ratings (1-5) of 1682 movies on
    #  943 users
    #
    #  R is a 1682x943 matrix, where R(i,j) = 1 if and only if user j gave a
    #  rating to movie i

    #  From the matrix, we can compute statistics like average rating.
    print('Average rating for movie 1 (Toy Story): %f / 5\n' %
          np.mean(Y[0, R[0, :] == 1]))

    #  We can "visualize" the ratings matrix by plotting it with imagesc
    plt.imshow(Y)
    plt.ylabel('Movies')
    plt.xlabel('Users')
    plt.savefig('figure1.png')

    print('\nProgram paused. Press enter to continue.')
    #pause

    ## ============ Part 2: Collaborative Filtering Cost Function ===========
    #  You will now implement the cost function for collaborative filtering.
    #  To help you debug your cost function, we have included set of weights
    #  that we trained on that. Specifically, you should complete the code in
    #  cofiCostFunc.m to return J.

    #  Load pre-trained weights (X, Theta, num_users, num_movies, num_features)
    mat = scipy.io.loadmat('ex8_movieParams.mat')
    X = mat['X']
    Theta = mat['Theta']
    num_users = mat['num_users']
    num_movies = mat['num_movies']
    num_features = mat['num_features']

    #  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, 0:num_users]
    R = R[:num_movies, 0:num_users]

    #  Evaluate cost function
    J, _ = cofiCostFunc(np.concatenate([X.ravel(), Theta.ravel()]), Y, R,
                        num_users, num_movies, num_features, 0)

    print(
        'Cost at loaded parameters: %f \n(this value should be about 22.22)' %
        J)

    print('\nProgram paused. Press enter to continue.')
    #pause

    ## ============== Part 3: Collaborative Filtering Gradient ==============
    #  Once your cost function matches up with ours, you should now implement
    #  the collaborative filtering gradient function. Specifically, you should
    #  complete the code in cofiCostFunc.m to return the grad argument.
    #
    print('\nChecking Gradients (without regularization) ... ')

    #  Check gradients by running checkNNGradients
    checkCostFunction()

    print('\nProgram paused. Press enter to continue.')
    #pause

    ## ========= Part 4: Collaborative Filtering Cost Regularization ========
    #  Now, you should implement regularization for the cost function for
    #  collaborative filtering. You can implement it by adding the cost of
    #  regularization to the original cost computation.
    #

    #  Evaluate cost function
    J, _ = cofiCostFunc(np.concatenate([X.ravel(), Theta.ravel()]), Y, R,
                        num_users, num_movies, num_features, 1.5)

    print(
        'Cost at loaded parameters (lambda = 1.5): %f \n(this value should be about 31.34)'
        % J)

    print('\nProgram paused. Press enter to continue.\n')
    #pause

    ## ======= Part 5: Collaborative Filtering Gradient Regularization ======
    #  Once your cost matches up with ours, you should proceed to implement
    #  regularization for the gradient.
    #

    #
    print('\nChecking Gradients (with regularization) ... ')

    #  Check gradients by running checkNNGradients
    checkCostFunction(1.5)

    print('\nProgram paused. Press enter to continue.')
    #pause

    ## ============== Part 6: Entering ratings for a new user ===============
    #  Before we will train the collaborative filtering model, we will first
    #  add ratings that correspond to a new user that we just observed. This
    #  part of the code will also allow you to put in your own ratings for the
    #  movies in our dataset!
    #
    movieList = loadMovieList()

    #  Initialize my ratings
    my_ratings = np.zeros(1682)

    # Check the file movie_idx.txt for id of each movie in our dataset
    # For example, Toy Story (1995) has ID 1, so to rate it "4", you can set
    my_ratings[0] = 4

    # Or suppose did not enjoy Silence of the Lambs (1991), you can set
    my_ratings[97] = 2

    # We have selected a few movies we liked / did not like and the ratings we
    # gave are as follows:
    my_ratings[6] = 3
    my_ratings[11] = 5
    my_ratings[53] = 4
    my_ratings[63] = 5
    my_ratings[65] = 3
    my_ratings[68] = 5
    my_ratings[182] = 4
    my_ratings[225] = 5
    my_ratings[354] = 5

    print('\n\nNew user ratings:')
    for i in range(len(my_ratings)):
        if my_ratings[i] > 0:
            print('Rated %d for %s' % (my_ratings[i], movieList[i]))
    #end

    print('\nProgram paused. Press enter to continue.')
    #pause

    ## ================== Part 7: Learning Movie Ratings ====================
    #  Now, you will train the collaborative filtering model on a movie rating
    #  dataset of 1682 movies and 943 users
    #

    print('\nTraining collaborative filtering...')

    #  Load data
    mat = scipy.io.loadmat('ex8_movies.mat')
    Y = mat['Y']
    R = mat['R']

    #  Y is a 1682x943 matrix, containing ratings (1-5) of 1682 movies by
    #  943 users
    #
    #  R is a 1682x943 matrix, where R(i,j) = 1 if and only if user j gave a
    #  rating to movie i

    #  Add our own ratings to the data matrix
    Y = np.hstack([my_ratings[:, None], Y])
    R = np.hstack([(my_ratings > 0)[:, None], R])

    #  Normalize Ratings
    Ynorm, Ymean = normalizeRatings(Y, R)

    #  Useful Values
    num_movies, num_users = Y.shape
    num_features = 10

    # Set Initial Parameters (Theta, X)
    X = np.random.randn(num_movies, num_features)
    Theta = np.random.randn(num_users, num_features)

    initial_parameters = np.concatenate([X.ravel(), Theta.ravel()])

    # Set options for fmincg
    options = {'maxiter': 100}

    # Set Regularization
    lambda_value = 10
    res = optimize.minimize(lambda x: cofiCostFunc(
        x, Ynorm, R, num_users, num_movies, num_features, lambda_value),
                            initial_parameters,
                            method='TNC',
                            jac=True,
                            options=options)
    theta = res.x

    # Unfold the returned theta back into U and W
    X = theta[:num_movies * num_features].reshape(num_movies, num_features)
    Theta = theta[num_movies * num_features:].reshape(num_users, num_features)

    print('Recommender system learning completed.')

    print('\nProgram paused. Press enter to continue.')
    #pause

    ## ================== Part 8: Recommendation for you ====================
    #  After training the model, you can now make recommendations by computing
    #  the predictions matrix.
    #

    p = np.dot(X, Theta.T)
    my_predictions = p[:, 0] + Ymean

    movieList = loadMovieList()

    ix = np.argsort(my_predictions)[::-1]
    print('\nTop recommendations for you:')
    for i in range(10):
        j = ix[i]
        print('Predicting rating %.1f for movie %s' %
              (my_predictions[j], movieList[j]))
    #end

    print('\n\nOriginal ratings provided:')
    for i in range(len(my_ratings)):
        if my_ratings[i] > 0:
            print('Rated %d for %s' % (my_ratings[i], movieList[i]))
コード例 #36
0
num_users = mat["num_users"]
num_movies = mat["num_movies"]
num_features = mat["num_features"]

#  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
params = np.concatenate((X.reshape(X.size, order='F'), Theta.reshape(Theta.size, order='F')))
J, _ = ccf.cofiCostFunc(params, Y, R, num_users, num_movies, num_features, 0)
           
print('Cost at loaded parameters: {:f}\n(this value should be about 22.22)\n'.format(J))

raw_input('Program paused. Press enter to continue.')


## ============== Part 3: Collaborative Filtering Gradient ==============
#  Once your cost function matches up with ours, you should now implement 
#  the collaborative filtering gradient function. Specifically, you should 
#  complete the code in cofiCostFunc.m to return the grad argument.
#  
print('\nChecking Gradients (without regularization) ... \n')

#  Check gradients by running checkNNGradients
chcf.checkCostFunction()
コード例 #37
0
 def costFunc(t):
     return cofiCostFunc(t, Y, R, num_users, num_movies, num_features,
                         lambda_par)
コード例 #38
0
Theta = data['Theta']
num_users = data['num_users']
num_movies = data['num_movies']
num_features = data['num_features']

#  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, grad = cofiCostFunc(np.hstack((X.T.flatten(), Theta.T.flatten())), Y, R, num_users, num_movies,
               num_features, 0)
           
print 'Cost at loaded parameters: %f \n(this value should be about 22.22)' % J

#raw_input("Program paused. Press Enter to continue...")  


## ============== Part 3: Collaborative Filtering Gradient ==============
#  Once your cost function matches up with ours, you should now implement 
#  the collaborative filtering gradient function. Specifically, you should 
#  complete the code in cofiCostFunc.m to return the grad argument.
#  
print 'Checking Gradients (without regularization) ...'

#  Check gradients by running checkNNGradients
checkCostFunction()
コード例 #39
0
ファイル: ex8_cofi.py プロジェクト: grixxy/ml_python
data = sio.loadmat(ml_dir + 'ex8_movieParams.mat')
Theta = data['Theta']
X = data['X']

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

#  Evaluate cost function

J, grad = cofiCostFunc(np.hstack((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)\n', J)

# ============== Part 3: Collaborative Filtering Gradient ==============
#  Once your cost function matches up with ours, you should now implement
#  the collaborative filtering gradient function. Specifically, you should
#  complete the code in cofiCostFunc.m to return the grad argument.

print('\nChecking Gradients (without regularization) ... \n')

#  Check gradients by running checkNNGradients
checkCostFunction(0)

# ========= Part 4: Collaborative Filtering Cost Regularization ========
#  Now, you should implement regularization for the cost function for
コード例 #40
0
#  cofiCostFunc.m to return J.

#  Load pre-trained weights (X, Theta, num_users, num_movies, num_features)
ex8_movieParams = loadmat('ex8_movieParams.mat')
X = ex8_movieParams['X']
Theta = ex8_movieParams['Theta']

#  Reduce the data set size so that this runs faster
num_users, num_movies, num_features = 4, 5, 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, _ = cofiCostFunc(serialize(X, Theta), Y, R, num_users, num_movies,
                    num_features, 0.)

print 'Cost at loaded parameters: %f ' % J
print '(this value should be about 22.22)'

print '\nProgram paused. Press enter to continue.'
raw_input()

## ============== Part 3: Collaborative Filtering Gradient ==============
#  Once your cost function matches up with ours, you should now implement
#  the collaborative filtering gradient function. Specifically, you should
#  complete the code in cofiCostFunc.m to return the grad argument.
#
print '\nChecking Gradients (without regularization) ... '

#  Check gradients by running checkNNGradients
コード例 #41
0
ファイル: ex8_cofi.py プロジェクト: atulrc/ML-ex8
X, Theta, num_users, num_movies, num_features = data['X'],\
        data['Theta'], data['num_users'], data['num_movies'], data['num_features']

#  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, 0:num_users]
R = R[:num_movies, 0:num_users]

#  Evaluate cost function
J, _ = cofiCostFunc.cofiCostFunc(np.concatenate([X.ravel(),
                                                 Theta.ravel()]), Y, R,
                                 num_users, num_movies, num_features)

print('Cost at loaded parameters:  %.2f \n(this value should be about 22.22)' %
      J)

## ============== Part 3: Collaborative Filtering Gradient ==============
#  Once your cost function matches up with ours, you should now implement
#  the collaborative filtering gradient function. Specifically, you should
#  complete the code in cofiCostFunc.m to return the grad argument.
#
print('\nChecking Gradients (without regularization) ... \n')

#  Check gradients by running checkNNGradients
utils.checkCostFunction(cofiCostFunc.cofiCostFunc)