otaf.surrogate package
Module contents
- class otaf.surrogate.BinaryClassificationModel(input_dim, output_dim, X, y, slack_threshold=0.1, pred_threshold=0.0, clamping=False, clamping_threshold=1.0, finish_critertion_epoch=20, loss_finish=1e-16, metric_finish=0.999, max_epochs=100, batch_size=100, train_size=0.7, input_normalization=True, noise_dims=None, noise_level=0.0, scheduler=None, device='cpu', display_progress_disable=False, squeeze_labels=False, labels_to_long=False, use_dual_target=False, save_path=None)[source]
Bases:
Module
- evaluate_model(x, batch_size=50000, return_on_gpu=False)[source]
Evaluates the model in non-standard space, processing the input in batches.
- Parameters:
x (array-like or torch.Tensor) – Input data to evaluate.
batch_size (int, optional) – Size of batches for evaluation. Default is 50000.
return_on_gpu (bool, optional) – Whether to return the result on the GPU. Default is False.
- Returns:
The model’s output.
- Return type:
torch.Tensor
- forward(x)[source]
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class otaf.surrogate.DualNetworkPredictor(model_defects_to_gaps, model_gap_def_to_slack)[source]
Bases:
Module
A PyTorch module that combines two neural networks to predict slack from defects.
This module first predicts gaps from defects using the first neural network, then combines the original input array with the predicted gaps to predict slack using the second neural network.
- model_defects_to_gaps
The first neural network model.
- Type:
nn.Module
- model_gap_def_to_slack
The second neural network model.
- Type:
nn.Module
- brute_force_probability_of_failure(composed_distribution, N_MC_MAX=1000000000, N_GEN_MAX=10000000, batch_size=500000, PF_STAB=1e-06, threshold=0.0)[source]
Brute-force calculation of the probability of failure.
- Parameters:
x (torch.Tensor) – Input tensor in real space.
threshold (float) – The threshold below which a prediction is considered a failure.
batch_size (int, optional) – Batch size for processing. Default is 50000.
N_MC_MAX (int, optional) – Maximum number of Monte Carlo samples to evaluate. Default is 1e9.
PF_STAB (float, optional) – Stability threshold for stopping criterion. Default is 1e-6.
- Returns:
The probability of failure.
- Return type:
float
- evaluate_in_batches(x, batch_size=50000, return_on_gpu=False)[source]
Helper function to evaluate the combined model in batches.
- Parameters:
x (torch.Tensor) – Input tensor in real space
batch_size (int, optional) – Batch size for processing. Default is 50000.
return_on_gpu (bool, optional) – Whether to return the result on the GPU. Default is False.
- Returns:
The concatenated output of the model evaluated in batches.
- Return type:
torch.Tensor
- forward(x)[source]
Forward pass through the combined networks.
- Parameters:
x (torch.Tensor) – Input tensor containing normalized defect features.
batch_size (int, optional) – Batch size for processing. Default is 250000.
- Returns:
Predicted slack values.
- Return type:
torch.Tensor
- get_train_test_data(test_size=0.2, random_seed=42)[source]
Splits the dataset into training and testing sets.
- Parameters:
test_size (float) – Proportion of the dataset to include in the test split. Default is 0.2.
random_seed (int) – Seed for random number generator. Default is 42.
- Returns:
Training and testing datasets (X_train, y_train_gaps, y_train_slack, X_test, y_test_gaps, y_test_slack).
- Return type:
tuple
- train_model(batch_size, num_epochs, loss_f_gaps, loss_f_slack, optimizer_gaps, optimizer_slack, device='cpu', display_progress_disable=False)[source]
Train the dual network model.
- Parameters:
batch_size (int) – Batch size for training.
num_epochs (int) – Number of epochs to train.
loss_f_gaps (torch.nn.Module) – Loss function.
optimizer (torch.optim.Optimizer) – Optimizer.
device (torch.device) – Device to use for training (CPU or GPU).
display_progress_disable (bool) – Flag to disable tqdm progress display.
- Returns:
None
- class otaf.surrogate.GaussianSurrogateModel(dimension, bounds, constraints=None, min_val=None, max_val=None)[source]
Bases:
object
- acquisition_function(X)[source]
Simple acquisition function: Upper Confidence Bound (UCB).
- Parameters:
X – Points to evaluate the acquisition function on.
- Returns:
Acquisition function values.
- add_point(X_new, y_new)[source]
Add a new point to the dataset and refit the model.
- Parameters:
X_new – New input point (1D array of length equal to the number of dimensions).
y_new – Corresponding function value at the new point.
- find_global_minima()[source]
Find the global minimum in the constrained space.
- Returns:
The input point that minimizes the surrogate model’s prediction.
- class otaf.surrogate.LPNeuralSurrogateTolereancing(input_dim, output_dim, X, y, nEq, nUb, A_eq_Def, A_eq_Gap, K_eq, A_ub_Def, A_ub_Gap, K_ub, clamping=False, finish_critertion_epoch=20, loss_finish=1e-16, metric_finish=0.999, max_epochs=100, batch_size=100, compile_model=True, train_size=0.7, save_path=None, input_description=None, display_progress_disable=True, noise_dims=[], noise_level=0.1)[source]
Bases:
Module
Compatibility equations (equality constraints) become A_eq_Def*X + A_eq_Gap*Y + K_eq = 0.
Interface equations (inequality constraints) become A_ub_Def*X + A_ub_Gap*Y + K_ub >= 0.
- evaluate_model_non_standard_space(x, batch_size=50000, return_on_gpu=False)[source]
Evaluates the model in non-standard space, processing the input in batches.
- Parameters:
x (array-like or torch.Tensor) – Input data to evaluate.
batch_size (int, optional) – Size of batches for evaluation. Default is 50000.
return_on_gpu (bool, optional) – Whether to return the result on the GPU. Default is False.
- Returns:
The model’s output.
- Return type:
torch.Tensor
- forward(x)[source]
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- pf_monte_carlo_bruteforce(composed_distribution, N_MC_MAX=1000000000, N_GEN_MAX=10000000, batch_size=500000, PF_STAB=1e-06, threshold=0.0)[source]
N_MC_MAX : Monte carlo size N_GEN_MAX : Max sample size to generate PF_STAB : Max variability of probability if one new point is added. threshold : failure threshold
- class otaf.surrogate.LimitSpaceFocusedLoss(a=0.04, b=3.0, square=True)[source]
Bases:
Module
Custom loss function which amplifies the penalty based on the sign disagreement between the model’s output and the target.
- a
A small constant added for numerical stability in computing the exponential function.
- Type:
float
- b
Scaling factor that adjusts the steepness of the exponential function.
- Type:
float
- square
If True, the squared difference is used; if False, the absolute difference is used.
- Type:
bool
- Parameters:
a (float) – Small constant to avoid division by zero. Default: 0.04.
b (float) – Scaling factor for the exponential part of the loss. Default: 3.0.
square (bool) – Indicator of whether to use square error or absolute error. Default: True.
- class otaf.surrogate.NeuralRegressorNetwork(input_dim, output_dim, X, y, clamping=False, finish_critertion_epoch=20, loss_finish=1e-16, metric_finish=0.999, max_epochs=100, batch_size=100, compile_model=True, train_size=0.7, save_path=None, input_description=None, display_progress_disable=True, input_normalization=True, output_normalization=True, noise_dims=[], noise_level=0.1)[source]
Bases:
Module
- evaluate_model_non_standard_space(x, batch_size=50000, return_on_gpu=False)[source]
Evaluates the model in non-standard space, processing the input in batches.
- Parameters:
x (array-like or torch.Tensor) – Input data to evaluate.
batch_size (int, optional) – Size of batches for evaluation. Default is 50000.
return_on_gpu (bool, optional) – Whether to return the result on the GPU. Default is False.
- Returns:
The model’s output.
- Return type:
torch.Tensor
- forward(x)[source]
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- pf_monte_carlo_bruteforce(composed_distribution, N_MC_MAX=1000000000, N_GEN_MAX=10000000, batch_size=500000, PF_STAB=1e-06, threshold=0.0)[source]
N_MC_MAX : Monte carlo size N_GEN_MAX : Max sample size to generate PF_STAB : Max variability of probability if one new point is added. threshold : failure threshold
- class otaf.surrogate.PositiveLimitSpaceFocusedLoss(a=0.04, b=3.0, c=4, square=True)[source]
Bases:
Module
Extends LimitSpaceFocusedLoss by specifically penalizing positive discrepancies between model output and target more heavily.
- a
A small constant added for numerical stability in computing the exponential function.
- Type:
float
- b
Scaling factor that adjusts the steepness of the exponential function.
- Type:
float
- c
Factor to amplify the loss when the target is positive.
- Type:
int
- square
If True, the squared difference is used; if False, the absolute difference is used.
- Type:
bool
- Parameters:
a (float) – Small constant to avoid division by zero. Default: 0.04.
b (float) – Scaling factor for the exponential part of the loss. Default: 3.0.
c (int) – Amplification factor for positive target values. Default: 4.
square (bool) – Indicator of whether to use square error or absolute error. Default: True.
- forward(output, target)[source]
Calculate the forward pass of the loss function, with added penalties for positive discrepancies.
- Parameters:
output (torch.Tensor) – The predictions from the model.
target (torch.Tensor) – The ground truth values.
- Returns:
The computed mean loss.
- Return type:
torch.Tensor
- otaf.surrogate.add_gaussian_noise(tensor, alpha)[source]
Add Gaussian noise to a tensor.
- Parameters:
tensor (torch.Tensor) – Input tensor of shape (N, M).
alpha (float) – Scaling factor for the standard deviation.
- Returns:
Tensor with added Gaussian noise.
- Return type:
torch.Tensor
- otaf.surrogate.evaluate_binary_predictions(predictions, labels)[source]
Evaluates binary predictions against ground truth labels and computes confusion matrix and accuracy.
Parameters: predictions (np.ndarray): Array of binary predictions (1 for positive class, 0 for negative class). labels (np.ndarray): Array of ground truth binary labels (1 for positive class, 0 for negative class).
Returns: dict: Dictionary containing confusion matrix, accuracy, and detailed metrics.
- otaf.surrogate.generate_corrected_binary_predictions(pred_probs, failure_threshold=-0.5, success_threshold=0.5, equality_decision='failure')[source]
Generates corrected binary predictions for failures and successes based on threshold values.
Parameters: pred_probs (np.ndarray): Array of predicted class probabilities with shape (n_samples, 2). failure_threshold (float): Threshold value for classifying failures. success_threshold (float): Threshold value for classifying successes. equality_decision (str): What is the outcome when both success and failure are positive? “failure” or “success”
Returns: np.ndarray: Corrected binary predictions.
- otaf.surrogate.get_custom_mlp_layers(layer_sizes, layer_class=<class 'torch.nn.modules.linear.Linear'>, activation_class=<class 'torch.nn.modules.activation.ReLU'>, layer_kwargs=None, activation_kwargs=None, dropout_class=None, dropout_kwargs=None)[source]
Creates a list of layers for a multi-layer perceptron (MLP) with specified layer sizes, layer class, activation function, and optional dropout layers.
- Parameters:
layer_sizes (list of int) – List of layer sizes [input, hidden1, …, hiddenN, output].
layer_class (callable) – Layer class to use (e.g., nn.Linear).
activation_class (callable) – Activation function class to use (e.g., nn.ReLU).
layer_kwargs (dict) – Dictionary of additional keyword arguments for the layer class.
activation_kwargs (dict) – Dictionary of additional keyword arguments for the activation function class.
dropout_prob (float, optional) – Dropout probability. If None, no dropout layers are added.
- Returns:
List of layers for the MLP.
- Return type:
list
- otaf.surrogate.get_variable_size_sequential_linear_model(input_dim, activation=None, relative_max_layer_size=3.0, relative_max_layer_depth=0.25, relative_layer_number=0.5, min_layer_number=4)[source]
Constructs a variable sized neural network models based on input dimensions.
- otaf.surrogate.optimize_thresholds_with_alpha(pred_probs, ground_truth, bounds=[-5.0, 5.0], optimize_for='minimize_fn_maximize_tn', equality_decision='failure', optimal_ratio=0.0001)[source]
Optimizes the failure and success thresholds based on the specified objective using basin hopping.
Parameters: pred_probs (np.ndarray): Array of predicted class probabilities with shape (n_samples, 2). ground_truth (np.ndarray): Array of true binary labels. bounds (list): Bounds for the threshold values, default is [-5, 5]. optimize_for (str): Objective for optimization, “minimize_fn_maximize_tn” or “minimize_fp_maximize_tp”. equality_decision (str): What is the outcome when both success and failure are positive? “failure” or “success”. optimal_ratio (float): Ratio for penalizing false negatives/positives, default is 1/10000.
Returns: dict: Best thresholds, alpha (currently unused), and corresponding evaluation metrics.