Compare commits
6 Commits
v0.4.4
...
kernel_dis
Author | SHA1 | Date | |
---|---|---|---|
|
09c80e2d54 | ||
|
65e0637b17 | ||
|
209f9e641b | ||
|
ba537fe1d5 | ||
|
b0cd2de18e | ||
|
7d353f5b5a |
@@ -1,5 +1,5 @@
|
|||||||
[bumpversion]
|
[bumpversion]
|
||||||
current_version = 0.4.4
|
current_version = 0.4.2
|
||||||
commit = True
|
commit = True
|
||||||
tag = True
|
tag = True
|
||||||
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)
|
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)
|
||||||
|
@@ -23,7 +23,7 @@ author = "Jensun Ravichandran"
|
|||||||
|
|
||||||
# The full version, including alpha/beta/rc tags
|
# The full version, including alpha/beta/rc tags
|
||||||
#
|
#
|
||||||
release = "0.4.4"
|
release = "0.4.2"
|
||||||
|
|
||||||
# -- General configuration ---------------------------------------------------
|
# -- General configuration ---------------------------------------------------
|
||||||
|
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
"""ProtoTorch package."""
|
"""ProtoTorch package."""
|
||||||
|
|
||||||
# Core Setup
|
# Core Setup
|
||||||
__version__ = "0.4.4"
|
__version__ = "0.4.2"
|
||||||
|
|
||||||
__all_core__ = [
|
__all_core__ = [
|
||||||
"datasets",
|
"datasets",
|
||||||
|
@@ -67,9 +67,8 @@ class LabeledComponents(Components):
|
|||||||
*,
|
*,
|
||||||
initialized_components=None):
|
initialized_components=None):
|
||||||
if initialized_components is not None:
|
if initialized_components is not None:
|
||||||
components, component_labels = initialized_components
|
super().__init__(initialized_components=initialized_components[0])
|
||||||
super().__init__(initialized_components=components)
|
self._labels = initialized_components[1]
|
||||||
self._labels = component_labels
|
|
||||||
else:
|
else:
|
||||||
self._initialize_labels(distribution)
|
self._initialize_labels(distribution)
|
||||||
super().__init__(number_of_components=len(self._labels),
|
super().__init__(number_of_components=len(self._labels),
|
||||||
|
@@ -1,6 +1,11 @@
|
|||||||
"""ProtoTorch datasets."""
|
"""ProtoTorch datasets."""
|
||||||
|
|
||||||
from .abstract import NumpyDataset
|
from .abstract import NumpyDataset
|
||||||
from .iris import Iris
|
|
||||||
from .spiral import Spiral
|
from .spiral import Spiral
|
||||||
from .tecator import Tecator
|
from .tecator import Tecator
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
"NumpyDataset",
|
||||||
|
"Spiral",
|
||||||
|
"Tecator",
|
||||||
|
]
|
||||||
|
@@ -1,15 +0,0 @@
|
|||||||
"""Thin wrapper for the Iris classification dataset from sklearn.
|
|
||||||
|
|
||||||
URL:
|
|
||||||
https://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_iris.html
|
|
||||||
|
|
||||||
"""
|
|
||||||
|
|
||||||
from prototorch.datasets.abstract import NumpyDataset
|
|
||||||
from sklearn.datasets import load_iris
|
|
||||||
|
|
||||||
|
|
||||||
class Iris(NumpyDataset):
|
|
||||||
def __init__(self):
|
|
||||||
x, y = load_iris(return_X_y=True)
|
|
||||||
super().__init__(x, y)
|
|
@@ -3,6 +3,7 @@
|
|||||||
import torch
|
import torch
|
||||||
|
|
||||||
|
|
||||||
|
# @torch.jit.script
|
||||||
def stratified_min(distances, labels):
|
def stratified_min(distances, labels):
|
||||||
clabels = torch.unique(labels, dim=0)
|
clabels = torch.unique(labels, dim=0)
|
||||||
nclasses = clabels.size()[0]
|
nclasses = clabels.size()[0]
|
||||||
@@ -30,15 +31,15 @@ def stratified_min(distances, labels):
|
|||||||
return winning_distances.T # return with `batch_size` first
|
return winning_distances.T # return with `batch_size` first
|
||||||
|
|
||||||
|
|
||||||
|
# @torch.jit.script
|
||||||
def wtac(distances, labels):
|
def wtac(distances, labels):
|
||||||
winning_indices = torch.min(distances, dim=1).indices
|
winning_indices = torch.min(distances, dim=1).indices
|
||||||
winning_labels = labels[winning_indices].squeeze()
|
winning_labels = labels[winning_indices].squeeze()
|
||||||
return winning_labels
|
return winning_labels
|
||||||
|
|
||||||
|
|
||||||
def knnc(distances, labels, k=1):
|
# @torch.jit.script
|
||||||
winning_indices = torch.topk(-distances, k=k, dim=1).indices
|
def knnc(distances, labels, k):
|
||||||
# winning_labels = torch.mode(labels[winning_indices].squeeze(),
|
winning_indices = torch.topk(-distances, k=k.item(), dim=1).indices
|
||||||
# dim=1).values
|
winning_labels = labels[winning_indices].squeeze()
|
||||||
winning_labels = torch.mode(labels[winning_indices], dim=1).values
|
|
||||||
return winning_labels
|
return winning_labels
|
||||||
|
@@ -3,8 +3,11 @@
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
import torch
|
import torch
|
||||||
|
|
||||||
from prototorch.functions.helper import (_check_shapes, _int_and_mixed_shape,
|
from prototorch.functions.helper import (
|
||||||
equal_int_shape)
|
_check_shapes,
|
||||||
|
_int_and_mixed_shape,
|
||||||
|
equal_int_shape,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def squared_euclidean_distance(x, y):
|
def squared_euclidean_distance(x, y):
|
||||||
@@ -261,5 +264,86 @@ def tangent_distance(signals, protos, subspaces, squared=False, epsilon=1e-10):
|
|||||||
return diss.permute([1, 0, 2]).squeeze(-1)
|
return diss.permute([1, 0, 2]).squeeze(-1)
|
||||||
|
|
||||||
|
|
||||||
|
class KernelDistance:
|
||||||
|
r"""Kernel Distance
|
||||||
|
|
||||||
|
Distance based on a kernel function.
|
||||||
|
"""
|
||||||
|
def __init__(self, kernel_fn):
|
||||||
|
self.kernel_fn = kernel_fn
|
||||||
|
|
||||||
|
def __call__(self, x_batch: torch.Tensor, y_batch: torch.Tensor):
|
||||||
|
return self._single_call(x_batch, y_batch)
|
||||||
|
|
||||||
|
def _single_call(self, x, y):
|
||||||
|
remove_dims = []
|
||||||
|
if len(x.shape) == 1:
|
||||||
|
x = x.unsqueeze(0)
|
||||||
|
remove_dims.append(0)
|
||||||
|
if len(y.shape) == 1:
|
||||||
|
y = y.unsqueeze(0)
|
||||||
|
remove_dims.append(-1)
|
||||||
|
|
||||||
|
output = self.kernel_fn(x, x).diag().unsqueeze(1) - 2 * self.kernel_fn(
|
||||||
|
x, y) + self.kernel_fn(y, y).diag()
|
||||||
|
|
||||||
|
for dim in remove_dims:
|
||||||
|
output.squeeze_(dim)
|
||||||
|
|
||||||
|
return torch.sqrt(output)
|
||||||
|
|
||||||
|
|
||||||
|
class BatchKernelDistance:
|
||||||
|
r"""Kernel Distance
|
||||||
|
|
||||||
|
Distance based on a kernel function.
|
||||||
|
"""
|
||||||
|
def __init__(self, kernel_fn):
|
||||||
|
self.kernel_fn = kernel_fn
|
||||||
|
|
||||||
|
def __call__(self, x_batch: torch.Tensor, y_batch: torch.Tensor):
|
||||||
|
remove_dims = 0
|
||||||
|
# Extend Single inputs
|
||||||
|
if len(x_batch.shape) == 1:
|
||||||
|
x_batch = x_batch.unsqueeze(0)
|
||||||
|
remove_dims += 1
|
||||||
|
if len(y_batch.shape) == 1:
|
||||||
|
y_batch = y_batch.unsqueeze(0)
|
||||||
|
remove_dims += 1
|
||||||
|
|
||||||
|
# Loop over batches
|
||||||
|
output = torch.FloatTensor(len(x_batch), len(y_batch))
|
||||||
|
for i, x in enumerate(x_batch):
|
||||||
|
for j, y in enumerate(y_batch):
|
||||||
|
output[i][j] = self._single_call(x, y)
|
||||||
|
|
||||||
|
for _ in range(remove_dims):
|
||||||
|
output.squeeze_(0)
|
||||||
|
|
||||||
|
return output
|
||||||
|
|
||||||
|
def _single_call(self, x, y):
|
||||||
|
kappa_xx = self.kernel_fn(x, x)
|
||||||
|
kappa_xy = self.kernel_fn(x, y)
|
||||||
|
kappa_yy = self.kernel_fn(y, y)
|
||||||
|
|
||||||
|
squared_distance = kappa_xx - 2 * kappa_xy + kappa_yy
|
||||||
|
|
||||||
|
return torch.sqrt(squared_distance)
|
||||||
|
|
||||||
|
|
||||||
|
class SquaredKernelDistance(KernelDistance):
|
||||||
|
r"""Squared Kernel Distance
|
||||||
|
|
||||||
|
Kernel distance without final squareroot.
|
||||||
|
"""
|
||||||
|
def single_call(self, x, y):
|
||||||
|
kappa_xx = self.kernel_fn(x, x)
|
||||||
|
kappa_xy = self.kernel_fn(x, y)
|
||||||
|
kappa_yy = self.kernel_fn(y, y)
|
||||||
|
|
||||||
|
return kappa_xx - 2 * kappa_xy + kappa_yy
|
||||||
|
|
||||||
|
|
||||||
# Aliases
|
# Aliases
|
||||||
sed = squared_euclidean_distance
|
sed = squared_euclidean_distance
|
28
prototorch/functions/kernels.py
Normal file
28
prototorch/functions/kernels.py
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
"""
|
||||||
|
Experimental Kernels
|
||||||
|
"""
|
||||||
|
|
||||||
|
import torch
|
||||||
|
|
||||||
|
|
||||||
|
class ExplicitKernel:
|
||||||
|
def __init__(self, projection=torch.nn.Identity()):
|
||||||
|
self.projection = projection
|
||||||
|
|
||||||
|
def __call__(self, x, y):
|
||||||
|
return self.projection(x) @ self.projection(y).T
|
||||||
|
|
||||||
|
|
||||||
|
class RadialBasisFunctionKernel:
|
||||||
|
def __init__(self, sigma) -> None:
|
||||||
|
self.s2 = sigma * sigma
|
||||||
|
|
||||||
|
def __call__(self, x, y):
|
||||||
|
remove_dim = False
|
||||||
|
if len(x.shape) > 1:
|
||||||
|
x = x.unsqueeze(1)
|
||||||
|
remove_dim = True
|
||||||
|
output = torch.exp(-torch.sum((x - y)**2, dim=-1) / (2 * self.s2))
|
||||||
|
if remove_dim:
|
||||||
|
output = output.squeeze(1)
|
||||||
|
return output
|
@@ -1,8 +1,7 @@
|
|||||||
import torch
|
import torch
|
||||||
from torch import nn
|
from torch import nn
|
||||||
|
|
||||||
from prototorch.functions.distances import (euclidean_distance_matrix,
|
from prototorch.functions.distances import euclidean_distance_matrix, tangent_distance
|
||||||
tangent_distance)
|
|
||||||
from prototorch.functions.helper import _check_shapes, _int_and_mixed_shape
|
from prototorch.functions.helper import _check_shapes, _int_and_mixed_shape
|
||||||
from prototorch.functions.normalization import orthogonalization
|
from prototorch.functions.normalization import orthogonalization
|
||||||
from prototorch.modules.prototypes import Prototypes1D
|
from prototorch.modules.prototypes import Prototypes1D
|
||||||
|
3
setup.py
3
setup.py
@@ -23,7 +23,6 @@ INSTALL_REQUIRES = [
|
|||||||
]
|
]
|
||||||
DATASETS = [
|
DATASETS = [
|
||||||
"requests",
|
"requests",
|
||||||
"sklearn",
|
|
||||||
"tqdm",
|
"tqdm",
|
||||||
]
|
]
|
||||||
DEV = ["bumpversion"]
|
DEV = ["bumpversion"]
|
||||||
@@ -43,7 +42,7 @@ ALL = DATASETS + DEV + DOCS + EXAMPLES + TESTS
|
|||||||
|
|
||||||
setup(
|
setup(
|
||||||
name="prototorch",
|
name="prototorch",
|
||||||
version="0.4.4",
|
version="0.4.2",
|
||||||
description="Highly extensible, GPU-supported "
|
description="Highly extensible, GPU-supported "
|
||||||
"Learning Vector Quantization (LVQ) toolbox "
|
"Learning Vector Quantization (LVQ) toolbox "
|
||||||
"built using PyTorch and its nn API.",
|
"built using PyTorch and its nn API.",
|
||||||
|
@@ -4,8 +4,14 @@ import unittest
|
|||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import torch
|
import torch
|
||||||
from prototorch.functions import (activations, competitions, distances,
|
|
||||||
initializers, losses)
|
from prototorch.functions import (
|
||||||
|
activations,
|
||||||
|
competitions,
|
||||||
|
distances,
|
||||||
|
initializers,
|
||||||
|
losses,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class TestActivations(unittest.TestCase):
|
class TestActivations(unittest.TestCase):
|
||||||
@@ -138,7 +144,7 @@ class TestCompetitions(unittest.TestCase):
|
|||||||
def test_knnc_k1(self):
|
def test_knnc_k1(self):
|
||||||
d = torch.tensor([[2.0, 3.0, 1.99, 3.01], [2.0, 3.0, 2.01, 3.0]])
|
d = torch.tensor([[2.0, 3.0, 1.99, 3.01], [2.0, 3.0, 2.01, 3.0]])
|
||||||
labels = torch.tensor([0, 1, 2, 3])
|
labels = torch.tensor([0, 1, 2, 3])
|
||||||
actual = competitions.knnc(d, labels, k=1)
|
actual = competitions.knnc(d, labels, k=torch.tensor([1]))
|
||||||
desired = torch.tensor([2, 0])
|
desired = torch.tensor([2, 0])
|
||||||
mismatch = np.testing.assert_array_almost_equal(actual,
|
mismatch = np.testing.assert_array_almost_equal(actual,
|
||||||
desired,
|
desired,
|
||||||
|
98
tests/test_kernels.py
Normal file
98
tests/test_kernels.py
Normal file
@@ -0,0 +1,98 @@
|
|||||||
|
"""ProtoTorch kernels test suite."""
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
import torch
|
||||||
|
|
||||||
|
from prototorch.functions.distances import KernelDistance
|
||||||
|
from prototorch.functions.kernels import ExplicitKernel, RadialBasisFunctionKernel
|
||||||
|
|
||||||
|
|
||||||
|
class TestExplicitKernel(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.single_x = torch.randn(1024)
|
||||||
|
self.single_y = torch.randn(1024)
|
||||||
|
|
||||||
|
self.batch_x = torch.randn(32, 1024)
|
||||||
|
self.batch_y = torch.randn(32, 1024)
|
||||||
|
|
||||||
|
def test_single_values(self):
|
||||||
|
kernel = ExplicitKernel()
|
||||||
|
self.assertEqual(
|
||||||
|
kernel(self.single_x, self.single_y).shape, torch.Size([]))
|
||||||
|
|
||||||
|
def test_single_batch(self):
|
||||||
|
kernel = ExplicitKernel()
|
||||||
|
self.assertEqual(
|
||||||
|
kernel(self.single_x, self.batch_y).shape, torch.Size([32]))
|
||||||
|
|
||||||
|
def test_batch_single(self):
|
||||||
|
kernel = ExplicitKernel()
|
||||||
|
self.assertEqual(
|
||||||
|
kernel(self.batch_x, self.single_y).shape, torch.Size([32]))
|
||||||
|
|
||||||
|
def test_batch_values(self):
|
||||||
|
kernel = ExplicitKernel()
|
||||||
|
self.assertEqual(
|
||||||
|
kernel(self.batch_x, self.batch_y).shape, torch.Size([32, 32]))
|
||||||
|
|
||||||
|
|
||||||
|
class TestRadialBasisFunctionKernel(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.single_x = torch.randn(1024)
|
||||||
|
self.single_y = torch.randn(1024)
|
||||||
|
|
||||||
|
self.batch_x = torch.randn(32, 1024)
|
||||||
|
self.batch_y = torch.randn(32, 1024)
|
||||||
|
|
||||||
|
def test_single_values(self):
|
||||||
|
kernel = RadialBasisFunctionKernel(1)
|
||||||
|
self.assertEqual(
|
||||||
|
kernel(self.single_x, self.single_y).shape, torch.Size([]))
|
||||||
|
|
||||||
|
def test_single_batch(self):
|
||||||
|
kernel = RadialBasisFunctionKernel(1)
|
||||||
|
self.assertEqual(
|
||||||
|
kernel(self.single_x, self.batch_y).shape, torch.Size([32]))
|
||||||
|
|
||||||
|
def test_batch_single(self):
|
||||||
|
kernel = RadialBasisFunctionKernel(1)
|
||||||
|
self.assertEqual(
|
||||||
|
kernel(self.batch_x, self.single_y).shape, torch.Size([32]))
|
||||||
|
|
||||||
|
def test_batch_values(self):
|
||||||
|
kernel = RadialBasisFunctionKernel(1)
|
||||||
|
self.assertEqual(
|
||||||
|
kernel(self.batch_x, self.batch_y).shape, torch.Size([32, 32]))
|
||||||
|
|
||||||
|
|
||||||
|
class TestKernelDistance(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.single_x = torch.randn(1024)
|
||||||
|
self.single_y = torch.randn(1024)
|
||||||
|
|
||||||
|
self.batch_x = torch.randn(32, 1024)
|
||||||
|
self.batch_y = torch.randn(32, 1024)
|
||||||
|
|
||||||
|
self.kernel = ExplicitKernel()
|
||||||
|
|
||||||
|
def test_single_values(self):
|
||||||
|
distance = KernelDistance(self.kernel)
|
||||||
|
self.assertEqual(
|
||||||
|
distance(self.single_x, self.single_y).shape, torch.Size([]))
|
||||||
|
|
||||||
|
def test_single_batch(self):
|
||||||
|
distance = KernelDistance(self.kernel)
|
||||||
|
self.assertEqual(
|
||||||
|
distance(self.single_x, self.batch_y).shape, torch.Size([32]))
|
||||||
|
|
||||||
|
def test_batch_single(self):
|
||||||
|
distance = KernelDistance(self.kernel)
|
||||||
|
self.assertEqual(
|
||||||
|
distance(self.batch_x, self.single_y).shape, torch.Size([32]))
|
||||||
|
|
||||||
|
def test_batch_values(self):
|
||||||
|
distance = KernelDistance(self.kernel)
|
||||||
|
self.assertEqual(
|
||||||
|
distance(self.batch_x, self.batch_y).shape, torch.Size([32, 32]))
|
Reference in New Issue
Block a user