Node-level embeddings from a fitted GAT
Arguments
- object
An
edaphos_gnn_gatfit. Agnn_fit_ensemble()result is not accepted; pass one of itsmembersinstead.
Value
A matrix with one row per node in the graph – every node,
not only the training ones, since message passing runs over the
whole graph. The column count follows the average_heads
argument of gnn_fit(): hidden columns when it is TRUE (the
default, heads averaged) and hidden * n_heads when it is
FALSE (heads concatenated).
Examples
data(br_cerrado)
set.seed(10)
sub <- br_cerrado[sample(nrow(br_cerrado), 150L), ]
names(sub)[names(sub) == "x"] <- "lon"
names(sub)[names(sub) == "y"] <- "lat"
g <- gnn_build_graph(sub, k = 6L,
feature_cols = c("elev", "slope", "twi", "map_mm"))
test <- rep(c(FALSE, FALSE, TRUE), length.out = nrow(sub))
y_obs <- sub$soc; y_obs[test] <- NA
fit <- gnn_fit(g, y_obs, hidden = 8L, n_heads = 2L, n_layers = 2L,
epochs = 60L, seed = 1L)
emb <- gnn_embed(fit)
# One row per NODE, not per training node: message passing ran over
# the whole graph, so the 50 held-out profiles carry embeddings too.
c(rows = nrow(emb), cols = ncol(emb),
nodes = g$n, trained_on = fit$n_train)
#> rows cols nodes trained_on
#> 150 8 150 100
# The embedding is NOT the design the head was fitted on. With
# `readout_skip` (the default) the head also saw the raw features
# un-smoothed, so a downstream model fed `emb` alone is missing
# four columns -- and the readout_skip comparison in [gnn_fit()]
# shows how much of the score lives in them.
c(emb = ncol(emb), design = ncol(fit$head_design),
raw_features = ncol(g$features))
#> emb design raw_features
#> 8 12 4
isTRUE(all.equal(fit$head_design, cbind(emb, g$features),
check.attributes = FALSE))
#> [1] TRUE
# The column count follows `average_heads`: averaged (the default)
# gives `hidden`, concatenated gives hidden * n_heads.
fit_cat <- gnn_fit(g, y_obs, hidden = 8L, n_heads = 2L, n_layers = 2L,
epochs = 60L, seed = 1L, average_heads = FALSE)
c(averaged = ncol(emb), concatenated = ncol(gnn_embed(fit_cat)))
#> averaged concatenated
#> 8 16
