<jupyter_start><jupyter_text>Inductive Predictionの評価ランダム性の排除のために，複数のモデルをトレーニングしてその平均を算出<jupyter_code>from sgcn import SignedGCNTrainer, SignedGCNPredictor
from parser import parameter_parser
from utils import tab_printer, read_graph, score_printer, save_logs
import easydict
import argparse
import pandas as pd
import numpy as np
import torch
import json
import networkx as nx
from sklearn.metrics import roc_auc_score, roc_curve, auc
from sklearn.metrics import accuracy_score,confusion_matrix
from sklearn.model_selection import train_test_split
from IPython.display import display
import matplotlib.pyplot as plt
%matplotlib inline<jupyter_output><empty_output><jupyter_text>まとめて学習<jupyter_code>def train_all(data_name_list,l1_lambda=0.0,l2_lambda=10e-4,iter_num=30):
    for data_name in data_name_list:
        for i in range(iter_num):
            print(f'{i}-th iteration')
            args = easydict.EasyDict({
                    "edge_path": f'../input/{data_name}/{data_name}_network.csv',
                    "features_path":  f'../input/{data_name}/{data_name}_node_feature.csv',
                    "nodes_path": f'../input/{data_name}/{data_name}_gt.csv',
                    "embedding_path": f'../tmp/embedding/{data_name}_sgcn_feature05.pkl', # tmp folder for cross-validation
                    "regression_weights_path": f'../tmp/weights/{data_name}_sgcn_feature05.pkl',
                    "inductive_model_path": f'../output/inductive_tmp/{data_name}_model_l1_{l1_lambda}_l2_{l2_lambda}_{i}th', # or None
                    "log_path": f'../logs/{data_name}_logs_feature05.json',
                    "epochs":300,
                    "test_size":0.2,
                    "reduction_iterations": 128,
                    "reduction_dimensions": 30,
                    "seed": 42,
                    "lamb": 0.0,
                    "learning_rate": 0.001,  
                    "weight_decay": l2_lambda, 
                    # "layers": [64, 32,16,8],
                    "layers": [32, 16, ],
                    "spectral_features":False,
                    "general_features": True,  
                    "sample_num":None,
                    "class_weights":False,
                    "node_under_sampling":False,
                    "hidden_residual":False,
                    "eval_freq":1,
                    "subgraph_training":False,
                    "l1_lambda":l1_lambda,
            })    
            edges, nodes_dict = read_graph(args) # nodes_dict['indice']:node_id , nodes_dict['label'] : label
            print(np.unique(nodes_dict['label'],return_counts=True))
            trainer = SignedGCNTrainer(args, edges, nodes_dict)
            trainer.setup_dataset()
            trainer.create_and_train_model()        
otc_list = ['otc_early0.03','otc_early0.04','otc_early0.05','otc_early0.07','otc_early0.1','otc_early0.15','otc_early0.2',]
train_all(otc_list,l1_lambda=10.0)
alpha_list = ['alpha_early0.04','alpha_early0.05','alpha_early0.07','alpha_early0.1','alpha_early0.15','alpha_early0.2','alpha_early0.3']
train_all(alpha_list,l1_lambda=10.0)
# epinions_list = ['epinions_early0.0005','epinions_early0.001','epinions_early0.005','epinions_early0.01',]

# epinions_list = ['epinions_early0.0007','epinions_early0.003','epinions_early0.007',]
epinions_list = ['epinions_early0.0005','epinions_early0.0007','epinions_early0.001',
                 'epinions_early0.003','epinions_early0.005','epinions_early0.007','epinions_early0.01',]
train_all(epinions_list,l1_lambda=0.0)<jupyter_output><empty_output><jupyter_text>まとめて評価  data loading<jupyter_code>data_name = input('推定対象データセット：')
new_args = easydict.EasyDict({
        "edge_path": f'../input/{data_name}/{data_name}_network.csv',
        "features_path":  f'../input/{data_name}/{data_name}_node_feature.csv',
        "nodes_path": f'../input/{data_name}/{data_name}_gt.csv',
        "embedding_path": f'../tmp/embedding/{data_name}_sgcn_feature05.pkl', # tmp folder for cross-validation
        "regression_weights_path": f'../tmp/weights/{data_name}_sgcn_feature05.pkl',
        "inductive_model_path": f'../output/inductive/{data_name}_model', # or None
        "log_path": f'../logs/{data_name}_logs_feature05.json',
        "epochs":300,
        "test_size":0.33,
        "reduction_iterations": 128,
        "reduction_dimensions": 30,
        "seed": 42,
        "lamb": 0.0,
        "learning_rate": 0.001,  
        "weight_decay": 10e-4, 
        # "layers": [64, 32,16,8],
        "layers": [32, 16, ],
        "spectral_features":False,
        "general_features": True,  
        "sample_num":None,
        "class_weights":False,
        "node_under_sampling":False,
        "hidden_residual":False,
        "eval_freq":1,
        "subgraph_training":False,
        "l1_lambda":0.0,
})
new_edges, new_nodes_dict = read_graph(new_args)

X = np.array(pd.read_csv(f'../input/{data_name}/{data_name}_node_feature.csv')) # general node features
def evaluate_all(trained_model_list,l1_lambda=0.0,l2_lambda=10e-4,iter_num=30):
    result_df = pd.DataFrame()
    bagging_df = pd.DataFrame()
    for training_dataset in trained_model_list:
        auc_scores = []
        bagging_pred_scores = []
        for i in range(iter_num):
            predictor = SignedGCNPredictor(new_args, f'../output/inductive_tmp/{training_dataset}_model_l1_{l1_lambda}_l2_{l2_lambda}_{i}th', 
                                           X, new_edges,new_nodes_dict)
            predictions = predictor.predict()
            trained_node_raw = np.load(f'../input/{training_dataset}/{training_dataset}_label_encoder.npy')
            newly_added_node_judger = ~np.isin(new_nodes_dict['indice'],trained_node_raw)
            y_true = new_nodes_dict['label'][newly_added_node_judger]
            y_score_indice = new_nodes_dict['indice'][newly_added_node_judger]
            bagging_pred_scores.append(predictions[y_score_indice])
            
            current_auc = roc_auc_score(y_true=[1 if i==-1 else 0 for i in y_true],y_score=predictions[y_score_indice])
            auc_scores.append(current_auc)        
        averaged_auc = np.mean(auc_scores)
        bagging_auc = roc_auc_score(y_true=[1 if i==-1 else 0 for i in y_true],y_score=sum(bagging_pred_scores)/iter_num)
        result_df = result_df.append(pd.io.json.json_normalize({training_dataset:averaged_auc}).T)
        bagging_df = bagging_df.append(pd.io.json.json_normalize({training_dataset:bagging_auc}).T)
    return result_df,bagging_df
result_df, bagging_df = evaluate_all(alpha_list,l1_lambda=10.0)

result_df['rate'] = result_df.reset_index()['index'].str.split('.',expand=True)[1].map(lambda x:'0.'+x).values

bagging_df['rate'] = bagging_df.reset_index()['index'].str.split('.',expand=True)[1].map(lambda x:'0.'+x).values
result_df.set_index('rate').T
# bagging_df.set_index('rate').T<jupyter_output><empty_output><jupyter_text>evaluating and averaging<jupyter_code>iter_num = 30
training_dataset = input('学習に使ったデータセット：')
l1_lambda = float(input('l1_lambda:'))

l2_lambda = float(input('l2_lambda:'))
auc_scores = []
for i in range(iter_num):
    predictor = SignedGCNPredictor(new_args, f'../output/inductive_tmp/{training_dataset}_model_l1_{l1_lambda}_l2_{l2_lambda}_{i}th', 
                                   X, new_edges,new_nodes_dict)
    predictions = predictor.predict()
    trained_node_raw = np.load(f'../input/{training_dataset}/{training_dataset}_label_encoder.npy')
    newly_added_node_judger = ~np.isin(new_nodes_dict['indice'],trained_node_raw)
    y_true = new_nodes_dict['label'][newly_added_node_judger]
    y_score_indice = new_nodes_dict['indice'][newly_added_node_judger]

    current_auc = roc_auc_score(y_true=[1 if i==-1 else 0 for i in y_true],y_score=predictions[y_score_indice])
    auc_scores.append(current_auc)
auc_scores
np.mean(auc_scores), np.std(auc_scores)<jupyter_output><empty_output><jupyter_text>個別に学習<jupyter_code>iter_num = 30
data_name = input('データセット：')
l1_lambda = float(input('l1_lambda:'))

l2_lambda = float(input('l2_lambda:'))
for i in range(iter_num):
    print(f'{i}-th iteration')
    args = easydict.EasyDict({
            "edge_path": f'../input/{data_name}/{data_name}_network.csv',
            "features_path":  f'../input/{data_name}/{data_name}_node_feature.csv',
            "nodes_path": f'../input/{data_name}/{data_name}_gt.csv',
            "embedding_path": f'../tmp/embedding/{data_name}_sgcn_feature05.pkl', # tmp folder for cross-validation
            "regression_weights_path": f'../tmp/weights/{data_name}_sgcn_feature05.pkl',
            "inductive_model_path": f'../output/inductive_tmp/{data_name}_model_l1_{l1_lambda}_l2_{l2_lambda}_{i}th', # or None
            "log_path": f'../logs/{data_name}_logs_feature05.json',
            "epochs":300,
            "test_size":0.2,
            "reduction_iterations": 128,
            "reduction_dimensions": 30,
            "seed": 42,
            "lamb": 0.0,
            "learning_rate": 0.001,  
            "weight_decay": l2_lambda, 
            # "layers": [64, 32,16,8],
            "layers": [32, 16, ],
            "spectral_features":False,
            "general_features": True,  
            "sample_num":None,
            "class_weights":False,
            "node_under_sampling":False,
            "hidden_residual":False,
            "eval_freq":1,
            "subgraph_training":False,
            "l1_lambda":l1_lambda,
    })    
    edges, nodes_dict = read_graph(args) # nodes_dict['indice']:node_id , nodes_dict['label'] : label
    print(np.unique(nodes_dict['label'],return_counts=True))
    trainer = SignedGCNTrainer(args, edges, nodes_dict)
    trainer.setup_dataset()
    trainer.create_and_train_model()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13712820512820512, 'fraction_numerical': 0.029743589743589743, 'mean_word_length': 3.24325500435161, 'pattern_counts': {'":': 78, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '477273', 'license': 'MIT', 'n_tokens_mistral': 3624, 'n_tokens_neox': 3404, 'n_words': 407, 'path': 'notebooks/inductive_eval.ipynb', 'repo_name': 'WataruKudo0914/SDGCN', 'size': '9750'}