From f39fc7f8f6f77be7d008a11dda183e68cb3d4ec7 Mon Sep 17 00:00:00 2001 From: Anderson Date: Thu, 7 Nov 2024 08:17:18 -0800 Subject: [PATCH 01/18] Track losses during training --- src/lasdi/gplasdi.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/lasdi/gplasdi.py b/src/lasdi/gplasdi.py index ac3d046..2507360 100644 --- a/src/lasdi/gplasdi.py +++ b/src/lasdi/gplasdi.py @@ -138,7 +138,7 @@ def __init__(self, physics, autoencoder, latent_dynamics, param_space, config): else: self.device = 'cpu' - self.best_loss = np.Inf + self.best_loss = np.inf self.best_coefs = None self.restart_iter = 0 @@ -163,6 +163,11 @@ def train(self): n_train = ps.n_train() ld = self.latent_dynamics + self.training_loss = [] + self.ae_loss = [] + self.ld_loss = [] + self.coef_loss = [] + ''' determine number of iterations. Perform n_iter iterations until overall iterations hit max_iter. @@ -184,6 +189,11 @@ def train(self): loss = loss_ae + self.ld_weight * loss_ld / n_train + self.coef_weight * loss_coef / n_train + self.training_loss.append(loss.item()) + self.ae_loss.append(loss_ae.item()) + self.ld_loss.append(loss_ld.item()) + self.coef_loss.append(loss_coef.item()) + loss.backward() self.optimizer.step() @@ -267,7 +277,8 @@ def export(self): dict_ = {'X_train': self.X_train, 'X_test': self.X_test, 'lr': self.lr, 'n_iter': self.n_iter, 'n_samples' : self.n_samples, 'best_coefs': self.best_coefs, 'max_iter': self.max_iter, 'max_iter': self.max_iter, 'ld_weight': self.ld_weight, 'coef_weight': self.coef_weight, - 'restart_iter': self.restart_iter, 'timer': self.timer.export(), 'optimizer': self.optimizer.state_dict() + 'restart_iter': self.restart_iter, 'timer': self.timer.export(), 'optimizer': self.optimizer.state_dict(), + 'training_loss' : self.training_loss, 'ae_loss' : self.ae_loss, 'ld_loss' : self.ld_loss, 'coeff_loss' : self.coef_loss } return dict_ From ff31500c6f218f27c1b7585c6e1524133d3d3e02 Mon Sep 17 00:00:00 2001 From: Anderson Date: Thu, 7 Nov 2024 08:25:48 -0800 Subject: [PATCH 02/18] Add convex hull parameter space option --- src/lasdi/param.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/lasdi/param.py b/src/lasdi/param.py index a8a2223..81c0676 100644 --- a/src/lasdi/param.py +++ b/src/lasdi/param.py @@ -1,4 +1,6 @@ import numpy as np +from scipy.spatial import ConvexHull, convex_hull_plot_2d +from matplotlib.path import Path from .inputs import InputParser def get_1dspace_from_list(config): @@ -16,8 +18,14 @@ def create_uniform_1dspace(config): paramRange = np.linspace(minval, maxval, Nx) return Nx, paramRange +def get_1dspace_for_exterior(config): + Nx = config['sample_size'] + paramRange = np.array(config['list']) + return Nx, paramRange + getParam1DSpace = {'list': get_1dspace_from_list, - 'uniform': create_uniform_1dspace} + 'uniform': create_uniform_1dspace, + 'exterior': get_1dspace_for_exterior} class ParameterSpace: param_list = [] @@ -46,6 +54,9 @@ def __init__(self, config): test_space_type = parser.getInput(['test_space', 'type'], datatype=str) if (test_space_type == 'grid'): self.test_grid_sizes, self.test_meshgrid, self.test_space = self.createTestGridSpace(self.param_list) + if (test_space_type == 'hull'): + assert len(self.param_list) == 2, 'Convex hull only implemented for 2D parameter space!' + self.test_grid_sizes, self.test_meshgrid, self.test_space = self.createTestSpaceFromHull(self.param_list) return @@ -78,6 +89,22 @@ def createTestGridSpace(self, param_list): mesh_grids = self.createHyperMeshGrid(paramRanges) return gridSizes, mesh_grids, self.createHyperGridSpace(mesh_grids) + def createTestSpaceFromHull(self, param_list): + #get the initial over the parameters + gridSizes, mesh_grids, test_space = self.createTestGridSpace(self.param_list) + hull = ConvexHull(test_space) + hull_path = Path( hull.points[hull.vertices] ) #note: Path only works in 2D + + k = [] + for i in range(test_space.shape[0]): + #We check if the point is inside the convex hull, otherwise we will discard it + if hull_path.contains_point(test_space[i,:]): + k.append(i) + + test_space = test_space[k] + + return gridSizes, mesh_grids, test_space + def getParameter(self, param_vector): ''' convert numpy array parameter vector to a dict. From 5034f1d69ad08c8bdffcd579da6dd6c0f6e79263 Mon Sep 17 00:00:00 2001 From: Anderson Date: Thu, 7 Nov 2024 08:38:16 -0800 Subject: [PATCH 03/18] typo --- src/lasdi/param.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lasdi/param.py b/src/lasdi/param.py index 81c0676..df6b404 100644 --- a/src/lasdi/param.py +++ b/src/lasdi/param.py @@ -90,8 +90,9 @@ def createTestGridSpace(self, param_list): return gridSizes, mesh_grids, self.createHyperGridSpace(mesh_grids) def createTestSpaceFromHull(self, param_list): - #get the initial over the parameters + #get the initial grid over the parameters gridSizes, mesh_grids, test_space = self.createTestGridSpace(self.param_list) + hull = ConvexHull(test_space) hull_path = Path( hull.points[hull.vertices] ) #note: Path only works in 2D From ce0206097509b0ba43f3a472111785292c1f0ebc Mon Sep 17 00:00:00 2001 From: Anderson Date: Thu, 7 Nov 2024 09:10:40 -0800 Subject: [PATCH 04/18] Changed np.inf back to Inf --- src/lasdi/gplasdi.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lasdi/gplasdi.py b/src/lasdi/gplasdi.py index 2507360..3e156d4 100644 --- a/src/lasdi/gplasdi.py +++ b/src/lasdi/gplasdi.py @@ -138,7 +138,7 @@ def __init__(self, physics, autoencoder, latent_dynamics, param_space, config): else: self.device = 'cpu' - self.best_loss = np.inf + self.best_loss = np.Inf self.best_coefs = None self.restart_iter = 0 @@ -291,4 +291,4 @@ def load(self, dict_): self.optimizer.load_state_dict(dict_['optimizer']) if (self.device != 'cpu'): optimizer_to(self.optimizer, self.device) - return \ No newline at end of file + return From 158baae20c8a6f5bcddb622a48c98b0dd1c1d424 Mon Sep 17 00:00:00 2001 From: Anderson Date: Thu, 7 Nov 2024 10:50:12 -0800 Subject: [PATCH 05/18] Convex hull working --- src/lasdi/param.py | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/lasdi/param.py b/src/lasdi/param.py index df6b404..a85b9a8 100644 --- a/src/lasdi/param.py +++ b/src/lasdi/param.py @@ -49,13 +49,14 @@ def __init__(self, config): self.param_name += [param['name']] self.train_space = self.createInitialTrainSpace(self.param_list) + self.train_space = self.createInitialTrainSpaceForHull(self.param_list) self.n_init = self.train_space.shape[0] test_space_type = parser.getInput(['test_space', 'type'], datatype=str) if (test_space_type == 'grid'): self.test_grid_sizes, self.test_meshgrid, self.test_space = self.createTestGridSpace(self.param_list) if (test_space_type == 'hull'): - assert len(self.param_list) == 2, 'Convex hull only implemented for 2D parameter space!' + assert self.n_param == 2, 'Convex hull only implemented for 2D parameter space!' self.test_grid_sizes, self.test_meshgrid, self.test_space = self.createTestSpaceFromHull(self.param_list) return @@ -77,6 +78,16 @@ def createInitialTrainSpace(self, param_list): mesh_grids = self.createHyperMeshGrid(paramRanges) return self.createHyperGridSpace(mesh_grids) + def createInitialTrainSpaceForHull(self, param_list): + paramRanges = [] + + for param in param_list: + _, paramRange = getParam1DSpace[param['test_space_type']](param) + paramRanges += [paramRange] + + mesh_grids = np.vstack((paramRanges)).T + return mesh_grids + def createTestGridSpace(self, param_list): paramRanges = [] gridSizes = [] @@ -89,11 +100,25 @@ def createTestGridSpace(self, param_list): mesh_grids = self.createHyperMeshGrid(paramRanges) return gridSizes, mesh_grids, self.createHyperGridSpace(mesh_grids) + def createTestGridSpaceForHull(self, param_list): + paramRanges = [] + gridSizes = [] + + for param in param_list: + Nx, _ = getParam1DSpace[param['test_space_type']](param) + minval = param['min'] + maxval = param['max'] + gridSizes += [Nx] + paramRanges += [np.linspace(minval, maxval, Nx)] + + mesh_grids = self.createHyperMeshGrid(paramRanges) + return gridSizes, mesh_grids, self.createHyperGridSpace(mesh_grids) + def createTestSpaceFromHull(self, param_list): #get the initial grid over the parameters - gridSizes, mesh_grids, test_space = self.createTestGridSpace(self.param_list) + gridSizes, mesh_grids, test_space = self.createTestGridSpaceForHull(self.param_list) - hull = ConvexHull(test_space) + hull = ConvexHull(self.train_space) hull_path = Path( hull.points[hull.vertices] ) #note: Path only works in 2D k = [] From 7d666ba0534b024c3b25775b574e48e93b69c0cd Mon Sep 17 00:00:00 2001 From: Anderson Date: Thu, 7 Nov 2024 10:51:55 -0800 Subject: [PATCH 06/18] Example we convex hull --- examples/burgers1d.yml | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/examples/burgers1d.yml b/examples/burgers1d.yml index 098945d..2792cfa 100644 --- a/examples/burgers1d.yml +++ b/examples/burgers1d.yml @@ -41,6 +41,25 @@ parameter_space: test_space: type: grid +#an example if we want to provide training points on exterior +#of region and train in convex hull of training points +# parameter_space: +# parameters: +# - name: a +# min: 0.7 +# max: 0.9 +# test_space_type: exterior +# sample_size: 21 +# list: [0.70, 0.725, 0.75, 0.800, 0.85, 0.90] +# - name: w +# min: 0.9 +# max: 1.1 +# test_space_type: exterior +# sample_size: 21 +# list: [0.90, 0.970, 1.00, 0.925, 0.98, 1.10] +# test_space: +# type: hull + latent_space: type: ae ae: From 724b9592cc749ed25224612dbafa9c46041f69df Mon Sep 17 00:00:00 2001 From: Anderson Date: Thu, 7 Nov 2024 11:56:22 -0800 Subject: [PATCH 07/18] Fix conditions for param.py --- src/lasdi/param.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/lasdi/param.py b/src/lasdi/param.py index a85b9a8..145a1ee 100644 --- a/src/lasdi/param.py +++ b/src/lasdi/param.py @@ -48,15 +48,19 @@ def __init__(self, config): for param in self.param_list: self.param_name += [param['name']] - self.train_space = self.createInitialTrainSpace(self.param_list) - self.train_space = self.createInitialTrainSpaceForHull(self.param_list) - self.n_init = self.train_space.shape[0] + # self.train_space = self.createInitialTrainSpace(self.param_list) + # self.train_space = self.createInitialTrainSpaceForHull(self.param_list) + # self.n_init = self.train_space.shape[0] test_space_type = parser.getInput(['test_space', 'type'], datatype=str) if (test_space_type == 'grid'): + self.train_space = self.createInitialTrainSpace(self.param_list) + self.n_init = self.train_space.shape[0] self.test_grid_sizes, self.test_meshgrid, self.test_space = self.createTestGridSpace(self.param_list) if (test_space_type == 'hull'): assert self.n_param == 2, 'Convex hull only implemented for 2D parameter space!' + self.train_space = self.createInitialTrainSpaceForHull(self.param_list) + self.n_init = self.train_space.shape[0] self.test_grid_sizes, self.test_meshgrid, self.test_space = self.createTestSpaceFromHull(self.param_list) return From fce21946b13c9548b15602f19eb6ebbaaf088208 Mon Sep 17 00:00:00 2001 From: Anderson Date: Fri, 8 Nov 2024 09:03:28 -0800 Subject: [PATCH 08/18] Cleanup and hull works in arbitrary dimensions --- src/lasdi/param.py | 49 +++++++++++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/src/lasdi/param.py b/src/lasdi/param.py index 145a1ee..911df50 100644 --- a/src/lasdi/param.py +++ b/src/lasdi/param.py @@ -1,6 +1,5 @@ import numpy as np -from scipy.spatial import ConvexHull, convex_hull_plot_2d -from matplotlib.path import Path +from scipy.spatial import Delaunay from .inputs import InputParser def get_1dspace_from_list(config): @@ -48,19 +47,17 @@ def __init__(self, config): for param in self.param_list: self.param_name += [param['name']] - # self.train_space = self.createInitialTrainSpace(self.param_list) - # self.train_space = self.createInitialTrainSpaceForHull(self.param_list) - # self.n_init = self.train_space.shape[0] - test_space_type = parser.getInput(['test_space', 'type'], datatype=str) if (test_space_type == 'grid'): self.train_space = self.createInitialTrainSpace(self.param_list) self.n_init = self.train_space.shape[0] + self.test_grid_sizes, self.test_meshgrid, self.test_space = self.createTestGridSpace(self.param_list) if (test_space_type == 'hull'): - assert self.n_param == 2, 'Convex hull only implemented for 2D parameter space!' + assert self.n_param >=2, 'Must have at least 2 parameters if test_space is \'hull\' ' self.train_space = self.createInitialTrainSpaceForHull(self.param_list) self.n_init = self.train_space.shape[0] + self.test_grid_sizes, self.test_meshgrid, self.test_space = self.createTestSpaceFromHull(self.param_list) return @@ -83,12 +80,29 @@ def createInitialTrainSpace(self, param_list): return self.createHyperGridSpace(mesh_grids) def createInitialTrainSpaceForHull(self, param_list): + ''' + If test_space is 'hull', then the provided training parameters must be + points on the exterior of our training space. So, we form the provided points + into an array. + ''' + paramRanges = [] + k = 0 for param in param_list: + assert (param['test_space_type'] == 'exterior'), ('test_space_type for all parameters must ' + 'be \'exterior\' when test_space is \'hull\'. ') + _, paramRange = getParam1DSpace[param['test_space_type']](param) paramRanges += [paramRange] + if k > 0: + assert (len(paramRanges[k])==len(paramRanges[k - 1])), (f'Training parameters {k} and {k-1} have ' + 'different lengths. All training parameters ' + 'must have same length when test_space is \'hull\'.') + k = k + 1 + + mesh_grids = np.vstack((paramRanges)).T return mesh_grids @@ -105,6 +119,12 @@ def createTestGridSpace(self, param_list): return gridSizes, mesh_grids, self.createHyperGridSpace(mesh_grids) def createTestGridSpaceForHull(self, param_list): + ''' + This is similar to createTestGridSpace, but with some different variables. + We take the min/max value of each parameter, and create a uniform rectangular grid + over the parameter space with 'sample_size' points in each dimension. + ''' + paramRanges = [] gridSizes = [] @@ -122,16 +142,13 @@ def createTestSpaceFromHull(self, param_list): #get the initial grid over the parameters gridSizes, mesh_grids, test_space = self.createTestGridSpaceForHull(self.param_list) - hull = ConvexHull(self.train_space) - hull_path = Path( hull.points[hull.vertices] ) #note: Path only works in 2D - - k = [] - for i in range(test_space.shape[0]): - #We check if the point is inside the convex hull, otherwise we will discard it - if hull_path.contains_point(test_space[i,:]): - k.append(i) - test_space = test_space[k] + #mesh training space. This will be slow in higher dimensions + cloud = Delaunay(self.train_space) + #Determine if each point is in/out of convex Hull + mask = cloud.find_simplex(test_space)>=0 + #Only keep points in convex Hull + test_space = test_space[mask] return gridSizes, mesh_grids, test_space From 4dd6781a04832eec00b573b0ee8832633f9c4005 Mon Sep 17 00:00:00 2001 From: Anderson Date: Mon, 11 Nov 2024 08:51:52 -0800 Subject: [PATCH 09/18] Remove 'exterior' option for test_space_type --- src/lasdi/param.py | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/src/lasdi/param.py b/src/lasdi/param.py index 911df50..4029016 100644 --- a/src/lasdi/param.py +++ b/src/lasdi/param.py @@ -17,14 +17,8 @@ def create_uniform_1dspace(config): paramRange = np.linspace(minval, maxval, Nx) return Nx, paramRange -def get_1dspace_for_exterior(config): - Nx = config['sample_size'] - paramRange = np.array(config['list']) - return Nx, paramRange - getParam1DSpace = {'list': get_1dspace_from_list, - 'uniform': create_uniform_1dspace, - 'exterior': get_1dspace_for_exterior} + 'uniform': create_uniform_1dspace} class ParameterSpace: param_list = [] @@ -88,20 +82,15 @@ def createInitialTrainSpaceForHull(self, param_list): paramRanges = [] - k = 0 - for param in param_list: - assert (param['test_space_type'] == 'exterior'), ('test_space_type for all parameters must ' - 'be \'exterior\' when test_space is \'hull\'. ') + for k, param in enumerate(param_list): - _, paramRange = getParam1DSpace[param['test_space_type']](param) + _, paramRange = getParam1DSpace['list'](param) paramRanges += [paramRange] if k > 0: assert (len(paramRanges[k])==len(paramRanges[k - 1])), (f'Training parameters {k} and {k-1} have ' 'different lengths. All training parameters ' 'must have same length when test_space is \'hull\'.') - k = k + 1 - mesh_grids = np.vstack((paramRanges)).T return mesh_grids @@ -129,11 +118,9 @@ def createTestGridSpaceForHull(self, param_list): gridSizes = [] for param in param_list: - Nx, _ = getParam1DSpace[param['test_space_type']](param) - minval = param['min'] - maxval = param['max'] + Nx, paramRange = getParam1DSpace['uniform'](param) gridSizes += [Nx] - paramRanges += [np.linspace(minval, maxval, Nx)] + paramRanges += [paramRange] mesh_grids = self.createHyperMeshGrid(paramRanges) return gridSizes, mesh_grids, self.createHyperGridSpace(mesh_grids) @@ -150,6 +137,9 @@ def createTestSpaceFromHull(self, param_list): #Only keep points in convex Hull test_space = test_space[mask] + np.save('test_space',test_space) + np.save('train_space',self.train_space) + return gridSizes, mesh_grids, test_space def getParameter(self, param_vector): From a609c4e6d8cca3a9886999015c4451667bc2bb93 Mon Sep 17 00:00:00 2001 From: Anderson Date: Mon, 11 Nov 2024 11:55:27 -0800 Subject: [PATCH 10/18] Documentation --- examples/burgers1d.yml | 28 ++++++-- src/lasdi/param.py | 147 ++++++++++++++++++++++++++++++++++------- 2 files changed, 145 insertions(+), 30 deletions(-) diff --git a/examples/burgers1d.yml b/examples/burgers1d.yml index 2792cfa..d0ccaa0 100644 --- a/examples/burgers1d.yml +++ b/examples/burgers1d.yml @@ -1,12 +1,26 @@ +# lasdi: +# type: gplasdi +# gplasdi: +# # device: mps +# n_samples: 20 +# lr: 0.001 +# max_iter: 28000 +# n_iter: 2000 +# max_greedy_iter: 8000 +# ld_weight: 0.1 +# coef_weight: 1.e-6 +# path_checkpoint: checkpoint +# path_results: results + lasdi: type: gplasdi gplasdi: # device: mps n_samples: 20 lr: 0.001 - max_iter: 28000 + max_iter: 10000 n_iter: 2000 - max_greedy_iter: 28000 + max_greedy_iter: 8000 ld_weight: 0.1 coef_weight: 1.e-6 path_checkpoint: checkpoint @@ -41,22 +55,24 @@ parameter_space: test_space: type: grid -#an example if we want to provide training points on exterior -#of region and train in convex hull of training points +## An example if we want to provide training points on exterior +## of region and train in convex hull of training points # parameter_space: # parameters: # - name: a # min: 0.7 # max: 0.9 -# test_space_type: exterior +# test_space_type: list # sample_size: 21 # list: [0.70, 0.725, 0.75, 0.800, 0.85, 0.90] +# log_scale: false # - name: w # min: 0.9 # max: 1.1 -# test_space_type: exterior +# test_space_type: list # sample_size: 21 # list: [0.90, 0.970, 1.00, 0.925, 0.98, 1.10] +# log_scale: false # test_space: # type: hull diff --git a/src/lasdi/param.py b/src/lasdi/param.py index 4029016..63cf30e 100644 --- a/src/lasdi/param.py +++ b/src/lasdi/param.py @@ -52,7 +52,7 @@ def __init__(self, config): self.train_space = self.createInitialTrainSpaceForHull(self.param_list) self.n_init = self.train_space.shape[0] - self.test_grid_sizes, self.test_meshgrid, self.test_space = self.createTestSpaceFromHull(self.param_list) + self.test_grid_sizes, self.test_meshgrid, self.test_space = self.createTestHullSpace(self.param_list) return @@ -74,24 +74,50 @@ def createInitialTrainSpace(self, param_list): return self.createHyperGridSpace(mesh_grids) def createInitialTrainSpaceForHull(self, param_list): - ''' - If test_space is 'hull', then the provided training parameters must be - points on the exterior of our training space. So, we form the provided points - into an array. - ''' + """ + If test_space is 'hull', then the provided training parameters must be + points on the exterior of our training space. This function concatenates the provided + training points into a 2D array. + + ------------------------------------------------------------------------------------------- + Arguments + ------------------------------------------------------------------------------------------- + + param_list: A list of parameter dictionaries. Each entry should be a dictionary with the + following keys: + - name + - min + - max + - sample_size + - list + - log_scale false + + ------------------------------------------------------------------------------------------- + Returns + ------------------------------------------------------------------------------------------- + + A 2d array of shape (d, k), where d is the number of points provided on the exterior of + the training space and k is the number of parameters (k == len(param_list)). + """ + + #A list we will use to store all of the provided training points. paramRanges = [] for k, param in enumerate(param_list): - + + # Fetch the training points associated with each parameter which are given by a list. _, paramRange = getParam1DSpace['list'](param) + # Store the training points into the list. paramRanges += [paramRange] if k > 0: assert (len(paramRanges[k])==len(paramRanges[k - 1])), (f'Training parameters {k} and {k-1} have ' 'different lengths. All training parameters ' 'must have same length when test_space is \'hull\'.') - + + + # Stack all the provided training points into an array. mesh_grids = np.vstack((paramRanges)).T return mesh_grids @@ -108,11 +134,46 @@ def createTestGridSpace(self, param_list): return gridSizes, mesh_grids, self.createHyperGridSpace(mesh_grids) def createTestGridSpaceForHull(self, param_list): - ''' - This is similar to createTestGridSpace, but with some different variables. - We take the min/max value of each parameter, and create a uniform rectangular grid - over the parameter space with 'sample_size' points in each dimension. - ''' + """ + This function sets up an initial grid for the testing parameters when the test_space is + 'hull'. Here, we form a uniform grid over the given training parameters based on the + provided min and max values of each parameter and specified number of samples. The function + 'createTestSpaceFromHull' will later be used to keep testing point which are in the + convex hull of training points. + + This function is similar to the function 'createTestGridSpace', except we do not specify + the 'test_space_type' value for any parameter. + + ------------------------------------------------------------------------------------------- + Arguments + ------------------------------------------------------------------------------------------- + + param_list: A list of parameter dictionaries. Each entry should be a dictionary with the + following keys: + - name + - min + - max + - sample_size + - list + - log_scale false + + ------------------------------------------------------------------------------------------- + Returns + ------------------------------------------------------------------------------------------- + + A three element tuple. + + The first is a list whose i'th element specifies the number of distinct values of the i'th + parameter we consider (this is the length of the i'th element of "paramRanges" below). + + The second is a a tuple of k numpy ndarrays (where k = len(param_list)), the i'th one of + which is a k-dimensional array with shape (N0, ... , N{k - 1}), where Ni = + param_list[i].size whose i(0), ... , i(k - 1) element specifies the value of the i'th + parameter in the i(0), ... , i(k - 1)'th unique combination of parameter values. + + The third one is a 2d array of parameter values. It has shape (M, k), where + M = \prod_{i = 0}^{k - 1} param_list[i].size. + """ paramRanges = [] gridSizes = [] @@ -125,21 +186,59 @@ def createTestGridSpaceForHull(self, param_list): mesh_grids = self.createHyperMeshGrid(paramRanges) return gridSizes, mesh_grids, self.createHyperGridSpace(mesh_grids) - def createTestSpaceFromHull(self, param_list): - #get the initial grid over the parameters - gridSizes, mesh_grids, test_space = self.createTestGridSpaceForHull(self.param_list) - - - #mesh training space. This will be slow in higher dimensions + def createTestHullSpace(self, param_list): + """ + This function sets up an initial grid for the testing parameters when the test_space is + 'hull'. Here, we form a uniform grid over the giving training parameters based on the + provided min and max values of each parameter and specified number of samples. The function + 'createTestSpaceFromHull' will later be used to only keep values of this grid which are in + the convex hull of our training parameters. + + This function is similar to the function 'createTestGridSpace', except we do not specify + the 'test_space_type' value for any parameter. + + ------------------------------------------------------------------------------------------- + Arguments + ------------------------------------------------------------------------------------------- + + param_list: A list of parameter dictionaries. Each entry should be a dictionary with the + following keys: + - name + - min + - max + - sample_size + - list + - log_scale false + + ------------------------------------------------------------------------------------------- + Returns + ------------------------------------------------------------------------------------------- + + A three element tuple. + + The first is a list whose i'th element specifies the number of distinct values of the i'th + parameter we consider (this is the length of the i'th element of "paramRanges" below). + + The second is a a tuple of k numpy ndarrays (where k = len(param_list)), the i'th one of + which is a k-dimensional array with shape (N0, ... , N{k - 1}), where Ni = + param_list[i].size whose i(0), ... , i(k - 1) element specifies the value of the i'th + parameter in the i(0), ... , i(k - 1)'th unique combination of parameter values. + + The third one is a 2d array of parameter values. It has shape (M, k), where M is the + number of testing points after removing points outside the convex hull of training + parameters. + """ + + # Get the initial uniform grid over the training parameters + gridSizes, mesh_grids, test_space = self.createTestGridSpaceForHull(param_list) + + # Mesh the training space. This will be slow in higher dimensions cloud = Delaunay(self.train_space) - #Determine if each point is in/out of convex Hull + # Determine which test points are contained in the convex hull of training points mask = cloud.find_simplex(test_space)>=0 - #Only keep points in convex Hull + # Only keep testing points in the convex hull of training points test_space = test_space[mask] - np.save('test_space',test_space) - np.save('train_space',self.train_space) - return gridSizes, mesh_grids, test_space def getParameter(self, param_vector): From 33cd084200d218653523b25c331833e3f43e564a Mon Sep 17 00:00:00 2001 From: Anderson Date: Mon, 11 Nov 2024 11:57:47 -0800 Subject: [PATCH 11/18] Cleanup comments --- examples/burgers1d.yml | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/examples/burgers1d.yml b/examples/burgers1d.yml index d0ccaa0..b18ce36 100644 --- a/examples/burgers1d.yml +++ b/examples/burgers1d.yml @@ -1,24 +1,10 @@ -# lasdi: -# type: gplasdi -# gplasdi: -# # device: mps -# n_samples: 20 -# lr: 0.001 -# max_iter: 28000 -# n_iter: 2000 -# max_greedy_iter: 8000 -# ld_weight: 0.1 -# coef_weight: 1.e-6 -# path_checkpoint: checkpoint -# path_results: results - lasdi: type: gplasdi gplasdi: # device: mps n_samples: 20 lr: 0.001 - max_iter: 10000 + max_iter: 28000 n_iter: 2000 max_greedy_iter: 8000 ld_weight: 0.1 From 6a1a26facaeb5fce45f1e03f6c8ca4f1b392da11 Mon Sep 17 00:00:00 2001 From: Anderson Date: Mon, 11 Nov 2024 12:09:35 -0800 Subject: [PATCH 12/18] Documentation --- src/lasdi/param.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lasdi/param.py b/src/lasdi/param.py index 63cf30e..4f93d60 100644 --- a/src/lasdi/param.py +++ b/src/lasdi/param.py @@ -155,7 +155,7 @@ def createTestGridSpaceForHull(self, param_list): - max - sample_size - list - - log_scale false + - log_scale ------------------------------------------------------------------------------------------- Returns @@ -208,7 +208,7 @@ def createTestHullSpace(self, param_list): - max - sample_size - list - - log_scale false + - log_scale ------------------------------------------------------------------------------------------- Returns From 623cca716b1c2313d413d2a13a79d0d22e52a87f Mon Sep 17 00:00:00 2001 From: Anderson Date: Tue, 12 Nov 2024 13:06:15 -0800 Subject: [PATCH 13/18] Documentation in param.py --- src/lasdi/param.py | 133 ++++++++------------------------------------- 1 file changed, 22 insertions(+), 111 deletions(-) diff --git a/src/lasdi/param.py b/src/lasdi/param.py index 4f93d60..69332c3 100644 --- a/src/lasdi/param.py +++ b/src/lasdi/param.py @@ -74,41 +74,20 @@ def createInitialTrainSpace(self, param_list): return self.createHyperGridSpace(mesh_grids) def createInitialTrainSpaceForHull(self, param_list): + ''' + Concatenates the provided lists of training points into a 2D array. + param_list: A list of parameter dictionaries + + output: mesh_grid + - np.array of size [d, k], where d is the number of points provided on the exterior of + the training space and k is the number of parameters (k == len(param_list)). + ''' - """ - If test_space is 'hull', then the provided training parameters must be - points on the exterior of our training space. This function concatenates the provided - training points into a 2D array. - - ------------------------------------------------------------------------------------------- - Arguments - ------------------------------------------------------------------------------------------- - - param_list: A list of parameter dictionaries. Each entry should be a dictionary with the - following keys: - - name - - min - - max - - sample_size - - list - - log_scale false - - ------------------------------------------------------------------------------------------- - Returns - ------------------------------------------------------------------------------------------- - - A 2d array of shape (d, k), where d is the number of points provided on the exterior of - the training space and k is the number of parameters (k == len(param_list)). - """ - - #A list we will use to store all of the provided training points. paramRanges = [] for k, param in enumerate(param_list): - # Fetch the training points associated with each parameter which are given by a list. _, paramRange = getParam1DSpace['list'](param) - # Store the training points into the list. paramRanges += [paramRange] if k > 0: @@ -117,7 +96,6 @@ def createInitialTrainSpaceForHull(self, param_list): 'must have same length when test_space is \'hull\'.') - # Stack all the provided training points into an array. mesh_grids = np.vstack((paramRanges)).T return mesh_grids @@ -134,46 +112,12 @@ def createTestGridSpace(self, param_list): return gridSizes, mesh_grids, self.createHyperGridSpace(mesh_grids) def createTestGridSpaceForHull(self, param_list): - """ - This function sets up an initial grid for the testing parameters when the test_space is - 'hull'. Here, we form a uniform grid over the given training parameters based on the - provided min and max values of each parameter and specified number of samples. The function - 'createTestSpaceFromHull' will later be used to keep testing point which are in the - convex hull of training points. - - This function is similar to the function 'createTestGridSpace', except we do not specify - the 'test_space_type' value for any parameter. - - ------------------------------------------------------------------------------------------- - Arguments - ------------------------------------------------------------------------------------------- - - param_list: A list of parameter dictionaries. Each entry should be a dictionary with the - following keys: - - name - - min - - max - - sample_size - - list - - log_scale - - ------------------------------------------------------------------------------------------- - Returns - ------------------------------------------------------------------------------------------- - - A three element tuple. - - The first is a list whose i'th element specifies the number of distinct values of the i'th - parameter we consider (this is the length of the i'th element of "paramRanges" below). - - The second is a a tuple of k numpy ndarrays (where k = len(param_list)), the i'th one of - which is a k-dimensional array with shape (N0, ... , N{k - 1}), where Ni = - param_list[i].size whose i(0), ... , i(k - 1) element specifies the value of the i'th - parameter in the i(0), ... , i(k - 1)'th unique combination of parameter values. - - The third one is a 2d array of parameter values. It has shape (M, k), where - M = \prod_{i = 0}^{k - 1} param_list[i].size. - """ + ''' + Builds an initial uniform grid for the testing parameters when the test_space is 'hull'. + param_list: A list of parameter dictionaries + + output: gridSizes, mesh_grids, param_grid + ''' paramRanges = [] gridSizes = [] @@ -187,47 +131,14 @@ def createTestGridSpaceForHull(self, param_list): return gridSizes, mesh_grids, self.createHyperGridSpace(mesh_grids) def createTestHullSpace(self, param_list): - """ - This function sets up an initial grid for the testing parameters when the test_space is - 'hull'. Here, we form a uniform grid over the giving training parameters based on the - provided min and max values of each parameter and specified number of samples. The function - 'createTestSpaceFromHull' will later be used to only keep values of this grid which are in - the convex hull of our training parameters. - - This function is similar to the function 'createTestGridSpace', except we do not specify - the 'test_space_type' value for any parameter. - - ------------------------------------------------------------------------------------------- - Arguments - ------------------------------------------------------------------------------------------- - - param_list: A list of parameter dictionaries. Each entry should be a dictionary with the - following keys: - - name - - min - - max - - sample_size - - list - - log_scale - - ------------------------------------------------------------------------------------------- - Returns - ------------------------------------------------------------------------------------------- - - A three element tuple. - - The first is a list whose i'th element specifies the number of distinct values of the i'th - parameter we consider (this is the length of the i'th element of "paramRanges" below). - - The second is a a tuple of k numpy ndarrays (where k = len(param_list)), the i'th one of - which is a k-dimensional array with shape (N0, ... , N{k - 1}), where Ni = - param_list[i].size whose i(0), ... , i(k - 1) element specifies the value of the i'th - parameter in the i(0), ... , i(k - 1)'th unique combination of parameter values. - - The third one is a 2d array of parameter values. It has shape (M, k), where M is the - number of testing points after removing points outside the convex hull of training - parameters. - """ + ''' + This function builds an initial uniform grid for the testing parameters, and then + returns any testing points which are within the convex hull of the provided + training parameters. + param_list: A list of parameter dictionaries + + output: gridSizes, mesh_grids, test_space + ''' # Get the initial uniform grid over the training parameters gridSizes, mesh_grids, test_space = self.createTestGridSpaceForHull(param_list) From 0c7a698c641b9c290410908f965bc8f8d604bf49 Mon Sep 17 00:00:00 2001 From: Anderson Date: Tue, 12 Nov 2024 13:09:07 -0800 Subject: [PATCH 14/18] Documentation in param.py --- src/lasdi/param.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/lasdi/param.py b/src/lasdi/param.py index 69332c3..8299f6b 100644 --- a/src/lasdi/param.py +++ b/src/lasdi/param.py @@ -76,11 +76,11 @@ def createInitialTrainSpace(self, param_list): def createInitialTrainSpaceForHull(self, param_list): ''' Concatenates the provided lists of training points into a 2D array. - param_list: A list of parameter dictionaries + param_list: A list of parameter dictionaries - output: mesh_grid - - np.array of size [d, k], where d is the number of points provided on the exterior of - the training space and k is the number of parameters (k == len(param_list)). + Output: mesh_grid + - np.array of size [d, k], where d is the number of points provided on the exterior of + the training space and k is the number of parameters (k == len(param_list)). ''' paramRanges = [] @@ -114,9 +114,9 @@ def createTestGridSpace(self, param_list): def createTestGridSpaceForHull(self, param_list): ''' Builds an initial uniform grid for the testing parameters when the test_space is 'hull'. - param_list: A list of parameter dictionaries + param_list: A list of parameter dictionaries - output: gridSizes, mesh_grids, param_grid + Output: gridSizes, mesh_grids, param_grid ''' paramRanges = [] @@ -135,9 +135,9 @@ def createTestHullSpace(self, param_list): This function builds an initial uniform grid for the testing parameters, and then returns any testing points which are within the convex hull of the provided training parameters. - param_list: A list of parameter dictionaries + param_list: A list of parameter dictionaries - output: gridSizes, mesh_grids, test_space + Output: gridSizes, mesh_grids, test_space ''' # Get the initial uniform grid over the training parameters From 62b6139070face2b8fba254e2b8525ad09e60c22 Mon Sep 17 00:00:00 2001 From: Anderson Date: Wed, 13 Nov 2024 09:54:13 -0800 Subject: [PATCH 15/18] Accidentally deleted a '2' in burgers1d.yml --- examples/burgers1d.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/burgers1d.yml b/examples/burgers1d.yml index b18ce36..dc43164 100644 --- a/examples/burgers1d.yml +++ b/examples/burgers1d.yml @@ -6,7 +6,7 @@ lasdi: lr: 0.001 max_iter: 28000 n_iter: 2000 - max_greedy_iter: 8000 + max_greedy_iter: 28000 ld_weight: 0.1 coef_weight: 1.e-6 path_checkpoint: checkpoint From 1efa0cdbfb0587eb63e243e9a7263db21d2e2d6a Mon Sep 17 00:00:00 2001 From: Anderson Date: Wed, 13 Nov 2024 09:59:03 -0800 Subject: [PATCH 16/18] Documentation formatting to numpy style --- src/lasdi/param.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/lasdi/param.py b/src/lasdi/param.py index 8299f6b..fd0981f 100644 --- a/src/lasdi/param.py +++ b/src/lasdi/param.py @@ -75,7 +75,8 @@ def createInitialTrainSpace(self, param_list): def createInitialTrainSpaceForHull(self, param_list): ''' - Concatenates the provided lists of training points into a 2D array. + Concatenates the provided lists of training points into a 2D array. + param_list: A list of parameter dictionaries Output: mesh_grid @@ -113,7 +114,8 @@ def createTestGridSpace(self, param_list): def createTestGridSpaceForHull(self, param_list): ''' - Builds an initial uniform grid for the testing parameters when the test_space is 'hull'. + Builds an initial uniform grid for the testing parameters when the test_space is 'hull'. + param_list: A list of parameter dictionaries Output: gridSizes, mesh_grids, param_grid @@ -132,9 +134,10 @@ def createTestGridSpaceForHull(self, param_list): def createTestHullSpace(self, param_list): ''' - This function builds an initial uniform grid for the testing parameters, and then - returns any testing points which are within the convex hull of the provided - training parameters. + This function builds an initial uniform grid for the testing parameters, and then + returns any testing points which are within the convex hull of the provided + training parameters. + param_list: A list of parameter dictionaries Output: gridSizes, mesh_grids, test_space From 446130fd0af0e4d0e8d2d940ff968ee266ab0396 Mon Sep 17 00:00:00 2001 From: Anderson Date: Wed, 13 Nov 2024 10:24:07 -0800 Subject: [PATCH 17/18] Formatting to numpy style --- src/lasdi/param.py | 48 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/src/lasdi/param.py b/src/lasdi/param.py index fd0981f..70e6104 100644 --- a/src/lasdi/param.py +++ b/src/lasdi/param.py @@ -76,12 +76,17 @@ def createInitialTrainSpace(self, param_list): def createInitialTrainSpaceForHull(self, param_list): ''' Concatenates the provided lists of training points into a 2D array. - - param_list: A list of parameter dictionaries + + Arguments + --------- + param_list : :obj:`list(dict)` + A list of parameter dictionaries - Output: mesh_grid - - np.array of size [d, k], where d is the number of points provided on the exterior of - the training space and k is the number of parameters (k == len(param_list)). + Returns + ------- + mesh_grids : :obj:`numpy.array` + np.array of size [d, k], where d is the number of points provided on the exterior of + the training space and k is the number of parameters (k == len(param_list)). ''' paramRanges = [] @@ -115,10 +120,21 @@ def createTestGridSpace(self, param_list): def createTestGridSpaceForHull(self, param_list): ''' Builds an initial uniform grid for the testing parameters when the test_space is 'hull'. - - param_list: A list of parameter dictionaries + + Arguments + --------- + param_list : :obj:`list(dict)` + A list of parameter dictionaries - Output: gridSizes, mesh_grids, param_grid + Returns + ------- + gridSizes : :obj:`list(Nx)` + A list containing the number of elements on the grid in each parameter. + mesh_grids : :obj:`numpy.array` + tuple of numpy nd arrays, corresponding to each parameter. + Dimension of the array equals to the number of parameters. + param_grid : :obj:`numpy.array` + numpy 2d array of size (grid size x number of parameters). ''' paramRanges = [] @@ -138,9 +154,21 @@ def createTestHullSpace(self, param_list): returns any testing points which are within the convex hull of the provided training parameters. - param_list: A list of parameter dictionaries + Arguments + --------- + param_list : :obj:`list(dict)` + A list of parameter dictionaries - Output: gridSizes, mesh_grids, test_space + Returns + ------- + gridSizes : :obj:`list(Nx)` + A list containing the number of elements on the grid in each parameter. + mesh_grids : :obj:`numpy.array` + tuple of numpy nd arrays, corresponding to each parameter. + Dimension of the array equals to the number of parameters. + test_space : :obj:`numpy.array` + numpy 2d array of size [d, k], where d is the number of testing points within + convex hull of the training space and k is the number of parameters (k == len(param_list)). ''' # Get the initial uniform grid over the training parameters From e335fcb805388c83c80702bb51f1382396503cd8 Mon Sep 17 00:00:00 2001 From: Anderson Date: Wed, 13 Nov 2024 11:36:35 -0800 Subject: [PATCH 18/18] Changing list() --- src/lasdi/param.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lasdi/param.py b/src/lasdi/param.py index 70e6104..38ca2e3 100644 --- a/src/lasdi/param.py +++ b/src/lasdi/param.py @@ -128,7 +128,7 @@ def createTestGridSpaceForHull(self, param_list): Returns ------- - gridSizes : :obj:`list(Nx)` + gridSizes : :obj:`list(int)` A list containing the number of elements on the grid in each parameter. mesh_grids : :obj:`numpy.array` tuple of numpy nd arrays, corresponding to each parameter. @@ -161,7 +161,7 @@ def createTestHullSpace(self, param_list): Returns ------- - gridSizes : :obj:`list(Nx)` + gridSizes : :obj:`list(int)` A list containing the number of elements on the grid in each parameter. mesh_grids : :obj:`numpy.array` tuple of numpy nd arrays, corresponding to each parameter.