def evaluate(model: nn.Module, loader: dgl.dataloading.NodeDataLoader, loss_func: nn.Module, sampled_edge_type: str, device: str, mode: str): """ :param model: model :param loader: data loader (validate or test) :param loss_func: loss function :param sampled_edge_type: str :param device: device str :param mode: str, evaluation mode, validate or test :return: """ model.eval() with torch.no_grad(): y_trues = [] y_predicts = [] total_loss = 0.0 loader_tqdm = tqdm(loader, ncols=120) for batch, (input_nodes, positive_graph, negative_graph, blocks) in enumerate(loader_tqdm): blocks = [convert_to_gpu(b, device=device) for b in blocks] positive_graph, negative_graph = convert_to_gpu(positive_graph, negative_graph, device=device) # target node relation representation in the heterogeneous graph input_features = { (stype, etype, dtype): blocks[0].srcnodes[dtype].data['feat'] for stype, etype, dtype in blocks[0].canonical_etypes } nodes_representation, _ = model[0](blocks, copy.deepcopy(input_features)) positive_score = model[1](positive_graph, nodes_representation, sampled_edge_type).squeeze(dim=-1) negative_score = model[1](negative_graph, nodes_representation, sampled_edge_type).squeeze(dim=-1) y_predict = torch.cat([positive_score, negative_score], dim=0) y_true = torch.cat([ torch.ones_like(positive_score), torch.zeros_like(negative_score) ], dim=0) loss = loss_func(y_predict, y_true) total_loss += loss.item() y_trues.append(y_true.detach().cpu()) y_predicts.append(y_predict.detach().cpu()) loader_tqdm.set_description( f'{mode} for the {batch}-th batch, {mode} loss: {loss.item()}') total_loss /= (batch + 1) y_trues = torch.cat(y_trues, dim=0) y_predicts = torch.cat(y_predicts, dim=0) return total_loss, y_trues, y_predicts
def evaluate(model: nn.Module, loader: dgl.dataloading.NodeDataLoader, loss_func: nn.Module, labels: torch.Tensor, predict_category: str, device: str, mode: str): """ :param model: model :param loader: data loader (validate or test) :param loss_func: loss function :param labels: node labels :param predict_category: str :param device: device str :param mode: str, evaluation mode, validate or test :return: """ model.eval() with torch.no_grad(): y_trues = [] y_predicts = [] total_loss = 0.0 loader_tqdm = tqdm(loader, ncols=120) for batch, (input_nodes, output_nodes, blocks) in enumerate(loader_tqdm): blocks = [convert_to_gpu(b, device=device) for b in blocks] # target node relation representation in the heterogeneous graph input_features = { (stype, etype, dtype): blocks[0].srcnodes[dtype].data['feat'] for stype, etype, dtype in blocks[0].canonical_etypes } nodes_representation, _ = model[0](blocks, copy.deepcopy(input_features)) y_predict = model[1](nodes_representation[predict_category]) # Tensor, (samples_num, ) y_true = convert_to_gpu(labels[output_nodes[predict_category]], device=device) loss = loss_func(y_predict, y_true) total_loss += loss.item() y_trues.append(y_true.detach().cpu()) y_predicts.append(y_predict.detach().cpu()) loader_tqdm.set_description( f'{mode} for the {batch}-th batch, {mode} loss: {loss.item()}') total_loss /= (batch + 1) y_trues = torch.cat(y_trues, dim=0) y_predicts = torch.cat(y_predicts, dim=0) return total_loss, y_trues, y_predicts
for ntype in graph.ntypes }, hidden_dim=args['hidden_units'], relation_input_dim=args['relation_hidden_units'], relation_hidden_dim=args['relation_hidden_units'], num_layers=args['n_layers'], n_heads=args['num_heads'], dropout=args['dropout'], residual=args['residual']) classifier = Classifier(n_hid=args['hidden_units'] * args['num_heads'], n_out=num_classes) model = nn.Sequential(r_hgnn, classifier) model = convert_to_gpu(model, device=args['device']) print(model) print(f'Model #Params: {get_n_params(model)}.') print(f'configuration is {args}') save_model_path = f"../save_model/{args['dataset']}/{args['model_name']}/{args['model_name']}.pkl" # load model parameter model.load_state_dict(torch.load(save_model_path, map_location='cpu')) # evaluate the best model model.eval() nodes_representation, _ = model[0].inference(
graph, labels, num_classes, train_idx, valid_idx, test_idx = load_dataset(data_path=args['data_path'], predict_category=args['predict_category'], data_split_idx_path=args[ 'data_split_idx_path']) hgconv = HGConv(graph=graph, input_dim_dict={ntype: graph.nodes[ntype].data['feat'].shape[1] for ntype in graph.ntypes}, hidden_dim=args['hidden_units'], num_layers=args['n_layers'], n_heads=args['num_heads'], dropout=args['dropout'], residual=args['residual']) classifier = Classifier(n_hid=args['hidden_units'] * args['num_heads'], n_out=num_classes) model = nn.Sequential(hgconv, classifier) model = convert_to_gpu(model, device=args['device']) print(model) print(f'Model #Params: {get_n_params(model)}') print(f'configuration is {args}') save_model_path = f"../save_model/{args['dataset']}/{args['model_name']}/{args['model_name']}.pkl" # load model parameter model.load_state_dict(torch.load(save_model_path, map_location='cpu')) print('performing model inference for test data...') # evaluate the best model model.eval()
for ntype in graph.ntypes }, hidden_dim=args['hidden_units'], relation_input_dim=args['relation_hidden_units'], relation_hidden_dim=args['relation_hidden_units'], num_layers=args['n_layers'], n_heads=args['num_heads'], dropout=args['dropout'], residual=args['residual']) classifier = Classifier(n_hid=args['hidden_units'] * args['num_heads'], n_out=num_classes) model = nn.Sequential(r_hgnn, classifier) model = convert_to_gpu(model, device=args['device']) print(model) print(f'Model #Params: {get_n_params(model)}.') print(f'configuration is {args}') optimizer, scheduler = get_optimizer_and_lr_scheduler( model, args['optimizer'], args['learning_rate'], args['weight_decay'], steps_per_epoch=len(train_loader), epochs=args['epochs']) save_model_folder = f"../save_model/{args['dataset']}/{args['model_name']}"
}, hidden_dim=args['hidden_units'], relation_input_dim=args['relation_hidden_units'], relation_hidden_dim=args['relation_hidden_units'], num_layers=args['n_layers'], n_heads=args['num_heads'], dropout=args['dropout'], residual=args['residual'], norm=args['norm']) link_score_predictor = LinkScorePredictor(hid_dim=args['hidden_units'] * args['num_heads']) model = nn.Sequential(r_hgnn, link_score_predictor) model = convert_to_gpu(model, device=args['device']) print(model) print(f'Model #Params: {get_n_params(model)}.') print(f'configuration is {args}') optimizer, scheduler = get_optimizer_and_lr_scheduler( model, args['optimizer'], args['learning_rate'], args['weight_decay'], steps_per_epoch=len(train_loader), epochs=args['epochs']) save_model_folder = f"../save_model/{args['dataset']}/{args['model_name']}"