autompc package¶
autompc.sysid module¶
Just doing system id using multi-layer perceptron. The code is similar to GP / RNN. The configuration space has to be carefully considered
- class autompc.sysid.mlp.ForwardNet(n_in, n_out, hidden_sizes, nonlintype)[source]¶
Bases:
Module- forward(x)[source]¶
Defines 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
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- training: bool¶
- class autompc.sysid.mlp.MLPFactory(*args, **kwargs)[source]¶
Bases:
ModelFactoryThe multi-layer perceptron (MLP) model uses a feed-forward neural network architecutret to predict the system dynamics. The network size, activation function, and learning rate are tunable hyperparameters.
Parameters
n_batch (Type: int, Default: 64): Training batch size of the neural net.
n_train_iters (Type: int, Default: 50): Number of training epochs
Hyperparameters:
n_hidden_layers (Type: str, Choices: [“1”, “2”, “3”, “4”], Default: “2”): The number of hidden layers in the network
hidden_size_1 (Type int, Low: 16, High: 256, Default: 32): Size of hidden layer 1.
hidden_size_2 (Type int, Low: 16, High: 256, Default: 32): Size of hidden layer 2. (Conditioned on n_hidden_layers >=2).
hidden_size_3 (Type int, Low: 16, High: 256, Default: 32): Size of hidden layer 3. (Conditioned on n_hidden_layers >=3).
hidden_size_4 (Type int, Low: 16, High: 256, Default: 32): Size of hidden layer 4. (Conditioned on n_hidden_layers >=4).
nonlintype (Type: str, choices: [“relu”, “tanh”, “sigmoid”, “selu”], Default: “relu): Type of activation function.
lr (Type: float, Low: 1e-5, High: 1, Default: 1e-3): Adam learning rate for the network.
- class autompc.sysid.mlp.MLP(system, n_hidden_layers=3, hidden_size=128, nonlintype='relu', n_train_iters=50, n_batch=64, lr=0.001, hidden_size_1=None, hidden_size_2=None, hidden_size_3=None, hidden_size_4=None, seed=100, use_cuda=True)[source]¶
Bases:
Model- traj_to_state(traj)[source]¶
- Parameters:
traj (Trajectory) – State and control history up to present time
- Returns:
state – Corresponding model state
- Return type:
numpy array of size self.state_dim
- update_state(state, new_ctrl, new_obs)[source]¶
- Parameters:
state (numpy array of size self.state_dim) – Current model state
new_ctrl (numpy array of size self.system.ctrl_dim) – New control input
new_obs (numpy array of size self.system.obs_dim) – New observation
- Returns:
state – Model state after observation and control
- Return type:
numpy array of size self.state_dim
- property state_dim¶
Returns the size of the model state
- train(trajs, silent=False, seed=100)[source]¶
- Parameters:
trajs (List of pairs (xs, us)) – Training set of trajectories
silent (bool) – Silence progress bar output
Only implemented for trainable models.
- pred(state, ctrl)[source]¶
Run model prediction.
- Parameters:
state (Numpy array of size self.state_dim) – Model state at time t
ctrl (Numpy array of size self.system.ctrl_dim) – Control applied at time t
- Returns:
state – Predicted model state at time t+1
- Return type:
Numpy array of size self.state_dim
- pred_batch(state, ctrl)[source]¶
Run batch model predictions. Depending on the model, this can be much faster than repeatedly calling pred.
- Parameters:
state (Numpy array of size (N, self.state_dim)) – N model input states
ctrl (Numpy array of size (N, self.system.ctrl_dim)) – N controls
- Returns:
state – N predicted states
- Return type:
Numpy array of size (N, self.state_dim)
- pred_diff(state, ctrl)[source]¶
Use code from https://gist.github.com/sbarratt/37356c46ad1350d4c30aefbd488a4faa .
- def get_batch_jacobian(net, x, to):
# noutputs: total output dim (e.g. net(x).shape(b,1,4,4) noutputs=1*4*4 # b: batch # i: in_dim # o: out_dim # ti: total input dim # to: total output dim x_batch = x.shape[0] x_shape = x.shape[1:] x = x.unsqueeze(1) # b, 1 ,i x = x.repeat(1, to, *(1,)*len(x.shape[2:])) # b * to,i copy to o dim x.requires_grad_(True) tmp_shape = x.shape y = net(x.reshape(-1, *tmp_shape[2:])) # x.shape = b*to,i y.shape = b*to,to y_shape = y.shape[1:] # y.shape = b*to,to y = y.reshape(x_batch, to, to) # y.shape = b,to,to input_val = torch.eye(to).reshape(1, to, to).repeat(x_batch, 1, 1) # input_val.shape = b,to,to value is (eye) y.backward(input_val) # y.shape = b,to,to return x.grad.reshape(x_batch, *y_shape, *x_shape).data # x.shape = b,o,i
- class autompc.sysid.sindy.SINDyFactory(*args, **kwargs)[source]¶
Bases:
ModelFactorySparse Identification of Nonlinear Dynamics (SINDy) is an system identification approach that works as follows. Using a library of \(k\) pre-selected functions (e.g. \(f \in \mathbb{R}^k\)), it computes numerically the derivatives of the system states (e.g. \(\dot{x} \in \mathbb{R}^n\)) iteratively solves a least-squares optimization to identify the weights \(K \in \mathbb{R}^{n \times k}\) that best fit the data: e.g. \(\|\dot{x} - Kf(x) \|^2\). In every iteration, functions whose weights are below a user-specified threshold \(\lambda\) are discarded. For more information, the reader is referred to https://arxiv.org/pdf/2004.08424.pdf
Hyperparameters:
time_mode (Type str, Choices: [“discrete”, “continuous”]): Whether to learn dynamics equations as discrete-time or continous-time.
method (Type str, Choices: [“lstsq, lasso”], Default: “lstsq”): Method for selecting model coefficients.
lasso_alpha (Type: str, Low: 10^-5, High: 10^2, Default: 1): α parameter for lasso regression. (Conditioned on method=”lasso”)
poly_basis (Type: bool): Whether to use polynomial basis functions
poly_degree (Type: int, Low: 2, High: 8, Default: 3): Maximum degree of polynomial terms. (Conditioned on poly_basis=”true”)
poly_cross_terms (Type: bool): Whether to include polynomial cross-terms. (Conditioned on poly_basis=”true”)
trig_basis (Type: bool): Whether to include trigonometric basis terms.
trig_freq (Type: int, Low: 1, High: 8, Default: 1): Maximum trig function frequency to include. (Conditioned on trig_basis=”true”)
trig_interaction (Type: bool): Whether to include cross-multiplication terms between trig functions and other state variables.
- class autompc.sysid.sindy.SINDy(system, method, lasso_alpha=None, threshold=0.01, poly_basis=False, poly_degree=1, poly_cross_terms=False, trig_basis=False, trig_freq=1, trig_interaction=False, time_mode='discrete')[source]¶
Bases:
Model- traj_to_state(traj)[source]¶
- Parameters:
traj (Trajectory) – State and control history up to present time
- Returns:
state – Corresponding model state
- Return type:
numpy array of size self.state_dim
- update_state(state, new_ctrl, new_obs)[source]¶
- Parameters:
state (numpy array of size self.state_dim) – Current model state
new_ctrl (numpy array of size self.system.ctrl_dim) – New control input
new_obs (numpy array of size self.system.obs_dim) – New observation
- Returns:
state – Model state after observation and control
- Return type:
numpy array of size self.state_dim
- property state_dim¶
Returns the size of the model state
- train(trajs, xdot=None, silent=False)[source]¶
- Parameters:
trajs (List of pairs (xs, us)) – Training set of trajectories
silent (bool) – Silence progress bar output
Only implemented for trainable models.
- pred(state, ctrl)[source]¶
Run model prediction.
- Parameters:
state (Numpy array of size self.state_dim) – Model state at time t
ctrl (Numpy array of size self.system.ctrl_dim) – Control applied at time t
- Returns:
state – Predicted model state at time t+1
- Return type:
Numpy array of size self.state_dim
- pred_batch(states, ctrls)[source]¶
Run batch model predictions. Depending on the model, this can be much faster than repeatedly calling pred.
- Parameters:
state (Numpy array of size (N, self.state_dim)) – N model input states
ctrl (Numpy array of size (N, self.system.ctrl_dim)) – N controls
- Returns:
state – N predicted states
- Return type:
Numpy array of size (N, self.state_dim)
- pred_diff(state, ctrl)[source]¶
Run model prediction and compute gradients.
- Parameters:
state (Numpy array of size self.state_dim) – Model state at time t
ctrl (Numpy array of size self.system.ctrl_dim) – Control at time t
- Returns:
state (Numpy array of size self.state_dim) – Predicted model state at time t+1
state_jac (Numpy array of shape (self.state_dim,) – self.state_dim) Gradient of predicted model state wrt to state
ctrl_jac (Numpy array of shape (self.state_dim,) – self.ctrl_dim) Gradient of predicted model state wrt to ctrl
- pred_diff_batch(states, ctrls)[source]¶
Run model prediction and compute gradients in batch.
- Parameters:
state (Numpy array of shape (N, self.state_dim)) – N input model states
ctrl (Numpy array of size (N, self.system.ctrl_dim)) – N input controls
- Returns:
state (Numpy array of size (N, self.state_dim)) – N predicted model states
state_jac (Numpy array of shape (N, self.state_dim,) – self.state_dim) Gradient of predicted model states wrt to state
ctrl_jac (Numpy array of shape (N, self.state_dim,) – self.ctrl_dim) Gradient of predicted model states wrt to ctrl