def f_eval_epoch(self, eval_data, network, optimizer, logger_obj): loss_list = [] precision_list = [] recall_list = [] F1_list = [] iteration = 0 # self.m_eval_iteration = 0 self.m_eval_iteration = self.m_train_iteration # logger_obj.f_add_output2IO("--"*20) logger_obj.f_add_output2IO(" " * 10 + " eval the user and item encoder" + " " * 10) # logger_obj.f_add_output2IO("--"*20) # exit() network.eval() topk = 3 with torch.no_grad(): for user_batch, item_batch, attr_batch, attr_len_batch, target_batch in eval_data: # for user_batch, item_batch, attr_batch, attr_len_batch, target_len_batch, target_batch in eval_data: user_gpu = user_batch.to(self.m_device) item_gpu = item_batch.to(self.m_device) attr_gpu = attr_batch.to(self.m_device) attr_len_gpu = attr_len_batch.to(self.m_device) target_gpu = target_batch.to(self.m_device) preds = network(user_gpu, item_gpu, attr_gpu, attr_len_gpu) precision, recall, F1 = get_precision_recall_F1(preds.cpu(), target_batch, k=topk) # precision, recall, F1= get_precision_recall_F1_test(preds.cpu(), target_batch, target_len_batch, k=topk) precision_list.append(precision) recall_list.append(recall) F1_list.append(F1) logger_obj.f_add_output2IO( "%d, precision:%.4f, recall:%.4f, F1:%.4f" % (self.m_eval_iteration, np.mean(precision_list), np.mean(recall_list), np.mean(F1_list))) logger_obj.f_add_scalar2tensorboard("eval/precision", np.mean(precision_list), self.m_eval_iteration) logger_obj.f_add_scalar2tensorboard("eval/recall", np.mean(recall_list), self.m_eval_iteration) self.m_mean_eval_loss = 0.0 self.m_mean_eval_precision = np.mean(precision_list) self.m_mean_eval_recall = np.mean(recall_list) self.m_mean_eval_F1 = np.mean(F1_list) network.train()
def f_eval_new(self, train_data, eval_data): batch_index = 0 precision_list = [] recall_list = [] F1_list = [] total_non_one_num = 0 print('--' * 10) # print("user output weight", self.m_network.m_user_output.weight) # print("item output weight", self.m_network.m_item_output.weight) total_num = 0 self.m_network.eval() with torch.no_grad(): topk = 3 for input_target_attr_batch, output_target_attr_batch, output_target_attr_len_batch, user_batch, item_batch in eval_data: # print("==="*20) user_gpu = user_batch.to(self.m_device) item_gpu = item_batch.to(self.m_device) preds = self.m_network.f_eval_forward(user_gpu, item_gpu) precision, recall, F1 = get_precision_recall_F1( preds, output_target_attr_batch, output_target_attr_len_batch, k=topk) precision_list.append(precision) recall_list.append(recall) F1_list.append(F1) total_num += user_batch.size(0) print("total_non_one_num", total_non_one_num) print("total_num", total_num) print("test num", len(precision_list)) mean_precision = np.mean(precision_list) print("precision: ", mean_precision) mean_recall = np.mean(recall_list) print("recall: ", mean_recall) mean_F1 = np.mean(F1_list) print("F1: ", mean_F1)
def f_eval_new(self, train_data, eval_data): batch_index = 0 precision_list = [] recall_list = [] F1_list = [] print('--' * 10) # print("user output weight", self.m_network.m_user_output.weight) # print("item output weight", self.m_network.m_item_output.weight) self.m_network.eval() with torch.no_grad(): topk = 3 for attr_batch, attr_ind_batch, attr_tf_batch, attr_feat_batch, attr_length_batch, attr_length_user_batch, attr_length_item_batch, user_batch, item_batch, target_batch, target_mask_batch in eval_data: attr_gpu = attr_batch.to(self.m_device) attr_ind_gpu = attr_ind_batch.to(self.m_device) attr_tf_gpu = attr_tf_batch.to(self.m_device) attr_feat_gpu = attr_feat_batch.to(self.m_device) attr_length_gpu = attr_length_batch.to(self.m_device) attr_length_user_gpu = attr_length_user_batch.to(self.m_device) user_gpu = user_batch.to(self.m_device) attr_length_item_gpu = attr_length_item_batch.to(self.m_device) item_gpu = item_batch.to(self.m_device) logits = self.m_network.f_eval_forward( attr_gpu, attr_ind_gpu, attr_tf_gpu, attr_feat_gpu, attr_length_gpu, attr_length_user_gpu, attr_length_item_gpu, user_gpu, item_gpu) precision, recall, F1 = get_precision_recall_F1( logits.cpu(), target_batch, target_mask_batch, k=topk) precision_list.append(precision) recall_list.append(recall) F1_list.append(F1) mean_precision = np.mean(precision_list) print("precision: ", mean_precision) mean_recall = np.mean(recall_list) print("recall: ", mean_recall) mean_F1 = np.mean(F1_list) print("F1: ", mean_F1)
def f_eval_train(self, train_data): precision_list = [] recall_list = [] F1_list = [] print('--' * 10) s_time = datetime.datetime.now() topk = 3 self.m_network.eval() with torch.no_grad(): for user_batch, item_batch, attr_batch, attr_len_batch, target_batch, target_len_batch in train_data: user_gpu = user_batch.to(self.m_device) item_gpu = item_batch.to(self.m_device) attr_gpu = attr_batch.to(self.m_device) attr_len_gpu = attr_len_batch.to(self.m_device) target_gpu = target_batch.to(self.m_device) logits = self.m_network(user_gpu, item_gpu, attr_gpu, attr_len_gpu) print("user_gpu", user_gpu) print("item_gpu", item_gpu) print("attr_gpu", attr_gpu) print("preds", torch.topk(logits, topk, -1)[1]) print("target_batch", target_batch) precision, recall, F1 = get_precision_recall_F1(logits.cpu(), target_batch, k=topk) # print("recall%.4f"%recall, end=", ") # loss_list.append(loss.item()) precision_list.append(precision) recall_list.append(recall) F1_list.append(F1) exit() mean_precision = np.mean(precision_list) print("precision:%.4f" % mean_precision) mean_recall = np.mean(recall_list) print("recall:%.4f" % mean_recall) mean_F1 = np.mean(F1_list) print("F1:%.4f" % mean_F1)
def f_eval_new(self, train_data, eval_data): precision_list = [] recall_list = [] F1_list = [] print('--'*10) topk = 3 self.m_network.eval() with torch.no_grad(): for attr_item_batch, attr_tf_item_batch, attr_length_item_batch, item_batch, attr_user_batch, attr_tf_user_batch, attr_length_user_batch, user_batch, target_batch, target_mask_batch in eval_data: attr_item_gpu = attr_item_batch.to(self.m_device) attr_tf_item_gpu = attr_tf_item_batch.to(self.m_device) attr_length_item_gpu = attr_length_item_batch.to(self.m_device) item_gpu = item_batch.to(self.m_device) attr_user_gpu = attr_user_batch.to(self.m_device) attr_tf_user_gpu = attr_tf_user_batch.to(self.m_device) attr_length_user_gpu = attr_length_user_batch.to(self.m_device) user_gpu = user_batch.to(self.m_device) logits = self.m_network.f_eval_forward(attr_item_gpu, attr_tf_item_gpu, attr_length_item_gpu, item_gpu, attr_user_gpu, attr_tf_user_gpu, attr_length_user_gpu, user_gpu) precision, recall, F1= get_precision_recall_F1(logits.cpu(), target_batch, target_mask_batch, k=topk) # if precision != 0 and recall != 0: # loss_list.append(loss.item()) precision_list.append(precision) recall_list.append(recall) F1_list.append(F1) mean_precision = np.mean(precision_list) print("precision: ", mean_precision) mean_recall = np.mean(recall_list) print("recall: ", mean_recall) mean_F1 = np.mean(F1_list) print("F1: ", mean_F1)
def f_eval_epoch(self, eval_data, network, optimizer, logger_obj): loss_list = [] precision_list = [] recall_list = [] F1_list = [] iteration = 0 # self.m_eval_iteration = 0 self.m_eval_iteration = self.m_train_iteration # logger_obj.f_add_output2IO("--"*20) logger_obj.f_add_output2IO(" " * 10 + " eval the user and item encoder" + " " * 10) # logger_obj.f_add_output2IO("--"*20) network.eval() with torch.no_grad(): for attr_batch, attr_ind_batch, attr_tf_batch, attr_feat_batch, attr_length_batch, attr_length_user_batch, attr_length_item_batch, user_batch, item_batch, target_batch, target_mask_batch in eval_data: attr_gpu = attr_batch.to(self.m_device) attr_ind_gpu = attr_ind_batch.to(self.m_device) attr_tf_gpu = attr_tf_batch.to(self.m_device) attr_feat_gpu = attr_feat_batch.to(self.m_device) attr_length_gpu = attr_length_batch.to(self.m_device) attr_length_user_gpu = attr_length_user_batch.to(self.m_device) user_gpu = user_batch.to(self.m_device) attr_length_item_gpu = attr_length_item_batch.to(self.m_device) item_gpu = item_batch.to(self.m_device) logits = network.f_eval_forward(attr_gpu, attr_ind_gpu, attr_tf_gpu, attr_feat_gpu, attr_length_gpu, attr_length_user_gpu, attr_length_item_gpu, user_gpu, item_gpu) precision, recall, F1 = get_precision_recall_F1( logits.cpu(), target_batch, target_mask_batch, k=3) precision_list.append(precision) recall_list.append(recall) F1_list.append(F1) logger_obj.f_add_output2IO( "%d, precision:%.4f, recall:%.4f, F1:%.4f" % (self.m_eval_iteration, np.mean(precision_list), np.mean(recall_list), np.mean(F1_list))) logger_obj.f_add_scalar2tensorboard("eval/precision", np.mean(precision_list), self.m_eval_iteration) logger_obj.f_add_scalar2tensorboard("eval/recall", np.mean(recall_list), self.m_eval_iteration) self.m_mean_eval_loss = 0.0 self.m_mean_eval_precision = np.mean(precision_list) self.m_mean_eval_recall = np.mean(recall_list) self.m_mean_eval_F1 = np.mean(F1_list) network.train()
def f_eval_epoch(self, eval_data, network, optimizer, logger_obj): loss_list = [] precision_list = [] recall_list = [] F1_list = [] iteration = 0 # self.m_eval_iteration = 0 self.m_eval_iteration = self.m_train_iteration # logger_obj.f_add_output2IO("--"*20) logger_obj.f_add_output2IO(" " * 10 + " eval the user and item encoder" + " " * 10) # logger_obj.f_add_output2IO("--"*20) network.eval() topk = 1 with torch.no_grad(): for user_batch, item_batch, attr_batch, target_batch, target_mask_batch in eval_data: batch_size = user_batch.size(0) user_gpu = user_batch.to(self.m_device) item_gpu = item_batch.to(self.m_device) attr_gpu = attr_batch.to(self.m_device) # target_gpu = target_batch.to(self.m_device) logits = [] for i in range(batch_size): user_i = user_gpu[i] item_i = item_gpu[i] ### attr_num attr_i = attr_gpu[i] ### attr_num*1 attr_i = attr_i.unsqueeze(-1) user_i = user_i.repeat(attr_i.size(0)).unsqueeze(-1) item_i = item_i.repeat(attr_i.size(0)).unsqueeze(-1) ### attr_num*1 logits_i = network(user_i, item_i, attr_i) ### logits_i: 1*attr_num logits_i = logits_i.reshape(1, -1) logits.append(logits_i) ### logits: batch_size*attr_num logits = torch.cat(logits, dim=0) precision, recall, F1 = get_precision_recall_F1( logits.cpu(), target_batch, target_mask_batch, k=topk) precision_list.append(precision) recall_list.append(recall) F1_list.append(F1) logger_obj.f_add_output2IO( "%d, precision:%.4f, recall:%.4f, F1:%.4f" % (self.m_eval_iteration, np.mean(precision_list), np.mean(recall_list), np.mean(F1_list))) logger_obj.f_add_scalar2tensorboard("eval/precision", np.mean(precision_list), self.m_eval_iteration) logger_obj.f_add_scalar2tensorboard("eval/recall", np.mean(recall_list), self.m_eval_iteration) self.m_mean_eval_loss = 0.0 self.m_mean_eval_precision = np.mean(precision_list) self.m_mean_eval_recall = np.mean(recall_list) self.m_mean_eval_F1 = np.mean(F1_list) network.train()
def f_train_epoch(self, train_data, network, optimizer, logger_obj): loss_list = [] precision_list = [] recall_list = [] F1_list = [] iteration = 0 # logger_obj.f_add_output2IO("--"*20) logger_obj.f_add_output2IO(" " * 10 + "training the user and item encoder" + " " * 10) # logger_obj.f_add_output2IO("--"*20) tmp_loss_list = [] tmp_precision_list = [] tmp_recall_list = [] tmp_F1_list = [] network.train() topk = 3 for user_batch, item_batch, attr_batch, attr_len_batch, target_batch in train_data: user_gpu = user_batch.to(self.m_device) item_gpu = item_batch.to(self.m_device) attr_gpu = attr_batch.to(self.m_device) attr_len_gpu = attr_len_batch.to(self.m_device) target_gpu = target_batch.to(self.m_device) logits = network(user_gpu, item_gpu, attr_gpu, attr_len_gpu) NLL_loss = self.m_rec_loss(logits, target_gpu) loss = NLL_loss # precision = 1.0 # recall = 1.0 # F1 = 1.0 precision, recall, F1 = get_precision_recall_F1(logits.cpu(), target_batch, k=topk) loss_list.append(loss.item()) precision_list.append(precision) recall_list.append(recall) F1_list.append(F1) tmp_loss_list.append(loss.item()) tmp_precision_list.append(precision) tmp_recall_list.append(recall) tmp_F1_list.append(F1) optimizer.zero_grad() loss.backward() optimizer.step() self.m_train_iteration += 1 iteration += 1 if iteration % self.m_print_interval == 0: logger_obj.f_add_output2IO( "%d, NLL_loss:%.4f, precision:%.4f, recall:%.4f, F1:%.4f" % (iteration, np.mean(tmp_loss_list), np.mean(tmp_precision_list), np.mean(tmp_recall_list), np.mean(tmp_F1_list))) tmp_loss_list = [] tmp_precision_list = [] tmp_recall_list = [] tmp_F1_list = [] logger_obj.f_add_output2IO( "%d, NLL_loss:%.4f, precision:%.4f, recall:%.4f, F1:%.4f" % (self.m_train_iteration, np.mean(loss_list), np.mean(precision_list), np.mean(recall_list), np.mean(F1_list))) logger_obj.f_add_scalar2tensorboard("train/loss", np.mean(loss_list), self.m_train_iteration) logger_obj.f_add_scalar2tensorboard("train/precision", np.mean(precision_list), self.m_train_iteration) logger_obj.f_add_scalar2tensorboard("train/recall", np.mean(recall_list), self.m_train_iteration) self.m_mean_train_loss = np.mean(loss_list) self.m_mean_train_precision = np.mean(precision_list) self.m_mean_train_recall = np.mean(recall_list) self.m_mean_train_F1 = np.mean(F1_list)
def f_eval_epoch(self, eval_data, network, optimizer, logger_obj): loss_list = [] precision_list = [] recall_list = [] F1_list = [] iteration = 0 # self.m_eval_iteration = 0 self.m_eval_iteration = self.m_train_iteration # logger_obj.f_add_output2IO("--"*20) logger_obj.f_add_output2IO(" " * 10 + " eval the user and item encoder" + " " * 10) # logger_obj.f_add_output2IO("--"*20) network.eval() topk = 3 with torch.no_grad(): for input_target_attr_batch, output_target_attr_batch, output_target_attr_len_batch, user_batch, item_batch in eval_data: user_gpu = user_batch.to(self.m_device) item_gpu = item_batch.to(self.m_device) preds = network.f_eval_forward(user_gpu, item_gpu) # print("pos_attr_set_gpu", pos_attr_set_gpu) precision, recall, F1 = get_precision_recall_F1( preds, output_target_attr_batch, output_target_attr_len_batch, k=topk) # loss_list.append(loss.item()) loss_list.append(1.0) precision_list.append(precision) recall_list.append(recall) F1_list.append(F1) logger_obj.f_add_output2IO( "%d, precision:%.4f, recall:%.4f, F1:%.4f" % (self.m_eval_iteration, np.mean(precision_list), np.mean(recall_list), np.mean(F1_list))) logger_obj.f_add_output2IO( "%d, loss:%.4f" % (self.m_eval_iteration, np.mean(loss_list))) logger_obj.f_add_scalar2tensorboard("eval/precision", np.mean(precision_list), self.m_eval_iteration) logger_obj.f_add_scalar2tensorboard("eval/recall", np.mean(recall_list), self.m_eval_iteration) self.m_mean_eval_loss = np.mean(loss_list) self.m_mean_eval_precision = np.mean(precision_list) self.m_mean_eval_recall = np.mean(recall_list) self.m_mean_eval_F1 = np.mean(F1_list) network.train()
def f_eval_epoch(self, eval_data, network, optimizer, logger_obj): loss_list = [] precision_list = [] recall_list = [] F1_list = [] iteration = 0 # self.m_eval_iteration = 0 self.m_eval_iteration = self.m_train_iteration # logger_obj.f_add_output2IO("--"*20) logger_obj.f_add_output2IO(" " * 10 + " eval the user and item encoder" + " " * 10) # logger_obj.f_add_output2IO("--"*20) network.eval() with torch.no_grad(): for item_batch, user_batch, target_batch, target_mask_batch in eval_data: item_gpu = item_batch.to(self.m_device) user_gpu = user_batch.to(self.m_device) logits = network.f_eval_forward(item_gpu, user_gpu) # output = network(None, None, None, item_gpu, None, None, None, user_gpu) # logits, mask, targets = network.f_pred_forward(output, pos_target_gpu, pos_length_gpu, neg_target_gpu, neg_length_gpu) # NLL_loss = self.m_rec_loss(logits, targets, mask) # loss = NLL_loss precision, recall, F1 = get_precision_recall_F1( logits.cpu(), target_batch, target_mask_batch, k=3) if precision != 0 and recall != 0: # loss_list.append(loss.item()) precision_list.append(precision) recall_list.append(recall) F1_list.append(F1) # logger_obj.f_add_output2IO("%d, NLL_loss:%.4f, precision:%.4f, recall:%.4f"%(self.m_eval_iteration, np.mean(loss_list), np.mean(precision_list), np.mean(recall_list))) logger_obj.f_add_output2IO( "%d, precision:%.4f, recall:%.4f, F1:%.4f" % (self.m_eval_iteration, np.mean(precision_list), np.mean(recall_list), np.mean(F1_list))) # logger_obj.f_add_scalar2tensorboard("eval/loss", np.mean(loss_list), self.m_eval_iteration) logger_obj.f_add_scalar2tensorboard("eval/precision", np.mean(precision_list), self.m_eval_iteration) logger_obj.f_add_scalar2tensorboard("eval/recall", np.mean(recall_list), self.m_eval_iteration) logger_obj.f_add_scalar2tensorboard("eval/F1", np.mean(F1_list), self.m_eval_iteration) self.m_mean_eval_loss = 0.0 # self.m_mean_eval_loss = np.mean(loss_list) self.m_mean_eval_precision = np.mean(precision_list) self.m_mean_eval_recall = np.mean(recall_list) self.m_mean_eval_F1 = np.mean(F1_list) network.train()
def f_eval_epoch(self, eval_data, network, optimizer, logger_obj, epoch_index): loss_list = [] precision_list = [] recall_list = [] F1_list = [] iteration = 0 # self.m_eval_iteration = 0 self.m_eval_iteration = self.m_train_iteration # logger_obj.f_add_output2IO("--"*20) logger_obj.f_add_output2IO(" " * 10 + " eval the user and item encoder" + " " * 10) # logger_obj.f_add_output2IO("--"*20) network.eval() topk = 3 alpha = 0.5 gamma = 0.001 with torch.no_grad(): for user_batch, item_batch, y_batch, y_mask_batch in eval_data: user_gpu = user_batch.to(self.m_device) item_gpu = item_batch.to(self.m_device) y_gpu = y_batch.to(self.m_device) y_mask_gpu = y_mask_batch.to(self.m_device) logits = network.f_eval_forward(user_gpu, item_gpu, epoch_index) # rec_loss = self.m_rec_loss(logits, y_gpu) # embed_loss = self.m_embed_loss(enc_x, enc_y) # norm_loss = self.m_norm_loss(network) # loss = rec_loss+alpha*embed_loss+gamma*norm_loss precision, recall, F1 = get_precision_recall_F1(logits.cpu(), y_batch, y_mask_batch, k=topk) precision_list.append(precision) recall_list.append(recall) F1_list.append(F1) logger_obj.f_add_output2IO( "%d, precision:%.4f, recall:%.4f, F1:%.4f" % (self.m_eval_iteration, np.mean(precision_list), np.mean(recall_list), np.mean(F1_list))) logger_obj.f_add_scalar2tensorboard("eval/precision", np.mean(precision_list), self.m_eval_iteration) logger_obj.f_add_scalar2tensorboard("eval/recall", np.mean(recall_list), self.m_eval_iteration) self.m_mean_eval_loss = 0.0 self.m_mean_eval_precision = np.mean(precision_list) self.m_mean_eval_recall = np.mean(recall_list) self.m_mean_eval_F1 = np.mean(F1_list) network.train()