| 39 | |
| 40 | |
| 41 | class PrepareData: |
| 42 | def __init__(self, args): |
| 43 | print("prepare data") |
| 44 | self.graph_path = "data/graph" |
| 45 | self.args = args |
| 46 | |
| 47 | # graph |
| 48 | graph = nx.read_weighted_edgelist(f"{self.graph_path}/{args.dataset}.txt" |
| 49 | , nodetype=int) |
| 50 | print_graph_detail(graph) |
| 51 | adj = nx.to_scipy_sparse_matrix(graph, |
| 52 | nodelist=list(range(graph.number_of_nodes())), |
| 53 | weight='weight', |
| 54 | dtype=np.float) |
| 55 | |
| 56 | adj = adj + adj.T.multiply(adj.T > adj) - adj.multiply(adj.T > adj) |
| 57 | |
| 58 | self.adj = preprocess_adj(adj, is_sparse=True) |
| 59 | |
| 60 | # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
| 61 | |
| 62 | # features |
| 63 | self.nfeat_dim = graph.number_of_nodes() |
| 64 | row = list(range(self.nfeat_dim)) |
| 65 | col = list(range(self.nfeat_dim)) |
| 66 | value = [1.] * self.nfeat_dim |
| 67 | shape = (self.nfeat_dim, self.nfeat_dim) |
| 68 | indices = th.from_numpy( |
| 69 | np.vstack((row, col)).astype(np.int64)) |
| 70 | values = th.FloatTensor(value) |
| 71 | shape = th.Size(shape) |
| 72 | |
| 73 | self.features = th.sparse.FloatTensor(indices, values, shape) |
| 74 | |
| 75 | # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
| 76 | # target |
| 77 | |
| 78 | target_fn = f"data/text_dataset/{self.args.dataset}.txt" |
| 79 | target = np.array(pd.read_csv(target_fn, |
| 80 | sep="\t", |
| 81 | header=None)[2]) |
| 82 | target2id = {label: indx for indx, label in enumerate(set(target))} |
| 83 | self.target = [target2id[label] for label in target] |
| 84 | self.nclass = len(target2id) |
| 85 | |
| 86 | # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
| 87 | # train val test split |
| 88 | |
| 89 | self.train_lst, self.test_lst = get_train_test(target_fn) |
| 90 | |
| 91 | |
| 92 | class TextGCNTrainer: |