GNN Models

class fedgraph.gnn_models.AggreGCN(nfeat: int, nhid: int, nclass: int, dropout: float, NumLayers: int)[source]

This class is an Aggregated GCN model with different methods of aggregation on the input features for the graph nodes on the first layer with a linear layer and the rest of the layers with GCNConv layers.

Parameters:
  • nfeat (int) – Number of input features.

  • nhid (int) – Number of hidden features in the hidden layers of the network.

  • nclass (int) – Number of output classes.

  • dropout (float) – Dropout probability.

  • NumLayers (int) – Number of GCN layers in the network.

forward(aggregated_feature: Tensor, adj_t: Tensor) Tensor[source]

Represents the forward pass computation of a GCN with different methods of aggregation on the input features for the graph nodes on the first layer with a linear layer and the rest of the layers with GCNConv layers.

Parameters:
  • x (torch.Tensor) – Input feature tensor for the graph nodes aggregated by the aggregation method.

  • adj_t (torch.Tensor) – Adjacency matrix of the graph.

Returns:

(tensor) – The log softmax of the output of the last layer.

Return type:

torch.Tensor

reset_parameters() None[source]
class fedgraph.gnn_models.AggreGCN_Arxiv(nfeat: int, nhid: int, nclass: int, dropout: float, NumLayers: int)[source]

This class is an Aggregated GCN model with different methods of aggregation on the input features for the graph nodes on the first layer with a linear layer and the rest of the layers with GCNConv layers.

Parameters:
  • nfeat (int) – Number of input features.

  • nhid (int) – Number of hidden features in the hidden layers of the network.

  • nclass (int) – Number of output classes.

  • dropout (float) – Dropout probability.

  • NumLayers (int) – Number of GCN layers in the network.

forward(aggregated_feature: Tensor, adj_t: Tensor) Tensor[source]

Represents the forward pass computation of a GCN with different methods of aggregation on the input features for the graph nodes on the first layer with a linear layer and the rest of the layers with GCNConv layers.

Parameters:
  • x (torch.Tensor) – Input feature tensor for the graph nodes aggregated by the aggregation method.

  • adj_t (torch.Tensor) – Adjacency matrix of the graph.

Returns:

(tensor) – The log softmax of the output of the last layer.

Return type:

torch.Tensor

reset_parameters() None[source]
class fedgraph.gnn_models.GCN(nfeat: int, nhid: int, nclass: int, dropout: float, NumLayers: int)[source]

A Graph Convolutional Network model implementation which creates a GCN with specified numbers of features, hidden layers, and output classes.

Parameters:
  • nfeat (int) – The number of input features

  • nhid (int) – The number of hidden features in each layer of the network

  • nclass (int) – The number of output classes

  • dropout (float) – The dropout probability

  • NumLayers (int) – The number of layers in the GCN

forward(x: Tensor, adj_t: Tensor) Tensor[source]

Represents the forward pass computation of a GCN

Parameters:
  • x (torch.Tensor) – Input feature tensor for the graph nodes.

  • adj_t (torch.Tensor) – Adjacency matrix of the graph.

Returns:

(tensor)

Return type:

torch.Tensor

reset_parameters() None[source]

Available to cater to weight initialization requirements as necessary.

class fedgraph.gnn_models.GCN_arxiv(nfeat: int, nhid: int, nclass: int, dropout: float, NumLayers: int)[source]

A variant of the GCN model tailored for the arXiv dataset.

Parameters:
  • nfeat (int) – Number of input features.

  • nhid (int) – Number of hidden features in the hidden layers of the network.

  • nclass (int) – Number of output classes.

  • dropout (float) – Dropout probability.

  • NumLayers (int) – Number of GCN layers in the network.

forward(x: Tensor, adj_t: Tensor) Tensor[source]

Represents the forward pass computation of a GCN

Parameters:
  • x (torch.Tensor) – Input feature tensor for the graph nodes.

  • adj_t (torch.Tensor) – Adjacency matrix of the graph.

Returns:

(tensor)

Return type:

torch.Tensor

reset_parameters() None[source]

This function is available to cater to weight initialization requirements as necessary.

class fedgraph.gnn_models.GCN_products(nfeat: int, nhid: int, nclass: int, dropout: float, NumLayers: int)[source]

A specialized GCN model implementation designed for product graphs.

Parameters:
  • nfeat (int) – Number of input features.

  • nhid (int) – Number of hidden features in the hidden layers of the network.

  • nclass (int) – Number of output classes.

  • dropout (float) – Dropout probability.

  • NumLayers (int) – Number of GCN layers in the network.

forward(x: Tensor, adj_t: Tensor) Tensor[source]

This function represents the forward pass computation of a GCN with products as input features for the graph nodes on the first layer and the rest of the layers with GCNConv layers.

xtorch.Tensor

Input feature tensor for the graph nodes.

adj_ttorch.Tensor

Adjacency matrix of the graph.

Returns:

(tensor)

Return type:

torch.Tensor

reset_parameters() None[source]

This function is available to cater to weight initialization requirements as necessary.

class fedgraph.gnn_models.GIN(nhid: int, nlayer: int, nfeat: int | None = None, nclass: int | None = None, dropout: float | None = None)[source]

A Graph Isomorphism Network (GIN) model implementation which creates a GIN with specified numbers of features, hidden units, classes, layers, and dropout. The GIN model is a variant of the Graph Convolutional Network (GCN) model.

Parameters:
  • nhid (int) – The number of hidden features in each layer of the GIN model.

  • nlayer (int) – The number of layers.

  • nfeat (int, optional) – The number of input features.

  • nclass (int, optional) – The number of output classes.

  • dropout (float, optional) – The dropout rate.

num_layers

The number of layers in the GIN model.

Type:

int

dropout

The dropout rate.

Type:

float

pre

The pre-neural network layer.

Type:

torch.nn.Sequential

graph_convs

The list of graph convolutional layers.

Type:

torch.nn.ModuleList

nn1

The first neural network layer.

Type:

torch.nn.Sequential

nnk

The k-th neural network layer.

Type:

torch.nn.Sequential

post

The post-neural network layer.

Type:

torch.nn.Sequential

Note

This base model applies for both the server and the trainer. When the model is used as a server, only nhid, and nlayer should be passed as arguments. When the model is used as a trainer, nfeat, nclass, and dropout should also be passed as arguments.

forward(data: Data) Tensor[source]

Forward pass of the GIN model, which takes in the input graph data and returns the model’s prediction.

Parameters:

data (torch_geometric.data.Data) – The input graph data.

Returns:

The prediction of the model.

Return type:

torch.Tensor

loss(pred: Tensor, label: Tensor) Tensor[source]

Compute the loss of the model.

Parameters:
  • pred (torch.Tensor) – The prediction of the model.

  • label (torch.Tensor) – The label of the input data.

Returns:

The nll loss of the model.

Return type:

torch.Tensor

class fedgraph.gnn_models.GNN_LP(user_nums: int, item_nums: int, data_meta_data: tuple, hidden_channels: int)[source]

A Graph Nerual Network (GNN) model implementation used for link prediction tasks, which creates a GNN with specified numbers of user and item nodes, hidden channels, and data metadata.

Parameters:
  • user_nums (int) – The number of user nodes.

  • item_nums (int) – The number of item nodes.

  • data_meta_data (tuple) – The meta data.

  • hidden_channels (int) – The number of hidden features in each layer of the GNN model.

user_emb

The user embedding layer.

Type:

torch.nn.Embedding

item_emb

The item embedding layer.

Type:

torch.nn.Embedding

gnn

The base GNN model.

Type:

GNN_base

forward(data: HeteroData) Tensor[source]

Represents the forward pass computation that is used in the training stage.

Parameters:

data (HeteroData) – The input graph data.

Returns:

(tensor) – The prediction output of the model.

Return type:

torch.Tensor

pred(train_data: HeteroData, test_data: HeteroData) Tensor[source]

Represents the prediction computation that is used in the test stage.

Parameters:
  • train_data (HeteroData) – The training graph data.

  • test_data (HeteroData) – The testing graph data.

Returns:

(tensor) – The prediction output of the model.

Return type:

torch.Tensor

class fedgraph.gnn_models.GNN_base(hidden_channels: int)[source]

A base Graph Neural Network model implementation which creates a GNN with two convolutional layers.

Parameters:

hidden_channels (int) – The number of hidden features in each layer of the GNN model.

conv1

The first convolutional layer.

Type:

torch_geometric.nn.conv.MessagePassing

conv2

The second convolutional layer.

Type:

torch_geometric.nn.conv.MessagePassing

forward(x: Tensor, edge_index: Tensor) Tensor[source]

Represents the forward pass computation

Parameters:
  • x (torch.Tensor) – Input feature tensor for the graph nodes.

  • edge_index (torch.Tensor) – Edge index tensor of the graph.

Returns:

(tensor)

Return type:

torch.Tensor

class fedgraph.gnn_models.SAGE_products(nfeat: int, nhid: int, nclass: int, dropout: float, NumLayers: int)[source]

A Graph SAGE model designed specifically for handling product graphs as another variant of GCN.

Parameters:
  • nfeat (int) – Number of input features.

  • nhid (int) – Number of hidden features in the hidden layers of the network.

  • nclass (int) – Number of output classes.

  • dropout (float) – Dropout probability.

  • NumLayers (int) – Number of Graph Sage layers in the network.

forward(x: Tensor, adj_t: Tensor) Tensor[source]

Represents the forward pass computation of a Graph Sage model

Parameters:
  • x (torch.Tensor) – Input feature tensor for the graph nodes.

  • adj_t (torch.Tensor) – Adjacency matrix of the graph.

Returns:

(tensor)

Return type:

torch.Tensor

reset_parameters() None[source]

Available to cater to weight initialization requirements as necessary.