Exemplo n.º 1
0
    data=feedback,
    test_size=0.1,
    val_size=0.1,
    exclude_unknowns=True,
    review_text=review_modality,
    verbose=True,
    seed=123,
)

pretrained_word_embeddings = {}  # You can load pretrained word embedding here

model = cornac.models.NARRE(
    embedding_size=100,
    id_embedding_size=32,
    n_factors=32,
    attention_size=16,
    kernel_sizes=[3],
    n_filters=64,
    dropout_rate=0.5,
    max_text_length=50,
    batch_size=64,
    max_iter=10,
    init_params={'pretrained_word_embeddings': pretrained_word_embeddings},
    verbose=True,
    seed=123,
)

cornac.Experiment(eval_method=ratio_split,
                  models=[model],
                  metrics=[cornac.metrics.RMSE()]).run()
Exemplo n.º 2
0
                        num_neg=50,
                        seed=123)
neumf1 = cornac.models.NeuMF(num_factors=8,
                             layers=[64, 32, 16, 8],
                             act_fn='tanh',
                             learner='adam',
                             num_epochs=10,
                             batch_size=256,
                             lr=0.001,
                             num_neg=50,
                             seed=123)
neumf2 = cornac.models.NeuMF(name='NeuMF_pretrained',
                             learner='adam',
                             num_epochs=10,
                             batch_size=256,
                             lr=0.001,
                             num_neg=50,
                             seed=123,
                             num_factors=gmf.num_factors,
                             layers=mlp.layers,
                             act_fn=mlp.act_fn).pretrain(gmf, mlp)

# Instantiate evaluation metrics
ndcg_50 = cornac.metrics.NDCG(k=50)
rec_50 = cornac.metrics.Recall(k=50)

# Put everything together into an experiment and run it
cornac.Experiment(eval_method=ratio_split,
                  models=[gmf, mlp, neumf1, neumf2],
                  metrics=[ndcg_50, rec_50]).run()
Exemplo n.º 3
0
                              tokenizer=BaseTokenizer('\t'),
                              max_vocab=8000,
                              max_doc_freq=0.5,
                              stop_words='english')

ratio_split = RatioSplit(data=data,
                         test_size=0.2,
                         exclude_unknowns=True,
                         item_text=item_text_module,
                         verbose=True,
                         seed=123,
                         rating_threshold=0.5)

cdr = cornac.models.CDR(k=50,
                        autoencoder_structure=[200],
                        max_iter=100,
                        batch_size=128,
                        lambda_u=0.01,
                        lambda_v=0.1,
                        lambda_w=0.0001,
                        lambda_n=5,
                        learning_rate=0.001,
                        vocab_size=8000)

rec_300 = cornac.metrics.Recall(k=300)

exp = cornac.Experiment(eval_method=ratio_split,
                        models=[cdr],
                        metrics=[rec_300])
exp.run()
Exemplo n.º 4
0
# build text module
item_text_modality = TextModality(corpus=plots,
                                  ids=movie_ids,
                                  tokenizer=BaseTokenizer(
                                      sep='\t', stop_words='english'),
                                  max_vocab=5000,
                                  max_doc_freq=0.5)

ratio_split = RatioSplit(data=ml_1m,
                         test_size=0.2,
                         exclude_unknowns=True,
                         item_text=item_text_modality,
                         verbose=True,
                         seed=123)

hft = cornac.models.HFT(k=10,
                        max_iter=40,
                        grad_iter=5,
                        l2_reg=0.001,
                        lambda_text=0.01,
                        vocab_size=5000,
                        seed=123)

mse = cornac.metrics.MSE()

exp = cornac.Experiment(eval_method=ratio_split,
                        models=[hft],
                        metrics=[mse],
                        user_based=False)
exp.run()
Exemplo n.º 5
0
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""Example for Social Bayesian Personalized Ranking with Epinions dataset"""

import cornac
from cornac.data import Reader, GraphModule
from cornac.datasets import epinions
from cornac.eval_methods import RatioSplit

ratio_split = RatioSplit(data=epinions.load_data(Reader(bin_threshold=4.0)),
                         test_size=0.1,
                         rating_threshold=0.5,
                         exclude_unknowns=True,
                         verbose=True,
                         user_graph=GraphModule(data=epinions.load_trust()))

sbpr = cornac.models.SBPR(k=10,
                          max_iter=50,
                          learning_rate=0.001,
                          lambda_u=0.015,
                          lambda_v=0.025,
                          lambda_b=0.01,
                          verbose=True)
rec_10 = cornac.metrics.Recall(k=10)

cornac.Experiment(eval_method=ratio_split, models=[sbpr],
                  metrics=[rec_10]).run()
Exemplo n.º 6
0
ml_100k = cn.datasets.movielens.load_100k()

# Split data based on ratio
ratio_split = cn.eval_methods.RatioSplit(data=ml_100k,
                                         test_size=0.2,
                                         rating_threshold=4.0,
                                         seed=123)

# Here we are comparing biased MF, PMF, and BPR
mf = cn.models.MF(k=10,
                  max_iter=25,
                  learning_rate=0.01,
                  lambda_reg=0.02,
                  use_bias=True)
pmf = cn.models.PMF(k=10, max_iter=100, learning_rate=0.001, lamda=0.001)
bpr = cn.models.BPR(k=10, max_iter=100, learning_rate=0.001, lambda_reg=0.01)

# Define metrics used to evaluate the models
mae = cn.metrics.MAE()
rmse = cn.metrics.RMSE()
rec_20 = cn.metrics.Recall(k=20)
ndcg_20 = cn.metrics.NDCG(k=20)
auc = cn.metrics.AUC()

# Put it together into an experiment and run
exp = cn.Experiment(eval_method=ratio_split,
                    models=[mf, pmf, bpr],
                    metrics=[mae, rmse, rec_20, ndcg_20, auc],
                    user_based=True)
exp.run()
Exemplo n.º 7
0
import cornac
from cornac.data import Reader
from cornac.datasets import citeulike
from cornac.eval_methods import RatioSplit

_, item_ids = citeulike.load_text()

data = citeulike.load_data(reader=Reader(item_set=item_ids))

ratio_split = RatioSplit(data=data,
                         test_size=0.2,
                         exclude_unknowns=True,
                         verbose=True,
                         seed=123,
                         rating_threshold=0.5)

cf = cornac.models.WMF(k=50,
                       max_iter=50,
                       learning_rate=0.001,
                       lambda_u=0.01,
                       lambda_v=0.01,
                       verbose=True,
                       seed=123)

rec_300 = cornac.metrics.Recall(k=300)

cornac.Experiment(eval_method=ratio_split,
                  models=[cf],
                  metrics=[rec_300],
                  user_based=True).run()
Exemplo n.º 8
0
    lambda_u=0.01,
    lambda_h=0.01,
    lambda_v=0.01,
    max_iter=10000,
    trainable=True,
    verbose=True,
)

n_items = eval_method.train_set.num_items

k_1 = int(n_items / 100)
k_5 = int(n_items * 5 / 100)
k_10 = int(n_items * 10 / 100)

cornac.Experiment(
    eval_method=eval_method,
    models=[model],
    metrics=[
        cornac.metrics.AUC(),
        cornac.metrics.Recall(k=k_1),
        cornac.metrics.Recall(k=k_5),
        cornac.metrics.Recall(k=k_10),
        cornac.metrics.NDCG(k=k_1),
        cornac.metrics.NDCG(k=k_5),
        cornac.metrics.NDCG(k=k_10),
    ],
    show_validation=True,
    save_dir="dist/toy/result",
    verbose=True,
).run()
Exemplo n.º 9
0
        Discrete('act_fn', ['tanh', 'relu', 'elu']),
        Discrete('n_epochs', [20, 50, 100]),
        Continuous("beta", low=0.4, high=1.8),
        Continuous("learning_rate", low=1e-3, high=3e-1),
    ],
    metric=hm,
    eval_method=eval_method,
    n_trails=50,
)

# In[ ]:

cornac.Experiment(
    eval_method=eval_method,
    models=[rs],
    metrics=[mae, rmse, recall25, recall50, ndcg, ncrr, auc, mAP, f1, hm],
    user_based=True,
    save_dir=GLOBAL_DIR,
).run()

# In[ ]:

import pickle
saved_path = rs.best_model.save(GLOBAL_DIR)
item_idx2id = list(rs.best_model.train_set.item_ids)
item_id2idx = rs.best_model.train_set.uid_map
mapping = {
    'item_idx2id': item_idx2id,
    'item_id2idx': item_id2idx,
}
Exemplo n.º 10
0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================


import cornac as cn

ml_100k = cn.datasets.movielens.load_100k()
ratio_split = cn.eval_methods.RatioSplit(data=ml_100k, test_size=0.2,
                                         rating_threshold=4.0, verbose=True)

bo = cn.models.BaselineOnly(max_iter=30, learning_rate=0.01, lambda_reg=0.02, verbose=True)
svd = cn.models.SVD(k=10, max_iter=30, learning_rate=0.01, lambda_reg=0.02, verbose=True)

mae = cn.metrics.MAE()
rmse = cn.metrics.RMSE()

cn.Experiment(eval_method=ratio_split,
              models=[bo, svd],
              metrics=[mae, rmse]).run()
Exemplo n.º 11
0
    seed=123,
)

# Instantiate a NMF recommender model.
nmf = cornac.models.NMF(
    k=15,
    max_iter=50,
    learning_rate=0.005,
    lambda_u=0.06,
    lambda_v=0.06,
    lambda_bu=0.02,
    lambda_bi=0.02,
    use_bias=False,
    verbose=True,
    seed=123,
)

# Instantiate evaluation metrics.
mae = cornac.metrics.MAE()
rmse = cornac.metrics.RMSE()
rec_20 = cornac.metrics.Recall(k=20)
pre_20 = cornac.metrics.Precision(k=20)

# Instantiate and then run an experiment.
cornac.Experiment(
    eval_method=eval_method,
    models=[nmf],
    metrics=[mae, rmse, rec_20, pre_20],
    user_based=True,
).run()
Exemplo n.º 12
0
    model=mf,
    space=[
        Discrete("k", [10, 30, 50]),
        Discrete("use_bias", [True, False]),
        Discrete("lambda_reg", [1e-1, 1e-2, 1e-3, 1e-4]),
    ],
    metric=rmse,
    eval_method=ratio_split,
)

# Wrap MF model inside RandomSearch along with the searching space, try 30 times
rs_mf = RandomSearch(
    model=mf,
    space=[
        Discrete("k", [10, 30, 50]),
        Discrete("use_bias", [True, False]),
        Continuous("lambda_reg", low=1e-4, high=1e-1),
    ],
    metric=rmse,
    eval_method=ratio_split,
    n_trails=30,
)

# Put everything together into an experiment and run it
cornac.Experiment(
    eval_method=ratio_split,
    models=[gs_mf, rs_mf],
    metrics=[mae, rmse],
    user_based=False,
).run()
Exemplo n.º 13
0
                                      weighting="bm25",
                                      name="UserKNN-BM25")
# ItemKNN methods
item_knn_cosine = cornac.models.ItemKNN(k=K,
                                        similarity="cosine",
                                        name="ItemKNN-Cosine")
item_knn_pearson = cornac.models.ItemKNN(k=K,
                                         similarity="pearson",
                                         name="ItemKNN-Pearson")
item_knn_adjusted = cornac.models.ItemKNN(k=K,
                                          similarity="cosine",
                                          mean_centered=True,
                                          name="ItemKNN-AdjustedCosine")

# Put everything together into an experiment
cornac.Experiment(
    eval_method=ratio_split,
    models=[
        user_knn_cosine,
        user_knn_pearson,
        user_knn_amp,
        user_knn_idf,
        user_knn_bm25,
        item_knn_cosine,
        item_knn_pearson,
        item_knn_adjusted,
    ],
    metrics=[cornac.metrics.RMSE()],
    user_based=True,
).run()
Exemplo n.º 14
0
    act_fn=best_params.get('act_fn'),
    beta=best_params.get('beta'),
    learning_rate=best_params.get('learning_rate'),
    batch_size=100,
    trainable=True,
    verbose=False,
    seed=123,
    use_gpu=True)
mp = cornac.models.most_pop.recom_most_pop.MostPop(name='MostPop')

# In[ ]:

cornac.Experiment(
    eval_method=ratio_split,
    models=[vaecf, mp],
    metrics=[mae, rmse, recall25, recall50, ndcg, ncrr, auc, mAP, f1, hm],
    user_based=True,
    #save_dir=GLOBAL_DIR,
).run()

# In[ ]:

saved_path = vaecf.save(GLOBAL_DIR)
item_idx2id = list(vaecf.train_set.item_ids)
item_id2idx = vaecf.train_set.uid_map
mp_item_idx2id = list(mp.train_set.item_ids)

mapping = {
    'vaecf': {
        'item_idx2id': item_idx2id,
        'item_id2idx': item_id2idx,
Exemplo n.º 15
0
# -*- coding: utf-8 -*-

"""
Example for Matrix Factorization with biases

@author: Quoc-Tuan Truong <*****@*****.**>
"""

import cornac
from cornac.datasets import movielens
from cornac.eval_methods import RatioSplit

ratio_split = RatioSplit(data=movielens.load_1m(),
                         test_size=0.2,
                         exclude_unknowns=False,
                         verbose=True)

mf = cornac.models.MF(k=10, max_iter=25, learning_rate=0.01, lambda_reg=0.02,
                      use_bias=True, early_stop=True, verbose=True)

mae = cornac.metrics.MAE()
rmse = cornac.metrics.RMSE()

exp = cornac.Experiment(eval_method=ratio_split,
                        models=[mf],
                        metrics=[mae, rmse],
                        user_based=True)
exp.run()
Exemplo n.º 16
0
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""Example for Maximum Margin Matrix Factorization on MovieLens 100K dataset"""

import cornac

# Load MovieLens 100K dataset, and binarise ratings using cornac.data.Reader
feedback = cornac.datasets.movielens.load_feedback(
    variant="100K", reader=cornac.data.Reader(bin_threshold=1.0))

# Define an evaluation method to split feedback into train and test sets
ratio_split = cornac.eval_methods.RatioSplit(data=feedback,
                                             test_size=0.2,
                                             verbose=True)

mmmf = cornac.models.MMMF(k=10, max_iter=200, learning_rate=0.01, verbose=True)

# Use NDCG@10 for evaluation
ndcg = cornac.metrics.NDCG(k=10)

# Put everything together into an experiment and run it
cornac.Experiment(eval_method=ratio_split, models=[mmmf], metrics=[ndcg]).run()
Exemplo n.º 17
0
ratio_split = RatioSplit(
    data=feedback,
    test_size=0.1,
    rating_threshold=0.5,
    exclude_unknowns=True,
    verbose=True,
    item_image=item_image_modality,
)

# Instantiate CVAE
vbpr = cornac.models.VBPR(
    k=10,
    k2=20,
    n_epochs=50,
    batch_size=100,
    learning_rate=0.005,
    lambda_w=1,
    lambda_b=0.01,
    lambda_e=0.0,
    use_gpu=True,
)

# Instantiate evaluation measures
auc = cornac.metrics.AUC()
rec_50 = cornac.metrics.Recall(k=50)

# Put everything together into an experiment and run it
cornac.Experiment(eval_method=ratio_split,
                  models=[vbpr],
                  metrics=[auc, rec_50]).run()
Exemplo n.º 18
0
    n_bpr_samples=args.bpr_samples,
    n_element_samples=args.element_samples,
    lambda_reg=args.lambda_reg,
    lambda_bpr=args.lambda_bpr,
    max_iter=args.epoch,
    lr=args.learning_rate,
    verbose=args.verbose,
    seed=args.seed,
)

exp = cornac.Experiment(
    eval_method=eval_method,
    models=[mter],
    metrics=[
        cornac.metrics.RMSE(),
        cornac.metrics.Recall(k=10),
        cornac.metrics.Recall(k=50),
        cornac.metrics.NDCG(k=50),
        cornac.metrics.AUC(),
    ],
)

exp.run()

# save params and trained weights
pd.DataFrame(
    data={
        "raw_id": list(eval_method.train_set.uid_map.keys()),
        "id": list(eval_method.train_set.uid_map.values()),
    })[["raw_id", "id"]].to_csv(os.path.join(args.out, "uid_map"),
                                header=None,
Exemplo n.º 19
0
rs = RatioSplit(data=ml_100k, test_size=0.2, rating_threshold=4.0, seed=123)

# initialize models, here we are comparing: Biased MF, PMF, and BPR
models = [
    MF(k=10,
       max_iter=25,
       learning_rate=0.01,
       lambda_reg=0.02,
       use_bias=True,
       seed=123),
    PMF(k=10, max_iter=100, learning_rate=0.001, lambda_reg=0.001, seed=123),
    BPR(k=10, max_iter=200, learning_rate=0.001, lambda_reg=0.01, seed=123),
]

# define metrics to evaluate the models
metrics = [
    MAE(),
    RMSE(),
    Precision(k=10),
    Recall(k=10),
    NDCG(k=10),
    AUC(),
    MAP()
]

# put it together in an experiment, voilà!
cornac.Experiment(eval_method=rs,
                  models=models,
                  metrics=metrics,
                  user_based=True).run()
Exemplo n.º 20
0
ml_1m = movielens.load_feedback(variant="1M")

# Define an evaluation method to split feedback into train and test sets
ratio_split = RatioSplit(
    data=ml_1m, test_size=0.2, exclude_unknowns=False, verbose=True
)

# Instantiate the global average baseline and MF model
global_avg = cornac.models.GlobalAvg()
mf = cornac.models.MF(
    k=10,
    max_iter=25,
    learning_rate=0.01,
    lambda_reg=0.02,
    use_bias=True,
    early_stop=True,
    verbose=True,
)

# Instantiate MAE and RMSE for evaluation
mae = cornac.metrics.MAE()
rmse = cornac.metrics.RMSE()

# Put everything together into an experiment and run it
cornac.Experiment(
    eval_method=ratio_split,
    models=[global_avg, mf],
    metrics=[mae, rmse],
    user_based=True,
).run()
Exemplo n.º 21
0
# Here we are comparing biased MF, PMF, and BPR
mf = cornac.models.MF(k=10,
                      max_iter=25,
                      learning_rate=0.01,
                      lambda_reg=0.02,
                      use_bias=True,
                      seed=123)
pmf = cornac.models.PMF(k=10,
                        max_iter=100,
                        learning_rate=0.001,
                        lambda_reg=0.001,
                        seed=123)
bpr = cornac.models.BPR(k=10,
                        max_iter=200,
                        learning_rate=0.001,
                        lambda_reg=0.01,
                        seed=123)

# Define metrics used to evaluate the models
mae = cornac.metrics.MAE()
rmse = cornac.metrics.RMSE()
recall = cornac.metrics.Recall(k=[10, 20])
ndcg = cornac.metrics.NDCG(k=[10, 20])
auc = cornac.metrics.AUC()

# Put it together into an experiment and run
cornac.Experiment(eval_method=rs,
                  models=[mf, pmf, bpr],
                  metrics=[mae, rmse, recall, ndcg, auc],
                  user_based=True).run()
Exemplo n.º 22
0
        Continuous("lambda_u", low=1e-4, high=1e-1),
        Continuous("lambda_v", low=1e-4, high=1e-1),
        Continuous("learning_rate", low=1e-3, high=1e-1),
        Discrete('batch_size', [128, 256]),
        Continuous('a', low=0.8, high=1.4),
        Continuous('b', low=0.0, high=2e-1),
    ],
    metric=auc,
    eval_method=cv,
    n_trails=30,
)

# In[9]:

cornac.Experiment(eval_method=cv,
                  models=[rs_wmf],
                  metrics=[mae, rmse, recall, ndcg, auc, mAP],
                  user_based=True).run()

# In[ ]:

with open("submission.txt", "w") as f:
    item_idx2id = list(rs_wmf.best_model.train_set.item_ids)
    last_ok = 0
    for i in range(9402):
        try:
            f.write(" ".join([
                str(item_idx2id[rec])
                for rec in rs_wmf.best_model.rank(i)[0][0:50]
            ]) + "\n")
            last_ok = i
        except:
Exemplo n.º 23
0
# ml_100k = cornac.datasets.movielens.load_feedback(variant="100K")

rs = cornac.eval_methods.RatioSplit(data=data, test_size=0.2, rating_threshold=1.0, seed=123)


bpr = cornac.models.BPR(k=10, max_iter=200, learning_rate=0.001, lambda_reg=0.01, seed=123)
mae = cornac.metrics.MAE()
rmse = cornac.metrics.RMSE()
prec = cornac.metrics.Precision(k=10)
recall = cornac.metrics.Recall(k=10)
ndcg = cornac.metrics.NDCG(k=10)


experi = cornac.Experiment(
  eval_method=rs,
  models=[ bpr],
  metrics=[mae, rmse, prec, recall, ndcg],
  user_based=True
)

experi.run()
# keys: ['MAE', 'RMSE', 'NDCG@10', 'Precision@10', 'Recall@10']

results = experi.result[0].metric_user_results
hr = list(results['Precision@10'].values())
ndcg = list(results['NDCG@10'].values())
recall = list(results['Recall@10'].values())

print(#"MAP:\t%f" % eval_map,
    "Precision@K:\t%f" % np.mean(hr),
      "NDCG:\t%f" % np.mean(ndcg),
      "Recall@K:\t%f" % np.mean(recall),
Exemplo n.º 24
0
def run_task(task_input):

    inputParam, username, current_run = task_input
    user_folder = "uploads/" + username
    run_folder = os.path.join(user_folder, str(current_run))

    try:
        # Reading of Dataset and Metadata
        pathData = os.path.abspath("uploads/dataset/" +
                                   inputParam['data_file'])
        dataset = read_data(pathData)
        # pathMeta = cache(url = os.path.abspath("uploads/metadata/" + inputParam["meta_file"]))
        # metadata = read_meta(pathMeta)

        # Using Cornac
        eval_method = select_eval(inputParam["evalmethod"], dataset)
        model = select_model(inputParam)
        metrics = select_metrics(inputParam["metrics"], inputParam)

        exp = cornac.Experiment(eval_method=eval_method,
                                models=[model],
                                metrics=metrics,
                                user_based=True)
        exp.run()
        exp_result = str(exp.result)
        result = exp_result.split("\n")

        # Splitting the output
        output = []
        for line in result:
            if '|' in line:
                store = []
                for data in line.split('|'):
                    store.append(data)
                output.append(store)

        # Creating the run folder
        os.makedirs(run_folder)

        # Saving the trained model
        model_file = "trained_model.pkl"
        model_path = os.path.join(run_folder, model_file)
        f = open(model_path, "wb")
        pickle.dump(model, f)
        f.close()

        # Saving the run results
        results_path = user_folder + "/user_results.pkl"
        run_result = {}
        run_result["parameter"] = inputParam
        run_result["output"] = output
        result_dict = {current_run: run_result}

        f = open(results_path, "rb")
        user_results = pickle.load(f)
        user_results.update(result_dict)
        f.close()

        f = open(results_path, "wb")
        pickle.dump(user_results, f)
        f.close()

        print("Task completed!")

    except:
        results_path = user_folder + "/user_results.pkl"
        result_dict = {current_run: "Training error! Try again..."}
        f = open(results_path, "rb")
        user_results = pickle.load(f)
        user_results.update(result_dict)
        f.close()

        f = open(results_path, "wb")
        pickle.dump(user_results, f)
        f.close()

        print("Training Error!")
Exemplo n.º 25
0
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================

import cornac
from cornac.datasets import movielens
from cornac.eval_methods import RatioSplit
from cornac.models import IBPR

# Load the MovieLens 1M dataset
ml_1m = movielens.load_1m()

# Instantiate an evaluation method.
ratio_split = RatioSplit(data=ml_1m, test_size=0.2, rating_threshold=1.0,
                         exclude_unknowns=True, verbose=True)

# Instantiate a IBPR recommender model.
ibpr = IBPR(k=10, init_params={'U': None, 'V': None}, verbose=True)

# Instantiate evaluation metrics.
rec_20 = cornac.metrics.Recall(k=20)
pre_20 = cornac.metrics.Precision(k=20)

# Instantiate and then run an experiment.
exp = cornac.Experiment(eval_method=ratio_split,
                        models=[ibpr],
                        metrics=[rec_20, pre_20],
                        user_based=True)
exp.run()
Exemplo n.º 26
0
import cornac
from cornac.datasets import movielens
from cornac.eval_methods import RatioSplit
from cornac.models import PMF

# Load the MovieLens 100K dataset
ml_100k = movielens.load_feedback()

# Instantiate an evaluation method.
ratio_split = RatioSplit(data=ml_100k,
                         test_size=0.2,
                         rating_threshold=4.0,
                         exclude_unknowns=False)

# Instantiate a PMF recommender model.
pmf = PMF(k=10, max_iter=100, learning_rate=0.001, lambda_reg=0.001)

# Instantiate evaluation metrics.
mae = cornac.metrics.MAE()
rmse = cornac.metrics.RMSE()
rec_20 = cornac.metrics.Recall(k=20)
pre_20 = cornac.metrics.Precision(k=20)

# Instantiate and then run an experiment.
cornac.Experiment(
    eval_method=ratio_split,
    models=[pmf],
    metrics=[mae, rmse, rec_20, pre_20],
    user_based=True,
).run()
Exemplo n.º 27
0
# limitations under the License.
# ============================================================================
"""Example for Bayesian Personalized Ranking with Netflix dataset"""

import cornac
from cornac.data import Reader
from cornac.datasets import netflix
from cornac.eval_methods import RatioSplit

ratio_split = RatioSplit(
    data=netflix.load_data_small(reader=Reader(bin_threshold=1.0)),
    test_size=0.1,
    rating_threshold=1.0,
    exclude_unknowns=True,
    verbose=True)

most_pop = cornac.models.MostPop()
bpr = cornac.models.BPR(k=10,
                        max_iter=100,
                        learning_rate=0.001,
                        lambda_reg=0.01,
                        verbose=True)

auc = cornac.metrics.AUC()
rec_20 = cornac.metrics.Recall(k=20)

cornac.Experiment(eval_method=ratio_split,
                  models=[most_pop, bpr],
                  metrics=[auc, rec_20],
                  user_based=True).run()
Exemplo n.º 28
0
    test_size=0.2,
    exclude_unknowns=True,
    rating_threshold=0.5,
    verbose=True,
    seed=123,
    item_text=item_text_modality,
)

# Instantiate CVAE model
cvae = cornac.models.CVAE(
    z_dim=50,
    vae_layers=[200, 100],
    act_fn="sigmoid",
    input_dim=8000,
    lr=0.001,
    batch_size=128,
    n_epochs=100,
    lambda_u=1e-4,
    lambda_v=0.001,
    lambda_r=10,
    lambda_w=1e-4,
    seed=123,
    verbose=True,
)

# Use Recall@300 for evaluation
rec_300 = cornac.metrics.Recall(k=300)

# Put everything together into an experiment and run it
cornac.Experiment(eval_method=ratio_split, models=[cvae], metrics=[rec_300]).run()
Exemplo n.º 29
0
    rating_threshold=0.5,
)

# Instantiate the VAECF model
vaecf = cornac.models.VAECF(
    k=10,
    autoencoder_structure=[20],
    act_fn="tanh",
    likelihood="mult",
    n_epochs=100,
    batch_size=100,
    learning_rate=0.001,
    beta=1.0,
    seed=123,
    use_gpu=True,
    verbose=True,
)

# Instantiate evaluation measures
rec_20 = cornac.metrics.Recall(k=20)
ndcg_20 = cornac.metrics.NDCG(k=20)
auc = cornac.metrics.AUC()

# Put everything together into an experiment and run it
cornac.Experiment(
    eval_method=ratio_split,
    models=[vaecf],
    metrics=[rec_20, ndcg_20, auc],
    user_based=True,
).run()
Exemplo n.º 30
0
                        sentiment=md,
                        seed=123)

# Instantiate the EFM model
efm = cornac.models.EFM(num_explicit_factors=40,
                        num_latent_factors=60,
                        num_most_cared_aspects=15,
                        rating_scale=5.0,
                        alpha=0.85,
                        lambda_x=1,
                        lambda_y=1,
                        lambda_u=0.01,
                        lambda_h=0.01,
                        lambda_v=0.01,
                        max_iter=100,
                        num_threads=1,
                        trainable=True,
                        verbose=True,
                        seed=123)

# Instantiate evaluation metrics
rmse = cornac.metrics.RMSE()
ndcg_50 = cornac.metrics.NDCG(k=50)
auc = cornac.metrics.AUC()

# Put everything together into an experiment and run it
exp = cornac.Experiment(eval_method=split_data,
                        models=[efm],
                        metrics=[rmse, ndcg_50, auc])
exp.run()