Learn R Programming

PortfolioTesteR (version 0.1.4)

ml_make_seq_model: Deterministic sequence model factory (GRU/LSTM/CNN1D with linear fallback)

Description

Returns fit/predict closures for sequence models that consume flattened tabular inputs (n \(\times\) (steps \(\times\) p)) and internally reshape to (n, steps, p). If Keras/TensorFlow is unavailable, falls back to a linear baseline so examples remain runnable on CPU-only machines.

Usage

ml_make_seq_model(
  type = c("linear", "gru", "lstm", "cnn1d"),
  steps = 26L,
  units = 16L,
  dense = NULL,
  dropout = 0.1,
  epochs = 12L,
  batch_size = 128L,
  lr = 0.01,
  patience = 2L,
  seed = 123L,
  deterministic = TRUE,
  pred_batch_size = 256L
)

Value

A list with $fit and $predict closures.

Arguments

type

One of "linear", "gru", "lstm", "cnn1d".

steps

Integer sequence length (e.g., 26 for 6 months of weeks).

units

Hidden units for GRU/LSTM or filters for CNN1D.

dense

Optional integer vector of additional dense layers.

dropout

Dropout rate for recurrent/CNN core.

epochs, batch_size

Training settings.

lr

Learning rate.

patience

Early-stopping patience.

seed

Integer seed.

deterministic

Logical; set determinism knobs when TRUE.

pred_batch_size

Fixed batch size used at prediction time.

Details

Determinism knobs: fixed seeds, TF_DETERMINISTIC_OPS=1, no shuffle, workers=1, and a fixed pred_batch_size to minimise retracing.

Optional dependencies: keras and tensorflow. When not available, the factory returns the linear fallback.

Examples

Run this code
if (FALSE) {
seq_gru <- ml_make_seq_model("gru", steps = 26, units = 16, epochs = 12)
}

Run the code above in your browser using DataLab