# str_item+=str(round(i5_mean.item(),7))+' ' # str_item+=str(round(i5_var.item(),7))+' ' # str_item+=str(round(i6_mean.item(),7))+' ' # str_item+=str(round(i6_var.item(),7))+' ' str_item += str(round(i_mean.item(), 7)) + ' ' str_item += str(round(i_var.item(), 7)) + ' ' print(str_user) print(str_item) return gcn_users_embedding, gcn_items_embedding, str_user, str_item test_batch = 52 #int(batch_size/32) testing_dataset = data_utils.resData(train_dict=testing_user_set, batch_size=test_batch, num_item=item_num, all_pos=training_user_set) testing_loader = DataLoader(testing_dataset, batch_size=1, shuffle=False, num_workers=0) model = BPR(user_num, item_num, factor_num, sparse_u_i, sparse_i_u, d_i_train, d_j_train) model = model.to('cuda') optimizer_bpr = torch.optim.Adam(model.parameters(), lr=0.001) #, betas=(0.5, 0.99)) ########################### TRAINING ##################################### # testing_loader_loss.dataset.ng_sample()
def test_amazons(args): print("start testing") dataset_base_path = '../../data/AmazonBooks/amazonbooks_x0' ##gowalla user_num = 52643 item_num = 91599 factor_num = args.embed_size batch_size = 2048 * 512 top_k = 20 num_negative_test_val = -1 ##all start_i_test = 130 end_i_test = 200 setp = 5 run_id = args.run_id print(run_id) dataset = 'amazon-book' path_save_base = './log/' + dataset + '/newloss' + run_id if (os.path.exists(path_save_base)): print('has results save path') else: print('error') pdb.set_trace() result_file = open(path_save_base + '/results_hdcg_hr.txt', 'a') #('./log/results_gcmc.txt','w+') copyfile('../../external/LR_gccf/test_amazons.py', path_save_base + '/test_amazos' + run_id + '.py') path_save_model_base = './newlossModel/' + dataset + '/s' + run_id if (os.path.exists(path_save_model_base)): print('has model save path') else: pdb.set_trace() training_user_set, training_item_set, training_set_count = np.load( dataset_base_path + '/datanpy/training_set.npy', allow_pickle=True) testing_user_set, testing_item_set, testing_set_count = np.load( dataset_base_path + '/datanpy/testing_set.npy', allow_pickle=True) user_rating_set_all = np.load(dataset_base_path + '/datanpy/user_rating_set_all.npy', allow_pickle=True).item() u_d = readD(training_user_set, user_num) i_d = readD(training_item_set, item_num) d_i_train = u_d d_j_train = i_d sparse_u_i = readTrainSparseMatrix(training_user_set, True, u_d, i_d) sparse_i_u = readTrainSparseMatrix(training_item_set, False, u_d, i_d) test_batch = 52 #int(batch_size/32) testing_dataset = data_utils.resData(train_dict=testing_user_set, batch_size=test_batch, num_item=item_num, all_pos=training_user_set) testing_loader = DataLoader(testing_dataset, batch_size=1, shuffle=False, num_workers=0) model = BPR(user_num, item_num, factor_num, sparse_u_i, sparse_i_u, d_i_train, d_j_train) model = model.to('cuda') # lr = 0.001 optimizer_bpr = torch.optim.Adam(model.parameters(), lr=args.lr) #, betas=(0.5, 0.99)) print('--------test processing-------') count, best_hr = 0, 0 for epoch in range(start_i_test, end_i_test, setp): model.train() PATH_model = path_save_model_base + '/epoch' + str(epoch) + '.pt' #torch.save(model.state_dict(), PATH_model) model.load_state_dict(torch.load(PATH_model)) model.eval() # ######test and val########### gcn_users_embedding, gcn_items_embedding, gcn_user_emb, gcn_item_emb = model( torch.cuda.LongTensor([0]), torch.cuda.LongTensor([0]), torch.cuda.LongTensor([0])) user_e = gcn_users_embedding.cpu().detach().numpy() item_e = gcn_items_embedding.cpu().detach().numpy() all_pre = np.matmul(user_e, item_e.T) HR, NDCG, RC = [], [], [] set_all = set(range(item_num)) #spend 461s test_start_time = time.time() for u_i in testing_user_set: item_i_list = list(testing_user_set[u_i]) index_end_i = len(item_i_list) item_j_list = list(set_all - training_user_set[u_i] - testing_user_set[u_i]) item_i_list.extend(item_j_list) pre_one = all_pre[u_i][item_i_list] indices = largest_indices(pre_one, top_k) indices = list(indices[0]) hr_t, ndcg_t, rc_t = evaluate.hr_ndcg(indices, index_end_i, top_k) elapsed_time = time.time() - test_start_time HR.append(hr_t) NDCG.append(ndcg_t) RC.append(rc_t) hr_test = round(np.mean(HR), 4) ndcg_test = round(np.mean(NDCG), 4) rc_test = round(np.mean(RC), 4) # test_loss,hr_test,ndcg_test = evaluate.metrics(model,testing_loader,top_k,num_negative_test_val,batch_size) str_print_evl = "epoch:" + str(epoch) + 'time:' + str( round(elapsed_time, 2)) + "\t test" + " hit:" + str(hr_test) + ' ndcg:' + str( ndcg_test) + ' recall:' + str(rc_test) print(str_print_evl) result_file.write(gcn_user_emb) result_file.write('\n') result_file.write(gcn_item_emb) result_file.write('\n') result_file.write(str_print_evl) result_file.write('\n') result_file.flush()