├── .DS_Store ├── .gitignore ├── requirements.txt ├── LICENSE ├── test.py ├── README.md ├── train.py ├── modules ├── convex_model.py ├── lyapunov_NN.py ├── rootfind_model.py └── stochastic_model.py ├── dynamics_plotting.py └── true_dynamics.py /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NPLawrence/stochastic_dynamics/HEAD/.DS_Store -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | .DS_Store/ 3 | __pycache__/ 4 | *.so 5 | log/ 6 | *.pt 7 | *.pkl 8 | *.pth 9 | *.ckpt 10 | *.log 11 | datasets/ 12 | runs/ 13 | saved_models/ 14 | misc/ 15 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | absl-py==0.10.0 2 | cachetools==4.1.1 3 | certifi==2020.6.20 4 | chardet==3.0.4 5 | cycler==0.10.0 6 | future==0.18.2 7 | google-auth==1.22.1 8 | google-auth-oauthlib==0.4.1 9 | grpcio==1.32.0 10 | idna==2.10 11 | joblib==0.17.0 12 | kiwisolver==1.2.0 13 | Markdown==3.3 14 | matplotlib==3.3.2 15 | numpy==1.19.2 16 | oauthlib==3.1.0 17 | pandas==1.1.3 18 | Pillow==7.2.0 19 | protobuf==3.13.0 20 | pyasn1==0.4.8 21 | pyasn1-modules==0.2.8 22 | pyparsing==2.4.7 23 | python-dateutil==2.8.1 24 | pytz==2020.1 25 | requests==2.24.0 26 | requests-oauthlib==1.3.0 27 | rsa==4.6 28 | scikit-learn==0.23.2 29 | scipy==1.5.2 30 | six==1.15.0 31 | sklearn==0.0 32 | tensorboard==2.3.0 33 | tensorboard-plugin-wit==1.7.0 34 | threadpoolctl==2.1.0 35 | torch==1.5.1 36 | torchvision==0.6.1 37 | urllib3==1.25.10 38 | Werkzeug==1.0.1 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 NPLawrence 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /test.py: -------------------------------------------------------------------------------- 1 | # Simple script for validating trained models 2 | # This just loads a model and visualizes trajectories (dynamics_plotting.py) 3 | # from the learned model against the true dynamics (true_dynamics.py) 4 | 5 | import numpy as np 6 | import matplotlib.pyplot as plt 7 | import torch 8 | 9 | import modules.convex_model as convex_model 10 | import modules.rootfind_model as rootfind_model 11 | import modules.stochastic_model as stochastic_model 12 | import modules.lyapunov_NN as L 13 | 14 | import dynamics_plotting as vis 15 | import true_dynamics 16 | 17 | n = 2 # state dimension 18 | 19 | layer_sizes = np.array([n, 50, 50, 1]) 20 | ICNN = L.ICNN(layer_sizes) 21 | V = L.MakePSD(ICNN,n) 22 | 23 | experiment = 'exp_name' + '.pth' 24 | PATH_f_net = './saved_models/' + experiment 25 | 26 | f = convex_model.dynamics_model(V, n, is_training = False) 27 | f.load_state_dict(torch.load(PATH_f_net)) 28 | plotting = vis.plot_dynamics(f,V) 29 | 30 | x0 = torch.randn((1,1,2)) + 3 31 | 32 | sample_trajectory = true_dynamics.data_linear() 33 | X_true = sample_trajectory.gen_data(x0) 34 | 35 | kwargs = {"color" : "tab:purple", "marker": ".", "markersize": 3, "alpha": 1, "label": "Prediction"} 36 | X = plotting.plot_trajectory(x0, kwargs, sample_paths = 1, show_ls = True, steps = 50, ax = plt) 37 | 38 | kwargs = {"color" : "tab:blue", "marker": ".", "markersize": 3, "label": "True dynamics"} 39 | plt.plot(X_true[:, 0], X_true[:, 1], **kwargs) 40 | 41 | plt.legend() 42 | plt.show() 43 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Almost Surely Stable Deep Dynamics 2 | This repository contains the accompanying code for our NeurIPS 2020 paper [Almost Surely Stable Deep Dynamics](https://dais.chbe.ubc.ca/assets/preprints/2020C6_Lawrence_NeurIPS.pdf) by Nathan Lawrence, Philip Loewen, Michael Forbes, Johan Backstrom, Bhushan Gopaluni. 3 | 4 | The focus of the paper is learning deep neural network based dynamic models with stability guarantees. Specifically, we consider stochastic discrete-time models. The method works by embedding a Lyapunov neural network into the dynamic model, thereby simulatenously learning the dynamics as well as a suitable Lyapunov function. We consider three cases of increasing difficulty: 5 | 6 | 1. We propose a method for learning stable deterministic dynamics subject to a convex Lyapunov function; 7 | 2. We generalize the first approach to the non-convex case through the use of an implicit layer designed to satisfy the stability criterion; 8 | 3. We show how to extend these approaches to the stochastic setting by imposing stability on the mean and variance parameters of a mixture density network. 9 | 10 | Here, we include code for the models described above along with instructions for training/testing and plotting. 11 | 12 | Paper reference: 13 | ``` 14 | @article{lawrence2020almost, 15 | title = {Almost Surely Stable Deep Dynamics}, 16 | author = {Lawrence, Nathan P and Loewen, Philip D and Forbes, Michael G and Backstrom, Johan U and Gopaluni, R Bhushan}, 17 | booktitle = {Advances in Neural Information Processing Systems}, 18 | year = {2020}, 19 | } 20 | ``` 21 | 22 | ## Requirements 23 | Install the necessary packages from _requirements.txt_ via 24 | ``` 25 | pip install -r requirements.txt 26 | ``` 27 | ## Usage 28 | 29 | _train.py_ and _test.py_ are lightweight scripts to locally train and evaluate your model. _test.py_ visualizes the trained model and Lyapunov function. Due to the way models are saved, you need to create a folder called _saved_models_ in your working directory. To test a new system, add it to _true_dynamics.py_ and follow the conventions used there. 30 | 31 | -------------------------------------------------------------------------------- /train.py: -------------------------------------------------------------------------------- 1 | # A generic script for training locally 2 | 3 | import numpy as np 4 | import pandas as pd 5 | from sklearn.model_selection import train_test_split 6 | import matplotlib.pyplot as plt 7 | from pathlib import Path 8 | 9 | import torch 10 | import torch.nn as nn 11 | import torch.nn.functional as F 12 | import torch.optim as optim 13 | from torch.optim.lr_scheduler import LambdaLR 14 | from torch.utils.data import DataLoader 15 | 16 | from torch.utils.tensorboard import SummaryWriter 17 | 18 | import modules.convex_model as convex_model 19 | import modules.rootfind_model as rootfind_model 20 | import modules.stochastic_model as stochastic_model 21 | import modules.lyapunov_NN as L 22 | 23 | import true_dynamics as dynamics 24 | 25 | # Create Lyapunov NN, create stable model (convex_model, rootfind_model, stochastic_model) 26 | # Optional: define nominal model fhat (the above modules automatically do this) 27 | n = 2 # state dimension 28 | is_stochastic = False 29 | layer_sizes = np.array([n, 50, 50, 1]) 30 | ICNN = L.ICNN(layer_sizes) 31 | V = L.MakePSD(ICNN,n) 32 | if is_stochastic: 33 | f_net = stochastic_model.dynamics_model(V,n,mode=1,is_training = True) 34 | else: 35 | f_net = convex_model.dynamics_model(V,n,is_training = True) 36 | 37 | # Specify epochs, batch_size, learning_rate, loss function, optimizer, state dimension 38 | epochs = 300 39 | batch_size = 512 40 | learning_rate = 0.0025 41 | criterion = nn.MSELoss() 42 | optimizer = optim.Adam(f_net.parameters(), lr=learning_rate) 43 | # lr_lambda = lambda epoch: 1/np.log(epoch+2) # Optional learning rate scheduler 44 | # lr_scheduler = LambdaLR(optimizer, lr_lambda = lr_lambda, last_epoch=-1) 45 | 46 | # Set paths, generate/load/split data 47 | experiment = 'exp_name' + '.pth' 48 | PATH_f_net = './saved_models/' + experiment # TODO: PyTorch's torch.save isn't compatible w/pathlib 49 | dynamics.data_linear().gen_data() 50 | data = pd.read_csv(Path('./datasets/data_linear.csv')) 51 | data_input = data.values[:,:2] 52 | data_output = data.values[:,2:] 53 | Trn_input, Val_inp, Trn_target,Val_target = train_test_split(data_input, data_output, test_size=0.2,random_state=123) 54 | # Train_data has our training dataset and Valid_data has our validation dataset. 55 | Train_data = pd.concat([pd.DataFrame(Trn_input), pd.DataFrame(Trn_target)], axis=1) 56 | Valid_data = pd.concat([pd.DataFrame(Val_inp), pd.DataFrame(Val_target)], axis=1) 57 | # training and validation dataset 58 | train_dataset = dynamics.oversampdata(Train_data) 59 | valid_dataset = dynamics.oversampdata(Valid_data) 60 | 61 | train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True) 62 | test_loader = DataLoader(valid_dataset, batch_size=batch_size, shuffle=True) 63 | 64 | summaryPath = Path('./runs/'+ experiment) 65 | summaryPath.mkdir(parents=True, exist_ok=True) 66 | writer = SummaryWriter(summaryPath) 67 | 68 | f_net.train() 69 | for epoch in range(epochs): 70 | 71 | running_loss = 0.0 72 | 73 | for i, data in enumerate(train_loader): 74 | 75 | inputs, labels = data 76 | optimizer.zero_grad() 77 | if is_stochastic: 78 | loss = f_net(inputs, labels) 79 | else: 80 | outputs = f_net(inputs) 81 | loss = criterion(outputs, labels) 82 | loss.backward() 83 | optimizer.step() 84 | running_loss += loss.item() 85 | 86 | # lr_scheduler.step() 87 | writer.add_scalar('Loss', running_loss, epoch) 88 | for name, weight in f_net.named_parameters(): 89 | writer.add_histogram(name, weight, epoch) 90 | 91 | if epoch % 10 == 0: 92 | print("Epoch: ", epoch, "Running loss: ", running_loss) 93 | 94 | print('Finished Training') 95 | 96 | writer.close() 97 | torch.save(f_net.state_dict(), PATH_f_net) 98 | -------------------------------------------------------------------------------- /modules/convex_model.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import torch 3 | import torch.nn as nn 4 | import torch.nn.functional as F 5 | 6 | # Stabilizing strategy for convex Lyapunov functions 7 | 8 | class fhat(nn.Module): 9 | """ 10 | This is our 'nominal' model before modifying the dynamics 11 | 12 | layer_sizes : 1D array of shape (m,) where m-2 is the number of layers. First and last 13 | entires are input and output dimensions, respectively. 14 | add_state : binary variable. Can be ignored; only used in an example in the appendix. 15 | """ 16 | def __init__(self, layer_sizes, add_state = False): 17 | super().__init__() 18 | 19 | self.add_state = add_state 20 | layers = [] 21 | for i in range(len(layer_sizes)-2): 22 | layers.append(nn.Linear(layer_sizes[i], layer_sizes[i+1])) 23 | layers.append(nn.Softplus()) 24 | layers.append(nn.Linear(layer_sizes[-2], layer_sizes[-1])) 25 | self.fhat = nn.Sequential(*layers) 26 | 27 | def forward(self, x): 28 | 29 | if self.add_state: 30 | z = x + self.fhat(x) 31 | else: 32 | z = self.fhat(x) 33 | return z 34 | 35 | class dynamics_model(nn.Module): 36 | """ 37 | Stable dynamics model based on convex Lyapunov function 38 | 39 | V : Lyapunov neural network 40 | n : state dimension 41 | beta : number in (0,1] in the stability criterion V(x') <= beta V(x) 42 | is_training : binary variable indicating if a model is being trained. 43 | For training the stochastic model we may want to return just the gamma term or 44 | keep track of the previous 'state' i.e. means. 45 | return_gamma : binary variable. Indicates whether to return gamma(x)*fhat(x) or gamma(x) 46 | f : optional user-defined nominal model. 47 | """ 48 | def __init__(self, V, n, beta = 0.99, is_training = False, return_gamma = False, f = None): 49 | super().__init__() 50 | 51 | if f is None: 52 | self.fhat = fhat(np.array([n, 25, 25, 25, n]), False) 53 | else: 54 | self.fhat = f 55 | self.V = V 56 | self.beta = beta 57 | self.is_training = is_training 58 | self.is_init = True 59 | self.return_gamma = return_gamma 60 | self.n = n 61 | 62 | def forward(self, x): 63 | 64 | if self.is_training: 65 | # This is for training 66 | target = self.beta*self.V(x) 67 | current = self.V(self.fhat(x)) 68 | 69 | fx = self.fhat(x)*((target - F.relu(target - current)) / current) 70 | if self.return_gamma: 71 | return ((target - F.relu(target - current)) / current) 72 | else: 73 | return fx 74 | else: 75 | # This is for testing -- particularly stochastic models in order to track the mean/var 76 | if self.is_init: 77 | target = self.beta*self.V(x) 78 | self.is_init = False 79 | 80 | else: 81 | target = self.beta*self.V(self.fx) 82 | 83 | current = self.V(self.fhat(x)) 84 | 85 | fx = self.fhat(x)*((target - F.relu(target - current)) / current) 86 | self.fx = fx 87 | if self.return_gamma: 88 | return ((target - F.relu(target - current)) / current) 89 | else: 90 | return self.fx 91 | 92 | def reset(self): 93 | self.is_init = True 94 | 95 | 96 | 97 | class dynamics_nonincrease(nn.Module): 98 | """ 99 | Modifies fhat by ensuring 'non-expansiveness'. See the appendix for explanation/example. 100 | i.e. it never moves in a direction that is acute with the gradient of V at x_t 101 | 102 | V : Lyapunov neural network 103 | n : state dimension 104 | f : optional user-defined nominal model. 105 | """ 106 | def __init__(self, V, n, f = None): 107 | super().__init__() 108 | 109 | if f is None: 110 | self.fhat = fhat(np.array([n, 25, 25, 25, n]), False) 111 | else: 112 | self.fhat = f 113 | 114 | self.V = V 115 | 116 | def forward(self, x): 117 | 118 | x = x.requires_grad_(True) 119 | fhatx = self.fhat(x) 120 | Vx = self.V(x) 121 | with torch.enable_grad(): 122 | gV = torch.autograd.grad(Vx, x, retain_graph = True, only_inputs=True, grad_outputs=torch.ones_like(Vx))[0] 123 | 124 | fx = x + self.fhat(x) - F.relu((gV*(self.fhat(x) - x)).sum(dim = -1, keepdim = True))*gV/(torch.norm(gV, dim = -1, keepdim = True)**2) 125 | 126 | return fx 127 | -------------------------------------------------------------------------------- /dynamics_plotting.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | import matplotlib.gridspec as gridspec 4 | 5 | from mpl_toolkits.mplot3d import Axes3D 6 | import pylab 7 | import random 8 | 9 | import torch 10 | import torch.nn as nn 11 | import torch.nn.functional as F 12 | import torch.optim as optim 13 | 14 | class plot_dynamics(nn.Module): 15 | def __init__(self, f, V, reset_model = False): 16 | super().__init__() 17 | 18 | self.f = f 19 | self.V = V 20 | self.reset_model = reset_model 21 | 22 | def get_trajectory(self, x0, steps): 23 | 24 | x = x0 25 | X = torch.empty([steps,x.shape[-1]]) 26 | X[0,:] = x.squeeze() 27 | 28 | for i in range(steps-1): 29 | 30 | with torch.no_grad(): 31 | 32 | x = self.f(x) 33 | X[i+1,:] = x 34 | 35 | return X.detach().numpy() 36 | 37 | 38 | def plot_trajectory(self, x0, kwargs, sample_paths = 1, show_ls = True, steps = 600, xy_plane = True, ax = plt): 39 | 40 | if show_ls: 41 | 42 | x = np.arange(-20.0, 20.0, 0.25) 43 | y = np.arange(-20.0, 20.0, 0.25) 44 | 45 | X, Y = np.meshgrid(x, y) 46 | Z = np.ndarray(X.shape) 47 | 48 | for i in range(0, len(x)): 49 | for j in range(0, len(x)): 50 | z = torch.tensor([[X[i][j],Y[i][j]]], dtype = torch.float) 51 | Z[i][j] = (self.V(z)) 52 | 53 | 54 | # Create contour lines or level curves using matpltlib.pyplot module 55 | contours = ax.contour(X, Y, Z, linewidths = 1) 56 | 57 | # Display z values on contour lines 58 | ax.clabel(contours, inline=1, fontsize=10, fmt = '%1.2f') 59 | 60 | if xy_plane: 61 | for i in range(sample_paths): 62 | if self.reset_model: 63 | self.f.reset() 64 | X_val = self.get_trajectory(x0, steps) 65 | if i > 0: 66 | kwargs["label"] = None 67 | ax.plot(X_val[:,0],X_val[:,1], **kwargs) 68 | if i==0: 69 | ax.plot(X_val[-1,0], X_val[-1,1], color = "tab:blue", marker = '*', markersize = 10) 70 | else: 71 | for i in range(sample_paths): 72 | X_val = self.get_trajectory(x0, steps = steps) 73 | if i > 0: 74 | kwargs["label"] = None 75 | ax.plot(X_val, **kwargs) 76 | 77 | return X_val 78 | 79 | def surface_plot(self, x0, plot_dynamics = True): 80 | 81 | fig = plt.figure() 82 | ax = fig.add_subplot(111, projection='3d') 83 | 84 | x = y = np.arange(-1.0, 1.0, 0.01) 85 | X, Y = np.meshgrid(x, y) 86 | Z = np.ndarray(X.shape) 87 | 88 | for i in range(0, len(x)): 89 | for j in range(0, len(x)): 90 | z = torch.tensor([[X[i][j],Y[i][j]]], dtype = torch.float) 91 | Z[i][j] = (self.V(z)) 92 | 93 | ax.plot_surface(X, Y, Z, cmap='viridis', edgecolor='none', alpha=.5) 94 | 95 | 96 | if plot_dynamics: 97 | X_val = self.get_trajectory(self.f, x0) 98 | X_val = torch.tensor(X_val, dtype = torch.float).view((-1,1,2)) 99 | with torch.no_grad(): 100 | V_vals = (self.V(X_val)).squeeze() 101 | ax.plot3D(X_val[:,:,0],X_val[:,:,1],V_vals, 'r') 102 | ax.scatter(X_val[:,:,0],X_val[:,:,1],V_vals, color = 'r',) 103 | 104 | ax.grid(False) 105 | ax.w_yaxis.set_pane_color((1.0, 1.0, 1.0, 1.0)) 106 | ax.w_xaxis.set_pane_color((1.0, 1.0, 1.0, 1.0)) 107 | ax.xaxis.set_ticklabels([]) 108 | ax.yaxis.set_ticklabels([]) 109 | ax.zaxis.set_ticklabels([]) 110 | ax.xaxis.set_ticks([]) 111 | ax.yaxis.set_ticks([]) 112 | ax.zaxis.set_ticks([]) 113 | plt.show() 114 | 115 | 116 | 117 | class plot_dynamics_3D(nn.Module): 118 | def __init__(self, f, V, show_mu = False, is_stochastic = False): 119 | super().__init__() 120 | 121 | self.f = f 122 | self.V = V 123 | self.show_mu = show_mu 124 | self.is_stochastic = is_stochastic 125 | 126 | def get_trajectory(self, x0, steps): 127 | 128 | if self.show_mu: 129 | mu = x0 130 | 131 | X = torch.empty([steps,mu.shape[-1]]) 132 | X[0,:] = mu.squeeze() 133 | else: 134 | x = x0 135 | X = torch.empty([steps, x.shape[-1]]) 136 | X[0,:] = x.squeeze() 137 | 138 | for i in range(steps-1): 139 | if self.show_mu: 140 | 141 | pi, normal = self.f(mu) 142 | mu = torch.sum(pi.probs.view(-1,1)*normal.loc,1).view(-1,1,x0.shape[-1]) 143 | X[i+1,:] = mu 144 | 145 | else: 146 | if self.is_stochastic: 147 | x = self.f.sample(x) 148 | else: 149 | x = self.f(x) 150 | X[i+1,:] = x 151 | 152 | return X.detach().numpy() 153 | 154 | 155 | def plot_trajectory(self, x0, kwargs, sample_paths = 1, steps = 200): 156 | 157 | for i in range(sample_paths): 158 | X_val = self.get_trajectory(x0, steps) 159 | if i > 0: 160 | kwargs["label"] = None 161 | 162 | plt.plot(X_val[:, 0], X_val[:, 1], X_val[:, 2], **kwargs) 163 | 164 | return X_val 165 | -------------------------------------------------------------------------------- /modules/lyapunov_NN.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | import torch 5 | import torch.nn as nn 6 | import torch.nn.functional as F 7 | import torch.optim as optim 8 | 9 | #ICNN-based Lyapunov NN : Taken from Manek + Kolter, with some modifications 10 | # https://github.com/locuslab/stable_dynamics 11 | 12 | class ReHU(nn.Module): 13 | """ Rectified Huber unit""" 14 | def __init__(self, d = 1): 15 | super().__init__() 16 | self.a = 1/(2*d) 17 | self.b = d/2 18 | 19 | def forward(self, x): 20 | 21 | return torch.max(torch.clamp(torch.sign(x)*self.a*x**2,min=0,max=self.b),x-self.b) 22 | 23 | 24 | class MakePSD(nn.Module): 25 | def __init__(self, f, n, eps=0.01, d=1.0): 26 | super().__init__() 27 | self.f = f 28 | 29 | self.eps = eps 30 | self.d = d 31 | self.n = n 32 | self.rehu = ReHU(self.d) 33 | 34 | 35 | def forward(self, x): 36 | 37 | 38 | zero = self.f(torch.zeros((1,1,x.shape[-1]))) 39 | smoothed_output = self.rehu(self.f(x) - zero) 40 | 41 | quadratic_under = self.eps*(torch.norm(x, dim = -1, keepdim = True)**2) 42 | 43 | return smoothed_output + quadratic_under 44 | 45 | 46 | class ICNN(nn.Module): 47 | def __init__(self, layer_sizes, activation=ReHU()): 48 | super().__init__() 49 | 50 | 51 | self.W = nn.ParameterList([nn.Parameter(torch.Tensor(l, layer_sizes[0])) 52 | for l in layer_sizes[1:]]) 53 | self.U = nn.ParameterList([nn.Parameter(torch.Tensor(layer_sizes[i+1], layer_sizes[i])) 54 | for i in range(1,len(layer_sizes)-1)]) 55 | self.bias = nn.ParameterList([nn.Parameter(torch.Tensor(l)) for l in layer_sizes[1:]]) 56 | 57 | self.act = activation 58 | self.reset_parameters() 59 | 60 | def reset_parameters(self): 61 | # copying from PyTorch Linear 62 | for W in self.W: 63 | nn.init.kaiming_uniform_(W, a=5**0.5) 64 | for U in self.U: 65 | nn.init.kaiming_uniform_(U, a=5**0.5) 66 | for i,b in enumerate(self.bias): 67 | fan_in, _ = nn.init._calculate_fan_in_and_fan_out(self.W[i]) 68 | bound = 1 / (fan_in**0.5) 69 | nn.init.uniform_(b, -bound, bound) 70 | 71 | def forward(self, x): 72 | z = F.linear(x, self.W[0], self.bias[0]) 73 | z = self.act(z) 74 | 75 | for W,b,U in zip(self.W[1:-1], self.bias[1:-1], self.U[:-1]): 76 | z = F.linear(x, W, b) + F.linear(z, F.softplus(U)) / U.shape[0] 77 | z = self.act(z) 78 | 79 | return F.linear(x, self.W[-1], self.bias[-1]) + F.linear(z, F.softplus(self.U[-1])) / self.U[-1].shape[0] 80 | 81 | 82 | #Lyapunov NN proposed by Richards et al 83 | # https://github.com/befelix/safe_learning/blob/master/examples/utilities.py 84 | 85 | #For simplicty, this version assumes all hidden dimensions are the same 86 | class Lyapunov_NN(nn.Module): 87 | def __init__(self, f, quadratic_under = True, epsilon = 0.01): 88 | super().__init__() 89 | 90 | self.f = f 91 | self.quadratic_under = quadratic_under 92 | self.eps = epsilon 93 | 94 | def forward(self, x): 95 | 96 | if self.quadratic_under: 97 | return (torch.norm(self.f(x), dim = -1, keepdim = True)**2) + self.eps*(torch.norm(x, dim = -1, keepdim = True)**2) 98 | else: 99 | return F.linear(self.f(x), self.f(x)) 100 | 101 | #Feedforward NN with positive definite weights 102 | class PD_weights(nn.Module): 103 | def __init__(self, layer_sizes, activation=F.relu, epsilon = 1e-6, make_convex = True): 104 | super().__init__() 105 | 106 | self.make_convex = make_convex 107 | 108 | self.G = nn.ParameterList([nn.Parameter(torch.Tensor(np.int(np.ceil((layer_sizes[i]+1)/2)), layer_sizes[i])) 109 | for i in range(0,len(layer_sizes)-1)]) 110 | self.G_2 = nn.Parameter(torch.Tensor(layer_sizes[1] - layer_sizes[0], layer_sizes[0])) 111 | 112 | 113 | if self.make_convex: 114 | self.G_conv = nn.ParameterList([nn.Parameter(torch.Tensor(l, layer_sizes[0])) 115 | for l in layer_sizes[1:]]) 116 | 117 | self.I_input = nn.Parameter(torch.eye(layer_sizes[0]), requires_grad = False) 118 | self.I = nn.Parameter(torch.eye(layer_sizes[1]), requires_grad = False) 119 | self.I_end = nn.Parameter(torch.eye(layer_sizes[-1]), requires_grad = False) 120 | self.act = activation 121 | self.eps = epsilon 122 | self.rehu = ReHU() 123 | self.reset_parameters() 124 | 125 | def reset_parameters(self): 126 | 127 | for G in self.G: 128 | nn.init.kaiming_uniform_(G, a=5**0.5) 129 | if self.make_convex: 130 | for G in self.G_conv: 131 | nn.init.kaiming_uniform_(G, a=5**0.5) 132 | nn.init.kaiming_uniform_(self.G_2, a=5**0.5) 133 | 134 | def forward(self, x): 135 | 136 | if self.make_convex: 137 | 138 | W1 = torch.mm(torch.transpose(self.G[0],0,1),self.G[0]) + self.eps*self.I_input 139 | W = torch.cat((W1, self.G_2),0) 140 | z = F.linear(x, W) 141 | z = self.rehu(z) 142 | 143 | for G,G_conv in zip(self.G[1:-1], self.G_conv[:-1]): 144 | W_conv = F.linear(x, G_conv) 145 | W = torch.mm(torch.transpose(self.rehu(G),0,1),self.rehu(G)) + self.eps*self.I 146 | z = W_conv + F.linear(z, W) 147 | z = self.rehu(z) 148 | 149 | W_conv = F.linear(x, self.rehu(self.G_conv[-1])) 150 | W = torch.mm(torch.transpose(self.rehu(self.G[-1]),0,1), self.rehu(self.G[-1])) + self.eps*self.I 151 | return W_conv + F.linear(z, W) 152 | 153 | else: 154 | 155 | W1 = torch.mm(torch.transpose(self.G[0],0,1),self.G[0]) + self.eps*self.I_input 156 | W = torch.cat((W1, self.G_2),0) 157 | z = F.linear(x, W) 158 | z = self.act(z) 159 | 160 | for G in self.G[1:-1]: 161 | 162 | W = torch.mm(torch.transpose(G,0,1),G) + self.eps*self.I 163 | z = F.linear(z, W) 164 | z = self.act(z) 165 | 166 | W = torch.mm(torch.transpose(self.G[-1],0,1), self.G[-1]) + self.eps*self.I 167 | 168 | return F.linear(z, W) 169 | -------------------------------------------------------------------------------- /modules/rootfind_model.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | from copy import deepcopy 4 | 5 | import torch 6 | import torch.nn as nn 7 | import torch.nn.functional as func 8 | import torch.optim as optim 9 | 10 | # Stabilizing strategy for non-convex Lyapunov functions (implicit method) 11 | 12 | class newton_iter(nn.Module): 13 | """ 14 | Newton iteration function. One step of Newton's method for finding gamma i.e. 15 | This is the function x - f(x)/f'(x) in Newton's method in terms of fhat and V wrt gamma. 16 | 17 | fhat : nominal model 18 | V : Lyapunov neural network 19 | """ 20 | def __init__(self, fhat, V): 21 | super().__init__() 22 | 23 | self.fhat = fhat 24 | self.V = V 25 | 26 | def forward(self, fhatx, target, gamma, backprop = False): 27 | 28 | Vfx = self.V(fhatx*gamma) - target 29 | Vfx.clone().detach().requires_grad_() 30 | with torch.enable_grad(): 31 | dV_da = torch.autograd.grad(Vfx, gamma, create_graph = backprop, grad_outputs=torch.ones_like(Vfx))[0] 32 | 33 | F = gamma - (self.V(fhatx*gamma) - target)/dV_da 34 | 35 | return F 36 | 37 | class dynamics_model(nn.Module): 38 | """ 39 | This is where we bring together: 40 | 1. The Newton iteration function (newton_iter) 41 | 2. The custom autograd function for backprop though Newton's method (rootfind_train) 42 | 43 | V : Lyapunov neural network 44 | n : state dimension 45 | is_training : binary variable. When True, this triggers a data management process. 46 | beta : number in (0,1] in the stability criterion V(x') <= beta V(x) 47 | f : optional user-defined nominal model. 48 | """ 49 | def __init__(self, V, n, is_training = False, beta = 0.99, f = None): 50 | super().__init__() 51 | 52 | if f is None: 53 | self.fhat = nn.Sequential(nn.Linear(n, 25), nn.Softplus(), 54 | nn.Linear(25, 25), nn.Softplus(), 55 | nn.Linear(25, n)) 56 | else: 57 | self.fhat = f 58 | 59 | self.V = V 60 | self.beta = beta 61 | self.is_training = is_training 62 | 63 | self.F = newton_iter(self.fhat, self.V) 64 | 65 | def forward(self, x): 66 | 67 | if self.is_training: 68 | y = torch.empty_like(x) 69 | x_usual, x_rootfind, m = self.split_rootfind(x) 70 | rootfind = rootfind_train.apply 71 | fhatx = self.fhat(x_rootfind) 72 | target = self.beta*self.V(x_rootfind) 73 | x_root = rootfind(self.V, self.F, fhatx, target, x_rootfind) 74 | y[torch.where(m)] = self.fhat(x_usual) 75 | y[torch.where(~m)] = x_root 76 | return y 77 | else: 78 | fhatx = self.fhat(x) 79 | target = self.beta*self.V(x) 80 | rootfind = rootfind_train.apply 81 | x_root = rootfind(self.V, self.F, fhatx, target, x) 82 | return x_root 83 | 84 | def split_rootfind(self, inputs): 85 | 86 | fhatx = self.fhat(inputs) 87 | target = self.beta*self.V(inputs) 88 | m = (self.V(fhatx) <= target).squeeze() 89 | 90 | x_usual = inputs[torch.where(m)] 91 | x_rootfind = inputs[torch.where(~m)] 92 | 93 | return x_usual, x_rootfind, m 94 | 95 | 96 | class rootfind_train(torch.autograd.Function): 97 | """ 98 | Performs forward and backward pass of implicit dynamics model by incorporating an 99 | implementation of Newton's method combined with bisection method. 100 | Newton's method is not guaranteed to converge in the case of nonconvex Lyapunov function, 101 | but the bisection method is. We use the bisection method for backup iterations 102 | when Newton's method iteration moves outside the current bisection method interval. 103 | 104 | V : Lyapunov neural network 105 | F : newton_iter 106 | fhatx : nominal model evaluated at x 107 | target : value of V to be less than or equal to e.g. beta*V(x). 108 | In order to train V it is important that target depends on V. 109 | x : current state 110 | """ 111 | @staticmethod 112 | def forward(ctx,V,F,fhatx,target,x): 113 | 114 | ctx.V = V 115 | ctx.F = F 116 | 117 | tol = 0.0001 118 | 119 | gamma_temp = torch.ones(size = (x.shape[0], 1, 1), requires_grad = True) 120 | 121 | # Since V(fhatx*1) > target, we stop iterating when we get sufficiently 122 | # close to the level set 123 | m = (ctx.V(fhatx) - target > 0.0).squeeze() 124 | end_1 = torch.zeros_like(gamma_temp, requires_grad = False) 125 | end_2 = torch.ones_like(gamma_temp, requires_grad = False) 126 | iter = 0 127 | 128 | while m.nonzero().shape[0] > 0 and iter < 1000: 129 | 130 | a = gamma_temp[torch.where(m)].requires_grad_(True) 131 | fx = fhatx[torch.where(m)].requires_grad_(True) 132 | t = target[torch.where(m)].requires_grad_(True) 133 | with torch.enable_grad(): 134 | a = ctx.F(fx,t,a) #take Newton step 135 | gamma_temp[torch.where(m)] = a 136 | #bisection method 137 | m1_bisec = (gamma_tempend_2).squeeze() 139 | m_bisec = ((m1_bisec + m2_bisec) > 0) 140 | if m_bisec.nonzero().shape[0] > 0: #check if bisection is necessary 141 | a_bisec = end_1[torch.where(m_bisec)] + (end_2[torch.where(m_bisec)] - end_1[torch.where(m_bisec)])/2 142 | fx_bisec = fhatx[torch.where(m_bisec)] 143 | t_bisec = target[torch.where(m_bisec)] 144 | end1_temp = end_1[torch.where(m_bisec)] 145 | end2_temp = end_2[torch.where(m_bisec)] 146 | 147 | m_end2 = (np.sign(ctx.V(fx_bisec*a_bisec) - t_bisec)*np.sign(ctx.V(fx_bisec*end1_temp) - t_bisec) < 0).squeeze() 148 | m_end1 = (np.sign(ctx.V(fx_bisec*a_bisec) - t_bisec)*np.sign(ctx.V(fx_bisec*end2_temp) - t_bisec) < 0).squeeze() 149 | 150 | end_1[torch.where(m_end2)] = end_1[torch.where(m_end2)] 151 | end_2[torch.where(m_end2)] = a_bisec[torch.where(m_end2)] 152 | 153 | end_1[torch.where(m_end1)] = a_bisec[torch.where(m_end1)] 154 | end_2[torch.where(m_end1)] = end_2[torch.where(m_end1)] 155 | 156 | gamma_temp[torch.where(m_bisec)] = a_bisec.requires_grad_(True) 157 | 158 | 159 | m = (torch.abs(ctx.V(fhatx*gamma_temp) - target) > tol).squeeze() 160 | iter += 1 161 | 162 | gamma = gamma_temp.clone().detach().requires_grad_(True) 163 | x_root = (fhatx*gamma) 164 | 165 | ctx.gamma = gamma 166 | 167 | #Since fhatx and target are inputs to the rootfinding algorithm, we backprop 168 | # through them and consequently through their parameters 169 | ctx.save_for_backward(fhatx, target, x_root) 170 | 171 | return x_root 172 | 173 | @staticmethod 174 | def backward(ctx, grad_output): 175 | 176 | grad_input = grad_output.clone() 177 | 178 | fhatx, target, x_root = ctx.saved_tensors 179 | 180 | gamma = ctx.gamma 181 | V = ctx.V 182 | F = ctx.F 183 | 184 | with torch.enable_grad(): 185 | 186 | Fx = F(fhatx, target, gamma, backprop = True) # TODO: We assume we are close to the root when differentiating -- ensure iterate is `stable` 187 | dF_df = torch.autograd.grad(Fx, fhatx, create_graph=True, grad_outputs=torch.ones_like(Fx))[0] 188 | dF_dt = torch.autograd.grad(Fx, target, create_graph=False, grad_outputs=torch.ones_like(Fx))[0] 189 | 190 | grad_rootfind_f = Fx*grad_input + torch.bmm(grad_input, torch.bmm(torch.transpose(fhatx,1,2),dF_df)) 191 | grad_rootfind_t = torch.bmm(grad_input, torch.transpose(fhatx,1,2))*dF_dt 192 | 193 | return None, None, grad_rootfind_f, grad_rootfind_t, None 194 | -------------------------------------------------------------------------------- /modules/stochastic_model.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | from copy import deepcopy 4 | 5 | import torch 6 | import torch.nn as nn 7 | import torch.nn.functional as F 8 | import torch.optim as optim 9 | 10 | from torch.distributions import Normal, OneHotCategorical 11 | from torch.distributions.multivariate_normal import MultivariateNormal 12 | from torch.distributions.beta import Beta 13 | 14 | from modules.lyapunov_NN import ReHU 15 | 16 | import modules.convex_model as convex_model 17 | import modules.rootfind_model as rootfind_model 18 | 19 | #Stabilizing strategy for stochastic systems (convex or nonconvex Lyapunov functions) 20 | 21 | #Snippets taken from: https://github.com/tonyduan/mdn/blob/master/mdn/models.py 22 | 23 | # class MDN_module(nn.Module): 24 | # #This is where we bring together: 25 | # # 1. Stochastic targets for V 26 | # # 2. The rootfinding-based training/inference method 27 | # 28 | # def __init__(self, fhat, V, n = None, beta = 0.99, k = 1, is_training = True, show_mu = False): 29 | # super().__init__() 30 | # 31 | # self.fhat = fhat 32 | # self.V = V 33 | # self.beta = beta 34 | # self.k = k 35 | # self.n = n 36 | # self.is_training = is_training 37 | # self.show_mu = show_mu 38 | # 39 | # def forward(self, x, y = None): 40 | # 41 | # fhatx = self.fhat(x) 42 | # 43 | # mu, var = torch.split(fhatx, fhatx.shape[-1] // 2, dim=-1) 44 | # mu = torch.stack(mu.split(mu.shape[1] // self.k, 1)).view(-1,1,self.n) 45 | # var = torch.exp(torch.stack(var.split(var.shape[1] // self.k, 1))).view(-1,1,self.n) 46 | # mu_stable = mu*((self.beta*self.V(x) - F.relu(self.beta*self.V(x) - self.V(mu))) / self.V(mu)) 47 | # 48 | # model_dist = MultivariateNormal(mu_stable, torch.diag_embed(var)) 49 | # fx = (model_dist.rsample()) 50 | # 51 | # if self.is_training: 52 | # logp_y = -(model_dist.log_prob(y).squeeze()).mean() 53 | # 54 | # return logp_y 55 | # else: 56 | # if self.show_mu: 57 | # return mu_stable 58 | # else: 59 | # return fx 60 | 61 | class dynamics_model(nn.Module): 62 | """ 63 | This is where we bring together: 64 | 1. Stochastic model (MDN) 65 | 2. Stable models (convex_model or rootfind_model) 66 | 67 | V : Lyapunov neural network 68 | n : state dimension 69 | f : nominal model 70 | beta : number in (0,1] in the stability criterion V(x') <= beta V(x) 71 | k : number of mixtures in the MDN 72 | mode : None, 1 or some other integer 73 | None is a vanilla MDN 74 | 1 is convexity-based MDN 75 | otherwise, use implicit method 76 | is_training : binary variable. Gets passed to other modules; also used in forward pass 77 | to indicate if we need negative log-likelihood or a sample from MDN. 78 | show_mu : binary variable. If is_training == False then this will either output the 79 | conditional mean or a sample from the MDN. 80 | """ 81 | def __init__(self, V, n, f = None, beta = 0.99, k = 1, mode = None, is_training = False, show_mu = False): 82 | super().__init__() 83 | 84 | if f is None: 85 | self.fhat = nn.Sequential(nn.Linear(n, 25), nn.Softplus(), 86 | nn.Linear(25, 25), nn.Softplus(), 87 | nn.Linear(25, 25), nn.Softplus(), 88 | nn.Linear(25, 2*n*k)) 89 | else: 90 | self.fhat = f 91 | 92 | self.V = V 93 | self.beta = beta 94 | self.k = k 95 | self.n = n 96 | self.is_training = is_training 97 | self.show_mu = show_mu 98 | 99 | self.pi = pi_Network(self.n, self.k) 100 | 101 | if mode is None: 102 | self.mu = MDN_dynamics(self.fhat, self.n, self.k, True) 103 | self.var_dynamics = MDN_dynamics(self.fhat, self.n, self.k,False) 104 | self.gamma = lambda x: 1.0 105 | self.mean_dynamics = lambda x: 1.0 106 | elif mode == 1: 107 | self.mu = MDN_dynamics(self.fhat, self.n, self.k, True) 108 | self.mean_dynamics = mean_dynamics(self.mu, self.pi, self.k, self.n) 109 | self.gamma = convex_model.dynamics_model(V = self.V, n = n, beta = beta, is_training = is_training, f = self.mean_dynamics) 110 | 111 | self.var = MDN_dynamics(self.fhat, self.n, self.k, False) 112 | self.var_dynamics = variance_dynamics(self.pi, self.gamma, self.var, self.V, self.k) 113 | 114 | else: 115 | 116 | self.mu = MDN_dynamics(self.fhat, self.n, self.k, True) 117 | self.mean_dynamics = mean_dynamics(self.mu, self.pi, self.k, self.n) 118 | self.gamma = rootfind_model.dynamics_model(self.V, self.n, is_training=is_training, beta=beta, f = self.mean_dynamics) 119 | 120 | self.var = MDN_dynamics(self.fhat, self.n, self.k,False) 121 | self.var_dynamics = variance_dynamics(self.pi, self.gamma, self.var, self.V, self.k) 122 | 123 | def forward(self, x, y = None): 124 | 125 | pi = self.pi(x) 126 | 127 | mu_stable = self.gamma(x)*(self.mu(x)/self.mean_dynamics(x)) 128 | var = torch.clamp(self.var_dynamics(x),min = 1e-8) 129 | model_dist = MultivariateNormal(mu_stable, covariance_matrix = torch.diag_embed(var)) 130 | 131 | if self.is_training: 132 | logp_y = (model_dist.log_prob(y).squeeze()) 133 | loss = torch.logsumexp(torch.log(pi.probs).squeeze() + logp_y, dim=-1) 134 | return -torch.mean(loss) 135 | 136 | else: 137 | if self.show_mu: 138 | mean = torch.sum(pi.probs.view(-1,self.k,1)*mu_stable,1) 139 | return mean 140 | else: 141 | return torch.sum(pi.sample().view(-1,self.k,1)*model_dist.sample(),1) 142 | 143 | def reset(self): 144 | self.gamma.reset() 145 | 146 | class MDN_dynamics(nn.Module): 147 | """ 148 | This module takes a MDN and outputs the mean and variance parameters 149 | 150 | fhat : The user-provided MDN model 151 | n : state dimension 152 | k : number of mixtures 153 | get_mu : binary variable indicating whether to output the mean parameters 154 | """ 155 | def __init__(self, fhat, n, k, get_mu = True): 156 | super().__init__() 157 | self.fhat = fhat 158 | self.n = n 159 | self.k = k 160 | self.get_mu = get_mu 161 | 162 | def forward(self, x): 163 | fhatx = self.fhat(x) 164 | mu, var = torch.split(fhatx, fhatx.shape[-1] // 2, dim=-1) 165 | if self.get_mu: 166 | output = torch.stack(mu.split(mu.shape[-1] // self.k, 1)).view(-1,self.k,self.n) 167 | else: 168 | # torch.exp is also possible here, but using ELU + 1 tends to be more `stable' 169 | output = torch.clamp(F.elu(torch.stack(var.split(var.shape[-1] // self.k, 1))).view(-1,self.k,self.n) + 1, 1e-8, max = 100) 170 | return output 171 | 172 | class pi_Network(nn.Module): 173 | """ 174 | This is where we define the mixture coefficients network 175 | 176 | in_dim : input dimension (e.g. n) 177 | out_dim : output dimension (e.g. k) 178 | hidden_dim : hidden dimension 179 | """ 180 | def __init__(self, in_dim, out_dim, hidden_dim=None): 181 | super().__init__() 182 | if hidden_dim is None: 183 | hidden_dim = 25 184 | self.network = nn.Sequential(nn.Linear(in_dim, hidden_dim), nn.Softplus(), 185 | nn.Linear(hidden_dim, hidden_dim), nn.Tanh(), 186 | nn.Linear(hidden_dim, out_dim)) 187 | 188 | def forward(self, x): 189 | params = self.network(x) 190 | return OneHotCategorical(logits=params) 191 | 192 | class mean_dynamics(nn.Module): 193 | """ 194 | Conditional mean dynamics derived from the means parameters and mixture coefficients 195 | of a MDN 196 | 197 | MDN_means : network for the mean parameters (e.g. MDN_dynamics) 198 | pi_Network : network for the mixture coefficients (e.g. pi_Network) 199 | k : number of mixtures 200 | n : state dimension 201 | """ 202 | def __init__(self, MDN_means, pi_Network, k, n): 203 | super().__init__() 204 | 205 | self.MDN_means = MDN_means 206 | self.pi = pi_Network 207 | self.k = k 208 | self.n = n 209 | 210 | def forward(self, x): 211 | 212 | mu = self.MDN_means(x) 213 | mean = torch.sum(self.pi(x).probs.view(-1,self.k,1)*mu,1).view(-1,1,self.n) 214 | return mean 215 | 216 | class variance_dynamics(nn.Module): 217 | """ 218 | Conditional covariance dynamics derived from the variance parameters of a MDN. 219 | Here the user can define the way in which the covariance goes to zero. 220 | 221 | pi_Network : network for the mixture coefficients (e.g. pi_Network) 222 | mean_dynamics : conditional mean (e.g. mean_dynamics module) 223 | MDN_vars : network for the variance parameters (e.g. MDN_dynamics) 224 | V : Lyapunov neural network 225 | k : number of mixtures 226 | """ 227 | def __init__(self, pi_Network, mean_dynamics, MDN_vars, V, k): 228 | super().__init__() 229 | 230 | self.pi = pi_Network 231 | self.mean_dynamics = mean_dynamics 232 | self.MDN_vars = MDN_vars 233 | self.V = V 234 | self.k = k 235 | 236 | def forward(self, x): 237 | 238 | # Define a function of mean_dynamics that goes to zero as mean_dynamics --> 0 239 | # e.g. norm, V 240 | scaleVar = torch.norm(self.mean_dynamics(x),dim = -1) 241 | 242 | output = scaleVar.unsqueeze(-1)*torch.clamp(self.MDN_vars(x), min = 1e-8, max = 100) 243 | 244 | return output 245 | -------------------------------------------------------------------------------- /true_dynamics.py: -------------------------------------------------------------------------------- 1 | #This is where we generate data for experiments 2 | 3 | import numpy as np 4 | import pandas as pd 5 | from pathlib import Path 6 | import matplotlib.pyplot as plt 7 | import torch 8 | from torch.utils.data import DataLoader, Dataset 9 | from sklearn.model_selection import train_test_split 10 | from torch.distributions import Beta 11 | 12 | mainPath = Path('./datasets') 13 | mainPath.mkdir(exist_ok=True) 14 | 15 | class data_linear(): 16 | def __init__(self, add_noise = False): 17 | 18 | self.add_noise = add_noise 19 | A = np.array([[0.90, 1],[0, 0.90]]) 20 | self.A = A.transpose() 21 | 22 | def f(self, state): 23 | 24 | if self.add_noise: 25 | x = np.dot(state,self.A) + 0.05*state*np.random.normal(0,1) 26 | else: 27 | x = np.dot(state,self.A) 28 | 29 | return x 30 | 31 | def gen_data(self, x0=None, steps = None): 32 | 33 | data = [] 34 | if x0 is None: 35 | 36 | X = np.linspace(-5,5,num=14) 37 | 38 | for x1 in X: 39 | for x2 in X: 40 | 41 | x = np.array([[x1,x2]]) 42 | 43 | for i in range(30): 44 | x_new = self.f(x) 45 | 46 | data.append(np.array((x,x_new)).reshape((1,4)).squeeze()) 47 | x = x_new 48 | 49 | if self.add_noise: 50 | np.savetxt(mainPath/'data_linear_noise.csv', data, delimiter=",") 51 | else: 52 | np.savetxt(mainPath/'data_linear.csv', data, delimiter=",") 53 | 54 | else: 55 | 56 | if steps is None: 57 | steps = 50 58 | else: 59 | steps = steps 60 | x = np.array(x0.view(1,-1).numpy()) 61 | data.append(np.array((x)).reshape((1,2)).squeeze()) 62 | for i in range(steps): 63 | x_new = self.f(x) 64 | data.append(np.array((x_new)).reshape((1,2)).squeeze()) 65 | x = x_new 66 | return np.array([data]).squeeze() 67 | 68 | 69 | class data_nonConvex(): 70 | def __init__(self): 71 | 72 | self.h = 0.1 73 | 74 | def f(self, state): 75 | x, y = np.squeeze(state) 76 | return np.array([[y, -y - np.sin(x) - 2*np.clip(x+y,a_min = -1, a_max = 1)]]) 77 | 78 | def gen_data(self, x0 = None, steps = None): 79 | data = [] 80 | if x0 is None: 81 | # X = np.linspace(-5,5,num=15) 82 | X = np.linspace(-6,6,num=15) 83 | for x1 in X: 84 | for x2 in X: 85 | x = np.array([[x1,x2]]) 86 | for i in range(40): 87 | 88 | k1 = self.f(x) 89 | k2 = self.f(x + self.h*(k1/2)) 90 | k3 = self.f(x + self.h*(k2/2)) 91 | k4 = self.f(x + self.h*k3) 92 | x_new = x + (self.h/6)*(k1 + 2*k2+ 2*k3 + k4) 93 | data.append(np.array((x,x_new)).reshape((1,4)).squeeze()) 94 | x = x_new 95 | 96 | np.savetxt(mainPath/'data_nonConvex.csv', data, delimiter=",") 97 | 98 | else: 99 | if steps is None: 100 | steps = 50 101 | else: 102 | steps = steps 103 | 104 | x = np.array(x0.view(1,-1).numpy()) 105 | data.append(np.array((x)).reshape((1,2)).squeeze()) 106 | for i in range(steps): 107 | k1 = self.f(x) 108 | k2 = self.f(x + self.h*(k1/2)) 109 | k3 = self.f(x + self.h*(k2/2)) 110 | k4 = self.f(x + self.h*k3) 111 | x_new = x + (self.h/6)*(k1 + 2*k2+ 2*k3 + k4) 112 | data.append(np.array((x_new)).reshape((1,2)).squeeze()) 113 | x = x_new 114 | return np.array([data]).squeeze() 115 | 116 | 117 | class data_stochasticNonlinear(): 118 | def __init__(self): 119 | 120 | self.h = 0.05 121 | 122 | def f(self, state): 123 | x, y = np.squeeze(state) 124 | f1 = -x*(1/(np.sqrt(np.linalg.norm(state,2)))) - x + y 125 | g1 = np.sin(x) 126 | f2 = -y*(1/(np.sqrt(np.linalg.norm(state,2)))) - (10/3)*y + x 127 | g2 = y 128 | a = np.array([[f1, f2]]) 129 | b = np.array([[g1, g2]]) 130 | return a, b 131 | 132 | def gen_data(self, x0=None, steps = None): 133 | 134 | h = self.h 135 | data = [] 136 | 137 | if x0 is None: 138 | 139 | X = np.linspace(-5,5,num=18) 140 | 141 | for x1 in X: 142 | for x2 in X: 143 | x = np.array([[x1,x2]]) 144 | for i in range(5): 145 | 146 | Z_t, S_t = np.random.normal(0,1), np.random.choice([-1,1]) 147 | W_t = np.sqrt(h)*Z_t 148 | 149 | a1, b1 = self.f(x) 150 | k1 = h*a1 + (W_t - np.sqrt(h)*S_t)*b1 151 | 152 | a2, b2 = self.f(x + k1) 153 | k2 = h*a2 + (W_t + np.sqrt(h)*S_t)*b2 154 | 155 | x_new = x + (1/2)*(k1 + k2) 156 | if np.isnan(np.array((x,x_new))).any(): 157 | print(x) 158 | break 159 | else: 160 | data.append(np.array((x,x_new)).reshape((1,4)).squeeze()) 161 | x = x_new 162 | 163 | np.savetxt(mainPath/'data_stochasticNonlinear.csv', data, delimiter=",") 164 | 165 | else: 166 | if steps is None: 167 | steps = 100 168 | else: 169 | steps = steps 170 | x = np.array(x0.view(1,-1).numpy()) 171 | data.append(np.array((x)).reshape((1,2)).squeeze()) 172 | for i in range(steps): 173 | 174 | Z_t, S_t = np.random.normal(0,1), np.random.choice([-1,1]) 175 | W_t = np.sqrt(h)*Z_t 176 | 177 | a1, b1 = self.f(x) 178 | k1 = h*a1 + (W_t - np.sqrt(h)*S_t)*b1 179 | 180 | a2, b2 = self.f(x + k1) 181 | k2 = h*a2 + (W_t + np.sqrt(h)*S_t)*b2 182 | 183 | x_new = x + (1/2)*(k1 + k2) 184 | data.append(np.array((x_new)).reshape((1,2)).squeeze()) 185 | x = x_new 186 | 187 | return np.array([data]).squeeze() 188 | 189 | 190 | class data_Lorenz(): 191 | def __init__(self, two_step = False): 192 | 193 | self.rho = 28.0 194 | # self.rho = 14 195 | self.sigma = 10.0 196 | self.beta = 8.0 / 3.0 197 | self.h = 0.01 198 | 199 | self.two_step = two_step 200 | 201 | def f(self, state): 202 | x, y, z = np.squeeze(state) 203 | return np.array([[self.sigma*(y - x), x*(self.rho - z) - y, x*y - self.beta*z]]) 204 | 205 | def gen_data(self, trajectories=1): 206 | steps = 3000 207 | data = [] 208 | x = np.array([[1.2,1.1,0.9]]) 209 | if self.two_step: 210 | k1 = self.f(x) 211 | k2 = self.f(x + self.h*(k1/2)) 212 | k3 = self.f(x + self.h*(k2/2)) 213 | k4 = self.f(x + self.h*k3) 214 | x_step = x + (self.h/6)*(k1 + 2*k2+ 2*k3 + k4) 215 | 216 | for i in range(steps): 217 | 218 | if self.two_step: 219 | k1 = self.f(x_step) 220 | k2 = self.f(x_step + self.h*(k1/2)) 221 | k3 = self.f(x_step + self.h*(k2/2)) 222 | k4 = self.f(x_step + self.h*k3) 223 | x_new = x_step + (self.h/6)*(k1 + 2*k2+ 2*k3 + k4) 224 | data.append(np.array((x,x_step,x_new)).reshape((1,9)).squeeze()) 225 | x = x_step 226 | x_step = x_new 227 | 228 | else: 229 | 230 | k1 = self.f(x) 231 | k2 = self.f(x + self.h*(k1/2)) 232 | k3 = self.f(x + self.h*(k2/2)) 233 | k4 = self.f(x + self.h*k3) 234 | x_new = x + (self.h/6)*(k1 + 2*k2+ 2*k3 + k4) 235 | data.append(np.array((x,x_new)).reshape((1,6)).squeeze()) 236 | x = x_new 237 | 238 | if self.two_step: 239 | np.savetxt(mainPath/'data_Lorenz_stable_twostep.csv', data, delimiter=",") 240 | else: 241 | np.savetxt(mainPath/'data_Lorenz.csv', data, delimiter=",") 242 | 243 | class data_VanderPol(): 244 | def __init__(self, two_step = False): 245 | 246 | self.mu = 1.0 247 | self.h = 0.1 248 | 249 | def f(self, state): 250 | x, y = np.squeeze(state) 251 | return np.array([[self.mu*(x - (1/3)*x**3 - y), (1/self.mu)*x]]) 252 | 253 | def gen_data(self, trajectories=1): 254 | data = [] 255 | x = np.array([[4,2]]) 256 | for i in range(400): 257 | 258 | k1 = self.f(x) 259 | k2 = self.f(x + self.h*(k1/2)) 260 | k3 = self.f(x + self.h*(k2/2)) 261 | k4 = self.f(x + self.h*k3) 262 | x_new = x + (self.h/6)*(k1 + 2*k2+ 2*k3 + k4) 263 | data.append(np.array((x,x_new)).reshape((1,4)).squeeze()) 264 | x = x_new 265 | 266 | np.savetxt(mainPath/'data_VanderPol_stable.csv', data, delimiter=",") 267 | 268 | 269 | 270 | #see https://github.com/bhuvanakundumani/pytorch_Dataloader 271 | class oversampdata(Dataset): 272 | 273 | def __init__(self, data, add_state = False, n=None): 274 | 275 | if add_state: 276 | self.inp_data = torch.FloatTensor(data.values.astype('float')[:,:2*n]).reshape((-1,1,2*n)) 277 | self.out_data = torch.FloatTensor(data.values.astype('float')[:,2*n:]).reshape((-1,1,n)) 278 | else: 279 | self.inp_data = torch.FloatTensor(data.values.astype('float')[:,:data.shape[1]//2]).reshape((-1,1,data.shape[1]//2)) 280 | self.out_data = torch.FloatTensor(data.values.astype('float')[:,data.shape[1]//2:]).reshape((-1,1,data.shape[1]//2)) 281 | 282 | # print(self.inp_data.shape) 283 | def __len__(self): 284 | return len(self.inp_data) 285 | 286 | def __getitem__(self, index): 287 | #target = self.out_data[ind] 288 | #data_val = self.data[index] [:-1] 289 | return self.inp_data[index], self.out_data[index] 290 | --------------------------------------------------------------------------------