-
Notifications
You must be signed in to change notification settings - Fork 6
Adding ability to define test space through convex hull #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 17 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
f39fc7f
Track losses during training
andersonw1 ff31500
Add convex hull parameter space option
andersonw1 5034f1d
typo
andersonw1 ce02060
Changed np.inf back to Inf
andersonw1 158baae
Convex hull working
andersonw1 7d666ba
Example we convex hull
andersonw1 724b959
Fix conditions for param.py
andersonw1 fce2194
Cleanup and hull works in arbitrary dimensions
andersonw1 4dd6781
Remove 'exterior' option for test_space_type
andersonw1 a609c4e
Documentation
andersonw1 33cd084
Cleanup comments
andersonw1 6a1a26f
Documentation
andersonw1 623cca7
Documentation in param.py
andersonw1 0c7a698
Documentation in param.py
andersonw1 62b6139
Accidentally deleted a '2' in burgers1d.yml
andersonw1 1efa0cd
Documentation formatting to numpy style
andersonw1 446130f
Formatting to numpy style
andersonw1 e335fcb
Changing list()
andersonw1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import numpy as np | ||
from scipy.spatial import Delaunay | ||
from .inputs import InputParser | ||
|
||
def get_1dspace_from_list(config): | ||
|
@@ -40,12 +41,18 @@ def __init__(self, config): | |
for param in self.param_list: | ||
self.param_name += [param['name']] | ||
|
||
self.train_space = self.createInitialTrainSpace(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, 'Must have at least 2 parameters if test_space is \'hull\' ' | ||
andersonw1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.createTestHullSpace(self.param_list) | ||
|
||
return | ||
|
||
|
@@ -66,6 +73,38 @@ def createInitialTrainSpace(self, param_list): | |
mesh_grids = self.createHyperMeshGrid(paramRanges) | ||
return self.createHyperGridSpace(mesh_grids) | ||
|
||
def createInitialTrainSpaceForHull(self, param_list): | ||
''' | ||
dreamer2368 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Concatenates the provided lists of training points into a 2D array. | ||
|
||
Arguments | ||
--------- | ||
param_list : :obj:`list(dict)` | ||
A list of parameter dictionaries | ||
|
||
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 = [] | ||
|
||
for k, param in enumerate(param_list): | ||
|
||
_, 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\'.') | ||
|
||
|
||
mesh_grids = np.vstack((paramRanges)).T | ||
return mesh_grids | ||
|
||
def createTestGridSpace(self, param_list): | ||
paramRanges = [] | ||
gridSizes = [] | ||
|
@@ -78,6 +117,72 @@ def createTestGridSpace(self, param_list): | |
mesh_grids = self.createHyperMeshGrid(paramRanges) | ||
return gridSizes, mesh_grids, self.createHyperGridSpace(mesh_grids) | ||
|
||
def createTestGridSpaceForHull(self, param_list): | ||
''' | ||
dreamer2368 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Builds an initial uniform grid for the testing parameters when the test_space is 'hull'. | ||
|
||
Arguments | ||
--------- | ||
param_list : :obj:`list(dict)` | ||
A list of parameter dictionaries | ||
|
||
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 = [] | ||
gridSizes = [] | ||
|
||
for param in param_list: | ||
Nx, paramRange = getParam1DSpace['uniform'](param) | ||
gridSizes += [Nx] | ||
paramRanges += [paramRange] | ||
|
||
mesh_grids = self.createHyperMeshGrid(paramRanges) | ||
return gridSizes, mesh_grids, self.createHyperGridSpace(mesh_grids) | ||
|
||
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. | ||
|
||
Arguments | ||
--------- | ||
param_list : :obj:`list(dict)` | ||
A list of parameter dictionaries | ||
|
||
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 | ||
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 which test points are contained in the convex hull of training points | ||
mask = cloud.find_simplex(test_space)>=0 | ||
# Only keep testing points in the convex hull of training points | ||
test_space = test_space[mask] | ||
|
||
return gridSizes, mesh_grids, test_space | ||
|
||
def getParameter(self, param_vector): | ||
''' | ||
convert numpy array parameter vector to a dict. | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.