def compute_grad(n_user, n_item, mal_user, mal_ratings, train, user_features_, mal_user_features_, \
    item_features_, lamda_v, n_feature, user_features_origin_, item_features_origin_):
    '''
    A : inv(lamda_v * Ik + sum(u_i* u_i))   (for u_i of item j)  k * k
    u_i : 1 * k
    grad_model: d(u_i * v_j.T)/d(M_ij) = u_i * A * u_i.T
    '''
    grad_R = compute_utility_grad(n_user, n_item, train, user_features_, \
            item_features_, user_features_origin_, item_features_origin_)
    ratings_csr_ = build_user_item_matrix(n_user, n_item, train)
    ratings_csc_ = ratings_csr_.tocsc()
    mal_ratings_csr_ = build_user_item_matrix(mal_user, n_item, mal_ratings)
    mal_ratings_csc_ = mal_ratings_csr_.tocsc()
    grad_total = np.zeros([mal_user, n_item])
    for i in xrange(mal_user):
        for j in xrange(n_item):
            if j % 100 == 0:
                print(
                    'Computing the %dth malicious user, the %d item(total users: %d, total items: %d)'
                    % (i, j, n_user, n_item))
            user_idx, _ = ratings_csc_[:, j].nonzero()
            mal_user_idx, _ = mal_ratings_csc_[:, j].nonzero()
            user_features = user_features_.take(user_idx, axis=0)
            mal_user_features = mal_user_features_.take(mal_user_idx, axis=0)
            U = np.vstack((user_features, mal_user_features))
            u_i = user_features_.take(i, axis=0)
            A = np.dot(U.T, U) + lamda_v * np.eye(n_feature)
            A_u = np.dot(A, u_i.T)
            grad_model = np.zeros([n_user, n_item])
            for m in xrange(n_user):
                u_m = user_features_.take(i, axis=0)
                grad_model[m, j] = np.dot(u_m, np.dot(inv(A), u_i.T))
            grad_total[i, j] = sum(sum(grad_model * grad_R))
    return grad_total
Beispiel #2
0
def compute_grad_SGLD(mal_data_index_dic, n_user, n_item, mal_user, mal_ratings, train, user_features_, mal_user_features_, \
    item_features_, diag_sqrt, n_feature, user_features_origin_, item_features_origin_, target_item):
    '''
    B: 
    A : inv(lamda_v * Ik + sum(u_i* u_i))   (for u_i of item j)  k * k
    u_i : 1 * k
    grad_model: d(u_i * v_j.T)/d(M_ij) = u_i * A * u_i.T
    '''
    lamda = 0.05
    tao = 0.01
#    W = np.zeros([944,1683])  #需要 优化
#    sum(1*(mal_ratings.take(1, axis=1) == 47))
#    ou[0] = mal_ratings.take(0, axis=1)
#    r = 
    grad_R = compute_utility_grad(n_user, n_item, train, user_features_, \
            item_features_, user_features_origin_, item_features_origin_, target_item)
    ratings_csr_ = build_user_item_matrix(n_user, n_item, train)
    ratings_csc_ = ratings_csr_.tocsc()
    mal_ratings_csr_ = build_user_item_matrix(mal_user, n_item, mal_ratings)
    mal_ratings_csc_ = mal_ratings_csr_.tocsc()
    grad_total = np.zeros([mal_user, n_item])
    diag_sqrt = diag_sqrt[0:n_feature,0:n_feature]
    import time
    
    t1= time.time()
    for i in range(mal_user):
        print('Computing the %dth malicious user' %i)
        mal_use_index = mal_data_index_dic[i][0]
        for j in range(mal_use_index.shape[0]):
#            print(j)
            mal_item = mal_use_index[j]
            user_idx, _ = ratings_csc_[:, mal_item].nonzero()
            item_idx, _ = ratings_csc_[i, :].nonzero()
            mal_user_idx, _ = mal_ratings_csc_[:, mal_item].nonzero()
            user_features = user_features_.take(user_idx, axis=0)
            item_features = item_features_.take(item_idx, axis=0)
            mal_user_features = mal_user_features_.take(mal_user_idx, axis=0)
            U = np.vstack((user_features, mal_user_features))  
            u_i = user_features_.take(i, axis = 0)
            v_j = item_features_.take(mal_item, axis = 0)
            B = np.dot(U, diag_sqrt + lamda * np.eye(n_feature))  
            Bi = np.dot(B.T , B) + tao * np.eye(n_feature)
#            A_u = np.dot(A, u_i.T)
            grad_model = np.zeros([n_user, n_item])
            for m in range(n_user):
                u_m = user_features_.take(i, axis = 0)
                v_grad = np.dot(np.dot(inv(Bi), diag_sqrt + lamda * np.eye(n_feature)), u_m)
                e_grad = 1/np.dot(mal_user_features, item_features.T)
                grad_model[m, mal_item] = np.dot(u_m, np.dot(v_grad, diag_sqrt)) + np.sum(sum(np.dot(np.dot(u_i,v_j), e_grad)))
            grad_total[i, mal_item] = sum(sum(grad_model * grad_R))
            
            
        t2 = time.time()
        print(t2-t1)
        t1= time.time()
    
    
    return grad_total   
def ALS(n_user, n_item, n_feature, mal_user, ratings, mean_rating_, mal_mean_rating_, mal_ratings, lamda_u, lamda_v, \
    user_features_, mal_user_features_, item_features_):
    ratings_csr_ = build_user_item_matrix(n_user, n_item, ratings)
    ratings_csc_ = ratings_csr_.tocsc()
    mal_ratings_csr_ = build_user_item_matrix(mal_user, n_item, mal_ratings)
    mal_ratings_csc_ = mal_ratings_csr_.tocsc()

    _update_user_feature(n_user, ratings_csr_, n_feature, lamda_u,
                         mean_rating_, user_features_, item_features_)
    _update_mal_feature(mal_user, mal_ratings_csr_, n_feature, lamda_u,
                        mal_mean_rating_, mal_user_features_, item_features_)
    _update_item_feature(n_item, ratings_csc_, mal_ratings_csc_, n_feature, lamda_v, mean_rating_, \
    mal_mean_rating_, user_features_, mal_user_features_, item_features_)
def ALS_origin(n_user, n_item, n_feature, ratings, mean_rating_, lamda_u,
               lamda_v, user_features_, item_features_):
    ratings_csr_ = build_user_item_matrix(n_user, n_item, ratings)
    ratings_csc_ = ratings_csr_.tocsc()
    _update_user_feature(n_user, ratings_csr_, n_feature, lamda_u,
                         mean_rating_, user_features_, item_features_)
    _update_item_feature(n_item, ratings_csc_, n_feature, lamda_v,
                         mean_rating_, user_features_, item_features_)
Beispiel #5
0
def compute_grad_PGA(mal_data_index_dic, n_user, n_item, mal_user, mal_ratings, train, user_features_, mal_user_features_, \
    item_features_, lamda_v, n_feature, user_features_origin_, item_features_origin_, target_item):
    '''
    A : inv(lamda_v * Ik + sum(u_i* u_i))   (for u_i of item j)  k * k
    u_i : 1 * k
    grad_model: d(u_i * v_j.T)/d(M_ij) = u_i * A * u_i.T
    '''
    grad_R = compute_utility_grad(n_user, n_item, train, user_features_, \
            item_features_, user_features_origin_, item_features_origin_, target_item)
    ratings_csr_ = build_user_item_matrix(n_user, n_item, train)
    ratings_csc_ = ratings_csr_.tocsc()
    mal_ratings_csr_ = build_user_item_matrix(mal_user, n_item, mal_ratings)
    mal_ratings_csc_ = mal_ratings_csr_.tocsc()
    grad_total = np.zeros([mal_user, n_item])
    import time
    t1= time.time()
    for i in range(mal_user):
        print('Computing the %dth malicious user' %i)
        mal_use_index = mal_data_index_dic[i][0]
        for j in range(mal_use_index.shape[0]):
#            print(j)
            mal_item = mal_use_index[j]
            user_idx, _ = ratings_csc_[:, mal_item].nonzero()
            mal_user_idx, _ = mal_ratings_csc_[:, mal_item].nonzero()
            user_features = user_features_.take(user_idx, axis=0)
            mal_user_features = mal_user_features_.take(mal_user_idx, axis=0)
            U = np.vstack((user_features, mal_user_features))  
            u_i = user_features_.take(i, axis = 0)
            A = np.dot(U.T, U) + lamda_v * np.eye(n_feature)  
#            A_u = np.dot(A, u_i.T)
            grad_model = np.zeros([n_user, n_item])
            for m in range(n_user):
                u_m = user_features_.take(i, axis = 0)
                grad_model[m, mal_item] = np.dot(u_m, np.dot(inv(A), u_i.T))
            grad_total[i, mal_item] = sum(sum(grad_model * grad_R))
        t2 = time.time()
        print(t2-t1)
        t1= time.time()
    return grad_total
def compute_utility_grad(n_user, n_item, train, user_features_, item_features_,user_features_origin_, item_features_origin_, \
    w_j0 = 0.8, u1 = 0.5, u2 = 0.5):
    ratings_csr_ = build_user_item_matrix(n_user, n_item, train)
    grad_av = 2 * (np.dot(user_features_, item_features_.T) -
                   np.dot(user_features_origin_, item_features_origin_.T))
    for i in xrange(n_user):
        _, item_idx = ratings_csr_[i, :].nonzero()
        grad_av[i, item_idx] = 0
    avg_rating = np.mean(np.dot(user_features_, item_features_.T), axis=0)
    perfer_index = np.where(avg_rating > 0.03)
    J0 = random.sample(list(perfer_index[0]), 1)
    grad_in = np.zeros([n_user, n_item])
    grad_in[:, J0] = w_j0
    grad_hy = u1 * grad_av + u2 * grad_in
    return grad_hy