Skip to content
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

fix optimization flag in ParameterHelper class #191

Merged
merged 16 commits into from
Jun 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions docs/source/tutorials/gpfa.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ Roadmap Figure
In this tutorial, we will walk through the first two steps contained in the below figure. the GP from AIMD module is designed to give you the tools necessary to extract FLARE structures from a previously existing molecular dynamics run.

.. figure:: ../../images/GPFA_tutorial.png
:figwidth: 800 %
:align: center


Step 1: Setting up a Gaussian Process Object
--------------------------------------------

Expand Down
16 changes: 12 additions & 4 deletions flare/mgp/mgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,21 @@ def __init__(self,
self.hyps_mask = None
self.cutoffs = None

species_labels = []
coded_species = []
for i, ele in enumerate(unique_species):
if isinstance(ele, str):
self.species_labels.append(ele)
self.coded_species.append(element_to_Z(ele))
species_labels.append(ele)
coded_species.append(element_to_Z(ele))
elif isinstance(ele, int):
self.coded_species.append(ele)
self.species_labels.append(Z_to_element(ele))
coded_species.append(ele)
species_labels.append(Z_to_element(ele))
else:
print("element type not accepted", ele, type(ele))
sort_id = np.argsort(coded_species)
for i in sort_id:
self.coded_species.append(coded_species[i])
self.species_labels.append(species_labels[i])
Comment on lines +114 to +128
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A quick question: Is there anything that we could do here to make this future-proofed for coarse graining? Maybe it's thinking too far ahead, but just so that if a user were to pass in a number outside of {1...118} or a species label outside of the periodic table that everything ends up working okay. I think that this might be too far in advance (and the logic here is easy enough that we could fix it later) but something to keep in mind. This could also matter sooner if someone wants to do FLARE with isotopes, to keep track of distinct species.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we can change the element_to_Z part and allow hyps_mask to take any number beyond 118. I can change the ParameterHelper to accommodate that.

But we need to think a bit how to make it consistent. Maybe leave a dictionary in the element_coder module that allows users to injet str-integer pair. And the element_to_Z and Z_to_element function can read from that dictionary.


self.load_grid = grid_params.get('load_grid', None)
self.update = grid_params.get('update', False)
Expand Down
52 changes: 34 additions & 18 deletions flare/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,19 @@
from flare.output import set_logger
from flare.utils.element_coder import element_to_Z, Z_to_element


class Parameters():
'''
'''

all_kernel_types = ['twobody', 'threebody', 'manybody']
cutoff_types = {'cut3b': 'threebody'}
ndim = {'twobody': 2, 'threebody': 3, 'manybody': 2, 'cut3b': 2}
n_kernel_parameters = {'twobody': 2, 'threebody': 2, 'manybody': 2, 'cut3b': 0}
n_kernel_parameters = {'twobody': 2,
'threebody': 2, 'manybody': 2, 'cut3b': 0}

cutoff_types_keys = list(cutoff_types.keys())
cutoff_types_values = list(cutoff_types.values())

logger = set_logger("Parameters", stream=True,
fileout_name=None, verbose="info")
Expand Down Expand Up @@ -53,7 +59,7 @@ def __init__(self):
'map': None,
'original_hyps': [],
'original_labels': []
}
}
self.hyps = None
self.hyp_labels = None
self.cutoffs = {}
Expand Down Expand Up @@ -103,7 +109,8 @@ def backward(kernels, param_dict):
# add a couple new keys that was not there previously
if 'train_noise' not in param_dict:
param_dict['train_noise'] = True
DeprecationWarning("train_noise has to be in hyps_mask, set to True")
DeprecationWarning(
"train_noise has to be in hyps_mask, set to True")
if 'nspecie' not in param_dict:
param_dict['nspecie'] = 1

Expand All @@ -117,14 +124,15 @@ def backward(kernels, param_dict):
if k+'_start' not in param_dict:
param_dict[k+'_start'] = start
if 'n'+k not in param_dict:
Parameters.logger.debug("add in hyper parameter separators"\
Parameters.logger.debug("add in hyper parameter separators"
"for", k)
param_dict['n'+k] = 1
start += Parameters.n_kernel_parameters[k]
else:
start += param_dict['n'+k] * Parameters.n_kernel_parameters[k]
start += param_dict['n'+k] * \
Parameters.n_kernel_parameters[k]
else:
Warning("inconsistency between input kernel and kernel list"\
Warning("inconsistency between input kernel and kernel list"
"stored in hyps_mask")

Parameters.logger.debug("Replace kernel array in param_dict")
Expand Down Expand Up @@ -161,12 +169,12 @@ def check_instantiation(hyps, cutoffs, kernels, param_dict):
# and the length of corresponding hyper-parameters
hyps_length = 0
used_parameters = np.zeros_like(hyps, dtype=bool)
for kernel in kernels+['cut3b']:
for kernel in kernels+list(Parameters.cutoff_types.keys()):

n = param_dict.get(f'n{kernel}', 0)
assert isinstance(n, int)

if kernel != 'cut3b':
if kernel not in list(Parameters.cutoff_types.keys()):
hyps_length += Parameters.n_kernel_parameters[kernel]*n
assert n > 0, f"{kernel} has n 0"

Expand Down Expand Up @@ -215,7 +223,7 @@ def check_instantiation(hyps, cutoffs, kernels, param_dict):
assert mask[mask_id] == mask_value, \
f'{kernel}_mask has to be symmetrical'

if kernel != 'cut3b':
if kernel not in list(Parameters.cutoff_types.keys()):
if kernel+'_cutoff_list' in param_dict:
cutoff_list = param_dict[kernel+'_cutoff_list']
assert len(cutoff_list) == n, \
Expand Down Expand Up @@ -322,8 +330,11 @@ def get_component_mask(param_dict, kernel_name, hyps=None):
'n'+kernel_name, kernel_name+'_mask',
kernel_name+'_cutoff_list']

if kernel_name == 'threebody':
name_list += ['ncut3b', 'cut3b_mask']
if kernel_name in Parameters.cutoff_types_values:

key_ind = Parameters.cutoff_types_values.index(kernel_name)
cutoff_key = Parameters.cutoff_types_keys[key_ind]
name_list += ['n'+cutoff_key, cutoff_key+'_mask']

for name in name_list:
if name in param_dict:
Expand Down Expand Up @@ -380,7 +391,7 @@ def get_cutoff(kernel_name, coded_species, param_dict):
specie_mask = param_dict['species_mask']
cutoff_list = param_dict[f'{kernel_name}_cutoff_list']

if kernel_name != 'threebody':
if kernel_name not in Parameters.cutoff_types_values:
mask_id = 0
for ele in coded_specie:
mask_id += specie_mask[ele]
Expand All @@ -389,13 +400,17 @@ def get_cutoff(kernel_name, coded_species, param_dict):
mask_id = param_dict[kernel_name+'_mask'][mask_id]
return cutoff_list[mask_id]
else:
cut3b_mask = param_dict['cut3b_mask']

key_ind = Parameters.cutoff_types_values.index(kernel_name)
cutoff_key = Parameters.cutoff_types_keys[cutoff_key]

cut_mask = param_dict[cutoff_key+'_mask']
ele1 = species_mask[coded_species[0]]
ele2 = species_mask[coded_species[1]]
ele3 = species_mask[coded_species[2]]
twobody1 = cut3b_mask[param_dict['nspecie']*ele1 + ele2]
twobody2 = cut3b_mask[param_dict['nspecie']*ele1 + ele3]
twobody12 = cut3b_mask[param_dict['nspecie']*ele2 + ele3]
twobody1 = cut_mask[param_dict['nspecie']*ele1 + ele2]
twobody2 = cut_mask[param_dict['nspecie']*ele1 + ele3]
twobody12 = cut_mask[param_dict['nspecie']*ele2 + ele3]
return np.array([cutoff_list[twobody1],
cutoff_list[twobody2],
cutoff_list[twobody12]])
Expand Down Expand Up @@ -454,8 +469,9 @@ def compare_dict(dict1, dict2):
list_of_names += [k+'_mask']
list_of_names += ['cutoff_'+k]
list_of_names += [k+'_cutoff_list']
list_of_names += ['ncut3b']
list_of_names += ['cut3b_mask']
for k in Parameters.cutoff_types:
list_of_names += ['n'+k]
list_of_names += [k+'_mask']

for k in list_of_names:
if (k in dict1) != (k in dict2):
Expand Down
Loading