Implements the GraphSAGE (Graph Sample and Aggregate) layer:
$$\mathbf{h}_{\mathcal{N}(v)}^{(k)} = \text{AGGREGATE}\left(\{\mathbf{h}_u^{(k-1)} : u \in \mathcal{N}(v)\}\right)$$
$$\mathbf{h}_v^{(k)} = \sigma\left(\mathbf{W}^{(k)} \cdot \text{CONCAT}\left(\mathbf{h}_v^{(k-1)}, \mathbf{h}_{\mathcal{N}(v)}^{(k)}\right)\right)$$
This layer:
Aggregates neighbor features using the specified aggregator
Concatenates node's own features with aggregated neighbor features
Applies linear transformation and optional normalization
Parameters:
\(W\):
(in_features + aggregated_features) x out_featureslearnable weight matrix\(b\):
out_featureslearnable bias term (optional)
Usage
layer_sage(
in_features,
out_features,
aggregator = MeanAggregator(),
bias = TRUE,
concat = TRUE
)Arguments
- in_features
Integer. Number of input features per node
- out_features
Integer. Number of output features per node
- aggregator
Aggregator S7 object. Default:
MeanAggregator()- bias
Logical. Add learnable bias. Default: TRUE
- concat
Logical. If TRUE, concatenates self and neighbor features. If FALSE, adds them. Default: TRUE
Details
The aggregator should be an S7 Aggregator object (e.g., MeanAggregator(), MaxAggregator()).
Each aggregator is responsible for its own normalization. For example, MeanAggregator()
applies row normalization internally, while MaxAggregator() uses the adjacency structure
without normalization.
Forward pass
layer(x, adj)
x: Tensorn_nodes x in_features. Node feature matrix.adj: Sparse COO tensorn_nodes x n_nodes. Adjacency matrix defining graph structure.
References
Hamilton, W., Ying, Z., & Leskovec, J. (2017). Inductive representation learning on large graphs. Advances in Neural Information Processing Systems, 30. doi:10.48550/arXiv.1706.02216
Examples
if (FALSE) { # torch::torch_is_installed()
adj <- adj_from_edgelist(from = c(1, 2, 3, 4), to = c(2, 3, 4, 1))
x <- torch::torch_randn(4, 8)
layer <- layer_sage(8, 4)
layer(x, adj)
# Any aggregator can be supplied
layer <- layer_sage(8, 4, aggregator = MaxAggregator())
layer(x, adj)
# Add self and neighbor features instead of concatenating them
layer <- layer_sage(8, 4, concat = FALSE)
layer(x, adj)
}