From d1b84b91f4b94b717de477e47b22946fd42be0db Mon Sep 17 00:00:00 2001 From: David Luevano Alvarado <55825613+luevano@users.noreply.github.com> Date: Tue, 25 Feb 2020 20:52:08 -0700 Subject: Refactor gauss kernel, more natural syntax --- ml_exp/__init__.py | 4 +++- 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) -- cgit v1.2.3-70-g09d2