num_epochs = 10

    # Read data
    train, test = data.get_movielens_data()
    num_users, num_items = train.shape

    # Prepare the test triplets
    test_uid, test_pid, test_nid = data.get_triplets(test)

    model = build_model(num_users, num_items, latent_dim)

    # Print the model structure
    print(model.summary())

    # Sanity check, should be around 0.5
    print('AUC before training %s' % metrics.full_auc(model, test))

    for epoch in range(num_epochs):

        print('Epoch %s' % epoch)

        # Sample triplets from the training data
        uid, pid, nid = data.get_triplets(train)

        X = {
            'user_input': uid,
            'positive_item_input': pid,
            'negative_item_input': nid
        }

        model.fit(X,
Beispiel #2
0
# Compile the model by including optimization function and the loss function

# 'optimization function': adam (learning rate = default in keras model)
# 'loss function': binary crossentropy to keep the output of the predictions betweeen [0, 1]

model.compile(optimizer='adam',loss= 'binary_crossentropy')

# print the model summary, if needed
model.summary()

# list for storing the training loss
loss = []
best_hr, best_ndcg, best_iter = None, None, None

# Sanity check (if needed), AUC should be around 0.5
auc, ndcg_value, hr_value = metrics.full_auc(model, test)
print('Metric before training: ndcg %s, hr %s' % (ndcg_value, hr_value))

# iterate over the num_epochs (default = 10)
for epoch in range(num_epochs):

    print('Epoch %s' % epoch)

    # get Sample triplets from the training data (to be fed into the model)
    uid, pid, nid = data.get_triplets(train)

    X = {
        'user_input': uid,
        'positive_item_input': pid,
        'negative_item_input': nid
    }
Beispiel #3
0
    test_uid, test_pid, test_nid = data.get_triplets(test)
    test_user_features, test_positive_item_features, test_negative_item_features = data.get_dense_triplets(
        test_uid, test_pid, test_nid, num_users, num_items)

    # Sample triplets from the training data
    uid, pid, nid = data.get_triplets(train)
    user_features, positive_item_features, negative_item_features = data.get_dense_triplets(
        uid, pid, nid, num_users, num_items)

    model = get_graph(num_users, num_items, 256)

    # Print the model structure
    print(model.summary())

    # Sanity check, should be around 0.5
    print('AUC before training %s' % metrics.full_auc(model, test))

    for epoch in range(num_epochs):

        print('Epoch %s' % epoch)

        model.fit(
            {
                'user_input': user_features,
                'positive_item_input': positive_item_features,
                'negative_item_input': negative_item_features,
                'triplet_loss': np.ones(len(uid))
            },
            validation_data={
                'user_input': test_user_features,
                'positive_item_input': test_positive_item_features,
Beispiel #4
0
def main():
    logging.info('reading data')

    item_mat = data.get_mult()

    trainM = sparse.csr_matrix(
        data.read_user(f_in='data/dummy/cf-train-10-users.dat',
                       num_u=50,
                       num_v=1929))
    testM = sparse.csr_matrix(
        data.read_user(f_in='data/dummy/cf-test-10-users.dat',
                       num_u=50,
                       num_v=1929))

    trainList = list()
    testList = list()
    for user in range(trainM.shape[0]):
        negative = 0
        for item in range(trainM.shape[1]):
            if trainM[user, item] == 1:
                trainList.append([user, item, 1])
            else:
                if negative < 20:
                    trainList.append([user, item, 0])
                    negative += 1
        train = np.array(trainList).astype('float32')

    testList = list()
    for user in range(testM.shape[0]):
        negative = 0
        for item in range(testM.shape[1]):
            if testM[user, item] == 1:
                testList.append([user, item, 1])
    #        else:
    #            if negative < 10:
    #                testList.append( [user, item, 0] )
    #                negative+=1
        test = np.array(testList).astype('float32')

    num_item_feat = item_mat.shape[1]

    model = CollaborativeDeepLearning(item_mat, [num_item_feat, 50, 10])
    model.pretrain(lamda_w=0.001, encoder_noise=0.3, epochs=10)
    model_history = model.fineture(train,
                                   test,
                                   lamda_u=0.01,
                                   lamda_v=0.1,
                                   lamda_n=0.1,
                                   lr=0.01,
                                   epochs=500)
    testing_rmse = model.getRMSE(test)
    print('Testing RMSE = {}'.format(testing_rmse))

    import metrics
    print('AUC %s' % metrics.full_auc(model.cdl_model, testM))

    import matplotlib.pyplot as plt
    M_low = 50
    M_high = 300
    recall_levels = M_high - M_low + 1
    recallArray = np.zeros(6)
    x = 0
    for n in [50, 100, 150, 200, 250, 300]:
        test_recall = metrics.recall_at_k(model.cdl_model, testM, k=n)
        recallArray[x] = test_recall
        print('Recall: %.2f.' % (test_recall))
        x += 1
    plt.plot([50, 100, 150, 200, 250, 300], recallArray)
    plt.ylabel("Recall")
    plt.xlabel("M")
    plt.title("Proposed: Recall@M")
    plt.show()