diff options
author | David Luevano <55825613+luevano@users.noreply.github.com> | 2019-12-08 21:54:19 -0700 |
---|---|---|
committer | David Luevano <55825613+luevano@users.noreply.github.com> | 2019-12-08 21:54:19 -0700 |
commit | 7c8b391dddd65ff58417df80dfc79cadddf4f4e6 (patch) | |
tree | 0bad908f5b40848addc3307de4307b1dae4c6e7e /gauss_kernel.py |
First commit
Diffstat (limited to 'gauss_kernel.py')
-rw-r--r-- | gauss_kernel.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/gauss_kernel.py b/gauss_kernel.py new file mode 100644 index 000000000..3b0a5a198 --- /dev/null +++ b/gauss_kernel.py @@ -0,0 +1,27 @@ +import math +import numpy as np +from frob_norm import frob_norm + + +def gauss_kernel(X_1, X_2, sigma): + """ + Calculates the Gaussian Kernel. + X_1: first representations. + X_2: 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]) + # print(f_norm) + K[i, j] = math.exp(inv_sigma * f_norm) + + return K |