diff options
author | David Luevano Alvarado <55825613+luevano@users.noreply.github.com> | 2020-03-02 13:19:57 -0700 |
---|---|---|
committer | David Luevano Alvarado <55825613+luevano@users.noreply.github.com> | 2020-03-02 13:19:57 -0700 |
commit | 01aba134690889e05e02529ea861442f3fed3832 (patch) | |
tree | 4246b5113a389129c7911efdd4e7ae4a42371e77 | |
parent | 1628e8c079c7c6c68c47a29347d2cfabd5abaa9f (diff) |
Use chosolve from scipy
-rw-r--r-- | ml_exp/do_ml.py | 5 | ||||
-rw-r--r-- | ml_exp/math.py | 60 |
2 files changed, 2 insertions, 63 deletions
diff --git a/ml_exp/do_ml.py b/ml_exp/do_ml.py index 5efd13690..480ca9b8f 100644 --- a/ml_exp/do_ml.py +++ b/ml_exp/do_ml.py @@ -22,9 +22,9 @@ SOFTWARE. """ import time import numpy as np +from scipy import linalg as LA from ml_exp.misc import printc from ml_exp.kernels import gaussian_kernel -from ml_exp.math import cholesky_solve from ml_exp.qm7db import qm7db @@ -82,8 +82,7 @@ def simple_ml(descriptors, X_training, sigma, opt=opt) - alpha = cholesky_solve(K_training, - Y_training) + alpha = LA.cho_solve(LA.cho_factor(K_training), Y_training) X_test = descriptors[-test_size:] Y_test = energies[-test_size:] diff --git a/ml_exp/math.py b/ml_exp/math.py deleted file mode 100644 index 253abe5d2..000000000 --- a/ml_exp/math.py +++ /dev/null @@ -1,60 +0,0 @@ -"""MIT License - -Copyright (c) 2019 David Luevano Alvarado - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -""" -import numpy as np - - -def cholesky_solve(K, - y): - """ - Applies Cholesky decomposition to solve Ka=y. Where a are the alpha - coeficients. - K: kernel. - y: known parameters. - """ - # Add a small lambda value. - K[np.diag_indices_from(K)] += 1e-8 - - # Get the Cholesky decomposition of the kernel. - L = np.linalg.cholesky(K) - size = K.shape[0] - - # Solve Lx=y for x. - x = np.zeros(size, dtype=np.float64) - x[0] = y[0] / L[0, 0] - for i in range(1, size): - temp_sum = 0.0 - for j in range(i): - temp_sum += L[i, j] * x[j] - x[i] = (y[i] - temp_sum) / L[i, i] - - # Now, solve LTa=x for a. - L2 = L.T - a = np.zeros(size, dtype=np.float64) - a[size - 1] = x[size - 1] / L2[size - 1, size - 1] - for i in range(0, size - 1)[::-1]: - temp_sum = 0.0 - for j in range(i, size)[::-1]: - temp_sum += L2[i, j] * a[j] - a[i] = (x[i] - temp_sum) / L2[i, i] - - return a |