make_train_step: Create an eager-mode train step function

View source: R/train.R

make_train_stepR Documentation

Create an eager-mode train step function

Description

Returns a closure that, when called with a mini-batch, computes the combined data + physics loss and applies gradients.

Usage

make_train_step(
  theta_model,
  optimizer,
  lambda_wet = 1,
  lambda_dry = 10,
  wet_split_cm = NULL,
  lambda3 = 1,
  lambda4 = 1000,
  lambda5 = 1000,
  lambda6 = 1,
  s3_form = c("inequality", "equality"),
  pf_left = -2,
  pf_right = 7.6,
  pF_lin_min = 5,
  pF_lin_max = 7.6,
  compile = TRUE
)

Arguments

theta_model

Keras model returned by build_swrc_model()⁠$theta_model⁠.

optimizer

A Keras/TF optimizer (e.g. tf$keras$optimizers$Adam).

lambda_wet

Weight for wet-end data loss (default 1.0).

lambda_dry

Weight for dry-end data loss (default 10.0).

wet_split_cm

Matric head threshold, in centimetres, separating wet from dry observations, or NULL (default) for a single data term with every observation weighted equally. Before 0.2.0 the default was 4.2, read as 4.2 cm (pF 0.62), which put every measured point on the dry side; a split at pF 4.2 is 10^4.2 cm.

lambda3

S1 linearity weight (default 1.0).

lambda4

S2 non-negativity weight (default 1000.0).

lambda5

S3 non-positivity weight (default 1000.0).

lambda6

S4 saturation weight (default 1.0).

s3_form

Form of the S3 dry-end residual, "inequality" (default, as published) or "equality"; see compute_physics_loss().

pf_left

Left boundary of pF domain (default -2).

pf_right

Right boundary of pF domain (default 7.6).

pF_lin_min, pF_lin_max

pF range over which S1 enforces dry-end linearity (defaults 5.0, 7.6).

compile

If TRUE (default), the step is wrapped in tensorflow::tf_function(). The returned function takes the collocation tensors (output of residual_to_tensors() for set1 - set4) as its fifth argument, res_tensors, so they can be redrawn without retracing.

Value

A function f(Xseq, pf_norm_obs, y, sw) that performs one gradient step and returns a named list of loss scalars.


soilFlux documentation built on Dec. 4, 2026, 5:10 p.m.