def test_subspaces(self): isa = ISA(2, 4, 2) # simple sanity checks self.assertEqual(isa.subspaces()[0].dim, 2) self.assertEqual(isa.subspaces()[1].dim, 2) self.assertEqual(sys.getrefcount(isa.subspaces()), 1) self.assertEqual(sys.getrefcount(isa.subspaces()[0]), 1)
def test_evaluate(self): isa1 = ISA(2) isa1.A = eye(2) subspaces = isa1.subspaces() for gsm in subspaces: gsm.scales = ones(gsm.num_scales) isa1.set_subspaces(subspaces) # equivalent overcomplete model isa2 = ISA(2, 4) A = copy(isa2.A) A[:, :2] = isa1.A / sqrt(2.) A[:, 2:] = isa1.A / sqrt(2.) isa2.A = A subspaces = isa2.subspaces() for gsm in subspaces: gsm.scales = ones(gsm.num_scales) isa2.set_subspaces(subspaces) data = isa1.sample(100) # the results should not depend on the parameters ll1 = isa1.evaluate(data) ll2 = isa2.evaluate(data) self.assertLess(abs(ll1 - ll2), 1e-5) isa1 = ISA(2) isa1.initialize() # equivalent overcomplete model isa2 = ISA(2, 4) isa2.set_subspaces(isa1.subspaces() * 2) A = isa2.basis() A[:, :2] = isa1.basis() A[:, 2:] = 0. isa2.set_basis(A) data = isa1.sample(100) params = isa2.default_parameters() params['ais']['num_iter'] = 100 params['ais']['num_samples'] = 100 ll1 = isa1.evaluate(data) ll2 = isa2.evaluate(data, params) self.assertLess(abs(ll1 - ll2), 0.1)
def test_sample_scales(self): isa = ISA(2, 5, num_scales=4) # get a copy of subspaces subspaces = isa.subspaces() # replace scales for gsm in subspaces: gsm.scales = asarray([1., 2., 3., 4.]) isa.set_subspaces(subspaces) samples = isa.sample_prior(100000) scales = isa.sample_scales(samples) # simple sanity checks self.assertEqual(scales.shape[0], isa.num_hiddens) self.assertEqual(scales.shape[1], samples.shape[1]) priors = mean( abs(scales.flatten() - asarray([[1., 2., 3., 4.]]).T) < 0.5, 1) # prior probabilities of scales should be equal and sum up to one self.assertLess(max(abs(priors - 1. / subspaces[0].num_scales)), 0.01) self.assertLess(abs(sum(priors) - 1.), 1e-10)
def test_merge(self): isa1 = ISA(5, ssize=2) isa2 = ISA(5) isa1.initialize() isa1.orthogonalize() isa2.initialize() isa2.A = isa1.A params = isa2.default_parameters() params['train_basis'] = False params['merge_subspaces'] = True params['merge']['verbosity'] = 0 isa2.train(isa1.sample(10000), params) ssizes1 = [gsm.dim for gsm in isa1.subspaces()] ssizes2 = [gsm.dim for gsm in isa2.subspaces()] # algorithm should be able to recover subspace sizes self.assertTrue(all(sort(ssizes1) == sort(ssizes2)))
def test_pickle(self): isa0 = ISA(4, 16, ssize=3) isa0.set_hidden_states(randn(16, 100)) tmp_file = mkstemp()[1] # store model with open(tmp_file, 'w') as handle: dump({'isa': isa0}, handle) # load model with open(tmp_file) as handle: isa1 = load(handle)['isa'] # make sure parameters haven't changed self.assertEqual(isa0.num_visibles, isa1.num_visibles) self.assertEqual(isa0.num_hiddens, isa1.num_hiddens) self.assertLess(max(abs(isa0.A - isa1.A)), 1e-20) self.assertLess(max(abs(isa0.hidden_states() - isa1.hidden_states())), 1e-20) self.assertLess(max(abs(isa0.subspaces()[1].scales - isa1.subspaces()[1].scales)), 1e-20)
def test_pickle(self): isa0 = ISA(4, 16, ssize=3) isa0.set_hidden_states(randn(16, 100)) tmp_file = mkstemp()[1] # store model with open(tmp_file, 'w') as handle: dump({'isa': isa0}, handle) # load model with open(tmp_file) as handle: isa1 = load(handle)['isa'] # make sure parameters haven't changed self.assertEqual(isa0.num_visibles, isa1.num_visibles) self.assertEqual(isa0.num_hiddens, isa1.num_hiddens) self.assertLess(max(abs(isa0.A - isa1.A)), 1e-20) self.assertLess(max(abs(isa0.hidden_states() - isa1.hidden_states())), 1e-20) self.assertLess( max(abs(isa0.subspaces()[1].scales - isa1.subspaces()[1].scales)), 1e-20)
def test_sample_scales(self): isa = ISA(2, 5, num_scales=4) # get a copy of subspaces subspaces = isa.subspaces() # replace scales for gsm in subspaces: gsm.scales = asarray([1., 2., 3., 4.]) isa.set_subspaces(subspaces) samples = isa.sample_prior(100000) scales = isa.sample_scales(samples) # simple sanity checks self.assertEqual(scales.shape[0], isa.num_hiddens) self.assertEqual(scales.shape[1], samples.shape[1]) priors = mean(abs(scales.flatten() - asarray([[1., 2., 3., 4.]]).T) < 0.5, 1) # prior probabilities of scales should be equal and sum up to one self.assertLess(max(abs(priors - 1. / subspaces[0].num_scales)), 0.01) self.assertLess(abs(sum(priors) - 1.), 1e-10)