Mercurial > pylearn
comparison _test_random_transformation.py @ 376:c9a89be5cb0a
Redesigning linear_regression
author | Yoshua Bengio <bengioy@iro.umontreal.ca> |
---|---|
date | Mon, 07 Jul 2008 10:08:35 -0400 |
parents | 18702ceb2096 |
children |
comparison
equal
deleted
inserted
replaced
375:12ce29abf27d | 376:c9a89be5cb0a |
---|---|
1 from random_transformation import row_random_transformation | |
2 | |
3 import unittest | |
4 from theano import compile | |
5 from theano import gradient | |
6 | |
7 from theano.sparse import _is_dense, _is_sparse, _is_dense_result, _is_sparse_result | |
8 from theano.sparse import _mtypes, _mtype_to_str | |
9 from theano.sparse import as_sparse | |
10 | |
11 from theano.tensor import as_tensor | |
12 from theano.scalar import as_scalar | |
13 | |
14 import random | |
15 import numpy.random | |
16 | |
17 class T_RowRandomTransformation(unittest.TestCase): | |
18 def setUp(self): | |
19 random.seed(44) | |
20 numpy.random.seed(44) | |
21 | |
22 def test_basic(self): | |
23 rows = 4 | |
24 cols = 20 | |
25 fakeseed = 0 | |
26 length = 3 | |
27 md = numpy.random.rand(rows, cols) | |
28 for mtype in _mtypes: | |
29 m = as_sparse(mtype(md)) | |
30 o = row_random_transformation(m, length, initial_seed=fakeseed) | |
31 y = compile.eval_outputs([o]) | |
32 expected = "[[ 0.88239119 1.03244463 -1.29297503]\n [ 0.02644961 1.50119695 -0.025081 ]\n [-0.60741013 1.25424625 0.30119422]\n [-1.08659967 -0.35531544 -1.38915467]]" | |
33 self.failUnless(str(y) == expected) | |
34 | |
35 def test_length(self): | |
36 """ Test that if length is increased, we obtain the same results | |
37 (except longer). """ | |
38 | |
39 for i in range(10): | |
40 mtype = random.choice(_mtypes) | |
41 rows = random.randint(1, 20) | |
42 cols = random.randint(1, 20) | |
43 fakeseed = random.randint(0, 100) | |
44 length = random.randint(1, 10) | |
45 extralength = random.randint(1, 10) | |
46 | |
47 m = as_sparse(mtype(numpy.random.rand(rows, cols))) | |
48 o1 = row_random_transformation(m, length, initial_seed=fakeseed) | |
49 o2 = row_random_transformation(m, length + extralength, initial_seed=fakeseed) | |
50 | |
51 y1 = compile.eval_outputs([o1]) | |
52 y2 = compile.eval_outputs([o2]) | |
53 | |
54 self.failUnless((y1 == y2[:,:length]).all()) | |
55 | |
56 def test_permute(self): | |
57 """ Test that if the order of the rows is permuted, we obtain the same results. """ | |
58 for i in range(10): | |
59 mtype = random.choice(_mtypes) | |
60 rows = random.randint(2, 20) | |
61 cols = random.randint(1, 20) | |
62 fakeseed = random.randint(0, 100) | |
63 length = random.randint(1, 10) | |
64 | |
65 permute = numpy.random.permutation(rows) | |
66 | |
67 | |
68 m1 = numpy.random.rand(rows, cols) | |
69 m2 = m1[permute] | |
70 for r in range(rows): | |
71 self.failUnless((m2[r] == m1[permute[r]]).all()) | |
72 s1 = as_sparse(mtype(m1)) | |
73 s2 = as_sparse(mtype(m2)) | |
74 o1 = row_random_transformation(s1, length, initial_seed=fakeseed) | |
75 o2 = row_random_transformation(s2, length, initial_seed=fakeseed) | |
76 y1 = compile.eval_outputs([o1]) | |
77 y2 = compile.eval_outputs([o2]) | |
78 | |
79 self.failUnless(y1.shape == y2.shape) | |
80 for r in range(rows): | |
81 self.failUnless((y2[r] == y1[permute[r]]).all()) | |
82 | |
83 if __name__ == '__main__': | |
84 unittest.main() |