def run(): args = NGCF_Args(parse_args()) # 获取训练集的dataloader形式 data = DATA(args.data_path, args.dataset_name) train_set, train_U2I, test_U2I, edge_indices, edge_weight, n_users, n_items = data.load( ) train_dl = get_loader(train_set, train_U2I, n_items, args.batch_size, args.cores) # 获取归一化的拉普拉斯矩阵 laplace_graph = Graph(edge_indices, edge_weight) laplace_graph.add_self_loop() laplace_graph.norm() norm_adj = laplace_graph.mat.cuda() # 定义网络 model = NGCF(n_users, n_items, norm_adj, args) model = model.cuda() optimizer = optim.Adam(model.parameters(), lr=args.lr) # 定义会话 sess = Session(model) for epoch in range(args.num_epochs): loss, mf_loss, emb_loss = sess.train(train_dl, optimizer) print("epoch: {:d}, loss = [{:.6f} == {:.6f} + {:.6f}]".format( epoch, loss, mf_loss, emb_loss)) perf_info = evaluate(model, n_users, n_items, train_U2I, test_U2I, args) print("precision: [{:.6f}] recall: [{:.6f}] ndcg: [{:.6f}]".format( perf_info[0], perf_info[1], perf_info[2]))
def run(): args = BPRMF_Args(parse_args()) # 获取训练集的dataloader形式 data = DATA(args.data_path, args.dataset_name) train_set, train_U2I, test_U2I, n_users, n_items = data.load() train_dl = get_loader(train_set, train_U2I, n_items, args.batch_size, args.cores) # 定义网络 model = BPRMF(n_users, n_items, args) model = model.cuda() optimizer = optim.Adam(model.parameters(), lr=args.lr) # 定义会话 sess = Session(model) for epoch in range(args.num_epochs): loss, mf_loss, emb_loss = sess.train(train_dl, optimizer) print("epoch: {:d}, loss = [{:.6f} == {:.6f} + {:.6f}]".format( epoch, loss, mf_loss, emb_loss)) perf_info = evaluate(model, n_users, n_items, train_U2I, test_U2I, args) print("precision: [{:.6f}] recall: [{:.6f}] ndcg: [{:.6f}]".format( perf_info[0], perf_info[1], perf_info[2]))
def build_args(self): if self.model_name in ['kgat', 'cfkg', 'nfm', 'cke']: from utility.parser import parse_args elif self.model_name.startswith('EKGCN'): from main import parse_args elif self.model_name == 'ripple': from ripple_main import parse_args args = parse_args() if self.arg_file is not None: args = load_args(self.arg_file, args) return args
def run(): parser = parse_args() d = Movielens(parser.data_path, parser.threshold) train_dl, test_dl = get_Dataloader(d.train_set, d.test_set, parser.batch_size, parser.cores) model = FactorMachine(d.sparse_n_features, d.dense_n_features, parser.n_classes, parser.n_factors, parser.batch_size, parser.decay) model = model.cuda() optimizer = optim.Adam(model.parameters(), lr=parser.lr) sess = Session(model, d.max_user_id, d.max_item_id) for epoch in range(parser.num_epoch): loss, cross_loss, l2_loss = sess.train(train_dl, optimizer) print("epoch: {:d}, loss = [{:.6f} == {:.6f} + {:.6f}]".format( epoch, loss, cross_loss, l2_loss)) loss, cross_loss, l2_loss, auc = sess.test(test_dl) print("loss = [{:.6f} == {:.6f} + {:.6f}], auc = [{:.6f}]".format( loss, cross_loss, l2_loss, auc)) print('\n')
from utility.parser import parse_args from utility.load_data import * import subprocess import torch args = parse_args() data_generator = Data(path=args.data_path + args.dataset, batch_size=args.batch_size) doc_dict, doc_dict_rev = data_generator.doc_dict, data_generator.doc_dict_rev qrl_dict, qrl_dict_rev = data_generator.qrl_dict, data_generator.qrl_dict_rev qrelf = args.data_path + args.dataset + '/qrels' def pad_sequences(items, maxlen, value=data_generator.n_words): result = [] for item in items: if len(item) < maxlen: item = item + [value] * (maxlen - len(item)) if len(item) > maxlen: item = item[:maxlen] result.append(item) return result def words_lookup(docs): if args.model == 'GHRM': return [data_generator.doc_unqiue_word_list[i] for i in docs] else: return [data_generator.doc_word_list[i] for i in docs]
''' Created on Oct 10, 2018 Tensorflow Implementation of Neural Graph Collaborative Filtering (NGCF) model in: Wang Xiang et al. Neural Graph Collaborative Filtering. In SIGIR 2019. @author: Xiang Wang ([email protected]) ''' import utility.metrics as metrics from utility.parser import parse_args from utility.load_data import * import multiprocessing import heapq cores = multiprocessing.cpu_count() // 2 args = parse_args() # It can be called in other scripts Ks = eval(args.Ks) # transfer string into dict, tuple and list data_generator = Data(path=args.data_path + args.dataset, batch_size=args.batch_size) USR_NUM, ITEM_NUM = data_generator.n_users, data_generator.n_items N_TRAIN, N_TEST = data_generator.n_train, data_generator.n_test BATCH_SIZE = args.batch_size def ranklist_by_heapq(user_pos_test, test_items, rating, Ks): item_score = {} for i in test_items: item_score[i] = rating[i] K_max = max(Ks)