def test_sparse_coded_signal(): scs = SG.SparseCodedSignal(n_samples=5, n_components=8, n_features=10, n_nonzero_coefs=3, random_state=0) Y = scs.latent_structure_task() D = scs.D # XXX use scs.descr X = scs.X # XXX use scs.meta tasks.assert_latent_structure(Y) assert_equal(Y.shape, (10, 5), "Y shape mismatch") assert_equal(D.shape, (10, 8), "D shape mismatch") assert_equal(X.shape, (8, 5), "X shape mismatch") for col in X.T: assert_equal(len(np.flatnonzero(col)), 3, "Non-zero coefs mismatch") assert_equal(np.dot(D, X), Y) assert_array_almost_equal(np.sqrt((D ** 2).sum(axis=0)), np.ones(D.shape[1]))
def test_low_rank_matrix(): lrm = SG.LowRankMatrix(n_samples=50, n_features=25, effective_rank=5, tail_strength=0.01, random_state=0) X = lrm.latent_structure_task() tasks.assert_latent_structure(X) assert_equal(X.shape, (50, 25), "X shape mismatch") from numpy.linalg import svd u, s, v = svd(X) assert sum(s) - 5 < 0.1, "X rank is not approximately 5" X, Y = lrm.matrix_completion_task() tasks.assert_matrix_completion(X, Y)
def test_sparse_coded_signal(): scs = SG.SparseCodedSignal(n_samples=5, n_components=8, n_features=10, n_nonzero_coefs=3, random_state=0) Y = scs.latent_structure_task() D = scs.D # XXX use scs.descr X = scs.X # XXX use scs.meta tasks.assert_latent_structure(Y) assert_equal(Y.shape, (10, 5), "Y shape mismatch") assert_equal(D.shape, (10, 8), "D shape mismatch") assert_equal(X.shape, (8, 5), "X shape mismatch") for col in X.T: assert_equal(len(np.flatnonzero(col)), 3, 'Non-zero coefs mismatch') assert_equal(np.dot(D, X), Y) assert_array_almost_equal(np.sqrt((D ** 2).sum(axis=0)), np.ones(D.shape[1]))
def test_several(): dsetnames = [ 'MNIST_Basic', 'MNIST_BackgroundImages', 'MNIST_BackgroundRandom', 'Rectangles', 'RectanglesImages', 'Convex' ] dsetnames.extend(['MNIST_Noise%i' % i for i in range(1, 7)]) for dsetname in dsetnames: aa = dset(dsetname) assert len(aa.meta) == sum( [aa.descr[s] for s in 'n_train', 'n_valid', 'n_test']) bb = dset(dsetname) assert aa.meta == bb.meta tasks.assert_classification(*aa.classification_task()) tasks.assert_latent_structure(aa.latent_structure_task())
def test_assert_latent_structure(self): # things that work: tasks.assert_latent_structure(rnd('float32', 4, 2)) tasks.assert_latent_structure(rnd('float64', 11, 1)) tasks.assert_latent_structure(rnd('float64', 11, 1), 11) # things that break: self.assertRaises(AssertionError, tasks.assert_latent_structure, rnd('int8', 4, 2)) # X not float self.assertRaises(AssertionError, tasks.assert_latent_structure, rnd('float32', 4, 2, 2)) # X wrong rank self.assertRaises(AssertionError, tasks.assert_latent_structure, rnd('float64', 4)) # X wrong rank self.assertRaises(AssertionError, tasks.assert_latent_structure, rnd('float64', 4, 3), 5) # N mismatch
def test_MNIST_latent_structure(): M = mnist.MNIST() # just make sure we can create the class M.DOWNLOAD_IF_MISSING = False X = M.latent_structure_task() tasks.assert_latent_structure(X, 70000)
assert aa.meta[10000] == dict(id=10000, label=3, split='valid') assert aa.meta[11999] == dict(id=11999, label=3, split='valid') assert aa.meta[12000] == dict(id=12000, label=7, split='test') assert aa.meta[50000] == dict(id=50000, label=3, split='test') assert aa.meta[61989] == dict(id=61989, label=4, split='test') assert len(aa.meta) == 62000 bb = dset(dsetname) assert bb.meta == aa.meta def test_several(): dsetnames = ['MNIST_Basic', 'MNIST_BackgroundImages', 'MNIST_BackgroundRandom', 'Rectangles', 'RectanglesImages', 'Convex'] dsetnames.extend(['MNIST_Noise%i' % i for i in range(1,7)]) for dsetname in dsetnames: aa = dset(dsetname) assert len(aa.meta) == sum( [aa.descr[s] for s in 'n_train', 'n_valid', 'n_test']) bb = dset(dsetname) assert aa.meta == bb.meta tasks.assert_classification(*aa.classification_task()) tasks.assert_latent_structure(aa.latent_structure_task())
def test_latent_structure(): cifar = cifar10.CIFAR10() # just make sure we can create the class cifar.DOWNLOAD_IF_MISSING = False X = cifar.latent_structure_task() tasks.assert_latent_structure(X, 60000)