Skip to content

Customisation

Hooks for changing how a fit is initialised, when it stops, and how its loop is scheduled. Pass them to Robusta as override_initialiser, override_conv_tester and override_frame.

Initialiser

robusta_hmf.Initialiser dataclass

Seeds (A, G) before the optimisation loop starts.

Pass one to Robusta as override_initialiser. N, M and K may be left unset, in which case Robusta.fit() fills them in from the data, so an initialiser can be built without knowing the shapes up front.

Parameters:

Name Type Description Default
N int | None

Rows and columns of Y, and the rank.

None
M int | None

Rows and columns of Y, and the rank.

None
K int | None

Rows and columns of Y, and the rank.

None
strategy (svd, random, custom)

"svd": A = U S^½ and G = V S^½ from the leading K singular values and vectors of Y (the zero-filled data, without the weights). "random": independent standard normals. "custom": the A and G passed to execute().

"svd"

with_dims(N, M, K)

Fill in whichever of N, M, K are unset. Explicit values win.

execute(seed=None, A=None, G=None, Y=None, opt=None)

Build the initial state.

Parameters:

Name Type Description Default
seed int | None

Random seed, required for strategy="random".

None
A Array | None

Initial factors, shapes (N, K) and (M, K), required for strategy="custom".

None
G Array | None

Initial factors, shapes (N, K) and (M, K), required for strategy="custom".

None
Y Array | None

The (N, M) data, required for strategy="svd".

None
opt GradientTransformation | None

If given, its state is initialised on (A, G) (for SGD).

None

Returns:

Name Type Description
state RHMFState

The initial factors, with it = 0.

ConvergenceTester

robusta_hmf.ConvergenceTester dataclass

Decides when a fit has converged, from consecutive checked states.

Robusta builds one from conv_strategy and conv_tol; pass one as override_conv_tester to set it directly.

Parameters:

Name Type Description Default
strategy (max_frac_G, max_frac_A, rel_frac_loss, none)

"max_frac_G": converged when max((G_new - G_old)²) / mean(G_new²) is below tol, i.e. the largest change in any entry of G is small compared with the typical size of G. "max_frac_A": the same for A (used by infer(), where G is fixed). "rel_frac_loss": converged when |loss_new - loss_old| / |loss_new| is below tol. "none": never converged, so the fit runs to max_iter.

"max_frac_G"
tol float

Tolerance. Note that Robusta passes its own conv_tol (default 1e-3).

1e-6

is_converged(old_state, new_state, old_loss, new_loss)

Whether the fit has converged between two checks.

Parameters:

Name Type Description Default
old_state RHMFState

The states at the previous and the current convergence check.

required
new_state RHMFState

The states at the previous and the current convergence check.

required
old_loss float

The losses at those checks.

required
new_loss float

The losses at those checks.

required

Returns:

Name Type Description
converged bool

OptFrame

robusta_hmf.OptFrame dataclass

Owns the optimisation loop: data placement, initialisation, iteration.

Robusta configures the model (an HMF) and delegates execution to a frame. A subclass can change how the loop is scheduled -- see ShardedFrame, which chunks and shards the row axis -- without re-declaring the model's parameter list.

compute_dtype is the dtype of the arithmetic and of A/G. Robusta sets it through bind(); None leaves arrays in whatever dtype they arrive in.