Fit an attention CANN
attention_cann_fit.RdA CANN whose correction is a transformer over feature tokens (an
FT-Transformer in the CANN setting). Each numeric column is a token and
each factor's indicator columns together form one token, so every level
gets a learned embedding. A learned [CLS] token joins them; pre-norm
blocks of multi-head self-attention and a feed-forward layer mix them;
the [CLS] state, through a zero-initialized head, corrects the linear
predictor, so training starts exactly at the GLM. attention_weights()
shows, per risk, which rating factors the model leans on.
Usage
attention_cann_fit(
formula,
data,
family = "poisson",
link = NULL,
d_model = 16,
heads = 2,
layers = 2,
feed_forward = 32,
epochs = 100,
batch_size = 64,
learning_rate = 0.001,
seed = 0,
validation_share = NULL,
patience = 10,
offset = NULL,
weights = NULL,
power = NULL
)Arguments
- formula
A model formula;
offset(...)terms are honoured.- data
A data frame.
- family
"poisson","gamma","tweedie"(needspower) or"gaussian".- link
NULLfor log (identity for the Gaussian), the only links the networks support.- d_model
Token width; a multiple of
heads.- heads
Attention heads.
- layers
Transformer blocks.
- feed_forward
Width of each block's feed-forward layer.
- epochs, batch_size, learning_rate
Adam's settings.
- seed
Seeds the initial weights and the mini-batch order.
NULL, or a share of rows held out for early stopping: the mean deviance on them is scored after each epoch and the best epoch kept.- patience
With early stopping, epochs without improvement before stopping.
- offset
Optional offset added to any
offset()terms.- weights
Optional prior weights.
- power
Tweedie power in
(1, 2).
Value
An attention_cann_model with the properties of a cann_model
and tokens.
Examples
set.seed(1)
d <- data.frame(age = runif(200, -1, 1), region = sample(c("A", "B", "C"), 200, TRUE))
d$y <- rgamma(200, 4, 4 / exp(0.5 * d$age + 0.4 * (d$region == "B")))
m <- attention_cann_fit(y ~ age + region, d, family = "gamma", d_model = 8,
layers = 1, epochs = 5)
m@tokens
#> [1] "age" "region"
colMeans(attention_weights(m, d)[, 1, ])
#> age region
#> 0.3674390 0.4765922