Automatic Formatting.
This commit is contained in:
@@ -12,26 +12,26 @@ from prototorch.datasets import abstract, tecator
|
||||
class TestAbstract(unittest.TestCase):
|
||||
def test_getitem(self):
|
||||
with self.assertRaises(NotImplementedError):
|
||||
abstract.Dataset('./artifacts')[0]
|
||||
abstract.Dataset("./artifacts")[0]
|
||||
|
||||
def test_len(self):
|
||||
with self.assertRaises(NotImplementedError):
|
||||
len(abstract.Dataset('./artifacts'))
|
||||
len(abstract.Dataset("./artifacts"))
|
||||
|
||||
|
||||
class TestProtoDataset(unittest.TestCase):
|
||||
def test_getitem(self):
|
||||
with self.assertRaises(NotImplementedError):
|
||||
abstract.ProtoDataset('./artifacts')[0]
|
||||
abstract.ProtoDataset("./artifacts")[0]
|
||||
|
||||
def test_download(self):
|
||||
with self.assertRaises(NotImplementedError):
|
||||
abstract.ProtoDataset('./artifacts').download()
|
||||
abstract.ProtoDataset("./artifacts").download()
|
||||
|
||||
|
||||
class TestTecator(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.artifacts_dir = './artifacts/Tecator'
|
||||
self.artifacts_dir = "./artifacts/Tecator"
|
||||
self._remove_artifacts()
|
||||
|
||||
def _remove_artifacts(self):
|
||||
@@ -39,23 +39,23 @@ class TestTecator(unittest.TestCase):
|
||||
shutil.rmtree(self.artifacts_dir)
|
||||
|
||||
def test_download_false(self):
|
||||
rootdir = self.artifacts_dir.rpartition('/')[0]
|
||||
rootdir = self.artifacts_dir.rpartition("/")[0]
|
||||
self._remove_artifacts()
|
||||
with self.assertRaises(RuntimeError):
|
||||
_ = tecator.Tecator(rootdir, download=False)
|
||||
|
||||
def test_download_caching(self):
|
||||
rootdir = self.artifacts_dir.rpartition('/')[0]
|
||||
rootdir = self.artifacts_dir.rpartition("/")[0]
|
||||
_ = tecator.Tecator(rootdir, download=True, verbose=False)
|
||||
_ = tecator.Tecator(rootdir, download=False, verbose=False)
|
||||
|
||||
def test_repr(self):
|
||||
rootdir = self.artifacts_dir.rpartition('/')[0]
|
||||
rootdir = self.artifacts_dir.rpartition("/")[0]
|
||||
train = tecator.Tecator(rootdir, download=True, verbose=True)
|
||||
self.assertTrue('Split: Train' in train.__repr__())
|
||||
self.assertTrue("Split: Train" in train.__repr__())
|
||||
|
||||
def test_download_train(self):
|
||||
rootdir = self.artifacts_dir.rpartition('/')[0]
|
||||
rootdir = self.artifacts_dir.rpartition("/")[0]
|
||||
train = tecator.Tecator(root=rootdir,
|
||||
train=True,
|
||||
download=True,
|
||||
@@ -67,7 +67,7 @@ class TestTecator(unittest.TestCase):
|
||||
self.assertEqual(x_train.shape[1], 100)
|
||||
|
||||
def test_download_test(self):
|
||||
rootdir = self.artifacts_dir.rpartition('/')[0]
|
||||
rootdir = self.artifacts_dir.rpartition("/")[0]
|
||||
test = tecator.Tecator(root=rootdir, train=False, verbose=False)
|
||||
x_test, y_test = test.data, test.targets
|
||||
self.assertEqual(x_test.shape[0], 71)
|
||||
@@ -75,19 +75,19 @@ class TestTecator(unittest.TestCase):
|
||||
self.assertEqual(x_test.shape[1], 100)
|
||||
|
||||
def test_class_to_idx(self):
|
||||
rootdir = self.artifacts_dir.rpartition('/')[0]
|
||||
rootdir = self.artifacts_dir.rpartition("/")[0]
|
||||
test = tecator.Tecator(root=rootdir, train=False, verbose=False)
|
||||
_ = test.class_to_idx
|
||||
|
||||
def test_getitem(self):
|
||||
rootdir = self.artifacts_dir.rpartition('/')[0]
|
||||
rootdir = self.artifacts_dir.rpartition("/")[0]
|
||||
test = tecator.Tecator(root=rootdir, train=False, verbose=False)
|
||||
x, y = test[0]
|
||||
self.assertEqual(x.shape[0], 100)
|
||||
self.assertIsInstance(y, int)
|
||||
|
||||
def test_loadable_with_dataloader(self):
|
||||
rootdir = self.artifacts_dir.rpartition('/')[0]
|
||||
rootdir = self.artifacts_dir.rpartition("/")[0]
|
||||
test = tecator.Tecator(root=rootdir, train=False, verbose=False)
|
||||
_ = torch.utils.data.DataLoader(test, batch_size=64, shuffle=True)
|
||||
|
||||
|
@@ -11,7 +11,7 @@ from prototorch.functions import (activations, competitions, distances,
|
||||
|
||||
class TestActivations(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.flist = ['identity', 'sigmoid_beta', 'swish_beta']
|
||||
self.flist = ["identity", "sigmoid_beta", "swish_beta"]
|
||||
self.x = torch.randn(1024, 1)
|
||||
|
||||
def test_registry(self):
|
||||
@@ -39,7 +39,7 @@ class TestActivations(unittest.TestCase):
|
||||
self.assertEqual(1, f(1))
|
||||
|
||||
def test_unknown_deserialization(self):
|
||||
for funcname in ['blubb', 'foobar']:
|
||||
for funcname in ["blubb", "foobar"]:
|
||||
with self.assertRaises(NameError):
|
||||
_ = activations.get_activation(funcname)
|
||||
|
||||
@@ -76,7 +76,7 @@ class TestCompetitions(unittest.TestCase):
|
||||
pass
|
||||
|
||||
def test_wtac(self):
|
||||
d = torch.tensor([[2., 3., 1.99, 3.01], [2., 3., 2.01, 3.]])
|
||||
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])
|
||||
actual = competitions.wtac(d, labels)
|
||||
desired = torch.tensor([2, 0])
|
||||
@@ -86,7 +86,7 @@ class TestCompetitions(unittest.TestCase):
|
||||
self.assertIsNone(mismatch)
|
||||
|
||||
def test_wtac_unequal_dist(self):
|
||||
d = torch.tensor([[2., 3., 4.], [2., 3., 1.]])
|
||||
d = torch.tensor([[2.0, 3.0, 4.0], [2.0, 3.0, 1.0]])
|
||||
labels = torch.tensor([0, 1, 1])
|
||||
actual = competitions.wtac(d, labels)
|
||||
desired = torch.tensor([0, 1])
|
||||
@@ -96,7 +96,7 @@ class TestCompetitions(unittest.TestCase):
|
||||
self.assertIsNone(mismatch)
|
||||
|
||||
def test_wtac_one_hot(self):
|
||||
d = torch.tensor([[1.99, 3.01], [3., 2.01]])
|
||||
d = torch.tensor([[1.99, 3.01], [3.0, 2.01]])
|
||||
labels = torch.tensor([[0, 1], [1, 0]])
|
||||
actual = competitions.wtac(d, labels)
|
||||
desired = torch.tensor([[0, 1], [1, 0]])
|
||||
@@ -106,38 +106,38 @@ class TestCompetitions(unittest.TestCase):
|
||||
self.assertIsNone(mismatch)
|
||||
|
||||
def test_stratified_min(self):
|
||||
d = torch.tensor([[1., 0., 2., 3.], [9., 8., 0, 1]])
|
||||
d = torch.tensor([[1.0, 0.0, 2.0, 3.0], [9.0, 8.0, 0, 1]])
|
||||
labels = torch.tensor([0, 0, 1, 2])
|
||||
actual = competitions.stratified_min(d, labels)
|
||||
desired = torch.tensor([[0., 2., 3.], [8., 0., 1.]])
|
||||
desired = torch.tensor([[0.0, 2.0, 3.0], [8.0, 0.0, 1.0]])
|
||||
mismatch = np.testing.assert_array_almost_equal(actual,
|
||||
desired,
|
||||
decimal=5)
|
||||
self.assertIsNone(mismatch)
|
||||
|
||||
def test_stratified_min_one_hot(self):
|
||||
d = torch.tensor([[1., 0., 2., 3.], [9., 8., 0, 1]])
|
||||
d = torch.tensor([[1.0, 0.0, 2.0, 3.0], [9.0, 8.0, 0, 1]])
|
||||
labels = torch.tensor([0, 0, 1, 2])
|
||||
labels = torch.eye(3)[labels]
|
||||
actual = competitions.stratified_min(d, labels)
|
||||
desired = torch.tensor([[0., 2., 3.], [8., 0., 1.]])
|
||||
desired = torch.tensor([[0.0, 2.0, 3.0], [8.0, 0.0, 1.0]])
|
||||
mismatch = np.testing.assert_array_almost_equal(actual,
|
||||
desired,
|
||||
decimal=5)
|
||||
self.assertIsNone(mismatch)
|
||||
|
||||
def test_stratified_min_simple(self):
|
||||
d = torch.tensor([[0., 2., 3.], [8., 0, 1]])
|
||||
d = torch.tensor([[0.0, 2.0, 3.0], [8.0, 0, 1]])
|
||||
labels = torch.tensor([0, 1, 2])
|
||||
actual = competitions.stratified_min(d, labels)
|
||||
desired = torch.tensor([[0., 2., 3.], [8., 0., 1.]])
|
||||
desired = torch.tensor([[0.0, 2.0, 3.0], [8.0, 0.0, 1.0]])
|
||||
mismatch = np.testing.assert_array_almost_equal(actual,
|
||||
desired,
|
||||
decimal=5)
|
||||
self.assertIsNone(mismatch)
|
||||
|
||||
def test_knnc_k1(self):
|
||||
d = torch.tensor([[2., 3., 1.99, 3.01], [2., 3., 2.01, 3.]])
|
||||
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])
|
||||
actual = competitions.knnc(d, labels, k=torch.tensor([1]))
|
||||
desired = torch.tensor([2, 0])
|
||||
@@ -194,12 +194,12 @@ class TestDistances(unittest.TestCase):
|
||||
desired = torch.empty(self.nx, self.ny)
|
||||
for i in range(self.nx):
|
||||
for j in range(self.ny):
|
||||
desired[i][j] = torch.nn.functional.pairwise_distance(
|
||||
desired[i][j] = (torch.nn.functional.pairwise_distance(
|
||||
self.x[i].reshape(1, -1),
|
||||
self.y[j].reshape(1, -1),
|
||||
p=2,
|
||||
keepdim=False,
|
||||
)**2
|
||||
)**2)
|
||||
mismatch = np.testing.assert_array_almost_equal(actual,
|
||||
desired,
|
||||
decimal=2)
|
||||
@@ -254,14 +254,14 @@ class TestDistances(unittest.TestCase):
|
||||
self.assertIsNone(mismatch)
|
||||
|
||||
def test_lpnorm_pinf(self):
|
||||
actual = distances.lpnorm_distance(self.x, self.y, p=float('inf'))
|
||||
actual = distances.lpnorm_distance(self.x, self.y, p=float("inf"))
|
||||
desired = torch.empty(self.nx, self.ny)
|
||||
for i in range(self.nx):
|
||||
for j in range(self.ny):
|
||||
desired[i][j] = torch.nn.functional.pairwise_distance(
|
||||
self.x[i].reshape(1, -1),
|
||||
self.y[j].reshape(1, -1),
|
||||
p=float('inf'),
|
||||
p=float("inf"),
|
||||
keepdim=False,
|
||||
)
|
||||
mismatch = np.testing.assert_array_almost_equal(actual,
|
||||
@@ -275,12 +275,12 @@ class TestDistances(unittest.TestCase):
|
||||
desired = torch.empty(self.nx, self.ny)
|
||||
for i in range(self.nx):
|
||||
for j in range(self.ny):
|
||||
desired[i][j] = torch.nn.functional.pairwise_distance(
|
||||
desired[i][j] = (torch.nn.functional.pairwise_distance(
|
||||
self.x[i].reshape(1, -1),
|
||||
self.y[j].reshape(1, -1),
|
||||
p=2,
|
||||
keepdim=False,
|
||||
)**2
|
||||
)**2)
|
||||
mismatch = np.testing.assert_array_almost_equal(actual,
|
||||
desired,
|
||||
decimal=2)
|
||||
@@ -293,12 +293,12 @@ class TestDistances(unittest.TestCase):
|
||||
desired = torch.empty(self.nx, self.ny)
|
||||
for i in range(self.nx):
|
||||
for j in range(self.ny):
|
||||
desired[i][j] = torch.nn.functional.pairwise_distance(
|
||||
desired[i][j] = (torch.nn.functional.pairwise_distance(
|
||||
self.x[i].reshape(1, -1),
|
||||
self.y[j].reshape(1, -1),
|
||||
p=2,
|
||||
keepdim=False,
|
||||
)**2
|
||||
)**2)
|
||||
mismatch = np.testing.assert_array_almost_equal(actual,
|
||||
desired,
|
||||
decimal=2)
|
||||
@@ -311,8 +311,12 @@ class TestDistances(unittest.TestCase):
|
||||
class TestInitializers(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.flist = [
|
||||
'zeros', 'ones', 'rand', 'randn', 'stratified_mean',
|
||||
'stratified_random'
|
||||
"zeros",
|
||||
"ones",
|
||||
"rand",
|
||||
"randn",
|
||||
"stratified_mean",
|
||||
"stratified_random",
|
||||
]
|
||||
self.x = torch.tensor(
|
||||
[[0, -1, -2], [10, 11, 12], [0, 0, 0], [2, 2, 2]],
|
||||
@@ -340,7 +344,7 @@ class TestInitializers(unittest.TestCase):
|
||||
self.assertEqual(1, f(1))
|
||||
|
||||
def test_unknown_deserialization(self):
|
||||
for funcname in ['blubb', 'foobar']:
|
||||
for funcname in ["blubb", "foobar"]:
|
||||
with self.assertRaises(NameError):
|
||||
_ = initializers.get_initializer(funcname)
|
||||
|
||||
@@ -383,7 +387,7 @@ class TestInitializers(unittest.TestCase):
|
||||
def test_stratified_mean_equal1(self):
|
||||
pdist = torch.tensor([1, 1])
|
||||
actual, _ = initializers.stratified_mean(self.x, self.y, pdist, False)
|
||||
desired = torch.tensor([[5., 5., 5.], [1., 1., 1.]])
|
||||
desired = torch.tensor([[5.0, 5.0, 5.0], [1.0, 1.0, 1.0]])
|
||||
mismatch = np.testing.assert_array_almost_equal(actual,
|
||||
desired,
|
||||
decimal=5)
|
||||
@@ -393,7 +397,7 @@ class TestInitializers(unittest.TestCase):
|
||||
pdist = torch.tensor([1, 1])
|
||||
actual, _ = initializers.stratified_random(self.x, self.y, pdist,
|
||||
False)
|
||||
desired = torch.tensor([[0., -1., -2.], [0., 0., 0.]])
|
||||
desired = torch.tensor([[0.0, -1.0, -2.0], [0.0, 0.0, 0.0]])
|
||||
mismatch = np.testing.assert_array_almost_equal(actual,
|
||||
desired,
|
||||
decimal=5)
|
||||
@@ -402,8 +406,8 @@ class TestInitializers(unittest.TestCase):
|
||||
def test_stratified_mean_equal2(self):
|
||||
pdist = torch.tensor([2, 2])
|
||||
actual, _ = initializers.stratified_mean(self.x, self.y, pdist, False)
|
||||
desired = torch.tensor([[5., 5., 5.], [5., 5., 5.], [1., 1., 1.],
|
||||
[1., 1., 1.]])
|
||||
desired = torch.tensor([[5.0, 5.0, 5.0], [5.0, 5.0, 5.0],
|
||||
[1.0, 1.0, 1.0], [1.0, 1.0, 1.0]])
|
||||
mismatch = np.testing.assert_array_almost_equal(actual,
|
||||
desired,
|
||||
decimal=5)
|
||||
@@ -413,8 +417,8 @@ class TestInitializers(unittest.TestCase):
|
||||
pdist = torch.tensor([2, 2])
|
||||
actual, _ = initializers.stratified_random(self.x, self.y, pdist,
|
||||
False)
|
||||
desired = torch.tensor([[0., -1., -2.], [0., -1., -2.], [0., 0., 0.],
|
||||
[0., 0., 0.]])
|
||||
desired = torch.tensor([[0.0, -1.0, -2.0], [0.0, -1.0, -2.0],
|
||||
[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]])
|
||||
mismatch = np.testing.assert_array_almost_equal(actual,
|
||||
desired,
|
||||
decimal=5)
|
||||
@@ -423,8 +427,8 @@ class TestInitializers(unittest.TestCase):
|
||||
def test_stratified_mean_unequal(self):
|
||||
pdist = torch.tensor([1, 3])
|
||||
actual, _ = initializers.stratified_mean(self.x, self.y, pdist, False)
|
||||
desired = torch.tensor([[5., 5., 5.], [1., 1., 1.], [1., 1., 1.],
|
||||
[1., 1., 1.]])
|
||||
desired = torch.tensor([[5.0, 5.0, 5.0], [1.0, 1.0, 1.0],
|
||||
[1.0, 1.0, 1.0], [1.0, 1.0, 1.0]])
|
||||
mismatch = np.testing.assert_array_almost_equal(actual,
|
||||
desired,
|
||||
decimal=5)
|
||||
@@ -434,8 +438,8 @@ class TestInitializers(unittest.TestCase):
|
||||
pdist = torch.tensor([1, 3])
|
||||
actual, _ = initializers.stratified_random(self.x, self.y, pdist,
|
||||
False)
|
||||
desired = torch.tensor([[0., -1., -2.], [0., 0., 0.], [0., 0., 0.],
|
||||
[0., 0., 0.]])
|
||||
desired = torch.tensor([[0.0, -1.0, -2.0], [0.0, 0.0, 0.0],
|
||||
[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]])
|
||||
mismatch = np.testing.assert_array_almost_equal(actual,
|
||||
desired,
|
||||
decimal=5)
|
||||
@@ -444,8 +448,8 @@ class TestInitializers(unittest.TestCase):
|
||||
def test_stratified_mean_unequal_one_hot(self):
|
||||
pdist = torch.tensor([1, 3])
|
||||
y = torch.eye(2)[self.y]
|
||||
desired1 = torch.tensor([[5., 5., 5.], [1., 1., 1.], [1., 1., 1.],
|
||||
[1., 1., 1.]])
|
||||
desired1 = torch.tensor([[5.0, 5.0, 5.0], [1.0, 1.0, 1.0],
|
||||
[1.0, 1.0, 1.0], [1.0, 1.0, 1.0]])
|
||||
actual1, actual2 = initializers.stratified_mean(self.x, y, pdist)
|
||||
desired2 = torch.tensor([[1, 0], [0, 1], [0, 1], [0, 1]])
|
||||
mismatch = np.testing.assert_array_almost_equal(actual1,
|
||||
@@ -460,8 +464,8 @@ class TestInitializers(unittest.TestCase):
|
||||
pdist = torch.tensor([1, 3])
|
||||
y = torch.eye(2)[self.y]
|
||||
actual1, actual2 = initializers.stratified_random(self.x, y, pdist)
|
||||
desired1 = torch.tensor([[0., -1., -2.], [0., 0., 0.], [0., 0., 0.],
|
||||
[0., 0., 0.]])
|
||||
desired1 = torch.tensor([[0.0, -1.0, -2.0], [0.0, 0.0, 0.0],
|
||||
[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]])
|
||||
desired2 = torch.tensor([[1, 0], [0, 1], [0, 1], [0, 1]])
|
||||
mismatch = np.testing.assert_array_almost_equal(actual1,
|
||||
desired1,
|
||||
|
@@ -29,10 +29,12 @@ class TestPrototypes(unittest.TestCase):
|
||||
_ = prototypes.Prototypes1D(nclasses=1, input_dim=1)
|
||||
|
||||
def test_prototypes1d_init_without_pdist(self):
|
||||
p1 = prototypes.Prototypes1D(input_dim=6,
|
||||
nclasses=2,
|
||||
prototypes_per_class=4,
|
||||
prototype_initializer='ones')
|
||||
p1 = prototypes.Prototypes1D(
|
||||
input_dim=6,
|
||||
nclasses=2,
|
||||
prototypes_per_class=4,
|
||||
prototype_initializer="ones",
|
||||
)
|
||||
protos = p1.prototypes
|
||||
actual = protos.detach().numpy()
|
||||
desired = torch.ones(8, 6)
|
||||
@@ -45,7 +47,7 @@ class TestPrototypes(unittest.TestCase):
|
||||
pdist = [2, 2]
|
||||
p1 = prototypes.Prototypes1D(input_dim=3,
|
||||
prototype_distribution=pdist,
|
||||
prototype_initializer='zeros')
|
||||
prototype_initializer="zeros")
|
||||
protos = p1.prototypes
|
||||
actual = protos.detach().numpy()
|
||||
desired = torch.zeros(4, 3)
|
||||
@@ -60,14 +62,15 @@ class TestPrototypes(unittest.TestCase):
|
||||
input_dim=3,
|
||||
nclasses=2,
|
||||
prototypes_per_class=1,
|
||||
prototype_initializer='stratified_mean',
|
||||
data=None)
|
||||
prototype_initializer="stratified_mean",
|
||||
data=None,
|
||||
)
|
||||
|
||||
def test_prototypes1d_init_torch_pdist(self):
|
||||
pdist = torch.tensor([2, 2])
|
||||
p1 = prototypes.Prototypes1D(input_dim=3,
|
||||
prototype_distribution=pdist,
|
||||
prototype_initializer='zeros')
|
||||
prototype_initializer="zeros")
|
||||
protos = p1.prototypes
|
||||
actual = protos.detach().numpy()
|
||||
desired = torch.zeros(4, 3)
|
||||
@@ -77,24 +80,30 @@ class TestPrototypes(unittest.TestCase):
|
||||
self.assertIsNone(mismatch)
|
||||
|
||||
def test_prototypes1d_init_without_inputdim_with_data(self):
|
||||
_ = prototypes.Prototypes1D(nclasses=2,
|
||||
prototypes_per_class=1,
|
||||
prototype_initializer='stratified_mean',
|
||||
data=[[[1.], [0.]], [1, 0]])
|
||||
_ = prototypes.Prototypes1D(
|
||||
nclasses=2,
|
||||
prototypes_per_class=1,
|
||||
prototype_initializer="stratified_mean",
|
||||
data=[[[1.0], [0.0]], [1, 0]],
|
||||
)
|
||||
|
||||
def test_prototypes1d_init_with_int_data(self):
|
||||
_ = prototypes.Prototypes1D(nclasses=2,
|
||||
prototypes_per_class=1,
|
||||
prototype_initializer='stratified_mean',
|
||||
data=[[[1], [0]], [1, 0]])
|
||||
_ = prototypes.Prototypes1D(
|
||||
nclasses=2,
|
||||
prototypes_per_class=1,
|
||||
prototype_initializer="stratified_mean",
|
||||
data=[[[1], [0]], [1, 0]],
|
||||
)
|
||||
|
||||
def test_prototypes1d_init_one_hot_without_data(self):
|
||||
_ = prototypes.Prototypes1D(input_dim=1,
|
||||
nclasses=2,
|
||||
prototypes_per_class=1,
|
||||
prototype_initializer='stratified_mean',
|
||||
data=None,
|
||||
one_hot_labels=True)
|
||||
_ = prototypes.Prototypes1D(
|
||||
input_dim=1,
|
||||
nclasses=2,
|
||||
prototypes_per_class=1,
|
||||
prototype_initializer="stratified_mean",
|
||||
data=None,
|
||||
one_hot_labels=True,
|
||||
)
|
||||
|
||||
def test_prototypes1d_init_one_hot_labels_false(self):
|
||||
"""Test if ValueError is raised when `one_hot_labels` is set to `False`
|
||||
@@ -105,9 +114,10 @@ class TestPrototypes(unittest.TestCase):
|
||||
input_dim=1,
|
||||
nclasses=2,
|
||||
prototypes_per_class=1,
|
||||
prototype_initializer='stratified_mean',
|
||||
data=([[0.], [1.]], [[0, 1], [1, 0]]),
|
||||
one_hot_labels=False)
|
||||
prototype_initializer="stratified_mean",
|
||||
data=([[0.0], [1.0]], [[0, 1], [1, 0]]),
|
||||
one_hot_labels=False,
|
||||
)
|
||||
|
||||
def test_prototypes1d_init_1d_y_data_one_hot_labels_true(self):
|
||||
"""Test if ValueError is raised when `one_hot_labels` is set to `True`
|
||||
@@ -118,9 +128,10 @@ class TestPrototypes(unittest.TestCase):
|
||||
input_dim=1,
|
||||
nclasses=2,
|
||||
prototypes_per_class=1,
|
||||
prototype_initializer='stratified_mean',
|
||||
data=([[0.], [1.]], [0, 1]),
|
||||
one_hot_labels=True)
|
||||
prototype_initializer="stratified_mean",
|
||||
data=([[0.0], [1.0]], [0, 1]),
|
||||
one_hot_labels=True,
|
||||
)
|
||||
|
||||
def test_prototypes1d_init_one_hot_labels_true(self):
|
||||
"""Test if ValueError is raised when `one_hot_labels` is set to `True`
|
||||
@@ -132,25 +143,27 @@ class TestPrototypes(unittest.TestCase):
|
||||
input_dim=1,
|
||||
nclasses=2,
|
||||
prototypes_per_class=1,
|
||||
prototype_initializer='stratified_mean',
|
||||
data=([[0.], [1.]], [[0], [1]]),
|
||||
one_hot_labels=True)
|
||||
prototype_initializer="stratified_mean",
|
||||
data=([[0.0], [1.0]], [[0], [1]]),
|
||||
one_hot_labels=True,
|
||||
)
|
||||
|
||||
def test_prototypes1d_init_with_int_dtype(self):
|
||||
with self.assertRaises(RuntimeError):
|
||||
_ = prototypes.Prototypes1D(
|
||||
nclasses=2,
|
||||
prototypes_per_class=1,
|
||||
prototype_initializer='stratified_mean',
|
||||
prototype_initializer="stratified_mean",
|
||||
data=[[[1], [0]], [1, 0]],
|
||||
dtype=torch.int32)
|
||||
dtype=torch.int32,
|
||||
)
|
||||
|
||||
def test_prototypes1d_inputndim_with_data(self):
|
||||
with self.assertRaises(ValueError):
|
||||
_ = prototypes.Prototypes1D(input_dim=1,
|
||||
nclasses=1,
|
||||
prototypes_per_class=1,
|
||||
data=[[1.], [1]])
|
||||
data=[[1.0], [1]])
|
||||
|
||||
def test_prototypes1d_inputdim_with_data(self):
|
||||
with self.assertRaises(ValueError):
|
||||
@@ -158,8 +171,9 @@ class TestPrototypes(unittest.TestCase):
|
||||
input_dim=2,
|
||||
nclasses=2,
|
||||
prototypes_per_class=1,
|
||||
prototype_initializer='stratified_mean',
|
||||
data=[[[1.], [0.]], [1, 0]])
|
||||
prototype_initializer="stratified_mean",
|
||||
data=[[[1.0], [0.0]], [1, 0]],
|
||||
)
|
||||
|
||||
def test_prototypes1d_nclasses_with_data(self):
|
||||
"""Test ValueError raise if provided `nclasses` is not the same
|
||||
@@ -170,13 +184,14 @@ class TestPrototypes(unittest.TestCase):
|
||||
input_dim=1,
|
||||
nclasses=1,
|
||||
prototypes_per_class=1,
|
||||
prototype_initializer='stratified_mean',
|
||||
data=[[[1.], [2.]], [1, 2]])
|
||||
prototype_initializer="stratified_mean",
|
||||
data=[[[1.0], [2.0]], [1, 2]],
|
||||
)
|
||||
|
||||
def test_prototypes1d_init_with_ppc(self):
|
||||
p1 = prototypes.Prototypes1D(data=[self.x, self.y],
|
||||
prototypes_per_class=2,
|
||||
prototype_initializer='zeros')
|
||||
prototype_initializer="zeros")
|
||||
protos = p1.prototypes
|
||||
actual = protos.detach().numpy()
|
||||
desired = torch.zeros(4, 3)
|
||||
@@ -186,9 +201,11 @@ class TestPrototypes(unittest.TestCase):
|
||||
self.assertIsNone(mismatch)
|
||||
|
||||
def test_prototypes1d_init_with_pdist(self):
|
||||
p1 = prototypes.Prototypes1D(data=[self.x, self.y],
|
||||
prototype_distribution=[6, 9],
|
||||
prototype_initializer='zeros')
|
||||
p1 = prototypes.Prototypes1D(
|
||||
data=[self.x, self.y],
|
||||
prototype_distribution=[6, 9],
|
||||
prototype_initializer="zeros",
|
||||
)
|
||||
protos = p1.prototypes
|
||||
actual = protos.detach().numpy()
|
||||
desired = torch.zeros(15, 3)
|
||||
@@ -201,10 +218,12 @@ class TestPrototypes(unittest.TestCase):
|
||||
def my_initializer(*args, **kwargs):
|
||||
return torch.full((2, 99), 99.0), torch.tensor([0, 1])
|
||||
|
||||
p1 = prototypes.Prototypes1D(input_dim=99,
|
||||
nclasses=2,
|
||||
prototypes_per_class=1,
|
||||
prototype_initializer=my_initializer)
|
||||
p1 = prototypes.Prototypes1D(
|
||||
input_dim=99,
|
||||
nclasses=2,
|
||||
prototypes_per_class=1,
|
||||
prototype_initializer=my_initializer,
|
||||
)
|
||||
protos = p1.prototypes
|
||||
actual = protos.detach().numpy()
|
||||
desired = 99 * torch.ones(2, 99)
|
||||
@@ -231,7 +250,7 @@ class TestPrototypes(unittest.TestCase):
|
||||
def test_prototypes1d_validate_extra_repr_not_empty(self):
|
||||
p1 = prototypes.Prototypes1D(input_dim=0, prototype_distribution=[0])
|
||||
rep = p1.extra_repr()
|
||||
self.assertNotEqual(rep, '')
|
||||
self.assertNotEqual(rep, "")
|
||||
|
||||
def tearDown(self):
|
||||
del self.x, self.y, self.gen
|
||||
@@ -243,11 +262,11 @@ class TestLosses(unittest.TestCase):
|
||||
pass
|
||||
|
||||
def test_glvqloss_init(self):
|
||||
_ = losses.GLVQLoss(0, 'swish_beta', beta=20)
|
||||
_ = losses.GLVQLoss(0, "swish_beta", beta=20)
|
||||
|
||||
def test_glvqloss_forward_1ppc(self):
|
||||
criterion = losses.GLVQLoss(margin=0,
|
||||
squashing='sigmoid_beta',
|
||||
squashing="sigmoid_beta",
|
||||
beta=100)
|
||||
d = torch.stack([torch.ones(100), torch.zeros(100)], dim=1)
|
||||
labels = torch.tensor([0, 1])
|
||||
@@ -259,7 +278,7 @@ class TestLosses(unittest.TestCase):
|
||||
|
||||
def test_glvqloss_forward_2ppc(self):
|
||||
criterion = losses.GLVQLoss(margin=0,
|
||||
squashing='sigmoid_beta',
|
||||
squashing="sigmoid_beta",
|
||||
beta=100)
|
||||
d = torch.stack([
|
||||
torch.ones(100),
|
||||
|
Reference in New Issue
Block a user