Neural network model#

This module presents the nn_model_obj class, which is the Python side of the escape::core::nn_model_t entity.

nn_model_obj is a thin adapter: it owns a C++ nn_model_t handle and a Python NNModel instance. The C++ handle is a python_nn_model_h, so every call that reaches the C++ side is routed back into the Python model through the callbacks of _NNModelBridge (_build, _fit, _predict, _score, _dumps, _loads, _stop, _shape, _class_name, _clone).

The C++ handler holds the bridge, and the bridge holds only the NNModel, so no reference cycle runs through the C++ side. Copying the C++ handle (which is what the regressor does to give every surrogate a network of its own) clones the NNModel through _clone.

The split matters because it keeps the C++ entity backend-agnostic: the C++ side only knows the shape contract, the training lifecycle and an opaque weights blob, while the Python model decides how the network is actually built, trained and serialized. Any library can be used as long as it implements NNModel.

See escape.utils.regressor.base.nn_model.NNModel for the Python contract and escape.utils.regressor.torch.SurrogateNet for the network.

escape.core.nn_model.nn_model(model: Any, name: str = 'nn_model') → nn_model_obj#

Create a network entity from a Python NNModel implementation.

Parameters#

modelNNModel

The Python network. Any library can be used as long as the object implements the NNModel contract.

namestr, optional

Name of the entity, by default “nn_model”

Returns#

nn_model_obj

The network, ready to be passed to regressor_obj.set_nn_model()

Examples#

>>> from escape.utils.regressor.torch import SurrogateNet
>>> net = nn_model(SurrogateNet("simple", epochs=200))
class escape.core.nn_model.nn_model_obj#

Wrapper class for a neural network model.

The object owns a C++ nn_model_t handle and a Python NNModel instance. The handle is created through the backend factory, so the C++ side always talks to a python_nn_model_h that forwards back into model.

Parameters#

modelNNModel

The Python network implementation. It must provide build, fit, predict, score, dumps, loads and class_name.

namestr, optional

Name of the entity, by default “nn_model”

backend#

Backend tag, “python” for this handler.

build(input_dim, output_dim, condition_dim=0, coord_dim=1) → None#

Fix the shape of the network and create the backend object.

Parameters#

input_dimint

Number of input features, i.e. of data points of the stack. Each coordinate set of a point produces exactly one intensity, so a 2-D map of 100 points still has input_dim = 100.

output_dimint

Number of predicted parameters

condition_dimint, optional

Number of per-sample condition values (the prior box), by default 0

coord_dimint, optional

Number of coordinates per data point: 1 for a curve, 2 for a map, 3 for a volume, by default 1

class_name#

Stable class name of the Python model.

condition_dim#

Number of per-sample condition values the network consumes next to the features.

It is 2 * output_dim for a network trained with a prior sampler, where every sample carries its own prior box, and 0 otherwise.

coord_dim#

Number of coordinates that make up one data point.

1 for a curve I(x), 2 for a map I(x, y), 3 for a volume I(x, y, z). It is informational: input_dim always counts intensities, one per point.

device#

Device the network is built, trained and restored on.

Forwarded to the underlying Python NNModel, so reg.network.device works without reaching through reg.network.model. Assigning to it moves an already built network, weights included.

Raises#

AttributeError

If the Python model does not support device selection, as a backend without a device concept does not.

dumps()#

Serialize the fitted network into an opaque blob.

fit(X: NDArray, y: NDArray, weights: Any = None, bounds: Any = None) → None#

Train the network.

Parameters#

XNDArray

Training features, shape (nsamples, input_dim)

yNDArray

Training targets, shape (nsamples, output_dim)

weightsNDArray, optional

Per-target loss weights, one per output column, used for this call only; by default the persistent target_weights apply.

boundsNDArray, optional

Per-sample condition values, shape (nsamples, condition_dim). With a prior sampler these are the prior boxes [lo_1..lo_P, hi_1..hi_P] in global-scaled space. Required when the network was built with condition_dim > 0.

input_dim#

Number of values the network consumes.

is_built#

Whether build has been called.

is_fitted#

Whether the network has been trained at least once.

is_stopped#

Whether stop has been called since the last fit.

loads(data)#

Restore a network from a blob produced by dumps.

model#

The underlying Python NNModel instance.

name#

Name of the entity.

output_dim#

Number of values the network produces.

parameters#

List of entity parameters.

predict(X: NDArray, bounds: Any = None) → double_array_obj#

Predict the targets for the given features.

Parameters#

XNDArray

Features, shape (nsamples, input_dim)

boundsNDArray, optional

Per-sample condition values, shape (nsamples, condition_dim)

Returns#

double_array_obj

Predictions, shape (nsamples, output_dim)

score(X: NDArray, y: NDArray, bounds: Any = None) → float#

Return a goodness-of-fit score (higher is better).

Parameters#

XNDArray

Features, shape (nsamples, input_dim)

yNDArray

Targets, shape (nsamples, output_dim)

boundsNDArray, optional

Per-sample condition values, shape (nsamples, condition_dim)

Returns#

float

Score

settings#

List of entity settings.

stop() → None#

Ask a running fit to stop as soon as possible.

target_weights#

Per-target loss weights, one per output column.

An empty array means the loss is unweighted. The regressor sets these before training so the network can undo the min-max scaling of the targets; see regressor_obj.loss_reweighting.