pyhgf.model.hybrid.DeepNetworkAdapter#

class pyhgf.model.hybrid.DeepNetworkAdapter(net, optimiser=None, learning_kind='precision_weighted', learning_kwargs=None, update_precisions=False, time_step=1.0, weight_reuse=1.0)[source]#

A learning part: a PyHGF DeepNetwork in the pipeline.

Declares the wrapped network and how it learns; the executor runs one batch-synchronous local learning step per training step (the same computation as batch_update(), staged in-trace) and threads the error at the network’s input onward.

Error convention bridge

This part is where the pipeline’s descent-error convention meets PyHGF’s observed-minus-predicted convention (see pyhgf.model.error_types). The executor performs the conversion at this single boundary:

Forward (pipeline → PyHGF):

Input (DescentError) is unchanged; used as usual.

Backward (PyHGF → pipeline):
  1. Pipeline passes DescentError (positive = too high)

  2. This part converts to ObservedMinusPredicted: observation = output - descent_error

  3. PyHGF’s batch_update treats this as the target, computing prediction_error = observation - output = -descent_error

  4. Learning uses the prediction error natively

  5. Input error (also prediction-error convention) is negated back to descent-error convention before passing to the previous part

This two-step conversion (descent↔observed-minus-predicted) happens in exactly one place: inside the executor’s backward pass for this adapter. No other part needs to know about the convention flip.

Parameters:
  • net (DeepNetwork) – The wrapped network. Its top (input) layer width is the part’s input size; its bottom (output) layer width is the part’s output size.

  • optimiser (Optional[optax.GradientTransformation]) – Optax optimiser for the local weight step. None freezes the weights (the beliefs still update).

  • learning_kind (str) – Weight-gradient mode, as in fit(). "synaptic_uncertainty" selects the weight-belief rule, whose settings come from learning_kwargs and whose step size is each weight’s own belief variance, so optimiser is then unused.

  • learning_kwargs (Optional[dict]) – Settings of the learning rule, used by learning_kind="synaptic_uncertainty" (see pyhgf.updates.vectorised.learning.resolve_synaptic_uncertainty_settings()).

  • update_precisions (bool) – Whether the precision state adapts across batches (see batch_update()). Defaults to False — the setting used for exact comparisons against backpropagation.

  • time_step (float) – Inference time step — scales the precision leak per batch (one batch counts as one observation of duration time_step).

  • weight_reuse (float) – How many times net’s weights are applied per sample, default 1.0. Set it when the caller feeds this part more rows than samples because the wrapped network’s weights are shared across several positions of each sample: the plain batch mean then divides by the reuse count, which that weight’s true per-sample gradient and curvature sum over instead. It rescales the mean gradient and, under learning_kind="synaptic_uncertainty", the importance increment too, so it reaches every learning path rather than only optimiser (see pyhgf.utils.vectorised_belief_propagation._batch_step()).

__init__(net, optimiser=None, learning_kind='precision_weighted', learning_kwargs=None, update_precisions=False, time_step=1.0, weight_reuse=1.0)[source]#
Parameters:
  • net (DeepNetwork)

  • optimiser (GradientTransformation | None)

  • learning_kind (str)

  • learning_kwargs (dict | None)

  • update_precisions (bool)

  • time_step (float)

  • weight_reuse (float)

Methods

__init__(net[, optimiser, learning_kind, ...])

init_state()

Return the (network, opt_state) state pytree.