Base layers

This module contains a miscellany of layers that are not specifically for graph neural networks.

[source]

Disjoint2Batch

spektral.layers.Disjoint2Batch()

Utility layer that converts data from disjoint mode to batch mode by zero-padding the node features and adjacency matrices.

Mode: disjoint.

This layer expects a sparse adjacency matrix.

Input

  • Node features of shape (n_nodes, n_node_features);
  • Binary adjacency matrix of shape (n_nodes, n_nodes);
  • Graph IDs of shape (n_nodes, );

Output

  • Batched node features of shape (batch, N_max, n_node_features);
  • Batched adjacency matrix of shape (batch, N_max, N_max);

[source]

GraphMasking

keras.engine.base_layer.GraphMasking()

A layer that starts the propagation of masks in a model.

This layer assumes that the node features given as input have been extended with a binary mask that indicates which nodes are valid in each graph. The layer is useful when using a data.BatchLoader with mask=True or in general when zero-padding graphs so that all batches have the same size. The binary mask indicates with a 1 those nodes that should be taken into account by the model.

The layer will remove the rightmost feature from the nodes and start a mask propagation to all subsequent layers:

print(x.shape)  # shape (batch, n_nodes, n_node_features + 1)
mask = x[..., -1:]  # shape (batch, n_nodes, 1)
x_new = x[..., :-1] # shape (batch, n_nodes, n_node_features)

[source]

InnerProduct

spektral.layers.InnerProduct(trainable_kernel=False, activation=None, kernel_initializer='glorot_uniform', kernel_regularizer=None, kernel_constraint=None)

Computes the inner product between elements of a 2d Tensor:

Mode: single.

Input

  • Tensor of shape (n_nodes, n_features);

Output

  • Tensor of shape (n_nodes, n_nodes).

Arguments

  • trainable_kernel: add a trainable square matrix between the inner product (e.g., X @ W @ X.T);

  • activation: activation function;

  • kernel_initializer: initializer for the weights;

  • kernel_regularizer: regularization applied to the kernel;

  • kernel_constraint: constraint applied to the kernel;


[source]

MinkowskiProduct

spektral.layers.MinkowskiProduct(activation=None)

Computes the hyperbolic inner product between elements of a rank 2 Tensor:

Mode: single.

Input

  • Tensor of shape (n_nodes, n_features);

Output

  • Tensor of shape (n_nodes, n_nodes).

Arguments

  • activation: activation function;