-
Notifications
You must be signed in to change notification settings - Fork 72
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
Serialization and representation GP + Small fixes #64
Merged
Merged
Changes from 16 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
5116cbc
Added unit-tested string and dictionary methods for GPs
stevetorr 956e3a3
Addded string-to-kernel helper functions to facilitate GPs from Dicts
stevetorr 1f626ea
Unit Tests for GP serialization / representation
stevetorr f4242da
Improved PEP8 compliance
stevetorr a102a74
Revert "Improved PEP8 compliance"
stevetorr 1e6fb32
Revert "Unit Tests for GP serialization / representation"
stevetorr 781a074
Revert "Addded string-to-kernel helper functions to facilitate GPs fr…
stevetorr d990bcc
Revert "Added unit-tested string and dictionary methods for GPs"
stevetorr 344c801
Re-Added unit-tested string and dictionary methods for GPs""
stevetorr 39a0314
Re-Addded string-to-kernel helper functions to facilitate GPs from Di…
stevetorr ee98fc3
Re-add Unit Tests for GP serialization / representation
stevetorr 253b8a7
"Improved PEP8 compliance
stevetorr 3cc2355
Fixed like attribuet being used in different places
stevetorr 17cadc5
Merge pull request #65 from mir-group/master
stevetorr 9a4cc93
Added multicomponent kernels
stevetorr 790deb6
Merge branch 'Serialization_and_representation_GP' of https://github.…
stevetorr f324957
some linting
jonpvandermause 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 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 |
---|---|---|
|
@@ -8,6 +8,8 @@ | |
from flare.gp_algebra import get_ky_mat, get_ky_and_hyp, \ | ||
get_like_from_ky_mat, get_like_grad_from_mats, get_neg_likelihood, \ | ||
get_neg_like_grad, get_ky_and_hyp_par | ||
from flare.kernels import str_to_kernel | ||
from flare.mc_simple import str_to_mc_kernel | ||
|
||
|
||
class GaussianProcess: | ||
|
@@ -36,17 +38,23 @@ def __init__(self, kernel: Callable, | |
self.hyp_labels = hyp_labels | ||
self.cutoffs = cutoffs | ||
self.algo = opt_algorithm | ||
self.l_mat = None | ||
self.alpha = None | ||
|
||
self.training_data = [] | ||
self.training_labels = [] | ||
self.training_labels_np = np.empty(0, ) | ||
self.maxiter = maxiter | ||
self.likelihood = None | ||
self.likelihood_gradient = None | ||
self.par = par | ||
self.output = output | ||
|
||
# Parameters set during training | ||
self.ky_mat = None | ||
self.l_mat = None | ||
self.alpha = None | ||
self.ky_mat_inv = None | ||
self.l_mat_inv = None | ||
self.likelihood = None | ||
self.likelihood_gradient = None | ||
|
||
# TODO unit test custom range | ||
def update_db(self, struc: Structure, forces: list, | ||
custom_range: List[int] = ()): | ||
|
@@ -128,6 +136,7 @@ def train(self, output=None, custom_bounds=None, | |
args = (self.training_data, self.training_labels_np, | ||
self.kernel_grad, self.cutoffs, output, | ||
self.par) | ||
res = None | ||
|
||
if self.algo == 'L-BFGS-B': | ||
|
||
|
@@ -166,7 +175,8 @@ def train(self, output=None, custom_bounds=None, | |
options={'disp': False, | ||
'maxiter': self.maxiter, | ||
'xtol': x_tol}) | ||
|
||
if res is None: | ||
raise RuntimeError("Optimization failed for some reason.") | ||
self.hyps = res.x | ||
self.set_L_alpha() | ||
self.likelihood = -res.fun | ||
|
@@ -185,12 +195,6 @@ def predict(self, x_t: AtomicEnvironment, d: int) -> [float, float]: | |
pred_var = self_kern - \ | ||
np.matmul(np.matmul(k_v, self.ky_mat_inv), k_v) | ||
|
||
# # get predictive variance (possibly slow) | ||
# v_vec = solve_triangular(self.l_mat, k_v, lower=True) | ||
# self_kern = self.kernel(x_t, x_t, self.bodies, d, d, self.hyps, | ||
# self.cutoffs) | ||
# pred_var = self_kern - np.matmul(v_vec, v_vec) | ||
|
||
return pred_mean, pred_var | ||
|
||
def predict_local_energy(self, x_t: AtomicEnvironment) -> float: | ||
|
@@ -292,8 +296,8 @@ def set_L_alpha(self): | |
self.ky_mat_inv = ky_mat_inv | ||
self.l_mat_inv = l_mat_inv | ||
|
||
self.like = like | ||
self.like_grad = like_grad | ||
self.likelihood = like | ||
self.likelihood_gradient = like_grad | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch, not sure why there were multiple versions of the likelihood. |
||
|
||
def update_L_alpha(self): | ||
""" | ||
|
@@ -331,3 +335,82 @@ def update_L_alpha(self): | |
self.alpha = alpha | ||
self.ky_mat_inv = ky_mat_inv | ||
self.l_mat_inv = l_mat_inv | ||
|
||
def __str__(self): | ||
|
||
thestr = "GaussianProcess Object\n" | ||
thestr += 'Kernel: {}\n'.format(self.kernel_name) | ||
thestr += "Training points: {}\n".format(len(self.training_data)) | ||
thestr += 'Cutoffs: {}\n'.format(self.cutoffs) | ||
thestr += 'Model Likelihood: {}\n'.format(self.likelihood) | ||
|
||
thestr += 'Hyperparameters: \n' | ||
if self.hyp_labels is None: | ||
# Put unlabeled hyperparameters on one line | ||
thestr = thestr[:-1] | ||
thestr += str(self.hyps) + '\n' | ||
else: | ||
for hyp, label in zip(self.hyps, self.hyp_labels): | ||
thestr += "{}: {}\n".format(label, hyp) | ||
|
||
return thestr | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Slick. |
||
|
||
def as_dict(self): | ||
|
||
out_dict = dict(vars(self)) | ||
|
||
out_dict['training_data'] = [env.as_dict() for env in | ||
self.training_data] | ||
# Remove the callables | ||
del out_dict['kernel'] | ||
del out_dict['kernel_grad'] | ||
|
||
return out_dict | ||
|
||
@staticmethod | ||
def from_dict(dictionary): | ||
|
||
if 'mc' in dictionary['kernel_name']: | ||
force_kernel, grad = str_to_mc_kernel(dictionary['kernel_name'], | ||
include_grad=True) | ||
else: | ||
force_kernel, grad = str_to_kernel(dictionary['kernel_name'], | ||
include_grad=True) | ||
|
||
if dictionary['energy_kernel'] is not None: | ||
energy_kernel = str_to_kernel(dictionary['energy_kernel']) | ||
else: | ||
energy_kernel = None | ||
|
||
if dictionary['energy_force_kernel'] is not None: | ||
energy_force_kernel = str_to_kernel(dictionary[ | ||
'energy_force_kernel']) | ||
else: | ||
energy_force_kernel = None | ||
|
||
new_gp = GaussianProcess(kernel=force_kernel, | ||
kernel_grad=grad, | ||
energy_kernel=energy_kernel, | ||
energy_force_kernel=energy_force_kernel, | ||
cutoffs=np.array(dictionary['cutoffs']), | ||
hyps=np.array(dictionary['hyps']), | ||
hyp_labels=dictionary['hyp_labels'], | ||
par=dictionary['par'], | ||
maxiter=dictionary['maxiter'], | ||
opt_algorithm=dictionary['algo']) | ||
|
||
# Save time by attempting to load in computed attributes | ||
new_gp.l_mat = np.array(dictionary.get('l_mat', None)) | ||
new_gp.l_mat_inv = np.array(dictionary.get('l_mat_inv', None)) | ||
new_gp.alpha = np.array(dictionary.get('alpha', None)) | ||
new_gp.ky_mat = np.array(dictionary.get('ky_mat', None)) | ||
new_gp.ky_mat_inv = np.array(dictionary.get('ky_mat_inv', None)) | ||
|
||
new_gp.training_data = [AtomicEnvironment.from_dict(env) for env in | ||
dictionary['training_data']] | ||
new_gp.training_labels = dictionary['training_labels'] | ||
|
||
new_gp.likelihood = dictionary['likelihood'] | ||
new_gp.likelihood_gradient = dictionary['likelihood_gradient'] | ||
|
||
return new_gp |
This file contains 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 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 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 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, these lines needed to go.