Fit a tabular network with embeddings and several heads
tabular_net_fit.RdRating factors entered as learned embeddings, a shared body of hidden
ReLU layers and one or more heads, each a linear output that starts at
zero on its own offset (a GLM's linear predictor or the log exposure):
eta_h = offset_h + out_h(body(x, e(f1), ...)). A factor named in
embeddings enters as a vector of the given dimensions in place of its
indicator columns (Wüthrich and Merz's entity embeddings, as in
Schelldorfer and Wüthrich's freMTPL2 tutorial); the other columns are
standardized. Training minimizes the sum over heads of each head's
weighted mean deviance times its loss weight, by Adam on mini-batches,
on the CPU in double precision, reproducible from seed. One head and no
embeddings is cann_fit(), bit for bit.
Usage
tabular_net_fit(
formula,
data,
heads = NULL,
embeddings = NULL,
responses = NULL,
hidden = c(16, 8),
epochs = 200,
batch_size = 64,
learning_rate = 0.001,
seed = 0,
validation_share = NULL,
patience = 10,
offset = NULL,
weights = NULL,
family = "poisson",
link = NULL,
power = NULL
)Arguments
- formula
A model formula for the network's columns. Its response, if any, is the one head's response when
responsesisNULL.- data
A data frame.
- heads
A list of
net_head()s;NULLfor one head on the formula's response withfamily,linkandpower.- embeddings
A named vector of each embedded factor's dimensions, such as
c(VehBrand = 2, Region = 2).- responses
With several heads, a list with one element per head (in order, or named by head): a numeric response, or a list of
yand optionalweights(claim counts for a severity head; a row with weight 0 does not enter the head's loss) andoffset. A head without its own weights or offset takesweightsand the formula's offset.Widths of the hidden ReLU layers.
- 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.
- family
"poisson","gamma","tweedie"(needspower) or"gaussian".- link
NULLfor log (identity for the Gaussian), the only links the networks support.- power
Tweedie power in
(1, 2).
Value
A tabular_net_model with properties heads, fitted (a list
by head), dispersions, best_epoch and validation_history. Use
stats::predict() with head =, network_embeddings(),
embedding_columns(), predict_distribution() (the first head) and
save_model().
Examples
set.seed(1)
d <- data.frame(age = runif(400, -1, 1), brand = sample(LETTERS[1:8], 400, TRUE))
d$claims <- rpois(400, exp(-1 + 0.2 * (match(d$brand, LETTERS) - 4)))
d$sev <- ifelse(d$claims > 0, rgamma(400, 2, 2 / exp(1 + d$age)), 0)
m <- tabular_net_fit(~ age + brand, d, embeddings = c(brand = 1),
heads = list(net_head("freq"), net_head("sev", "gamma")),
responses = list(freq = d$claims,
sev = list(y = d$sev, weights = d$claims)),
epochs = 5, learning_rate = 0.01)
m@heads
#> [1] "freq" "sev"
network_embeddings(m, "brand")
#> level brand_e1
#> 1 A -0.24557867
#> 2 B -0.23969347
#> 3 C -0.31800501
#> 4 D -0.11736530
#> 5 E -0.15211921
#> 6 F -0.05147560
#> 7 G -0.08135077
#> 8 H 0.04872944