NoFAS Module

class nofas.Surrogate(model_name, model_func, input_size, output_size, dnn_arch=None, dnn_activation='relu', model_folder='./', limits=None, memory_len=20, surrogate=None, device='cpu')[source]

Bases: object

Class to create surrogate models for inference with NoFAS

Parameters
  • model_name (string) – name of the true model to be approximated

  • model_func (function) – with only one input of torch.Tensor

  • input_size (int) – input dimension of true model.

  • output_size (int) – output dimension of true model.

  • dnn_arch (list of int) – list containing the number of neurons for each hidden layer. Default None for [64,32] architecture.

  • dnn_activation (stirng) – type of activation function, either ‘relu’,

  • model_folder (string) – folder where surrogate model is stored.

  • limits (list or lists) – bounds for all inputs, in format of [[low_0, high_0], [low_1, high_1], … ]

  • memory_len (int) – the maximal number of batches stored in buffer. Default: 20

  • surrogate (None or torch.nn.Module) – the implementation of surrogate model used. Default: FNN This allows the user to spacify any NN architecture for the surrogate. If None the parameters “dnn_arch” and “dnn_activation” can be used to specify a dense neural network.

forward(x)[source]

Function to evaluate the surrogate

Parameters

x (torch.Tensor) – Contains a matrix of model inputs in the form [data_num, feature_dim]

Returns

Value of the surrogate at x.

gen_grid(input_limits=None, grid_type='tensor', gridnum=4, store=True)[source]

Generates a pre-grid.

Parameters
  • input_limits (None or list[list]) – If None, use self.limits. If list[list], rewrite self.limits and use it.

  • gridnum (int) – Contains the number of grid points per dimension.

  • store (bool) – Flag indicating the pre-grid is store in self.pre_grid. If False then self.pre_grid is None.

Returns

Input values for the full tensor grid in dim dimensions stored in a matrix [gridnum ** dim, dim].

Return type

torch.Tensor

property limits

Retrieves the limits of all inputs

property pre_grid
pre_train(max_iters, lr, lr_exp, record_interval, store=True, reg=False)[source]

Train surrogate model with pre-grid.

Training is performed with the RMSprop optimizer and with an exponential learning rate scheduler.

Parameters
  • max_iters (int) – The maximal number of iterations.

  • lr (double) – Learning rate for RMSprop.

  • lr_exp (double) – Decay factor for exponential scheduler.

  • record_interval (int) – The number of iterations to print loss info

  • store (bool) – If true, self.surrogate_save() will be called.

  • reg (bool) – If true, L1 regularization will be used with parameter 0.0001

Returns

None

surrogate_load()[source]

Load surrogate model from [self.name].sur and [self.name].npz

Returns

None

surrogate_save()[source]

Save surrogate model to [self.name].sur and [self.name].npz

Returns

None

update(x, max_iters=10000, lr=0.01, lr_exp=0.999, record_interval=500, store=False, tol=1e-05, reg=False)[source]

Model calibration for NoFAS.

Fine tuning is performed with the RMSprop optimizer and an exponential learning rate scheduler.

Parameters
  • x (torch.Tensor) – Input feature that needs true model output.

  • max_iters (int) – Maximum number of iteration. Default 10000.

  • lr (double) – Learning rate for RMSprop. Default 0.01.

  • lr_exp (double) – Decay factor of exponential scheduler.

  • record_interval (int) – The number of iterations to print loss information.

  • store (bool) – If ture, self.surrogate_save() will be called.

  • tol (double) – Optimization will be terminated if loss < tol ** 2.

  • reg (bool) – If ture, L1 regularizaiton will be used with parameter 0.1.

Returns

None

nofas.test_surrogate()[source]