Exemple #1
0
def main():
    D = 4
    L = 2
    
    # randomly generate parameter values for testing
    np.random.seed(28031987)
    W1 = np.random.randn(D,2*D)
    b1 = np.random.randn(D)
    W2 = np.random.randn(2*D,D)
    b2 = np.random.randn(2*D)
    Wlabel = np.random.randn(L,D)
    
    # flattened arrays
    W1flat = W1.flatten()
    W2flat = W2.flatten()
    Wlabelflat = Wlabel.flatten()
    allflat = np.append(W1flat,b1)
    allflat = np.append(allflat,W2flat)
    allflat = np.append(allflat,b2)
    allflat = np.append(allflat,Wlabelflat)
    Np = len(allflat)
    
    # hyperparameters
    eps = 0.00000001
    lambda_reg = 0.0
    alpha = 0.2
    
    # get the data
    neg_list,pos_list = format_data()
    neg_list = neg_list[:1]
    pos_list = pos_list[:1]
    vocab = np.random.randn(268810,D)
    
    # do k_fold
    mean,SEM = k_fold(neg_list,pos_list,10,L,alpha,lambda_reg,vocab,normalized=False)
    exit(0)
    
    # calculate finite difference approximation to gradient
    numgrad = np.zeros(Np)
    for i in range(Np):
        print 'P ' + str(i)
        allflat[i] += eps
        fxpe = full_j(allflat,D,L,lambda_reg,alpha,neg_list,pos_list,
                      vocab,normalized=False)
        allflat[i] -= 2.0*eps
        fxme = full_j(allflat,D,L,lambda_reg,alpha,neg_list,pos_list,
                       vocab,normalized=False)
        allflat[i] += eps
        numgrad[i] = (fxpe - fxme)/(2.0*eps)
    
    # now use backprop
    bpgrad = compute_grad(allflat,D,L,lambda_reg,alpha,neg_list,pos_list,
                          vocab,normalized=False)
    
    print numgrad
    print bpgrad
    print numgrad-bpgrad
def top_matches(prefs, person, n=5):
    """ Rank movie critics matches for `person` from `perfs`.

    :param prefs: dict - critics as defined in data.py
    :param person: str - movie critic's name to compare others to
    :param n: int - number of movie critics to rank

    :return: list[tuple(float, str)] - ranking of (score, critic's name)
    """
    svd = dt.train_svd(prefs)
    result = svd.similar(person, n)
    return dt.format_data(result)
def get_recommendations(prefs, person):
    """ Recommend movies to `person` matching others' ratings with his.

    :param prefs: dict - critics as defined in data.py
    :param person: str - movie critic's name to advise

    :return: list[tuple(float, str)] - ranking of (score, film's title)

    Note that in this function, only movie which user has not rated
    will be recommended
    """
    n = 3
    svd = dt.train_svd(prefs)
    result = svd.recommend(person, n, only_unknowns=True, is_row=True)
    return dt.format_data(result)