Skip to contents

Trains K_ens independent temporal_convlstm_fit() models with different random seeds and collects them in a single object. This formalises the hand-rolled ensemble loop from the v1.5.0 Pillar 3 Cerrado runner (data-raw/temporal_cerrado_run.R) and produces the natural forecast-ensemble input for temporal_kalman_update() or for as_edaphos_posterior().

Usage

temporal_convlstm_ensemble_fit(
  sequence,
  target,
  hidden_dims = c(8L, 4L),
  kernel_size = 3L,
  return_sequence = FALSE,
  epochs = 80L,
  lr = 0.02,
  K_ens = 10L,
  base_seed = 101L,
  physics_lambda = 0,
  physics_k_in = 0.03,
  physics_k_out = 0.015,
  physics_driver_channel = 2L,
  physics_scaling = NULL,
  verbose = FALSE
)

Arguments

sequence, target, hidden_dims, kernel_size, return_sequence, epochs, lr, physics_lambda, physics_scaling, physics_k_in, physics_k_out, physics_driver_channel, verbose

Passed through unchanged to temporal_convlstm_fit().

K_ens

Integer; number of ensemble members. Defaults to 10L.

base_seed

Integer; each member uses base_seed + k - 1L.

Value

A list with class edaphos_temporal_convlstm_ensemble containing

members

List of K edaphos_temporal_convlstm fits.

K_ens

Integer, the ensemble size.

final_losses

Numeric vector of per-member final training losses.

loss_histories

List of K numeric vectors (one per member).

See also

temporal_convlstm_rollout() to roll each member forward, temporal_kalman_update() to assimilate observations into the forecast ensemble, as_edaphos_posterior() for the unified uncertainty wrapper.

Examples

# \donttest{
# Pillar 3 on a 12-month synthetic Cerrado SOC cube: 8 x 8 cells,
# two channels (standardised elevation, monthly precipitation) and
# SOC in g/kg. Months 1-9 are the training window.
if (requireNamespace("torch", quietly = TRUE) &&
    isTRUE(torch::torch_is_installed())) {

  cube <- temporal_synth_soc_cube(H = 8L, W = 8L, T_total = 12L,
                                  seed = 42L)
  tt   <- temporal_cube_to_tensor(cube)
  past_seq <- tt$sequence[, 1:9, , , , drop = FALSE]  # (1, 9, 2, 8, 8)
  past_tgt <- tt$target[,   1:9, , , drop = FALSE]    # (1, 9, 8, 8)

  # return_sequence = TRUE is not the default here, but it is what
  # temporal_convlstm_ensemble_rollout() insists on further down.
  ens <- temporal_convlstm_ensemble_fit(
    sequence = past_seq, target = past_tgt,
    hidden_dims = 4L, return_sequence = TRUE,
    epochs = 20L, lr = 0.05, K_ens = 3L, base_seed = 7L)
  print(ens)

  # base_seed is the ONLY source of diversity: no bagging, no
  # augmentation, every member sees the identical cube and differs
  # purely by initialisation. At 20 epochs that has not settled --
  # the worst member's loss is 2.1x the best member's. K_ens = 3 is
  # kept here only for run time; it leaves the across-member sd with
  # 2 degrees of freedom, which is why the default is 10.
  print(round(ens$final_losses, 4))

  # final_loss is MSE in the standardised units of tt$target, so
  # quote it against the MSE of predicting the training-window mean.
  # Even the worst member beats that trivial model 9x.
  print(round(c(worst_member     = max(ens$final_losses),
                predict_the_mean = mean((past_tgt -
                                         mean(past_tgt))^2)), 4))

  # Rescaled, those are in-sample RMSEs of 1.8-2.6 g/kg on a cube
  # whose SOC spans 16.7-52.2 g/kg. In-sample only -- the forecast
  # picture is in ?temporal_convlstm_ensemble_rollout.
  print(round(sqrt(ens$final_losses) * tt$scaling$soc_sd, 2))
  print(round(range(cube$soc), 1))

  # loss_histories keeps the full per-member Adam trace.
  print(round(ens$loss_histories[[1L]][c(1L, 10L, 20L)], 4))

  # Trap: the default return_sequence = FALSE trains happily on a
  # (batch, H, W) target, and only the rollout helper complains.
  flat <- temporal_convlstm_ensemble_fit(
    sequence = past_seq,
    target   = array(past_tgt[1L, 9L, , ], dim = c(1L, 8L, 8L)),
    hidden_dims = 4L, epochs = 3L, K_ens = 2L, base_seed = 7L)
  print(tryCatch(temporal_convlstm_ensemble_rollout(
                   flat, past_seq,
                   tt$sequence[, 10:12, , , , drop = FALSE]),
                 error = function(e) conditionMessage(e)))
}
#> <edaphos_temporal_convlstm_ensemble>
#>   K_ens         : 3
#>   hidden_dims   : 4
#>   final_losses  : min=0.03375  median=0.04765  max=0.07133
#> [1] 0.0337 0.0713 0.0476
#>     worst_member predict_the_mean 
#>           0.0713           0.6628 
#> [1] 1.76 2.56 2.09
#> [1] 16.7 52.2
#> [1] 0.6993 0.1495 0.0337
#> [1] "isTRUE(object$return_sequence) is not TRUE"
# }