### Run Supervised GraphSage Source: https://github.com/williamleif/graphsage/blob/master/README.md Example script demonstrating the usage of the supervised variant of the GraphSage algorithm. Use '--sigmoid' for multi-output datasets. ```Shell sh example_supervised.sh ``` -------------------------------- ### Configure GraphSAGE Hyperparameters (Command Line) Source: https://context7.com/williamleif/graphsage/llms.txt Provides an example of how to configure key hyperparameters for GraphSAGE training using command-line arguments. This includes layer sampling, hidden dimensions, learning rate, batch size, epochs, dropout, and model architecture. ```bash # Example flag configuration python -m graphsage.supervised_train \ --train_prefix ./data/my_graph \ --model graphsage_mean \ --samples_1 25 --samples_2 10 \ --dim_1 128 --dim_2 128 \ --learning_rate 0.01 \ --epochs 10 --batch_size 512 \ --dropout 0.1 --max_degree 128 ``` -------------------------------- ### Install Dependencies Source: https://github.com/williamleif/graphsage/blob/master/README.md Installs all required Python packages for GraphSage using pip. Ensure you have networkx version 1.11 or lower. ```Shell $ pip install -r requirements.txt ``` -------------------------------- ### Run Unsupervised GraphSage Source: https://github.com/williamleif/graphsage/blob/master/README.md Example script demonstrating the usage of the unsupervised variant of the GraphSage algorithm. Consider using '--identity_dim' for static graphs. ```Shell sh example_unsupervised.sh ``` -------------------------------- ### Configuration Loading (Python) Source: https://github.com/williamleif/graphsage/blob/master/example_data/toy-ppi-walks.txt This code snippet shows how to load configuration parameters for the GraphSAGE model and training process. This might involve reading from a JSON or YAML file, allowing for easy modification of hyperparameters and settings. ```python import json def load_config(config_path): with open(config_path, 'r') as f: config = json.load(f) return config ``` -------------------------------- ### Build and Run GPU Docker Image Source: https://github.com/williamleif/graphsage/blob/master/README.md Builds a Docker image with GPU support for GraphSage using a specific Dockerfile and runs it using nvidia-docker. ```Shell $ docker build -t graphsage:gpu -f Dockerfile.gpu . $ nvidia-docker run -it graphsage:gpu bash ``` -------------------------------- ### Training Loop Implementation (Python) Source: https://github.com/williamleif/graphsage/blob/master/example_data/toy-ppi-walks.txt This snippet outlines a typical training loop for a GraphSAGE model. It includes data loading, forward and backward passes, and optimizer steps. This is essential for training the model on a given dataset and requires a deep learning framework like PyTorch. ```python def train_graphSAGE(model, data, optimizer, epochs): for epoch in range(epochs): # Get mini-batch of nodes # Sample neighbors # Forward pass # Calculate loss # Backward pass and optimize pass ``` -------------------------------- ### Graph Data Loading and Preprocessing (Python) Source: https://github.com/williamleif/graphsage/blob/master/example_data/toy-ppi-walks.txt This snippet demonstrates how to load and preprocess graph data, likely for use with GraphSAGE models. It includes handling of node features, adjacency lists, and potentially sampling strategies. No external dependencies are explicitly mentioned but common libraries like NumPy or Pandas might be implied. ```python def load_graph_data(data_dir): # Load features, adjacencies, labels, etc. # Preprocess the data into a suitable format for GraphSAGE pass def preprocess_graph(graph): # Perform graph preprocessing steps like normalization or feature engineering pass ``` -------------------------------- ### Data Loading and Preprocessing for GraphSage Source: https://github.com/williamleif/graphsage/blob/master/example_data/toy-ppi-walks.txt This snippet demonstrates common data loading and preprocessing techniques used in graph-based machine learning, likely for GraphSage. It may involve reading graph structures, node features, and labels from various sources. Understanding these steps is crucial for preparing data for graph neural network training. ```python def load_data(path): # Implementation for loading graph data (e.g., from files) # This would include parsing adjacency lists, node features, and labels. graph = nx.read_edgelist(f"{path}/adj.csv", nodetype=int) features = np.load(f"{path}/features.npy") labels = np.load(f"{path}/labels.npy") return graph, features, labels ``` -------------------------------- ### Run GraphSage with Docker Source: https://github.com/williamleif/graphsage/blob/master/README.md Builds and runs a Docker image for GraphSage. This provides a consistent environment for executing the algorithm. ```Shell $ docker build -t graphsage . $ docker run -it graphsage bash ``` -------------------------------- ### GraphSAGE Model Implementation (Python) Source: https://github.com/williamleif/graphsage/blob/master/example_data/toy-ppi-walks.txt This code outlines the core implementation of a GraphSAGE model in Python. It likely involves defining the aggregation functions, update rules, and the overall network architecture. This snippet is central to the GraphSAGE framework and requires libraries like PyTorch or TensorFlow. ```python import torch import torch.nn as nn class GraphSAGE(nn.Module): def __init__(self, feature_dim, hidden_dim, output_dim, aggregator_type='mean'): super(GraphSAGE, self).__init__() # Initialize layers and aggregator pass def forward(self, x, adj): # Implement the forward pass of the GraphSAGE model pass ``` -------------------------------- ### Training Loop for GraphSage in PyTorch Source: https://github.com/williamleif/graphsage/blob/master/example_data/toy-ppi-walks.txt This Python snippet outlines the training loop for a GraphSage model using PyTorch. It details the process of iterating through epochs, calculating loss, performing backpropagation, and updating model weights. This is a fundamental part of training any neural network, adapted here for graph data. ```python def train(model, data, epochs=10): optimizer = torch.optim.Adam(model.parameters(), lr=0.01) criterion = nn.CrossEntropyLoss() for epoch in range(epochs): model.train() optimizer.zero_grad() # Assuming 'data' contains features, labels, and adjacency info features = data['features'] labels = data['labels'] adj = data['adj'] outputs = model(features, adj) loss = criterion(outputs, labels) loss.backward() optimizer.step() print(f'Epoch {epoch+1}, Loss: {loss.item():.4f}') ``` -------------------------------- ### Run GraphSage with Docker (Jupyter) Source: https://github.com/williamleif/graphsage/blob/master/README.md Builds and runs a Docker image for GraphSage, exposing port 8888 to allow access to a Jupyter Notebook environment. ```Shell $ docker run -it -p 8888:8888 graphsage ``` -------------------------------- ### Prepare Graph Data for GraphSAGE (Python) Source: https://context7.com/williamleif/graphsage/llms.txt Prepares input data files in the required format for GraphSAGE training. This involves creating a graph with node attributes, marking validation and test nodes, and saving the graph as JSON, an ID map, a class map, and optionally features as a NumPy array. ```python import json import numpy as np import networkx as nx from networkx.readwrite import json_graph # Create graph with required node attributes G = nx.Graph() for node_id in range(num_nodes): G.add_node(node_id, val=False, # True if validation node test=False) # True if test node # Mark validation and test nodes for node in val_nodes: G.node[node]['val'] = True for node in test_nodes: G.node[node]['test'] = True # Save as JSON G_data = json_graph.node_link_data(G) with open('train-G.json', 'w') as f: json.dump(G_data, f) # Create ID map (node ID to consecutive integers) id_map = {node: i for i, node in enumerate(G.nodes())} with open('train-id_map.json', 'w') as f: json.dump(id_map, f) # Create class map (node ID to label) class_map = {node: label for node, label in zip(nodes, labels)} with open('train-class_map.json', 'w') as f: json.dump(class_map, f) # Save features as numpy array (optional) # Order must match id_map ordering features = np.random.randn(num_nodes, feature_dim) np.save('train-feats.npy', features) ``` -------------------------------- ### Neighbor Sampling for GraphSage Source: https://github.com/williamleif/graphsage/blob/master/example_data/toy-ppi-walks.txt This code snippet illustrates the process of neighbor sampling, a key optimization technique in GraphSage. Efficiently sampling a fixed number of neighbors for each node prevents the computational cost from growing with the graph's degree. This is crucial for scaling GraphSage to large graphs. ```python import random def sample_neighbors(adj, node_id, sample_size): neighbors = adj[node_id] if len(neighbors) > sample_size: return random.sample(neighbors, sample_size) else: return neighbors # Example usage: # Assuming 'adj' is an adjacency list representation of the graph # sampled_neighbors = sample_neighbors(adj, node_id=5, sample_size=10) ``` -------------------------------- ### Training Loop with TensorFlow Session (Python) Source: https://context7.com/williamleif/graphsage/llms.txt Sets up a TensorFlow session and executes the training loop for GraphSAGE models. Includes variable initialization, batch iteration, loss calculation, prediction, and validation steps. Requires a trained model and minibatch iterator. ```python import tensorflow as tf from graphsage.metrics import f1_score config = tf.ConfigProto() config.gpu_options.allow_growth = True sess = tf.Session(config=config) # Initialize variables sess.run(tf.global_variables_initializer(), feed_dict={adj_info_ph: minibatch.adj}) # Training loop for epoch in range(epochs): minibatch.shuffle() while not minibatch.end(): feed_dict, labels = minibatch.next_minibatch_feed_dict() feed_dict.update({placeholders['dropout']: 0.1}) # Training step for supervised model _, loss, preds = sess.run( [model.opt_op, model.loss, model.preds], feed_dict=feed_dict ) if iter_num % 50 == 0: print(f"Iter: {iter_num:04d} loss={loss:.5f}") # Validation after epoch val_feed_dict, val_labels = minibatch.node_val_feed_dict(size=256) val_preds, val_loss = sess.run( [model.preds, model.loss], feed_dict=val_feed_dict ) ``` -------------------------------- ### Graph Sampling Utility (Python) Source: https://github.com/williamleif/graphsage/blob/master/example_data/toy-ppi-walks.txt This code provides utility functions for sampling nodes and their neighbors from a large graph. This is crucial for efficient training of GraphSAGE on massive datasets. It likely handles different sampling strategies and returns sampled subgraphs or neighbor lists. ```python def sample_neighbors(graph, nodes, num_samples): # Sample neighbors for a given set of nodes pass def sample_graph(graph, nodes, num_samples_list): # Sample a computational graph for training pass ``` -------------------------------- ### Create Minibatch Iterators (Python) Source: https://context7.com/williamleif/graphsage/llms.txt Initializes minibatch iterators for efficient training on large graphs. Supports both edge-based (unsupervised) and node-based (supervised) iterators. Requires graph data, ID mappings, placeholders, and batching configurations. ```python from graphsage.minibatch import EdgeMinibatchIterator, NodeMinibatchIterator # For unsupervised training (edge-based) edge_minibatch = EdgeMinibatchIterator( G=G, id2idx=id_map, placeholders=placeholders, context_pairs=context_pairs, # from random walks batch_size=512, max_degree=100, num_neg_samples=20 ) # For supervised training (node-based) node_minibatch = NodeMinibatchIterator( G=G, id2idx=id_map, placeholders=placeholders, label_map=class_map, num_classes=121, # depends on dataset batch_size=512, max_degree=128 ) # Iterate through batches while not edge_minibatch.end(): feed_dict = edge_minibatch.next_minibatch_feed_dict() # Use feed_dict with sess.run() ``` -------------------------------- ### GraphSage Model Definition in PyTorch Source: https://github.com/williamleif/graphsage/blob/master/example_data/toy-ppi-walks.txt This code defines the GraphSage model architecture using PyTorch. It outlines the layers, aggregation functions, and training logic necessary for implementing a graph convolutional network. Key components include message passing, aggregation, and update steps. This snippet is essential for understanding the core of the GraphSage implementation. ```python import torch import torch.nn as nn import torch.nn.functional as F class GraphSage(nn.Module): def __init__(self, input_size, hidden_size, output_size, aggregator_type='mean'): super(GraphSage, self).__init__() self.fc1 = nn.Linear(input_size * 2, hidden_size) # Node features concatenated with aggregated neighborhood features self.fc2 = nn.Linear(hidden_size * 2, output_size) self.aggregator = self.get_aggregator(aggregator_type, input_size, hidden_size) def get_aggregator(self, aggregator_type, input_size, hidden_size): if aggregator_type == 'mean': return MeanAggregator(input_size, hidden_size) elif aggregator_type == 'sum': return SumAggregator(input_size, hidden_size) elif aggregator_type == 'max': return MaxAggregator(input_size, hidden_size) else: raise ValueError(f"Unknown aggregator type: {aggregator_type}") def forward(self, x, adj): # x: node features (batch_size, input_size) # adj: adjacency matrix (batch_size, batch_size) aggregated_neighbors = self.aggregator(x, adj) combined = torch.cat([x, aggregated_neighbors], dim=1) hidden = F.relu(self.fc1(combined)) output = self.fc2(torch.cat([hidden, aggregated_neighbors], dim=1)) # Concatenate again for the second layer return output class MeanAggregator(nn.Module): def __init__(self, input_size, hidden_size): super(MeanAggregator, self).__init__() # Aggregation logic would be implemented here class SumAggregator(nn.Module): def __init__(self, input_size, hidden_size): super(SumAggregator, self).__init__() # Aggregation logic would be implemented here class MaxAggregator(nn.Module): def __init__(self, input_size, hidden_size): super(MaxAggregator, self).__init__() # Aggregation logic would be implemented here ``` -------------------------------- ### Generate Random Walks for Unsupervised GraphSAGE Source: https://github.com/williamleif/graphsage/blob/master/README.md This function is used to generate random walks for the unsupervised version of GraphSAGE, creating the required `-walks.txt` file. It is located in the `graphsage.utils` module. ```Python from graphsage.utils import run_walks # Example usage: # run_walks(train_prefix='your_data_prefix') ``` -------------------------------- ### Load Graph Data (Python) Source: https://context7.com/williamleif/graphsage/llms.txt Loads preprocessed graph data using the `load_data` utility function from `graphsage.utils`. It handles graph structure, node features, ID mapping, random walks, and class labels. The function supports optional normalization and loading of pre-computed walks. Outputs include a NetworkX graph, feature matrix, ID map, walks, and class map. ```python from graphsage.utils import load_data # Load training data G, features, id_map, walks, class_map = load_data( prefix='./example_data/ppi', normalize=True, load_walks=True ) # G: NetworkX graph with 'val' and 'test' node attributes # features: numpy array (num_nodes x feature_dim) or None # id_map: dict mapping node IDs to indices {node_id: int} # walks: list of random walk pairs for unsupervised training # class_map: dict mapping node IDs to class labels # Check data print(f"Nodes: {len(G.nodes())}, Edges: {len(G.edges())}") print(f"Feature dimension: {features.shape[1] if features is not None else 0}") ``` -------------------------------- ### Build GraphSAGE Model Components (Python) Source: https://context7.com/williamleif/graphsage/llms.txt Constructs core GraphSAGE model components using TensorFlow. This includes setting up placeholders for input data and dropout, creating an adjacency information variable, configuring a `UniformNeighborSampler`, and defining `SAGEInfo` objects for each layer with specified sampling parameters and output dimensions. Dependencies include TensorFlow and GraphSAGE modules. ```python import tensorflow as tf from graphsage.models import SampleAndAggregate, SAGEInfo from graphsage.neigh_samplers import UniformNeighborSampler from graphsage.aggregators import MeanAggregator # Setup placeholders placeholders = { 'batch1': tf.placeholder(tf.int32, shape=(None), name='batch1'), 'batch2': tf.placeholder(tf.int32, shape=(None), name='batch2'), 'neg_samples': tf.placeholder(tf.int32, shape=(None,)), 'dropout': tf.placeholder_with_default(0., shape=()), 'batch_size': tf.placeholder(tf.int32, name='batch_size'), } # Create adjacency info tensor adj_info_ph = tf.placeholder(tf.int32, shape=adj.shape) adj_info = tf.Variable(adj_info_ph, trainable=False, name="adj_info") # Configure layer architecture sampler = UniformNeighborSampler(adj_info) layer_infos = [ SAGEInfo("node", sampler, samples_1=25, output_dim=128), SAGEInfo("node", sampler, samples_2=10, output_dim=128) ] ``` -------------------------------- ### Graph Representation Conversion (Python) Source: https://github.com/williamleif/graphsage/blob/master/example_data/toy-ppi-walks.txt This snippet likely handles the conversion of graph data between different representations, such as from an adjacency list to a sparse matrix format or vice versa. This is important for compatibility with various graph processing libraries and algorithms. ```python from scipy.sparse import csr_matrix def adj_list_to_sparse(adj_list, num_nodes): # Convert adjacency list to a SciPy sparse matrix pass def sparse_to_adj_list(sparse_matrix): # Convert a sparse matrix back to an adjacency list pass ``` -------------------------------- ### Node Feature Aggregation (Python) Source: https://github.com/williamleif/graphsage/blob/master/example_data/toy-ppi-walks.txt This snippet focuses on the aggregation step within the GraphSAGE algorithm, showcasing different aggregation strategies like 'mean', 'pool', or 'lstm'. The input typically includes node features and neighbor information. The output is an aggregated representation for each node. ```python class MeanAggregator(nn.Module): def __init__(self, feature_dim, hidden_dim): super(MeanAggregator, self).__init__() # Mean aggregation logic pass def forward(self, x, neighbors): # Calculate mean of neighbor features pass class PoolAggregator(nn.Module): def __init__(self, feature_dim, hidden_dim): super(PoolAggregator, self).__init__() # Pooling aggregation logic pass def forward(self, x, neighbors): # Calculate pooled features from neighbors pass ``` -------------------------------- ### Build SampleAndAggregate Model Source: https://context7.com/williamleif/graphsage/llms.txt Constructs a GraphSAGE model using the SampleAndAggregate method. It requires placeholders, features, adjacency information, degrees, and layer configurations. The aggregator type, model size, and concatenation options can be specified. ```python model = SampleAndAggregate( placeholders=placeholders, features=features, # numpy array or None adj=adj_info, degrees=degrees, layer_infos=layer_infos, aggregator_type="mean", model_size="small", identity_dim=0, concat=True ) ``` -------------------------------- ### Configure GraphSAGE for Multi-Label Classification (Python) Source: https://context7.com/williamleif/graphsage/llms.txt Sets up GraphSAGE for multi-label node classification, where nodes can belong to multiple classes. It involves initializing the model with `sigmoid_loss=True` and preparing a class map where each node's label is represented as a binary vector. ```python # For datasets like PPI where nodes have multiple labels model = SupervisedGraphsage( num_classes=121, placeholders=placeholders, features=features, adj=adj_info, degrees=degrees, layer_infos=layer_infos, aggregator_type="mean", sigmoid_loss=True # Use sigmoid instead of softmax ) # Class map should contain lists of labels class_map = { 0: [1, 0, 1, 0, ...], # Binary vector of length num_classes 1: [0, 1, 1, 0, ...], # ... } # Predictions are independent probabilities per class predictions = sess.run(model.preds, feed_dict=feed_dict) predicted_labels = (predictions > 0.5).astype(int) ``` -------------------------------- ### Node Embeddings Generation (Python) Source: https://github.com/williamleif/graphsage/blob/master/example_data/toy-ppi-walks.txt This code snippet demonstrates how to generate node embeddings using a trained GraphSAGE model. After training, the model can be used to infer embeddings for all nodes in the graph. These embeddings can then be used for downstream tasks. ```python def generate_embeddings(model, graph_data): model.eval() # Set model to evaluation mode with torch.no_grad(): embeddings = model.forward(graph_data.features, graph_data.adj) return embeddings ``` -------------------------------- ### Train Supervised GraphSAGE Model (Bash) Source: https://context7.com/williamleif/graphsage/llms.txt Trains a supervised GraphSAGE model for node classification using mean aggregator. Requires specifying training data prefix, model type, epochs, learning rate, sampling strategy, dimension sizes, batch size, and other hyperparameters. Expected output includes F1 scores during validation. ```bash # Train on protein-protein interaction data with mean aggregator python -m graphsage.supervised_train \ --train_prefix ./example_data/ppi \ --model graphsage_mean \ --epochs 10 \ --learning_rate 0.01 \ --samples_1 25 \ --samples_2 10 \ --dim_1 128 \ --dim_2 128 \ --batch_size 512 \ --sigmoid \ --identity_dim 0 \ --max_degree 128 # Expected output: F1 scores printed every few iterations # Iter: 0050 train_loss= 0.45230 train_f1_mic= 0.73421 val_f1_mic= 0.69234 # Final validation stats written to log directory ``` -------------------------------- ### Configure Aggregator Types (Python) Source: https://context7.com/williamleif/graphsage/llms.txt Demonstrates the configuration of different aggregator types for GraphSAGE models, including mean, max-pooling, LSTM-based, and GCN-style aggregators. Each type offers different ways to combine neighborhood information. ```python from graphsage.supervised_models import SupervisedGraphsage # Mean aggregator (simple averaging) model_mean = SupervisedGraphsage( num_classes=121, placeholders=placeholders, features=features, adj=adj_info, degrees=degrees, layer_infos=layer_infos, aggregator_type="mean", model_size="small" ) # Max-pooling aggregator (element-wise max after MLP) model_maxpool = SupervisedGraphsage( num_classes=121, placeholders=placeholders, features=features, adj=adj_info, degrees=degrees, layer_infos=layer_infos, aggregator_type="maxpool", model_size="big" # 1024 hidden units ) # LSTM-based sequential aggregator model_lstm = SupervisedGraphsage( num_classes=121, placeholders=placeholders, features=features, adj=adj_info, degrees=degrees, layer_infos=layer_infos, aggregator_type="seq" ) # GCN-style aggregator (same weight for self and neighbors) model_gcn = SupervisedGraphsage( num_classes=121, placeholders=placeholders, features=features, adj=adj_info, degrees=degrees, layer_infos=layer_infos, aggregator_type="gcn", concat=False # GCN uses addition not concatenation ) ``` -------------------------------- ### Train Unsupervised GraphSAGE Model (Bash) Source: https://context7.com/williamleif/graphsage/llms.txt Trains an unsupervised GraphSAGE model to generate node embeddings using random walks and a max-pooling aggregator. Key parameters include training data prefix, model type, epochs, learning rate, sampling strategy, maximum steps, validation frequency, and saving embeddings. Output includes MRR metrics and saved embeddings. ```bash # Train unsupervised model with max-pooling aggregator python -m graphsage.unsupervised_train \ --train_prefix ./example_data/ppi \ --model graphsage_maxpool \ --epochs 1 \ --learning_rate 0.00001 \ --samples_1 25 \ --samples_2 10 \ --max_total_steps 1000 \ --validate_iter 10 \ --save_embeddings True # Output: MRR (mean reciprocal rank) metrics during training # Iter: 0050 train_loss= 0.23145 train_mrr= 0.67832 val_mrr= 0.65234 # Embeddings saved as val.npy with node IDs in val.txt ``` -------------------------------- ### GraphSage Aggregation Functions (Conceptual) Source: https://github.com/williamleif/graphsage/blob/master/example_data/toy-ppi-walks.txt This snippet presents conceptual implementations of different aggregation functions used in GraphSage, such as 'mean', 'sum', and 'max'. These functions are responsible for combining the feature representations of a node's neighbors. The choice of aggregator can significantly impact the model's performance. ```python def mean_aggregator(neighbor_features): return np.mean(neighbor_features, axis=0) def sum_aggregator(neighbor_features): return np.sum(neighbor_features, axis=0) def max_aggregator(neighbor_features): return np.max(neighbor_features, axis=0) # In a real implementation, these would be part of a PyTorch module # and operate on tensors. ``` -------------------------------- ### Node Embeddings Generation Source: https://github.com/williamleif/graphsage/blob/master/example_data/toy-ppi-walks.txt This code snippet focuses on generating node embeddings using a trained GraphSage model. After training, the model can be used to produce fixed-size vector representations for each node, capturing its structural and feature information within the graph. These embeddings are useful for downstream tasks like node classification or link prediction. ```python def generate_embeddings(model, data): model.eval() # Set model to evaluation mode with torch.no_grad(): features = data['features'] adj = data['adj'] # The forward pass might need to be adapted based on how the model handles full graph vs batches embeddings = model(features, adj) return embeddings.cpu().numpy() ``` -------------------------------- ### Generate Random Walks (Python) Source: https://context7.com/williamleif/graphsage/llms.txt Generates random walk co-occurrence pairs for unsupervised GraphSAGE training using `run_random_walks` from `graphsage.utils`. It requires a NetworkX graph and a list of training nodes. The function produces pairs of nodes that co-occur within a specified walk length. The generated walks can be saved to a file. ```python from graphsage.utils import run_random_walks import networkx as nx # Create or load graph G = nx.Graph() # ... add nodes and edges ... # Generate walks for training nodes train_nodes = [n for n in G.nodes() if not G.node[n]['val'] and not G.node[n]['test']] context_pairs = run_random_walks(G, train_nodes, num_walks=50) # Returns list of (source, context) node pairs # [(node1, node2), (node1, node3), ...] # Walk length: 5 hops, 50 walks per node # Save to file with open('train-walks.txt', 'w') as f: f.write('\n'.join([f"{p[0]} {p[1]}" for p in context_pairs])) ``` -------------------------------- ### Load Embeddings for Downstream Tasks (Python) Source: https://context7.com/williamleif/graphsage/llms.txt Loads previously saved node embeddings from a NumPy file and corresponding node IDs from a text file. This is typically done after training to prepare data for subsequent tasks. ```python # Load embeddings for downstream tasks embeddings = np.load('val.npy') with open('val.txt', 'r') as f: node_ids = [line.strip() for line in f] ``` -------------------------------- ### Use Identity Features for Static Graphs (Python) Source: https://context7.com/williamleif/graphsage/llms.txt Enables the use of identity embeddings for graphs lacking node features or to enhance performance on static graphs. This can be done by setting `features=None` and specifying `identity_dim`, or by combining identity features with existing node features. ```python # Train with identity features for featureless graphs model = SupervisedGraphsage( num_classes=num_classes, placeholders=placeholders, features=None, # No input features adj=adj_info, degrees=degrees, layer_infos=layer_infos, aggregator_type="mean", identity_dim=256 # Learnable embedding dimension per node ) # Combine identity features with existing features model_combined = SupervisedGraphsage( num_classes=num_classes, placeholders=placeholders, features=features, # Original features adj=adj_info, degrees=degrees, layer_infos=layer_infos, identity_dim=64 # Additional identity features ) # Total feature dim = original_dim + identity_dim ``` -------------------------------- ### Save Embeddings with GraphSAGE (Python) Source: https://context7.com/williamleif/graphsage/llms.txt Saves node embeddings and their corresponding IDs after unsupervised training. It iterates through minibatches to generate embeddings, saves them as a NumPy array, and writes node IDs to a text file. Includes logic to switch to test adjacency. ```python if save_embeddings: sess.run(val_adj_info.op) # Switch to test adjacency val_embeddings = [] nodes = [] iter_num = 0 finished = False while not finished: feed_dict, finished, edges = minibatch.incremental_embed_feed_dict( size=256, iter_num=iter_num ) outputs = sess.run(model.outputs1, feed_dict=feed_dict) for i, edge in enumerate(edges): if edge[0] not in seen: val_embeddings.append(outputs[i, :]) nodes.append(edge[0]) iter_num += 1 # Save embeddings and node IDs embeddings = np.vstack(val_embeddings) np.save('val.npy', embeddings) with open('val.txt', 'w') as f: f.write('\n'.join(map(str, nodes))) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.