Skip to content
Projects

opencr.ml.surrogate

A baseline surrogate of the CR solver with scikit-learn.

Surrogate learns ln(outputs) from ln(inputs) of a dataset (generate_dataset), by default with a Gaussian process (a neural network is the optional next step). The solver stays the reference: Surrogate.evaluate reports the held-out error against it and the speed-up, and Surrogate.predict flags inputs outside the training ranges (out of domain), where the surrogate is not to be trusted. scikit-learn is imported only here (the ml extra).

Classes

NameDescription
PredictionSurrogate outputs and whether each input is inside the training domain.
SurrogateA regression of the dataset's outputs on its inputs, in log space.

Predictionclassdataclass#

class Prediction(values: np.ndarray, in_domain: np.ndarray)

Surrogate outputs and whether each input is inside the training domain.

Attributes

NameTypeDescription
valuesnp.ndarrayThe predicted outputs (sample, output).
in_domainnp.ndarrayTrue where every input is inside the training ranges.

in_domainattributeinstance attribute#

in_domain: np.ndarray

valuesattributeinstance attribute#

values: np.ndarray

Surrogateclass#

class Surrogate(estimator: Any = None, seed: int = 0)

A regression of the dataset’s outputs on its inputs, in log space.

Parameters

NameTypeDefaultDescription
estimatorAnyNoneA scikit-learn regressor with multi-output fit and predict; by default a Gaussian process with an anisotropic RBF kernel on standard-scaled inputs (smooth, few samples).
seedint0The seed of the default estimator.

estimatorattributeinstance attribute#

estimator = estimator

highattributeinstance attribute#

high = np.empty(0)

lowattributeinstance attribute#

low = np.empty(0)

outputsattributeinstance attribute#

outputs: tuple[str, ...] = ()

parametersattributeinstance attribute#

parameters: tuple[str, ...] = ()

provenanceattributeinstance attribute#

provenance: dict[str, Any] = {}

seedattributeinstance attribute#

seed = seed

evaluatemethod#

def evaluate(dataset: xr.Dataset, split: int = 2, solve_time: float | None = None) -> xr.Dataset

Return the held-out relative errors and the speed-up on one split.

Parameters

NameTypeDefaultDescription
datasetxr.DatasetrequiredThe dataset the surrogate was trained on (or a new one from the same model).
splitint21 for validation, 2 for test.
solve_timefloat | NoneNoneThe solver’s time per sample in s, for the speed-up; by default the dataset’s solve_time.

Returns

xr.Dataset
Per output: the median, 95th percentile and maximum of
xr.Dataset
|predicted / solved - 1|, and in the attributes the surrogate’s
xr.Dataset
time per sample and the speed-up.

fitmethod#

def fit(dataset: xr.Dataset) -> Surrogate

Train on the dataset’s training split and return self.

forwardmethod#

def forward(theta: np.ndarray) -> np.ndarray

One sample’s outputs, as a forward model for opencr.inference.

predictmethod#

def predict(inputs: np.ndarray) -> Prediction

Return the outputs at inputs (sample, parameter) with the domain flag.