API Documentation
rom.gradients module
- rom.gradients.local_linear_gradients(X, Y, n=None)[source]
Estimates gradients via local linear approximations.
Note
This code follows that used in: https://github.com/paulcon/active_subspaces
- Parameters
X (ndarray) – M-by-d 2D numpy array containing input training samples
Y (ndarray) – M length 1D numpy array containing the 1D output training samples
n (int) – number of nearest neighbors to use for local linear approximation
- Returns
M-by-d 2D numpy array containing local linear approximation of gradients for each training sample
- Return type
DY (ndarray)
rom.surr_model module
- class rom.surr_model.NN_alt(U, dim_layers, lr=0.001, lr_decay=0.9, alpha=0.001)[source]
Bases:
objectShallow ReLU Network with alternating minimization surrogate model.
- net
two layer ReLU network
- optimizer
optimizer for network
- loss_func
loss function for network
- lr
learning rate
- Type
float
- lr_decay
learning rate decay
- Type
float
- alpha
regularization parameter (ell 2 regularizer)
- Type
float
- U
d-by-k 2D numpy array to reduce input dimension
- Type
ndarray
- loss_train
contains training loss values at each epoch
- Type
list
- loss_val
contains validation loss values at each epoch
- Type
list
- predict(X)[source]
Calculates output of trained NN model.
- Parameters
X (ndarray) – ?-by-d 2D numpy array containing input data
- Returns
?-by-d2 2D numpy array containing output data
- Return type
Y (ndarray)
- rel_error_tensor(Y_true, Y_calc)[source]
Calculates relative error (via ell 2 norm) between 2 PyTorch tensors.
- Parameters
Y_true (ndarray) – numpy array containing true values
Y_calc (ndarray) – numpy array containing calculated/predicted values
- Returns
relative error between Y_true and Y_calc
- Return type
(float)
- sub_dres(U, X_train, Y_train)[source]
Defines derivative of cost function w.r.t. U.
Note
This function is defined for the pymanopt optimization problem. Must take in U as variable.
- Parameters
U (ndarray) – d-by-k 2D numpy array containing reduced basis
X_train (ndarray) – M-by-d 2D numpy array containing input training data
Y_train (ndarray) – M-by-d2 2D numpy array containing output training data
- Returns
d-by-k 2D numpy array containing reduced basis
- Return type
(ndarray)
- sub_res(U, X_train, Y_train)[source]
Defines cost function w.r.t. U.
Note
This function is defined for the pymanopt optimization problem. Must take in U as variable.
- Parameters
U (ndarray) – d-by-k 2D numpy array containing reduced basis
X_train (ndarray) – M-by-d 2D numpy array containing input training data
Y_train (ndarray) – M-by-d2 2D numpy array containing output training data
- Returns
cost value for given U
- Return type
out (float)
- train(dataset_train, dataset_val=None, num_outer=10, num_epoch=5000, batch_size=16, record=True, verbose=2)[source]
Trains NN model via alternating minimization scheme.
- Parameters
dataset_train (tuple,ndarray) – [X_train,Y_train], numpy array containing input/output training data
dataset_val (tuple,ndarray) – [X_val,Y_val], numpy array containing input/output validation data
num_outer (int) – number of outer iterations to perform
num_epoch (int) – number of inner iterations for NN training to perform
batch_size (int) – number of training samples in each training batch
record (bool) – choose to record loss during training or not
verbose (int) – amount of information to print; 0 = nothing printed; 1 = loss values printed every 1000 epochs; 2 = loss values printed every 100 epochs; >2 = loss values printed every epoch
- train_NN(dataset_train, dataset_val, num_epoch, batch_size, record, verbose)[source]
Trains and validates NN.
- Parameters
dataset_train (tuple,ndarray) – [X_train,Y_train], numpy array containing input/output training data
dataset_val (tuple,ndarray) – [X_val,Y_val], numpy array containing input/output validation data
num_outer (int) – number of outer iterations to perform
num_epoch (int) – number of inner iterations for NN training to perform
batch_size (int) – number of training samples in each training batch
record (bool) – choose to record loss during training or not
verbose (int) – amount of information to print; 0 = nothing printed; 1 = loss values printed every 1000 epochs; 2 = loss values printed every 100 epochs; >2 = loss values printed every epoch
- Returns
contains training loss at each epoch loss_val (list): contains validation loss at each epoch
- Return type
loss_train (list)
- class rom.surr_model.RF[source]
Bases:
objectRandom Feature Expansion surrogate model.
Note
All attributes are set after the “train” method is called.
- c
N length 1D numpy array containing coefficients of learned RF model
- Type
ndarray
- Omega
N-by-d 2D numpy array containing weights of the RF model
- Type
ndarray
- bias
N length 1D numpy array containing biases of the RF model
- Type
ndarray
- scale_A_normalize
N length 1D numpy array to normalize columns of A_train (RF matrix with training data)
- Type
ndarray
- construct_A(X)[source]
Constructs random feature matrix A.
- Parameters
X (ndarray) – ?-by-d 2D numpy array containing ? number of input data points
- Returns
?-by-N 2D numpy array representing the random feature matrix A
- Return type
A (ndarray)
- construct_weights(X_train, k, N, s)[source]
Constructs weights and biases for RF model.
- Parameters
X_train (ndarray) – M-by-d 2D numpy array containing input training data
k (int) – dimension of input data
N (int) – number of weights to use for RF model
s (int) – sparsity for weights Omega
- phi(nodes)[source]
Activation function phi for RF model.
- Parameters
nodes (ndarray) – N length 1D numpy array containing nodal values
- Returns
N length 1D numpy array containing nodes passed through activation function phi
- Return type
out (ndarray)
- predict(X)[source]
Calculates output of trained RF model.
- Parameters
X (ndarray) – ?-by-d 2D numpy array containing ? number of input data points
- Returns
? length 1D numpy array containing predicted output values
- Return type
Y (ndarray)
- train(dataset_train, N=None, s=None, alpha=0.001)[source]
Trains RF model by learning coefficients c via ridge regression.
Note
Assumes output data is 1D.
- Parameters
X_train (ndarray) – M-by-d 2D numpy array containing input training data
Y_train (ndarray) – M length 1D numpy array containing output training data
N (int) – number of weights to use for RF model
alpha (float) – regularizing hyperparameter for ridge regression model
rom.subspaces module
rom.utils module
- rom.utils.check_1D(X)[source]
Enforces requirement that X be a 1D numpy array.
- Parameters
X (ndarray) – ND numpy array containing data points
- Returns
unaltered 1D numpy array
- Return type
X (ndarray)
- rom.utils.check_2D(X)[source]
Enforces requirement that X be a 2D numpy array.
- Parameters
X (ndarray) – ND numpy array containing data points
- Returns
unaltered 2D numpy array
- Return type
X (ndarray)
- rom.utils.print_epoch(verbose, epoch, num_epoch, loss_train, loss_val=None, overwrite=False)[source]
Prints training/validation error at given epoch.
- Parameters
verbose (int) – amount of information to print; 0 = nothing printed; 1 = loss values printed every 1000 epochs; 2 = loss values printed every 100 epochs; >2 = loss values printed every epoch
epoch (int) – current epoch number
num_epoch (int) – total epoch number
loss_train (float) – current loss value for training data
loss_val (float) – current loss value for validation data
overwrite (boolean) – choose to overwrite previous line
- rom.utils.rel_error(Y_true, Y_calc)[source]
Calculates relative error (via ell 2 norm) between 2 arrays.
- Parameters
Y_true (ndarray) – numpy array containing true values
Y_calc (ndarray) – numpy array containing calculated/predicted values
- Returns
relative error between Y_true and Y_calc
- Return type
out (float)