# coding=utf-8 import os from tf_geometric.utils.graph_utils import RandomNeighborSampler os.environ["CUDA_VISIBLE_DEVICES"] = "0" from tf_geometric.datasets import CoraDataset import time graph, (train_index, valid_index, test_index) = CoraDataset().load_data() neighbor_sampler = RandomNeighborSampler(graph.edge_index) for _ in range(100): start = time.time() print(neighbor_sampler.sample(ratio=0.5)) print(time.time() - start)
os.environ["CUDA_VISIBLE_DEVICES"] = "1" import tf_geometric as tfg from tf_geometric.datasets.ppi import PPIDataset from tf_geometric.utils.graph_utils import RandomNeighborSampler import tensorflow as tf from tensorflow import keras import numpy as np from sklearn.metrics import f1_score from tqdm import tqdm train_graphs, valid_graphs, test_graphs = PPIDataset().load_data() # traverse all graphs for graph in train_graphs + valid_graphs + test_graphs: neighbor_sampler = RandomNeighborSampler(graph.edge_index) graph.cache["sampler"] = neighbor_sampler num_classes = train_graphs[0].y.shape[1] graph_sages = [ tfg.layers.MaxPoolGraphSage(units=128, activation=tf.nn.relu), tfg.layers.MaxPoolGraphSage(units=128, activation=tf.nn.relu) ] fc = tf.keras.Sequential( [keras.layers.Dropout(0.3), tf.keras.layers.Dense(num_classes)]) num_sampled_neighbors_list = [25, 10]
# coding=utf-8 import os os.environ["CUDA_VISIBLE_DEVICES"] = "0" from tf_geometric.utils.graph_utils import reindex_sampled_edge_index, RandomNeighborSampler from tf_geometric.datasets import CoraDataset import time graph, (train_index, valid_index, test_index) = CoraDataset().load_data() neighbor_sampler = RandomNeighborSampler(graph.edge_index) for _ in range(100): start = time.time() sampled_edge_index, sampled_edge_weight = neighbor_sampler.sample(ratio=0.5) print(sampled_edge_index, sampled_edge_weight) print(time.time() - start) for _ in range(100): start = time.time() print("sample for central nodes: ") central_node_index = [0, 1, 5, 24] (sampled_edge_index, sampled_edge_weight), (sampled_node_index, central_node_index, non_central_node_index) = \ neighbor_sampler.sample(ratio=0.5, central_node_index=central_node_index) print("sampled_node_index: ", sampled_node_index) print("central_node_index: ", central_node_index) print("non_central_node_index: ", non_central_node_index) print("sampled_edge_index: \n", sampled_edge_index) print("sampled_edge_weight: ", sampled_edge_weight) print("reindex sampled nodes and edges to construct edges for a subgraph: ") reindexed_edge_index = reindex_sampled_edge_index(sampled_edge_index, sampled_node_index) print("reindexed_edge_index: \n", reindexed_edge_index)