diff options
author | David Luevano Alvarado <55825613+luevano@users.noreply.github.com> | 2020-02-25 20:52:08 -0700 |
---|---|---|
committer | David Luevano Alvarado <55825613+luevano@users.noreply.github.com> | 2020-02-25 20:52:08 -0700 |
commit | d1b84b91f4b94b717de477e47b22946fd42be0db (patch) | |
tree | cb3138a779d6e5f6cc2a07b4d9dd9c90d3cdac80 | |
parent | 2bdd175a6eb1f6edd8689987fef1a29fc1043da1 (diff) |
Refactor gauss kernel, more natural syntax
-rw-r--r-- | ml_exp/__init__.py | 4 | ||||
-rw-r--r-- | ml_exp/kernels.py | 22 |
2 files changed, 12 insertions, 14 deletions
diff --git a/ml_exp/__init__.py b/ml_exp/__init__.py index dcb10a1df..e9dd83eae 100644 --- a/ml_exp/__init__.py +++ b/ml_exp/__init__.py @@ -23,6 +23,7 @@ SOFTWARE. from ml_exp.compound import Compound from ml_exp.representations import coulomb_matrix, lennard_jones_matrix,\ first_neighbor_matrix, adjacency_matrix, check_bond, bag_of_stuff +from ml_exp.math import cholesky_solve # If somebody does "from package import *", this is what they will # be able to access: @@ -32,4 +33,5 @@ __all__ = ['Compound', 'first_neighbor_matrix', 'adjacency_matrix', 'check_bond', - 'bag_of_stuff'] + 'bag_of_stuff', + 'cholesky_solve'] diff --git a/ml_exp/kernels.py b/ml_exp/kernels.py index 834d62408..7a61a1e1e 100644 --- a/ml_exp/kernels.py +++ b/ml_exp/kernels.py @@ -22,27 +22,23 @@ SOFTWARE. """ import math import numpy as np -from ml_exp.frob_norm import frob_norm -def gauss_kernel(X_1, X_2, sigma): +def gaussian_kernel(X1, + X2, + sigma): """ Calculates the Gaussian Kernel. - X_1: first representations. - X_2: second representations. + X1: first representations. + X2: second representations. sigma: kernel width. """ - x1_l = len(X_1) - x1_range = range(x1_l) - x2_l = len(X_2) - x2_range = range(x2_l) - inv_sigma = -0.5 / (sigma*sigma) - K = np.zeros((x1_l, x2_l)) - for i in x1_range: - for j in x2_range: - f_norm = frob_norm(X_1[i] - X_2[j]) + K = np.zeros((X1.shape[0], X2.shape[0]), dtype=float) + for i in X1: + for j in X2: + f_norm = np.linalg.norm(i - j) # print(f_norm) K[i, j] = math.exp(inv_sigma * f_norm) |