summaryrefslogtreecommitdiff
path: root/ml_exp/kernels.py
diff options
context:
space:
mode:
authorDavid Luevano Alvarado <55825613+luevano@users.noreply.github.com>2020-03-02 14:33:19 -0700
committerDavid Luevano Alvarado <55825613+luevano@users.noreply.github.com>2020-03-02 14:33:19 -0700
commit1647f76052b016e4102a3af234ac47401e04819d (patch)
tree224c292377839449b00d99f5dafabf2b756b1fb4 /ml_exp/kernels.py
parent01aba134690889e05e02529ea861442f3fed3832 (diff)
Start to add tf support
Diffstat (limited to 'ml_exp/kernels.py')
-rw-r--r--ml_exp/kernels.py34
1 files changed, 17 insertions, 17 deletions
diff --git a/ml_exp/kernels.py b/ml_exp/kernels.py
index feaf9a990..c79f93efa 100644
--- a/ml_exp/kernels.py
+++ b/ml_exp/kernels.py
@@ -20,36 +20,36 @@ 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 math
+# import math
import numpy as np
def gaussian_kernel(X1,
X2,
- sigma,
- opt=True):
+ sigma):
"""
Calculates the Gaussian Kernel.
X1: first representations.
X2: second representations.
sigma: kernel width.
- opt: if the optimized algorithm should be used. For benchmarking purposes.
"""
i_sigma = -0.5 / (sigma*sigma)
K = np.zeros((X1.shape[0], X2.shape[0]), dtype=np.float64)
- if opt:
- # Faster way of calculating the kernel (no numba support).
- for i, x1 in enumerate(X1):
- if X2.ndim == 3:
- norm = np.linalg.norm(X2 - x1, axis=(1, 2))
- else:
- norm = np.linalg.norm(X2 - x1, axis=-1)
- K[i, :] = np.exp(i_sigma * np.square(norm))
- else:
- for i, x1 in enumerate(X1):
- for j, x2 in enumerate(X2):
- f_norm = np.linalg.norm(x2 - x1)
- K[i, j] = math.exp(i_sigma * f_norm**2)
+ # Faster way of calculating the kernel (no numba support).
+ for i, x1 in enumerate(X1):
+ if X2.ndim == 3:
+ norm = np.linalg.norm(X2 - x1, axis=(1, 2))
+ else:
+ norm = np.linalg.norm(X2 - x1, axis=-1)
+ K[i, :] = np.exp(i_sigma * np.square(norm))
+
+ # Old way of calculating the kernel (numba support).
+ """
+ for i, x1 in enumerate(X1):
+ for j, x2 in enumerate(X2):
+ f_norm = np.linalg.norm(x2 - x1)
+ K[i, j] = math.exp(i_sigma * f_norm**2)
+ """
return K