def test_seed_same(): """Verifies that two MNDs initialized with the same seed produce the same samples """ skip_if_no_scipy() rng = np.random.RandomState([1,2,3]) #the number in the argument here is the limit on #seed value seed = rng.randint(2147462579) dim = 3 mu = rng.randn(dim) rank = dim X = rng.randn(rank,dim) cov = np.dot(X.T,X) mnd1 = MND( sigma = cov, mu = mu, seed = seed) num_samples = 5 rd1 = mnd1.random_design_matrix(num_samples) rd1 = function([],rd1)() mnd2 = MND( sigma = cov, mu = mu, seed = seed) rd2 = mnd2.random_design_matrix(num_samples) rd2 = function([],rd2)() assert np.all(rd1 == rd2)
def test_seed_same(): """Verifies that two MNDs initialized with the same seed produce the same samples """ skip_if_no_scipy() rng = np.random.RandomState([1, 2, 3]) #the number in the argument here is the limit on #seed value seed = rng.randint(2147462579) dim = 3 mu = rng.randn(dim) rank = dim X = rng.randn(rank, dim) cov = np.dot(X.T, X) mnd1 = MND(sigma=cov, mu=mu, seed=seed) num_samples = 5 rd1 = mnd1.random_design_matrix(num_samples) rd1 = function([], rd1)() mnd2 = MND(sigma=cov, mu=mu, seed=seed) rd2 = mnd2.random_design_matrix(num_samples) rd2 = function([], rd2)() assert np.all(rd1 == rd2)
def test_dbm_loader(): """ Loads an example model and some data and makes sure that inference gets the same result as Ruslan Salakhutdino's demo code. """ skip_if_no_scipy() pylearn2_path = pylearn2.__path__[0] dbm_path = pylearn2_path + '/models/tests/dbm.mat' data_path = pylearn2_path + '/models/tests/dbm_data.mat' dbm = load_matlab_dbm(dbm_path) d = io.loadmat( data_path ) for key in d: try: d[key] = np.cast[config.floatX](d[key]) except: pass ip = InferenceProcedure( layer_schedule = [0,1] * 10 ) ip.register_model(dbm) V = T.matrix() if config.compute_test_value != 'off': V.tag.test_value = d['data'] h1_theano, h2_theano = ip.infer(V)['H_hat'] f = function([V],[h1_theano,h2_theano]) data = d['data'] h1_numpy, h2_numpy = f(data) assert np.allclose(d['h1'],h1_numpy) assert np.allclose(d['h2'],h2_numpy)
def test_dbm_loader(): """ Loads an example model and some data and makes sure that inference gets the same result as Ruslan Salakhutdino's demo code. """ skip_if_no_scipy() pylearn2_path = pylearn2.__path__[0] dbm_path = pylearn2_path + '/models/tests/dbm.mat' data_path = pylearn2_path + '/models/tests/dbm_data.mat' dbm = load_matlab_dbm(dbm_path) d = io.loadmat(data_path) for key in d: try: d[key] = np.cast[config.floatX](d[key]) except: pass ip = InferenceProcedure(layer_schedule=[0, 1] * 10) ip.register_model(dbm) V = T.matrix() if config.compute_test_value != 'off': V.tag.test_value = d['data'] h1_theano, h2_theano = ip.infer(V)['H_hat'] f = function([V], [h1_theano, h2_theano]) data = d['data'] h1_numpy, h2_numpy = f(data) assert np.allclose(d['h1'], h1_numpy) assert np.allclose(d['h2'], h2_numpy)
def test_linear_cg(): rng = numpy.random.RandomState([1,2,3]) n = 5 M = rng.randn(2*n,n) M = numpy.dot(M.T,M).astype(config.floatX) b = rng.randn(n).astype(config.floatX) c = rng.randn(1).astype(config.floatX)[0] x = theano.tensor.vector('x') f = 0.5 * tensor.dot(x,tensor.dot(M,x)) - tensor.dot(b,x) + c sol = linear_cg.linear_cg(f,[x]) fn_sol = theano.function([x], sol) start = time.time() sol = fn_sol( rng.randn(n).astype(config.floatX))[0] my_lcg = time.time() -start eval_f = theano.function([x],f) cgf = eval_f(sol) print "conjugate gradient's value of f:", str(cgf), 'time (s)', my_lcg skip_if_no_scipy() spf = eval_f( scipy.linalg.solve(M,b) ) print "scipy.linalg.solve's value of f: "+str(spf) abs_diff = abs(cgf - spf) if not (abs_diff < 1e-5): raise AssertionError("Expected abs_diff < 1e-5, got abs_diff of " + str(abs_diff))
def test_pooling_1d_topology_tuples(): mat = pooling_matrix((3, ), (4, ), (2, )) assert mat.shape == (3, 8) expected = np.array([[1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 1, 1, 1, 1]]) assert np.all(mat == expected) skip_if_no_scipy() spmat = pooling_matrix(3, 4, 2, sparse='csr') assert np.all(spmat.todense() == expected)
def test_pooling_no_topology(): mat = pooling_matrix(4, 5) assert mat.shape == (4, 20) expected = np.array([[1] * 5 + [0] * 15, [0] * 5 + [1] * 5 + [0] * 10, [0] * 10 + [1] * 5 + [0] * 5, [0] * 15 + [1] * 5]) assert np.all(mat == expected) skip_if_no_scipy() spmat = pooling_matrix(4, 5, sparse='csr') assert np.all(spmat.todense() == expected)
def test_pooling_1d_topology_tuples(): mat = pooling_matrix((3,), (4,), (2,)) assert mat.shape == (3, 8) expected = np.array([[1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 1, 1, 1, 1]]) assert np.all(mat == expected) skip_if_no_scipy() spmat = pooling_matrix(3, 4, 2, sparse='csr') assert np.all(spmat.todense() == expected)
def test_pooling_2d_topology_stride2(): mat = pooling_matrix((3, 3), (3, 3), (2, 2)) expected = np.zeros((9, 49)) maps = expected.reshape((3, 3, 7, 7)) maps[0, 0, 0:3, 0:3] = 1. maps[0, 1, 0:3, 2:5] = 1. maps[0, 2, 0:3, 4:7] = 1. maps[1, 0, 2:5, 0:3] = 1. maps[1, 1, 2:5, 2:5] = 1. maps[1, 2, 2:5, 4:7] = 1. maps[2, 0, 4:7, 0:3] = 1. maps[2, 1, 4:7, 2:5] = 1. maps[2, 2, 4:7, 4:7] = 1. assert np.all(mat == expected) skip_if_no_scipy() spmat = pooling_matrix((3, 3), (3, 3), (2, 2), sparse='csr') assert np.all(spmat.todense() == expected)
def test_pooling_2d_non_overlapping(): mat = pooling_matrix((3, 3), (3, 3), (3, 3)) assert mat.shape == (9, 81) expected = np.zeros((9, 81)) maps = expected.reshape((3, 3, 9, 9)) maps[0, 0, 0:3, 0:3] = 1. maps[0, 1, 0:3, 3:6] = 1. maps[0, 2, 0:3, 6:9] = 1. maps[1, 0, 3:6, 0:3] = 1. maps[1, 1, 3:6, 3:6] = 1. maps[1, 2, 3:6, 6:9] = 1. maps[2, 0, 6:9, 0:3] = 1. maps[2, 1, 6:9, 3:6] = 1. maps[2, 2, 6:9, 6:9] = 1. assert np.all(mat == expected) skip_if_no_scipy() spmat = pooling_matrix((3, 3), (3, 3), (3, 3), sparse='csr') assert np.all(spmat.todense() == expected)
def test_pooling_2d_topology(): mat = pooling_matrix((3, 3), (2, 2), (1, 1)) assert mat.shape == (9, 16) expected = np.zeros((9, 16)) maps = expected.reshape((3, 3, 4, 4)) maps[0, 0, 0:2, 0:2] = 1. maps[1, 0, 1:3, 0:2] = 1. maps[2, 0, 2:4, 0:2] = 1. maps[0, 1, 0:2, 1:3] = 1. maps[1, 1, 1:3, 1:3] = 1. maps[2, 1, 2:4, 1:3] = 1. maps[0, 2, 0:2, 2:4] = 1. maps[1, 2, 1:3, 2:4] = 1. maps[2, 2, 2:4, 2:4] = 1. assert np.all(mat == expected) skip_if_no_scipy() spmat = pooling_matrix((3, 3), (2, 2), (1, 1), sparse='csr') assert np.all(spmat.todense() == expected)
def test_seed_diff(): """Verifies that two MNDs initialized with different seeds produce samples that differ at least somewhat (theoretically the samples could match even under valid behavior but this is extremely unlikely)""" skip_if_no_scipy() rng = np.random.RandomState([1, 2, 3]) #the number in the argument here is the limit on #seed value, and we subtract 1 so it will be #possible to add 1 to it for the second MND seed = rng.randint(2147462579) - 1 dim = 3 mu = rng.randn(dim) rank = dim X = rng.randn(rank, dim) cov = np.dot(X.T, X) mnd1 = MND(sigma=cov, mu=mu, seed=seed) num_samples = 5 rd1 = mnd1.random_design_matrix(num_samples) rd1 = function([], rd1)() mnd2 = MND(sigma=cov, mu=mu, seed=seed + 1) rd2 = mnd2.random_design_matrix(num_samples) rd2 = function([], rd2)() assert np.any(rd1 != rd2)
def test_seed_diff(): """Verifies that two MNDs initialized with different seeds produce samples that differ at least somewhat (theoretically the samples could match even under valid behavior but this is extremely unlikely)""" skip_if_no_scipy() rng = np.random.RandomState([1,2,3]) #the number in the argument here is the limit on #seed value, and we subtract 1 so it will be #possible to add 1 to it for the second MND seed = rng.randint(2147462579) -1 dim = 3 mu = rng.randn(dim) rank = dim X = rng.randn(rank,dim) cov = np.dot(X.T,X) mnd1 = MND( sigma = cov, mu = mu, seed = seed) num_samples = 5 rd1 = mnd1.random_design_matrix(num_samples) rd1 = function([],rd1)() mnd2 = MND( sigma = cov, mu = mu, seed = seed + 1) rd2 = mnd2.random_design_matrix(num_samples) rd2 = function([],rd2)() assert np.any(rd1 != rd2)