summaryrefslogtreecommitdiff
path: root/gauss_kernel.py
blob: 3b0a5a198bd8c15d19efc66677346c35e25be0cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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