コード例 #1
0
ファイル: test_raw_random.py プロジェクト: gokul-uf/Theano
    def test_uniform_vector(self):
        rng_R = random_state_type()
        low = tensor.vector()
        high = tensor.vector()
        post_r, out = uniform(rng_R, low=low, high=high)
        assert out.ndim == 1
        f = compile.function([rng_R, low, high], [post_r, out], accept_inplace=True)

        def as_floatX(thing):
            return numpy.asarray(thing, dtype=theano.config.floatX)

        low_val = as_floatX([0.1, 0.2, 0.3])
        high_val = as_floatX([1.1, 2.2, 3.3])
        rng = numpy.random.RandomState(utt.fetch_seed())
        numpy_rng = numpy.random.RandomState(utt.fetch_seed())

        # Arguments of size (3,)
        rng0, val0 = f(rng, low_val, high_val)
        numpy_val0 = as_floatX(numpy_rng.uniform(low=low_val, high=high_val))
        assert numpy.all(val0 == numpy_val0)

        # arguments of size (2,)
        rng1, val1 = f(rng0, low_val[:-1], high_val[:-1])
        numpy_val1 = as_floatX(numpy_rng.uniform(low=low_val[:-1], high=high_val[:-1]))
        assert numpy.all(val1 == numpy_val1)

        # Specifying the size explicitly
        g = compile.function([rng_R, low, high], uniform(rng_R, low=low, high=high, size=(3,)), accept_inplace=True)
        rng2, val2 = g(rng1, low_val, high_val)
        numpy_val2 = as_floatX(numpy_rng.uniform(low=low_val, high=high_val, size=(3,)))
        assert numpy.all(val2 == numpy_val2)
        self.assertRaises(ValueError, g, rng2, low_val[:-1], high_val[:-1])
コード例 #2
0
ファイル: test_raw_random.py プロジェクト: gokul-uf/Theano
    def test_random_function_noshape_args(self):
        """Test if random_function helper works with args but without shape"""
        rng_R = random_state_type()

        # No shape, default args -> OK
        post_out, out = uniform(rng_R, size=None, ndim=2)
        f = compile.function(
            [compile.In(rng_R, value=numpy.random.RandomState(utt.fetch_seed()), update=post_out, mutable=True)],
            [out],
            accept_inplace=True,
        )
        o, = f()

        # No shape, args that have to be broadcasted -> OK
        low = tensor.TensorType(dtype="float64", broadcastable=(False, True, True))()
        high = tensor.TensorType(dtype="float64", broadcastable=(True, True, True, False))()
        post_out2, out2 = uniform(rng_R, size=None, ndim=2, low=low, high=high)
        self.assertEqual(out2.ndim, 4)
        self.assertEqual(out2.broadcastable, (True, False, True, False))

        g = compile.function(
            [
                low,
                high,
                compile.In(rng_R, value=numpy.random.RandomState(utt.fetch_seed()), update=post_out2, mutable=True),
            ],
            [out2],
            accept_inplace=True,
        )
        low_v = [[[3]], [[4]], [[-5]]]
        high_v = [[[[5, 8]]]]
        o2, = g(low_v, high_v)
        self.assertEqual(o2.shape, (1, 3, 1, 2))
コード例 #3
0
    def test_uniform_vector(self):
        m = Module()
        m.random = RandomStreams(utt.fetch_seed())
        low = tensor.vector()
        high = tensor.vector()
        out = m.random.uniform(low=low, high=high)
        assert out.ndim == 1
        m.f = Method([low, high], out)
        # Specifying the size explicitly
        m.g = Method([low, high],
                m.random.uniform(low=low, high=high, size=(3,)))
        made = m.make()
        made.random.initialize()

        low_val = numpy.asarray([.1, .2, .3], dtype=config.floatX)
        high_val = numpy.asarray([1.1, 2.2, 3.3], dtype=config.floatX)
        seed_gen = numpy.random.RandomState(utt.fetch_seed())
        numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30)))

        # Arguments of size (3,)
        val0 = made.f(low_val, high_val)
        numpy_val0 = numpy_rng.uniform(low=low_val, high=high_val)
        assert numpy.allclose(val0, numpy_val0)

        # arguments of size (2,)
        val1 = made.f(low_val[:-1], high_val[:-1])
        numpy_val1 = numpy_rng.uniform(low=low_val[:-1], high=high_val[:-1])
        assert numpy.allclose(val1, numpy_val1)

        # Specifying the size explicitly
        numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30)))
        val2 = made.g(low_val, high_val)
        numpy_val2 = numpy_rng.uniform(low=low_val, high=high_val, size=(3,))
        assert numpy.allclose(val2, numpy_val2)
        self.assertRaises(ValueError, made.g, low_val[:-1], high_val[:-1])
コード例 #4
0
    def test_ndim(self):
        """Test that the behaviour of 'ndim' optional parameter"""
        # 'ndim' is an optional integer parameter, specifying the length
        # of the 'shape', passed as a keyword argument.

        # ndim not specified, OK
        m1 = Module()
        m1.random = RandomStreams(utt.fetch_seed())
        m1.fn = Method([], m1.random.uniform((2,2)))
        made1 = m1.make()
        made1.random.initialize()

        # ndim specified, consistent with shape, OK
        m2 = Module()
        m2.random = RandomStreams(utt.fetch_seed())
        m2.fn = Method([], m2.random.uniform((2,2), ndim=2))
        made2 = m2.make()
        made2.random.initialize()

        val1 = made1.fn()
        val2 = made2.fn()
        assert numpy.all(val1 == val2)

        # ndim specified, inconsistent with shape, should raise ValueError
        m3 = Module()
        m3.random = RandomStreams(utt.fetch_seed())
        self.assertRaises(ValueError, m3.random.uniform, (2,2), ndim=1)
コード例 #5
0
    def test_binomial_vector(self):
        random = RandomStreams(utt.fetch_seed())
        n = tensor.lvector()
        prob = tensor.vector()
        out = random.binomial(n=n, p=prob)
        assert out.ndim == 1
        f = function([n, prob], out)

        n_val = [1, 2, 3]
        prob_val = numpy.asarray([.1, .2, .3], dtype=config.floatX)
        seed_gen = numpy.random.RandomState(utt.fetch_seed())
        numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30)))

        # Arguments of size (3,)
        val0 = f(n_val, prob_val)
        numpy_val0 = numpy_rng.binomial(n=n_val, p=prob_val)
        assert numpy.all(val0 == numpy_val0)

        # arguments of size (2,)
        val1 = f(n_val[:-1], prob_val[:-1])
        numpy_val1 = numpy_rng.binomial(n=n_val[:-1], p=prob_val[:-1])
        assert numpy.all(val1 == numpy_val1)

        # Specifying the size explicitly
        g = function([n, prob], random.binomial(n=n, p=prob, size=(3,)))
        val2 = g(n_val, prob_val)
        numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30)))
        numpy_val2 = numpy_rng.binomial(n=n_val, p=prob_val, size=(3,))
        assert numpy.all(val2 == numpy_val2)
        self.assertRaises(ValueError, g, n_val[:-1], prob_val[:-1])
コード例 #6
0
    def test_normal_vector(self):
        m = Module()
        m.random = RandomStreams(utt.fetch_seed())
        avg = tensor.vector()
        std = tensor.vector()
        out = m.random.normal(avg=avg, std=std)
        assert out.ndim == 1
        m.f = Method([avg, std], out)
        # Specifying the size explicitly
        m.g = Method([avg, std],
                m.random.normal(avg=avg, std=std, size=(3,)))
        made = m.make()
        made.random.initialize()

        avg_val = [1, 2, 3]
        std_val = numpy.asarray([.1, .2, .3], dtype=config.floatX)
        seed_gen = numpy.random.RandomState(utt.fetch_seed())
        numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30)))

        # Arguments of size (3,)
        val0 = made.f(avg_val, std_val)
        numpy_val0 = numpy_rng.normal(loc=avg_val, scale=std_val)
        assert numpy.allclose(val0, numpy_val0)

        # arguments of size (2,)
        val1 = made.f(avg_val[:-1], std_val[:-1])
        numpy_val1 = numpy_rng.normal(loc=avg_val[:-1], scale=std_val[:-1])
        assert numpy.allclose(val1, numpy_val1)

        # Specifying the size explicitly
        numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30)))
        val2 = made.g(avg_val, std_val)
        numpy_val2 = numpy_rng.normal(loc=avg_val, scale=std_val, size=(3,))
        assert numpy.allclose(val2, numpy_val2)
        self.assertRaises(ValueError, made.g, avg_val[:-1], std_val[:-1])
コード例 #7
0
ファイル: test_raw_random.py プロジェクト: SamuelZeng/Theano
    def test_binomial(self):
        """Test that raw_random.binomial generates the same results
        as numpy."""
        # Check over two calls to see if the random state is correctly updated.
        rng_R = random_state_type()
        # Use non-default parameters, and larger dimensions because of
        # the integer nature of the result
        post_r, bin = binomial(rng_R, (7, 12), 5, 0.8)

        f = compile.function(
                [compile.In(rng_R,
                    value=numpy.random.RandomState(utt.fetch_seed()),
                    update=post_r, mutable=True)],
                [bin], accept_inplace=True)

        numpy_rng = numpy.random.RandomState(utt.fetch_seed())
        val0 = f()
        val1 = f()
        numpy_val0 = numpy_rng.binomial(5, 0.8, size=(7, 12))
        numpy_val1 = numpy_rng.binomial(5, 0.8, size=(7, 12))
        print val0
        print numpy_val0
        print val1
        print numpy_val1
        self.assertTrue(numpy.all(val0 == numpy_val0))
        self.assertTrue(numpy.all(val1 == numpy_val1))
コード例 #8
0
    def test_random_integers_vector(self):
        random = RandomStreams(utt.fetch_seed())
        low = tensor.lvector()
        high = tensor.lvector()
        out = random.random_integers(low=low, high=high)
        assert out.ndim == 1
        f = function([low, high], out)

        low_val = [100, 200, 300]
        high_val = [110, 220, 330]
        seed_gen = numpy.random.RandomState(utt.fetch_seed())
        numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30)))

        # Arguments of size (3,)
        val0 = f(low_val, high_val)
        numpy_val0 = numpy.asarray([numpy_rng.randint(low=lv, high=hv+1)
            for lv, hv in zip(low_val, high_val)])
        assert numpy.all(val0 == numpy_val0)

        # arguments of size (2,)
        val1 = f(low_val[:-1], high_val[:-1])
        numpy_val1 = numpy.asarray([numpy_rng.randint(low=lv, high=hv+1)
            for lv, hv in zip(low_val[:-1], high_val[:-1])])
        assert numpy.all(val1 == numpy_val1)

        # Specifying the size explicitly
        g = function([low, high], random.random_integers(low=low, high=high, size=(3,)))
        val2 = g(low_val, high_val)
        numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30)))
        numpy_val2 = numpy.asarray([numpy_rng.randint(low=lv, high=hv+1)
            for lv, hv in zip(low_val, high_val)])
        assert numpy.all(val2 == numpy_val2)
        self.assertRaises(ValueError, g, low_val[:-1], high_val[:-1])
コード例 #9
0
ファイル: test_raw_random.py プロジェクト: SamuelZeng/Theano
    def test_permutation(self):
        """Test that raw_random.permutation generates the same
        results as numpy."""
        rng_R = random_state_type()
        post_r, out = permutation(rng_R, size=(9,), n=6)
        print 'OUT NDIM', out.ndim
        f = compile.function(
                [compile.In(rng_R,
                    value=numpy.random.RandomState(utt.fetch_seed()),
                    update=post_r, mutable=True)],
                [out], accept_inplace=True)

        numpy_rng = numpy.random.RandomState(utt.fetch_seed())
        # Check over two calls to see if the random state is correctly updated.
        # numpy_rng.permutation outputs one vector at a time,
        # so we call it iteratively to generate all the samples.
        val0 = f()
        val1 = f()
        numpy_val0 = numpy.asarray([numpy_rng.permutation(6)
                                    for i in range(9)])
        numpy_val1 = numpy.asarray([numpy_rng.permutation(6)
                                    for i in range(9)])
        print val0
        print numpy_val0
        print val1
        print numpy_val1
        self.assertTrue(numpy.all(val0 == numpy_val0))
        self.assertTrue(numpy.all(val1 == numpy_val1))
コード例 #10
0
ファイル: test_raw_random.py プロジェクト: SamuelZeng/Theano
    def test_random_integers(self):
        """Test that raw_random.random_integers generates the same
        results as numpy."""
        # Check over two calls to see if the random state is correctly updated.
        rng_R = random_state_type()
        # Use non-default parameters, and larger dimensions because of
        # the integer nature of the result
        post_r, out = random_integers(rng_R, (11, 8), -3, 16)

        f = compile.function(
                [compile.In(rng_R,
                    value=numpy.random.RandomState(utt.fetch_seed()),
                    update=post_r, mutable=True)],
                [out], accept_inplace=True)

        numpy_rng = numpy.random.RandomState(utt.fetch_seed())
        val0 = f()
        val1 = f()
        numpy_val0 = numpy_rng.random_integers(-3, 16, size=(11, 8))
        numpy_val1 = numpy_rng.random_integers(-3, 16, size=(11, 8))
        print val0
        print numpy_val0
        print val1
        print numpy_val1
        self.assertTrue(numpy.allclose(val0, numpy_val0))
        self.assertTrue(numpy.allclose(val1, numpy_val1))
コード例 #11
0
ファイル: test_raw_random.py プロジェクト: SamuelZeng/Theano
    def test_multinomial(self):
        """Test that raw_random.multinomial generates the same
        results as numpy."""
        # Check over two calls to see if the random state is correctly updated.
        rng_R = random_state_type()
        post_r, out = multinomial(rng_R, (7, 3), 6, [0.2] * 5)

        f = compile.function(
                [compile.In(rng_R,
                    value=numpy.random.RandomState(utt.fetch_seed()),
                    update=post_r, mutable=True)],
                [out], accept_inplace=True)

        numpy_rng = numpy.random.RandomState(utt.fetch_seed())
        val0, = f()
        val1, = f()
        numpy_val0 = numpy_rng.multinomial(6, [0.2] * 5, (7, 3))
        numpy_val1 = numpy_rng.multinomial(6, [0.2] * 5, (7, 3))
        print val0
        print numpy_val0
        print val1
        print numpy_val1
        self.assertTrue(numpy.all(val0 == numpy_val0))
        self.assertTrue(numpy.all(val1 == numpy_val1))

        self.assertTrue(val0.shape == (7, 3, 5))
        self.assertTrue(val1.shape == (7, 3, 5))
コード例 #12
0
ファイル: test_raw_random.py プロジェクト: SamuelZeng/Theano
    def test_binomial_vector(self):
        rng_R = random_state_type()
        n = tensor.lvector()
        prob = tensor.vector()
        post_r, out = binomial(rng_R, n=n, p=prob)
        assert out.ndim == 1
        f = compile.function([rng_R, n, prob], [post_r, out],
                             accept_inplace=True)

        n_val = [1, 2, 3]
        prob_val = numpy.asarray([.1, .2, .3], dtype=config.floatX)
        rng = numpy.random.RandomState(utt.fetch_seed())
        numpy_rng = numpy.random.RandomState(utt.fetch_seed())

        # Arguments of size (3,)
        rng0, val0 = f(rng, n_val, prob_val)
        numpy_val0 = numpy_rng.binomial(n=n_val, p=prob_val)
        assert numpy.all(val0 == numpy_val0)

        # arguments of size (2,)
        rng1, val1 = f(rng0, n_val[:-1], prob_val[:-1])
        numpy_val1 = numpy_rng.binomial(n=n_val[:-1], p=prob_val[:-1])
        assert numpy.all(val1 == numpy_val1)

        # Specifying the size explicitly
        g = compile.function([rng_R, n, prob],
                binomial(rng_R, n=n, p=prob, size=(3,)),
                accept_inplace=True)
        rng2, val2 = g(rng1, n_val, prob_val)
        numpy_val2 = numpy_rng.binomial(n=n_val, p=prob_val, size=(3,))
        assert numpy.all(val2 == numpy_val2)
        self.assertRaises(ValueError, g, rng2, n_val[:-1], prob_val[:-1])
コード例 #13
0
ファイル: test_raw_random.py プロジェクト: SamuelZeng/Theano
    def test_random_integers_vector(self):
        rng_R = random_state_type()
        low = tensor.lvector()
        high = tensor.lvector()
        post_r, out = random_integers(rng_R, low=low, high=high)
        assert out.ndim == 1
        f = compile.function([rng_R, low, high], [post_r, out],
                             accept_inplace=True)

        low_val = [100, 200, 300]
        high_val = [110, 220, 330]
        rng = numpy.random.RandomState(utt.fetch_seed())
        numpy_rng = numpy.random.RandomState(utt.fetch_seed())

        # Arguments of size (3,)
        rng0, val0 = f(rng, low_val, high_val)
        numpy_val0 = numpy.asarray([numpy_rng.random_integers(low=lv, high=hv)
            for lv, hv in zip(low_val, high_val)])
        assert numpy.all(val0 == numpy_val0)

        # arguments of size (2,)
        rng1, val1 = f(rng0, low_val[:-1], high_val[:-1])
        numpy_val1 = numpy.asarray([numpy_rng.random_integers(low=lv, high=hv)
            for lv, hv in zip(low_val[:-1], high_val[:-1])])
        assert numpy.all(val1 == numpy_val1)

        # Specifying the size explicitly
        g = compile.function([rng_R, low, high],
                random_integers(rng_R, low=low, high=high, size=(3,)),
                accept_inplace=True)
        rng2, val2 = g(rng1, low_val, high_val)
        numpy_val2 = numpy.asarray([numpy_rng.random_integers(low=lv, high=hv)
            for lv, hv in zip(low_val, high_val)])
        assert numpy.all(val2 == numpy_val2)
        self.assertRaises(ValueError, g, rng2, low_val[:-1], high_val[:-1])
コード例 #14
0
ファイル: test_raw_random.py プロジェクト: gokul-uf/Theano
    def test_no_inplace(self):
        """Test that when not running inplace, the RandomState is
        not updated"""
        rf = RandomFunction("uniform", tensor.dvector)
        rng_R = random_state_type()

        post_r, out = rf(rng_R, (3,), 0.0, 1.0)
        f = compile.function([rng_R], [post_r, out])
        rng = numpy.random.RandomState(utt.fetch_seed())

        rng0, val0 = f(rng)
        rng_ = numpy.random.RandomState(utt.fetch_seed())
        # rng should still be in a fresh state
        self.assertTrue(rng_R.type.values_eq(rng, rng_))
        # rng0 should be in an updated state
        self.assertFalse(rng_R.type.values_eq(rng, rng0))

        f2 = compile.function([compile.In(rng_R, value=rng, update=post_r, mutable=False)], [post_r, out])
        rng2, val2 = f2()
        # rng should be in a fresh state
        self.assertTrue(rng_R.type.values_eq(rng, rng_))
        # rng2 should be in an updated state
        self.assertFalse(rng_R.type.values_eq(rng, rng2))
        # The updated state should be the same for both functions
        self.assertTrue(rng_R.type.values_eq(rng2, rng0))

        rng3, val3 = f2()
        # rng2 should not have changed
        self.assertTrue(rng_R.type.values_eq(rng2, rng0))
        # rng3 should be an updated again version of rng2
        self.assertFalse(rng_R.type.values_eq(rng3, rng2))
        self.assertFalse(rng_R.type.values_eq(rng3, rng))
コード例 #15
0
ファイル: test_slinalg.py プロジェクト: gyenney/Tools
 def test_infer_shape(self):
     if not imported_scipy:
         raise SkipTest("Scipy needed for the Cholesky op.")
     rng = numpy.random.RandomState(utt.fetch_seed())
     A = theano.tensor.matrix()
     b = theano.tensor.matrix()
     self._compile_and_check([A, b],  # theano.function inputs
                             [self.op(A, b)],  # theano.function outputs
                             # A must be square
                             [numpy.asarray(rng.rand(5, 5),
                                            dtype=config.floatX),
                              numpy.asarray(rng.rand(5, 1),
                                            dtype=config.floatX)],
                             self.op_class,
                             warn=False)
     rng = numpy.random.RandomState(utt.fetch_seed())
     A = theano.tensor.matrix()
     b = theano.tensor.vector()
     self._compile_and_check([A, b],  # theano.function inputs
                             [self.op(A, b)],  # theano.function outputs
                             # A must be square
                             [numpy.asarray(rng.rand(5, 5),
                                            dtype=config.floatX),
                              numpy.asarray(rng.rand(5),
                                            dtype=config.floatX)],
                             self.op_class,
                             warn=False)
コード例 #16
0
    def test_multinomial_vector(self):
        random = RandomStreams(utt.fetch_seed())
        n = tensor.lvector()
        pvals = tensor.matrix()
        out = random.multinomial(n=n, pvals=pvals)
        assert out.ndim == 2
        f = function([n, pvals], out)

        n_val = [1, 2, 3]
        pvals_val = [[.1, .9], [.2, .8], [.3, .7]]
        pvals_val = numpy.asarray(pvals_val, dtype=config.floatX)
        seed_gen = numpy.random.RandomState(utt.fetch_seed())
        numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30)))

        # Arguments of size (3,)
        val0 = f(n_val, pvals_val)
        numpy_val0 = numpy.asarray([numpy_rng.multinomial(n=nv, pvals=pv)
            for nv, pv in zip(n_val, pvals_val)])
        assert numpy.all(val0 == numpy_val0)

        # arguments of size (2,)
        val1 = f(n_val[:-1], pvals_val[:-1])
        numpy_val1 = numpy.asarray([numpy_rng.multinomial(n=nv, pvals=pv)
            for nv, pv in zip(n_val[:-1], pvals_val[:-1])])
        assert numpy.all(val1 == numpy_val1)

        # Specifying the size explicitly
        g = function([n, pvals], random.multinomial(n=n, pvals=pvals, size=(3,)))
        val2 = g(n_val, pvals_val)
        numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30)))
        numpy_val2 = numpy.asarray([numpy_rng.multinomial(n=nv, pvals=pv)
            for nv, pv in zip(n_val, pvals_val)])
        assert numpy.all(val2 == numpy_val2)
        self.assertRaises(ValueError, g, n_val[:-1], pvals_val[:-1])
コード例 #17
0
ファイル: test_raw_random.py プロジェクト: 317070/Theano
    def test_choice(self):
        """Test that raw_random.choice generates the same
        results as numpy."""
        # numpy.random.choice is only available for numpy versions >= 1.7
        major, minor, _ = numpy.version.short_version.split('.')
        if (int(major), int(minor)) < (1, 7):
            raise utt.SkipTest('choice requires at NumPy version >= 1.7 '
                               '(%s)' % numpy.__version__)
        
        # Check over two calls to see if the random state is correctly updated.
        rng_R = random_state_type()
        # Use non-default parameters, and larger dimensions because of
        # the integer nature of the result
        post_r, out = choice(rng_R, (11, 8), 10, 1, 0)

        f = compile.function(
                [compile.In(rng_R,
                    value=numpy.random.RandomState(utt.fetch_seed()),
                    update=post_r, mutable=True)],
                [out], accept_inplace=True)

        numpy_rng = numpy.random.RandomState(utt.fetch_seed())
        val0 = f()
        val1 = f()
        numpy_val0 = numpy_rng.choice(10, (11, 8), True, None)
        numpy_val1 = numpy_rng.choice(10, (11, 8), True, None)
        print val0
        print numpy_val0
        print val1
        print numpy_val1
        self.assertTrue(numpy.allclose(val0, numpy_val0))
        self.assertTrue(numpy.allclose(val1, numpy_val1))
コード例 #18
0
    def test_normal_vector(self):
        random = RandomStreams(utt.fetch_seed())
        avg = tensor.dvector()
        std = tensor.dvector()
        out = random.normal(avg=avg, std=std)
        assert out.ndim == 1
        f = function([avg, std], out)

        avg_val = [1, 2, 3]
        std_val = [.1, .2, .3]
        seed_gen = numpy.random.RandomState(utt.fetch_seed())
        numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30)))

        # Arguments of size (3,)
        val0 = f(avg_val, std_val)
        numpy_val0 = numpy_rng.normal(loc=avg_val, scale=std_val)
        assert numpy.allclose(val0, numpy_val0)

        # arguments of size (2,)
        val1 = f(avg_val[:-1], std_val[:-1])
        numpy_val1 = numpy_rng.normal(loc=avg_val[:-1], scale=std_val[:-1])
        assert numpy.allclose(val1, numpy_val1)

        # Specifying the size explicitly
        g = function([avg, std], random.normal(avg=avg, std=std, size=(3,)))
        val2 = g(avg_val, std_val)
        numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30)))
        numpy_val2 = numpy_rng.normal(loc=avg_val, scale=std_val, size=(3,))
        assert numpy.allclose(val2, numpy_val2)
        self.assertRaises(ValueError, g, avg_val[:-1], std_val[:-1])
コード例 #19
0
ファイル: test_scan.py プロジェクト: 317070/Theano
    def test_gpu4_gibbs_chain(self):
        rng = numpy.random.RandomState(utt.fetch_seed())
        v_vsample = numpy.array(rng.binomial(1, .5, size=(3, 20),),
                                dtype='float32')
        vsample = theano.shared(v_vsample)
        trng = theano.sandbox.rng_mrg.MRG_RandomStreams(
                                utt.fetch_seed())

        def f(vsample_tm1):
            return trng.binomial(vsample_tm1.shape, n=1, p=0.3,
                                 dtype='float32') * vsample_tm1

        theano_vsamples, updates = theano.scan(f,
                                               [],
                                               vsample,
                                               [],
                                               n_steps=10,
                                               truncate_gradient=-1,
                                               go_backwards=False,
                                               mode=mode_with_gpu)
        my_f = theano.function([],
                               theano_vsamples[-1],
                               updates=updates,
                               allow_input_downcast=True,
                               mode=mode_with_gpu)

        # I leave this to tested by debugmode, this test was anyway
        # more of does the graph compile kind of test
        t_result = my_f()
コード例 #20
0
    def test_uniform_vector(self):
        random = RandomStreams(utt.fetch_seed())
        low = tensor.dvector()
        high = tensor.dvector()
        out = random.uniform(low=low, high=high)
        assert out.ndim == 1
        f = function([low, high], out)

        low_val = [.1, .2, .3]
        high_val = [1.1, 2.2, 3.3]
        seed_gen = numpy.random.RandomState(utt.fetch_seed())
        numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30)))

        # Arguments of size (3,)
        val0 = f(low_val, high_val)
        numpy_val0 = numpy_rng.uniform(low=low_val, high=high_val)
        print('THEANO', val0)
        print('NUMPY', numpy_val0)
        assert numpy.all(val0 == numpy_val0)

        # arguments of size (2,)
        val1 = f(low_val[:-1], high_val[:-1])
        numpy_val1 = numpy_rng.uniform(low=low_val[:-1], high=high_val[:-1])
        print('THEANO', val1)
        print('NUMPY', numpy_val1)
        assert numpy.all(val1 == numpy_val1)

        # Specifying the size explicitly
        g = function([low, high], random.uniform(low=low, high=high, size=(3,)))
        val2 = g(low_val, high_val)
        numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30)))
        numpy_val2 = numpy_rng.uniform(low=low_val, high=high_val, size=(3,))
        assert numpy.all(val2 == numpy_val2)
        self.assertRaises(ValueError, g, low_val[:-1], high_val[:-1])
コード例 #21
0
ファイル: test_raw_random.py プロジェクト: gokul-uf/Theano
    def test_multinomial_vector(self):
        rng_R = random_state_type()
        n = tensor.lvector()
        pvals = tensor.matrix()
        post_r, out = multinomial(rng_R, n=n, pvals=pvals)
        assert out.ndim == 2
        f = compile.function([rng_R, n, pvals], [post_r, out], accept_inplace=True)

        n_val = [1, 2, 3]
        pvals_val = [[0.1, 0.9], [0.2, 0.8], [0.3, 0.7]]
        pvals_val = numpy.asarray(pvals_val, dtype=config.floatX)
        rng = numpy.random.RandomState(utt.fetch_seed())
        numpy_rng = numpy.random.RandomState(utt.fetch_seed())

        # Arguments of size (3,)
        rng0, val0 = f(rng, n_val, pvals_val)
        numpy_val0 = numpy.asarray([numpy_rng.multinomial(n=nv, pvals=pv) for nv, pv in zip(n_val, pvals_val)])
        assert numpy.all(val0 == numpy_val0)

        # arguments of size (2,)
        rng1, val1 = f(rng0, n_val[:-1], pvals_val[:-1])
        numpy_val1 = numpy.asarray(
            [numpy_rng.multinomial(n=nv, pvals=pv) for nv, pv in zip(n_val[:-1], pvals_val[:-1])]
        )
        assert numpy.all(val1 == numpy_val1)

        # Specifying the size explicitly
        g = compile.function([rng_R, n, pvals], multinomial(rng_R, n=n, pvals=pvals, size=(3,)), accept_inplace=True)
        rng2, val2 = g(rng1, n_val, pvals_val)
        numpy_val2 = numpy.asarray([numpy_rng.multinomial(n=nv, pvals=pv) for nv, pv in zip(n_val, pvals_val)])
        assert numpy.all(val2 == numpy_val2)
        self.assertRaises(ValueError, g, rng2, n_val[:-1], pvals_val[:-1])
コード例 #22
0
    def test_binomial_vector(self):
        m = Module()
        m.random = RandomStreams(utt.fetch_seed())
        n = tensor.lvector()
        prob = tensor.vector()
        out = m.random.binomial(n=n, p=prob)
        assert out.ndim == 1
        m.f = Method([n, prob], out)
        # Specifying the size explicitly
        m.g = Method([n, prob],
                m.random.binomial(n=n, p=prob, size=(3,)))
        made = m.make()
        made.random.initialize()

        n_val = [1, 2, 3]
        prob_val = numpy.asarray([.1, .2, .3], dtype=config.floatX)
        seed_gen = numpy.random.RandomState(utt.fetch_seed())
        numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30)))

        # Arguments of size (3,)
        val0 = made.f(n_val, prob_val)
        numpy_val0 = numpy_rng.binomial(n=n_val, p=prob_val)
        assert numpy.all(val0 == numpy_val0)

        # arguments of size (2,)
        val1 = made.f(n_val[:-1], prob_val[:-1])
        numpy_val1 = numpy_rng.binomial(n=n_val[:-1], p=prob_val[:-1])
        assert numpy.all(val1 == numpy_val1)

        # Specifying the size explicitly
        numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30)))
        val2 = made.g(n_val, prob_val)
        numpy_val2 = numpy_rng.binomial(n=n_val, p=prob_val, size=(3,))
        assert numpy.all(val2 == numpy_val2)
        self.assertRaises(ValueError, made.g, n_val[:-1], prob_val[:-1])
コード例 #23
0
ファイル: test_raw_random.py プロジェクト: gokul-uf/Theano
    def test_normal_vector(self):
        rng_R = random_state_type()
        avg = tensor.vector()
        std = tensor.vector()
        post_r, out = normal(rng_R, avg=avg, std=std)
        assert out.ndim == 1
        f = compile.function([rng_R, avg, std], [post_r, out], accept_inplace=True)

        def as_floatX(thing):
            return numpy.asarray(thing, dtype=theano.config.floatX)

        avg_val = [1, 2, 3]
        std_val = as_floatX([0.1, 0.2, 0.3])
        rng = numpy.random.RandomState(utt.fetch_seed())
        numpy_rng = numpy.random.RandomState(utt.fetch_seed())

        # Arguments of size (3,)
        rng0, val0 = f(rng, avg_val, std_val)
        numpy_val0 = as_floatX(numpy_rng.normal(loc=as_floatX(avg_val), scale=as_floatX(std_val)))
        assert numpy.all(val0 == numpy_val0)

        # arguments of size (2,)
        rng1, val1 = f(rng0, avg_val[:-1], std_val[:-1])
        numpy_val1 = numpy.asarray(numpy_rng.normal(loc=avg_val[:-1], scale=std_val[:-1]), dtype=theano.config.floatX)
        assert numpy.all(val1 == numpy_val1)

        # Specifying the size explicitly
        g = compile.function([rng_R, avg, std], normal(rng_R, avg=avg, std=std, size=(3,)), accept_inplace=True)
        rng2, val2 = g(rng1, avg_val, std_val)
        numpy_val2 = numpy.asarray(numpy_rng.normal(loc=avg_val, scale=std_val, size=(3,)), dtype=theano.config.floatX)
        assert numpy.all(val2 == numpy_val2)
        self.assertRaises(ValueError, g, rng2, avg_val[:-1], std_val[:-1])
コード例 #24
0
    def test_broadcast_arguments(self):
        m = Module()
        m.random = RandomStreams(utt.fetch_seed())
        low = tensor.vector()
        high = tensor.col()
        out = m.random.uniform(low=low, high=high)
        assert out.ndim == 2
        m.f = Method([low, high], out)
        made = m.make()
        made.random.initialize()

        rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30)
        numpy_rng = numpy.random.RandomState(int(rng_seed))
        low_vals = [
                numpy.asarray([-5, .5, 0, 1], dtype=config.floatX),
                numpy.asarray([.9], dtype=config.floatX),
                numpy.asarray([-5, .5, 0, 1], dtype=config.floatX) ]
        high_vals = [
                numpy.asarray([[1.]], dtype=config.floatX),
                numpy.asarray([[1.], [1.1], [1.5]], dtype=config.floatX),
                numpy.asarray([[1.], [1.1], [1.5]], dtype=config.floatX) ]

        val0 = made.f(low_vals[0], high_vals[0])
        val1 = made.f(low_vals[1], high_vals[1])
        val2 = made.f(low_vals[2], high_vals[2])

        numpy_val0 = numpy_rng.uniform(low=low_vals[0], high=high_vals[0])
        numpy_val1 = numpy_rng.uniform(low=low_vals[1], high=high_vals[1])
        numpy_val2 = numpy_rng.uniform(low=low_vals[2], high=high_vals[2])

        assert numpy.allclose(val0, numpy_val0)
        assert numpy.allclose(val1, numpy_val1)
        assert numpy.allclose(val2, numpy_val2)
コード例 #25
0
    def test_permutation(self):
        # Test that raw_random.permutation generates the same results as numpy.
        rng_R = random_state_type()
        post_r, out = permutation(rng_R, size=(9,), n=6)
        f = compile.function(
                [compile.In(rng_R,
                    value=np.random.RandomState(utt.fetch_seed()),
                    update=post_r, mutable=True)],
                [out], accept_inplace=True)

        numpy_rng = np.random.RandomState(utt.fetch_seed())
        # Check over two calls to see if the random state is correctly updated.
        # numpy_rng.permutation outputs one vector at a time,
        # so we call it iteratively to generate all the samples.
        val0 = f()
        val1 = f()
        numpy_val0 = np.asarray([numpy_rng.permutation(6)
                                    for i in range(9)])
        numpy_val1 = np.asarray([numpy_rng.permutation(6)
                                    for i in range(9)])
        self.assertTrue(np.all(val0 == numpy_val0))
        self.assertTrue(np.all(val1 == numpy_val1))

        # Test that we can generate a list: have size=None or ().
        for ndim in [1, None]:
            post_r, out = permutation(rng_R, n=10, size=None, ndim=ndim)
            inp = compile.In(rng_R,
                             value=np.random.RandomState(utt.fetch_seed()),
                             update=post_r, mutable=True)
            f = theano.function([inp], out)
            o = f()
            assert o.shape == (10,)
            assert (np.sort(o) == np.arange(10)).all()
        # Wrong number of dimensions asked
        self.assertRaises(TypeError, permutation, rng_R, size=None, ndim=2)
コード例 #26
0
ファイル: test_nlinalg.py プロジェクト: EugenePY/Theano
def test_inverse_grad():
    rng = np.random.RandomState(utt.fetch_seed())
    r = rng.randn(4, 4)
    tensor.verify_grad(matrix_inverse, [r], rng=np.random)

    rng = np.random.RandomState(utt.fetch_seed())

    r = rng.randn(4, 4)
    tensor.verify_grad(matrix_inverse, [r], rng=np.random)
コード例 #27
0
ファイル: test_raw_random.py プロジェクト: SamuelZeng/Theano
    def test_permutation_helper(self):
        """Test that raw_random.permutation_helper generates the same
        results as numpy,
        and that the 'ndim_added' keyword behaves correctly."""
        # permutation_helper needs "ndim_added=1", because its output
        # is one dimension more than its "shape" argument (and there's
        # no way to determine that automatically).
        # Check the working case, over two calls to see if the random
        # state is correctly updated.
        rf = RandomFunction(permutation_helper, tensor.imatrix, 8,
                            ndim_added=1)
        rng_R = random_state_type()
        post_r, out = rf(rng_R, (7,), 8)

        f = compile.function(
                [compile.In(rng_R,
                    value=numpy.random.RandomState(utt.fetch_seed()),
                    update=post_r, mutable=True)],
                [out], accept_inplace=True)

        numpy_rng = numpy.random.RandomState(utt.fetch_seed())
        val0 = f()
        val1 = f()
        # numpy_rng.permutation outputs one vector at a time,
        # so we call it iteratively to generate all the samples.
        numpy_val0 = numpy.asarray([numpy_rng.permutation(8)
                                    for i in range(7)])
        numpy_val1 = numpy.asarray([numpy_rng.permutation(8)
                                    for i in range(7)])
        print val0
        print numpy_val0
        print val1
        print numpy_val1
        self.assertTrue(numpy.all(val0 == numpy_val0))
        self.assertTrue(numpy.all(val1 == numpy_val1))

        # This call lacks "ndim_added=1", so ndim_added defaults to 0.
        # A ValueError should be raised.
        rf0 = RandomFunction(permutation_helper, tensor.imatrix, 8)
        post_r0, out0 = rf0(rng_R, (7,), 8)
        f0 = compile.function(
                [compile.In(rng_R,
                    value=numpy.random.RandomState(utt.fetch_seed()),
                    update=post_r0, mutable=True)],
                [out0], accept_inplace=True)
        self.assertRaises(ValueError, f0)

        # Here, ndim_added is 2 instead of 1. A ValueError should be raised.
        rf2 = RandomFunction(permutation_helper, tensor.imatrix, 8,
                             ndim_added=2)
        post_r2, out2 = rf2(rng_R, (7,), 8)
        f2 = compile.function(
                [compile.In(rng_R,
                    value=numpy.random.RandomState(utt.fetch_seed()),
                    update=post_r2, mutable=True)],
                [out2], accept_inplace=True)
        self.assertRaises(ValueError, f2)
コード例 #28
0
    def test_shuffle_row_elements(self):
        """Test that RandomStreams.shuffle_row_elements generates the right results"""
        # Check over two calls to see if the random state is correctly updated.

        # On matrices, for each row, the elements of that row should be shuffled.
        # Note that this differs from numpy.random.shuffle, where all the elements
        # of the matrix are shuffled.
        random = RandomStreams(utt.fetch_seed())
        m_input = tensor.dmatrix()
        f = function([m_input], random.shuffle_row_elements(m_input), updates=random.updates())

        # Generate the elements to be shuffled
        val_rng = numpy.random.RandomState(utt.fetch_seed()+42)
        in_mval = val_rng.uniform(-2, 2, size=(20, 5))
        fn_mval0 = f(in_mval)
        fn_mval1 = f(in_mval)
        print(in_mval[0])
        print(fn_mval0[0])
        print(fn_mval1[0])
        assert not numpy.all(in_mval == fn_mval0)
        assert not numpy.all(in_mval == fn_mval1)
        assert not numpy.all(fn_mval0 == fn_mval1)

        rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30)
        rng = numpy.random.RandomState(int(rng_seed))
        numpy_mval0 = in_mval.copy()
        numpy_mval1 = in_mval.copy()
        for row in numpy_mval0:
            rng.shuffle(row)
        for row in numpy_mval1:
            rng.shuffle(row)

        assert numpy.all(numpy_mval0 == fn_mval0)
        assert numpy.all(numpy_mval1 == fn_mval1)

        # On vectors, the behaviour is the same as numpy.random.shuffle,
        # except that it does not work in place, but returns a shuffled vector.
        random1 = RandomStreams(utt.fetch_seed())
        v_input = tensor.dvector()
        f1 = function([v_input], random1.shuffle_row_elements(v_input))

        in_vval = val_rng.uniform(-3, 3, size=(12,))
        fn_vval = f1(in_vval)
        numpy_vval = in_vval.copy()
        vrng = numpy.random.RandomState(int(rng_seed))
        vrng.shuffle(numpy_vval)
        print(in_vval)
        print(fn_vval)
        print(numpy_vval)
        assert numpy.all(numpy_vval == fn_vval)

        # Trying to shuffle a vector with function that should shuffle
        # matrices, or vice versa, raises a TypeError
        self.assertRaises(TypeError, f1, in_mval)
        self.assertRaises(TypeError, f, in_vval)
コード例 #29
0
    def test_vector_arguments(self):
        m = Module()
        m.random = RandomStreams(utt.fetch_seed())
        low = tensor.vector()
        out = m.random.uniform(low=low, high=1)
        assert out.ndim == 1
        m.f = Method([low], out)

        high = tensor.vector()
        outb = m.random.uniform(low=low, high=high)
        assert outb.ndim == 1
        m.fb = Method([low, high], outb)

        size = tensor.lvector()
        outc = m.random.uniform(low=low, high=high, size=size, ndim=1)
        m.fc = Method([low, high, size], outc)

        made = m.make()
        made.random.initialize()

        seed_gen = numpy.random.RandomState(utt.fetch_seed())
        numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30)))
        low_val0 = numpy.asarray([-5, .5, 0, 1], dtype=config.floatX)
        low_val1 = numpy.asarray([.9], dtype=config.floatX)
        val0 = made.f(low_val0)
        val1 = made.f(low_val1)
        numpy_val0 = numpy_rng.uniform(low=low_val0, high=1)
        numpy_val1 = numpy_rng.uniform(low=low_val1, high=1)
        assert numpy.allclose(val0, numpy_val0)
        assert numpy.allclose(val1, numpy_val1)

        numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30)))
        val0b = made.fb([-4., -2], [-1, 0])
        val1b = made.fb([-4.], [-1])
        numpy_val0b = numpy_rng.uniform(low=[-4., -2], high=[-1, 0])
        numpy_val1b = numpy_rng.uniform(low=[-4.], high=[-1])
        assert numpy.allclose(val0b, numpy_val0b)
        assert numpy.allclose(val1b, numpy_val1b)
        self.assertRaises(ValueError, made.fb, [-4., -2], [-1, 0, 1])
        #TODO: do we want that?
        #self.assertRaises(ValueError, made.fb, [-4., -2], [-1])

        numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30)))
        val0c = made.fc([-4., -2], [-1, 0], [2])
        val1c = made.fc([-4.], [-1], [1])
        numpy_val0c = numpy_rng.uniform(low=[-4., -2], high=[-1, 0])
        numpy_val1c = numpy_rng.uniform(low=[-4.], high=[-1])
        assert numpy.allclose(val0c, numpy_val0c)
        assert numpy.allclose(val1c, numpy_val1c)
        self.assertRaises(ValueError, made.fc, [-4., -2], [-1, 0], [1])
        self.assertRaises(ValueError, made.fc, [-4., -2], [-1, 0], [1,2])
        self.assertRaises(ValueError, made.fc, [-4., -2], [-1, 0], [2,1])
        self.assertRaises(ValueError, made.fc, [-4., -2], [-1], [1])
コード例 #30
0
ファイル: test_raw_random.py プロジェクト: SamuelZeng/Theano
    def test_vector_arguments(self):
        rng_R = random_state_type()
        low = tensor.vector()
        post_r, out = uniform(rng_R, low=low, high=1)
        assert out.ndim == 1
        f = compile.function([rng_R, low], [post_r, out], accept_inplace=True)

        def as_floatX(thing):
            return numpy.asarray(thing, dtype=theano.config.floatX)

        rng_state0 = numpy.random.RandomState(utt.fetch_seed())
        numpy_rng = numpy.random.RandomState(utt.fetch_seed())
        post0, val0 = f(rng_state0, [-5, .5, 0, 1])
        post1, val1 = f(post0, as_floatX([.9]))
        numpy_val0 = as_floatX(numpy_rng.uniform(low=[-5, .5, 0, 1], high=1))
        numpy_val1 = as_floatX(numpy_rng.uniform(low=as_floatX([.9]), high=1))

        assert numpy.all(val0 == numpy_val0)
        assert numpy.all(val1 == numpy_val1)

        high = tensor.vector()
        post_rb, outb = uniform(rng_R, low=low, high=high)
        assert outb.ndim == 1
        fb = compile.function([rng_R, low, high], [post_rb, outb],
                              accept_inplace=True)

        post0b, val0b = fb(post1, [-4., -2], [-1, 0])
        post1b, val1b = fb(post0b, [-4.], [-1])
        numpy_val0b = as_floatX(numpy_rng.uniform(low=[-4., -2], high=[-1, 0]))
        numpy_val1b = as_floatX(numpy_rng.uniform(low=[-4.], high=[-1]))
        assert numpy.all(val0b == numpy_val0b)
        assert numpy.all(val1b == numpy_val1b)
        self.assertRaises(ValueError, fb, post1b, [-4., -2], [-1, 0, 1])
        #TODO: do we want that?
        #self.assertRaises(ValueError, fb, post1b, [-4., -2], [-1])

        size = tensor.lvector()
        post_rc, outc = uniform(rng_R, low=low, high=high, size=size, ndim=1)
        fc = compile.function([rng_R, low, high, size], [post_rc, outc],
                              accept_inplace=True)
        post0c, val0c = fc(post1b, [-4., -2], [-1, 0], [2])
        post1c, val1c = fc(post0c, [-4.], [-1], [1])
        numpy_val0c = as_floatX(numpy_rng.uniform(low=[-4., -2], high=[-1, 0]))
        numpy_val1c = as_floatX(numpy_rng.uniform(low=[-4.], high=[-1]))
        assert numpy.all(val0c == numpy_val0c)
        assert numpy.all(val1c == numpy_val1c)
        self.assertRaises(ValueError, fc, post1c, [-4., -2], [-1, 0], [1])
        self.assertRaises(ValueError, fc, post1c, [-4., -2], [-1, 0], [1, 2])
        self.assertRaises(ValueError, fc, post1c, [-4., -2], [-1, 0], [2, 1])
        self.assertRaises(ValueError, fc, post1c, [-4., -2], [-1], [1])
コード例 #31
0
 def setUp(self):
     self.rng = numpy.random.RandomState(seed=utt.fetch_seed())
コード例 #32
0
def test_extract_diag_grad():
    rng = numpy.random.RandomState(utt.fetch_seed())
    x = rng.rand(5, 4)
    tensor.verify_grad(extract_diag, [x], rng=rng)
コード例 #33
0
ファイル: test_mlp.py プロジェクト: YanzhaoWu/Theano-1
    # print "CPU time: %.3f, GPU time: %.3f, speed up %f" % (
    #        (time_cpu, time_gpu, time_cpu/time_gpu))
    # print "Estimated time for one pass through MNIST with CPU: %f" % (
    #        (time_cpu * (60000.0 / (n_train*bsize))))
    # print "Estimated time for one pass through MNIST with GPU: %f" % (
    #        (time_gpu * (60000.0 / (n_train*bsize))))


# Default parameters for all subsequent tests
gpu_only = False
cpu_only = False
ignore_error = False
verbose = 0
version = -1
seed = utt.fetch_seed()


def test_lenet_28():  # MNIST
    cmp_run_conv_nnet2_classif(seed, 28, 5, 60, n_train=10,
                               ignore_error=ignore_error, gpu_only=gpu_only,
                               cpu_only=cpu_only, verbose=verbose, version=version)


def test_lenet_32():  # CIFAR10 / Shapeset
    cmp_run_conv_nnet2_classif(seed, 32, 5, 60, n_train=8,
                               ignore_error=ignore_error, gpu_only=gpu_only,
                               verbose=verbose, version=version)


def test_lenet_32_long():  # CIFAR10 / Shapeset
コード例 #34
0
ファイル: test_nlinalg.py プロジェクト: vikkamath/Theano-1
def test_det_grad():
    rng = numpy.random.RandomState(utt.fetch_seed())

    r = rng.randn(5, 5).astype(config.floatX)
    tensor.verify_grad(det, [r], rng=numpy.random)
コード例 #35
0
                         enable_cuda=False)
    theano.sandbox.gpuarray.init_dev('cuda')

if not theano.sandbox.gpuarray.pygpu_activated:
    raise SkipTest("pygpu disabled")

from ..type import (GpuArrayType, gpuarray_shared_constructor)
from ..basic_ops import (host_from_gpu, gpu_from_host, gpu_alloc, GpuAlloc,
                         gpu_from_cuda, cuda_from_gpu, HostFromGpu,
                         GpuContiguous, GpuFromHost, GpuReshape, gpu_join,
                         GpuJoin, GpuSplit, GpuEye, gpu_contiguous)
from ..subtensor import GpuSubtensor

from theano.tests import unittest_tools as utt
utt.seed_rng()
rng = numpy.random.RandomState(seed=utt.fetch_seed())

from pygpu import gpuarray

if theano.config.mode == 'FAST_COMPILE':
    mode_with_gpu = theano.compile.mode.get_mode('FAST_RUN').including(
        'gpuarray').excluding('gpu')
    mode_without_gpu = theano.compile.mode.get_mode('FAST_RUN').excluding(
        'gpuarray')
else:
    mode_with_gpu = theano.compile.mode.get_default_mode().including(
        'gpuarray').excluding('gpu')
    mode_without_gpu = theano.compile.mode.get_default_mode().excluding(
        'gpuarray')

コード例 #36
0
        def test_specify_shape_inplace(self):
            # test that specify_shape don't break inserting inplace op

            dtype = self.dtype
            if dtype is None:
                dtype = theano.config.floatX

            rng = np.random.RandomState(utt.fetch_seed())
            a = np.asarray(rng.uniform(1, 2, [40, 40]), dtype=dtype)
            a = self.cast_value(a)
            a_shared = self.shared_constructor(a)
            b = np.asarray(rng.uniform(1, 2, [40, 40]), dtype=dtype)
            b = self.cast_value(b)
            b_shared = self.shared_constructor(b)
            s = np.zeros((40, 40), dtype=dtype)
            s = self.cast_value(s)
            s_shared = self.shared_constructor(s)
            f = theano.function([],
                                updates=[
                                    (s_shared,
                                     theano.dot(a_shared, b_shared) + s_shared)
                                ])
            topo = f.maker.fgraph.toposort()
            f()
            # [Gemm{inplace}(<TensorType(float64, matrix)>, 0.01, <TensorType(float64, matrix)>, <TensorType(float64, matrix)>, 2e-06)]
            if theano.config.mode != 'FAST_COMPILE':
                assert sum([
                    node.op.__class__.__name__
                    in ["Gemm", "GpuGemm", "StructuredDot"] for node in topo
                ]) == 1
                assert all(node.op == tensor.blas.gemm_inplace for node in topo
                           if isinstance(node.op, tensor.blas.Gemm))
                assert all(node.op.inplace for node in topo
                           if node.op.__class__.__name__ == "GpuGemm")
            # Their is no inplace gemm for sparse
            # assert all(node.op.inplace for node in topo if node.op.__class__.__name__ == "StructuredDot")
            s_shared_specify = tensor.specify_shape(
                s_shared,
                s_shared.get_value(borrow=True).shape)

            # now test with the specify shape op in the output
            f = theano.function([],
                                s_shared.shape,
                                updates=[(s_shared,
                                          theano.dot(a_shared, b_shared) +
                                          s_shared_specify)])
            topo = f.maker.fgraph.toposort()
            shp = f()
            assert np.all(shp == (40, 40))
            if theano.config.mode != 'FAST_COMPILE':
                assert sum([
                    node.op.__class__.__name__
                    in ["Gemm", "GpuGemm", "StructuredDot"] for node in topo
                ]) == 1
                assert all(node.op == tensor.blas.gemm_inplace for node in topo
                           if isinstance(node.op, tensor.blas.Gemm))
                assert all(node.op.inplace for node in topo
                           if node.op.__class__.__name__ == "GpuGemm")
            # now test with the specify shape op in the inputs and outputs
            a_shared = tensor.specify_shape(
                a_shared,
                a_shared.get_value(borrow=True).shape)
            b_shared = tensor.specify_shape(
                b_shared,
                b_shared.get_value(borrow=True).shape)

            f = theano.function([],
                                s_shared.shape,
                                updates=[(s_shared,
                                          theano.dot(a_shared, b_shared) +
                                          s_shared_specify)])
            topo = f.maker.fgraph.toposort()
            shp = f()
            assert np.all(shp == (40, 40))
            if theano.config.mode != 'FAST_COMPILE':
                assert sum([
                    node.op.__class__.__name__
                    in ["Gemm", "GpuGemm", "StructuredDot"] for node in topo
                ]) == 1
                assert all(node.op == tensor.blas.gemm_inplace for node in topo
                           if isinstance(node.op, tensor.blas.Gemm))
                assert all(node.op.inplace for node in topo
                           if node.op.__class__.__name__ == "GpuGemm")
コード例 #37
0
ファイル: test_raw_random.py プロジェクト: yubow/Theano
    def test_permutation_helper(self):
        """Test that raw_random.permutation_helper generates the same
        results as numpy,
        and that the 'ndim_added' keyword behaves correctly."""
        # permutation_helper needs "ndim_added=1", because its output
        # is one dimension more than its "shape" argument (and there's
        # no way to determine that automatically).
        # Check the working case, over two calls to see if the random
        # state is correctly updated.
        rf = RandomFunction(permutation_helper,
                            tensor.imatrix,
                            8,
                            ndim_added=1)
        rng_R = random_state_type()
        post_r, out = rf(rng_R, (7, ), 8)

        f = compile.function([
            compile.In(rng_R,
                       value=numpy.random.RandomState(utt.fetch_seed()),
                       update=post_r,
                       mutable=True)
        ], [out],
                             accept_inplace=True)

        numpy_rng = numpy.random.RandomState(utt.fetch_seed())
        val0 = f()
        val1 = f()
        # numpy_rng.permutation outputs one vector at a time,
        # so we call it iteratively to generate all the samples.
        numpy_val0 = numpy.asarray(
            [numpy_rng.permutation(8) for i in range(7)])
        numpy_val1 = numpy.asarray(
            [numpy_rng.permutation(8) for i in range(7)])
        print val0
        print numpy_val0
        print val1
        print numpy_val1
        self.assertTrue(numpy.all(val0 == numpy_val0))
        self.assertTrue(numpy.all(val1 == numpy_val1))

        # This call lacks "ndim_added=1", so ndim_added defaults to 0.
        # A ValueError should be raised.
        rf0 = RandomFunction(permutation_helper, tensor.imatrix, 8)
        post_r0, out0 = rf0(rng_R, (7, ), 8)
        f0 = compile.function([
            compile.In(rng_R,
                       value=numpy.random.RandomState(utt.fetch_seed()),
                       update=post_r0,
                       mutable=True)
        ], [out0],
                              accept_inplace=True)
        self.assertRaises(ValueError, f0)

        # Here, ndim_added is 2 instead of 1. A ValueError should be raised.
        rf2 = RandomFunction(permutation_helper,
                             tensor.imatrix,
                             8,
                             ndim_added=2)
        post_r2, out2 = rf2(rng_R, (7, ), 8)
        f2 = compile.function([
            compile.In(rng_R,
                       value=numpy.random.RandomState(utt.fetch_seed()),
                       update=post_r2,
                       mutable=True)
        ], [out2],
                              accept_inplace=True)
        self.assertRaises(ValueError, f2)
コード例 #38
0
    def test_dimshuffle(self):
        utt.seed_rng()
        rng = numpy.random.RandomState(utt.fetch_seed())

        # 2d -> 0d
        a = theano._asarray(rng.randn(1, 1), dtype='float32')
        b = cuda_ndarray.CudaNdarray(a)
        assert numpy.allclose(numpy.transpose(a),
                              cuda_ndarray.dimshuffle(b, ()))

        # Test when we drop a axis that don't have shape 1
        a = theano._asarray(rng.randn(2, 1), dtype='float32')
        b = cuda_ndarray.CudaNdarray(a)
        self.assertRaises(ValueError, cuda_ndarray.dimshuffle, b, ())

        # Test that we can't take a dimensions multiple time
        a = theano._asarray(rng.randn(2, 1), dtype='float32')
        b = cuda_ndarray.CudaNdarray(a)
        self.assertRaises(ValueError, cuda_ndarray.dimshuffle, b, (1, 1))

        # 1d
        a = theano._asarray(rng.randn(3, ), dtype='float32')
        b = cuda_ndarray.CudaNdarray(a)
        assert numpy.allclose(numpy.transpose(a),
                              cuda_ndarray.dimshuffle(b, (0, )))
        assert numpy.allclose(a[None, :, None],
                              cuda_ndarray.dimshuffle(b, (-1, 0, -1)))

        # 2d
        a = theano._asarray(rng.randn(3, 11), dtype='float32')
        b = cuda_ndarray.CudaNdarray(a)
        assert numpy.allclose(numpy.transpose(a),
                              cuda_ndarray.dimshuffle(b, (1, 0)))
        assert numpy.allclose(
            numpy.transpose(a)[None, :, None, :, None],
            cuda_ndarray.dimshuffle(b, (-1, 1, -1, 0, -1)))

        # 2d -> 1d
        a = theano._asarray(rng.randn(1, 11), dtype='float32')
        b = cuda_ndarray.CudaNdarray(a)
        assert numpy.allclose(a[:], cuda_ndarray.dimshuffle(b, (1, )))
        a = theano._asarray(rng.randn(11, 1), dtype='float32')
        b = cuda_ndarray.CudaNdarray(a)
        assert numpy.allclose(a.reshape((11, )),
                              cuda_ndarray.dimshuffle(b, (0, )))

        # 3d
        a = theano._asarray(rng.randn(3, 4, 5), dtype='float32')
        b = cuda_ndarray.CudaNdarray(a)
        assert numpy.allclose(a, cuda_ndarray.dimshuffle(b, (0, 1, 2)))
        assert numpy.allclose(numpy.swapaxes(a, 0, 1),
                              cuda_ndarray.dimshuffle(b, (1, 0, 2)))
        assert numpy.allclose(numpy.swapaxes(a, 0, 2),
                              cuda_ndarray.dimshuffle(b, (2, 1, 0)))
        assert numpy.allclose(numpy.swapaxes(a, 1, 2),
                              cuda_ndarray.dimshuffle(b, (0, 2, 1)))
        assert numpy.allclose(
            numpy.swapaxes(a, 1, 2)[None, :, None, :, :, None],
            cuda_ndarray.dimshuffle(b, (-1, 0, -1, 2, 1, -1)))

        # 4d
        a = theano._asarray(rng.randn(3, 11, 4, 5), dtype='float32')
        b = cuda_ndarray.CudaNdarray(a)
        assert numpy.allclose(numpy.swapaxes(a, 0, 1),
                              cuda_ndarray.dimshuffle(b, (1, 0, 2, 3)))
        assert numpy.allclose(numpy.swapaxes(a, 0, 2),
                              cuda_ndarray.dimshuffle(b, (2, 1, 0, 3)))
        assert numpy.allclose(numpy.swapaxes(a, 0, 3),
                              cuda_ndarray.dimshuffle(b, (3, 1, 2, 0)))
        assert numpy.allclose(numpy.swapaxes(a, 0, 3),
                              cuda_ndarray.dimshuffle(b, (3, 1, 2, 0)))
        assert numpy.allclose(
            numpy.swapaxes(a, 0, 3)[None, :, None, :, :, :],
            cuda_ndarray.dimshuffle(b, (-1, 3, -1, 1, 2, 0)))
コード例 #39
0
def test_reshape():
    shapelist = [((1, 2, 3), (1, 2, 3)), ((1, ), (1, )),
                 ((1, 2, 3), (3, 2, 1)), ((1, 2, 3), (6, )),
                 ((1, 2, 3, 2), (6, 2)), ((2, 3, 2), (6, 2)),
                 ((2, 3, 2), (12, ))]

    bad_shapelist = [((1, 2, 3), (1, 2, 4)), ((1, ), (2, )),
                     ((1, 2, 3), (2, 2, 1)), ((1, 2, 3), (5, )),
                     ((1, 2, 3, 2), (6, 3)), ((2, 3, 2), (5, 2)),
                     ((2, 3, 2), (11, ))]

    utt.seed_rng()
    rng = numpy.random.RandomState(utt.fetch_seed())

    def subtest(shape_1, shape_2, rng):
        #print >> sys.stdout, "INFO: shapes", shape_1, shape_2
        a = theano._asarray(rng.randn(*shape_1), dtype='float32')
        b = cuda_ndarray.CudaNdarray(a)

        aa = a.reshape(shape_2)
        bb = b.reshape(shape_2)

        n_bb = numpy.asarray(bb)

        # print n_bb

        assert numpy.all(aa == n_bb)
        assert aa.shape == n_bb.shape

        # Test the not contiguous case
        shape_1_2x = (shape_1[0] * 2, ) + shape_1[1:]
        a = theano._asarray(rng.randn(*shape_1_2x), dtype='float32')
        b = cuda_ndarray.CudaNdarray(a)
        a = a[::2]
        b = b[::2]

        aa = a.reshape(shape_2)
        bb = b.reshape(shape_2)

        n_bb = numpy.asarray(bb)

        # print n_bb

        assert numpy.all(aa == n_bb)
        assert aa.shape == n_bb.shape

    def bad_subtest(shape_1, shape_2, rng):
        a = theano._asarray(rng.randn(*shape_1), dtype='float32')
        b = cuda_ndarray.CudaNdarray(a)

        try:
            bb = b.reshape(shape_2)
        except Exception as ValueError:
            return
        assert False

    # test working shapes
    for shape_1, shape_2 in shapelist:
        subtest(shape_1, shape_2, rng)
        subtest(shape_2, shape_1, rng)

    # test shape combinations that should give error
    for shape_1, shape_2 in bad_shapelist:
        bad_subtest(shape_1, shape_2, rng)
        bad_subtest(shape_2, shape_1, rng)
コード例 #40
0
ファイル: test_scan.py プロジェクト: shivamvats/Theano
    def new_run(self,
              inputs_info,
              states_info,
              parameters_info,
              n_outputs,
              n_shared_updates):
        """Generates a test for scan.

        :param inputs_info: list of lists of dictionaries
            Each list of dictionary represents one input sequence. Each
            dictionary is one tap of that sequence. The dictionary has two
            keys. ``use`` is either True or False, and it indicates if this
            tap should be used in the inner graph or not. ``tap`` is the tap
            value.
        :param states_info: list of lists of dictionaries
            see param ``inputs_info``. ``states_info`` has the same
            semantics, just that it is for states and not for inputs
        :param paramters_info: list of dictionary
            Each dictionary is a different parameter. It has only one key,
            namely ``use`` which says if the parameter should be used
            internally or not
        :param n_outputs: int
            Number of pure outputs for scan
        :param n_shared_updates: int
            Number of shared variable with updates. They are all numeric.

        """
        # Check the scan node has at least one output
        if n_outputs + n_shared_updates + len(states_info) == 0:
            return

        rng = numpy.random.RandomState(utt.fetch_seed())
        n_ins = len(inputs_info)
        inputs = [tensor.matrix('u%d' % k) for k in xrange(n_ins)]
        scan_inputs = []
        for inp, info in zip(inputs, inputs_info):
            scan_inputs.append(dict(input=inp, taps=[x['tap'] for x in
                                                     info]))
        n_states = len(states_info)
        scan_states = []
        states = []
        for info in states_info:
            if len(info) == 1 and info[0]['tap'] == -1:
                state = tensor.vector('x%d' % k)
                states.append(state)
                scan_states.append(state)
            else:
                state = tensor.matrix('x%d' % k)
                states.append(state)
                scan_states.append(
                    dict(initial=state, taps=[x['tap'] for x in info]))
        n_parameters = len(parameters_info)
        parameters = [tensor.vector('p%d' % k) for k in xrange(n_parameters)]
        original_shared_values = []
        shared_vars = []

        for k in xrange(n_shared_updates):
            data = rng.uniform(size=(4,)).astype(theano.config.floatX)
            original_shared_values.append(data)
            shared_vars.append(theano.shared(data, name='z%d' % k))

        def inner_function(*args):
            """
            Functions that constructs the inner graph of scan
            """
            arg_pos = 0
            to_add = None
            for in_info in inputs_info:
                for info in in_info:
                    arg = args[arg_pos]
                    arg_pos += 1
                    # Construct dummy graph around input
                    if info['use']:
                        if to_add is None:
                            to_add = arg * 2
                        else:
                            to_add = to_add + arg * 2
            states_out = [to_add] * n_states
            for dx, st_info in enumerate(states_info):
                for info in st_info:
                    arg = args[arg_pos]
                    arg_pos += 1
                    if info['use']:
                        if states_out[dx]:
                            states_out[dx] = states_out[dx] + arg * 3
                        else:
                            states_out[dx] = arg * 3
            for info in parameters_info:
                arg = args[arg_pos]
                arg_pos += 1
                if info['use']:
                    if to_add is None:
                        to_add = arg * 4
                    else:
                        to_add = to_add + arg * 4
            if to_add is not None:
                shared_outs = [sh * 5 + to_add for sh in shared_vars]
                rval = []
                for arg in states_out:
                    if arg is None:
                        rval.append(to_add)
                    else:
                        rval.append(arg + to_add)
                states_out = rval
                pure_outs = [to_add ** 2 for x in xrange(n_outputs)]
            else:
                shared_outs = [sh * 5 for sh in shared_vars]
                states_out = [x for x in states_out]
                pure_outs = [2 for x in xrange(n_outputs)]
            return states_out + pure_outs, dict(izip(shared_vars, shared_outs))

        def execute_inner_graph(*args):
            """
            Functions that computes numerically the values that scan should
            return
            """
            # Check if you need to go back in time over the sequences (the
            # first argument is n_steps, the second is go_backwards)
            nsteps = args[0]
            invert = False
            if args[1]:
                nsteps = nsteps * -1
            if nsteps < 0:
                new_ins = [x[::-1] for x in args[2: 2 + n_ins]]
            else:
                new_ins = [x for x in args[2: 2 + n_ins]]
            nsteps = abs(nsteps)
            # Simplify the inputs by slicing them according to the taps
            nw_inputs = []
            for inp, info in zip(new_ins, inputs_info):
                taps = [x['tap'] for x in info]

                if numpy.min(taps) < 0:
                    _offset = abs(numpy.min(taps))
                else:
                    _offset = 0
                nw_inputs += [inp[_offset + k:] for k in taps]
            # Simplify the states by slicing them according to the taps.
            # Note that if the memory buffer for the inputs and outputs is
            # the same, by changing the outputs we also change the outputs
            nw_states_inputs = []
            nw_states_outs = []
            for st, info in zip(args[2 + n_ins:2 + n_ins + n_states],
                                states_info):
                taps = [x['tap'] for x in info]

                membuf = numpy.zeros((nsteps + abs(numpy.min(taps)), 4))
                if abs(numpy.min(taps)) != 1:
                    membuf[:abs(numpy.min(taps))] = st[:abs(numpy.min(taps))]
                else:
                    membuf[:abs(numpy.min(taps))] = st

                nw_states_inputs += [membuf[abs(numpy.min(taps)) + k:]
                                     for k in taps]
                nw_states_outs.append(membuf[abs(numpy.min(taps)):])

            parameters_vals = args[2 + n_ins + n_states:]
            out_mem_buffers = [numpy.zeros((nsteps, 4)) for k in
                               xrange(n_outputs)]
            shared_values = [x.copy() for x in original_shared_values]

            for step in xrange(nsteps):
                arg_pos = 0
                to_add = None
                for in_info in inputs_info:
                    for info in in_info:
                        arg = nw_inputs[arg_pos][step]
                        arg_pos += 1
                        # Construct dummy graph around input
                        if info['use']:
                            if to_add is None:
                                to_add = arg * 2
                            else:
                                to_add = to_add + arg * 2
                arg_pos = 0
                for dx, st_info in enumerate(states_info):
                    if to_add is not None:
                        nw_states_outs[dx][step] = to_add
                    for info in st_info:
                        arg = nw_states_inputs[arg_pos][step]
                        arg_pos += 1
                        if info['use']:
                            nw_states_outs[dx][step] += arg * 3
                for arg, info in zip(parameters_vals, parameters_info):
                    if info['use']:
                        if to_add is None:
                            to_add = arg * 4
                        else:
                            to_add = to_add + arg * 4
                if to_add is not None:
                    shared_values = [sh * 5 + to_add for sh in shared_values]
                    for state in nw_states_outs:
                        state[step] += to_add
                    for out in out_mem_buffers:
                        out[step] = to_add ** 2
                else:
                    shared_values = [sh * 5 for sh in shared_values]
                    for out in out_mem_buffers:
                        out[step] = 2
            return nw_states_outs + out_mem_buffers, shared_values
        possible_n_steps = [-1, 1, 5, -5]
        if n_ins > 0:
            possible_n_steps.append(None)
        for n_steps in [-1, 1, 5, -5, None]:
            for go_backwards in [True, False]:
                outputs, updates = scan_module.scan(
                    inner_function,
                    sequences=scan_inputs,
                    outputs_info=scan_states,
                    non_sequences=parameters,
                    n_steps=n_steps,
                    go_backwards=go_backwards,
                    truncate_gradient=-1)
                my_f = theano.function(inputs + states + parameters,
                                       outputs,
                                       updates=updates,
                                       allow_input_downcast=True)

                if n_steps is not None and abs(n_steps) == 1:
                    all_nodes = my_f.maker.fgraph.toposort()
                    assert len([x for x in all_nodes
                                if isinstance(x.op, ScanOp)]) == 0
                print('   n_steps', n_steps, file=sys.stderr)
                print('   go_backwards', go_backwards, file=sys.stderr)

                print('       Scenario 1. Correct shape', file=sys.stderr)
                if n_steps is not None:
                    _n_steps = n_steps
                else:
                    _n_steps = 8
                # Generating data
                # Scenario 1 : Good fit shapes
                input_values = []
                for info in inputs_info:
                    taps = [x['tap'] for x in info]
                    offset = 0
                    if len([x for x in taps if x < 0]) > 0:
                        offset += abs(numpy.min([x for x in taps if x < 0]))
                    if len([x for x in taps if x > 0]) > 0:
                        offset += numpy.max([x for x in taps if x > 0])
                    data = rng.uniform(size=(abs(_n_steps) + offset, 4))
                    input_values.append(data)
                state_values = []
                for info in states_info:
                    taps = [x['tap'] for x in info]
                    offset = abs(numpy.min(taps))
                    if offset > 1:
                        data = rng.uniform(size=(offset, 4))
                    else:
                        data = rng.uniform(size=(4,))
                        data = numpy.arange(4)
                    state_values.append(data)
                param_values = [rng.uniform(size=(4,)) for k in
                                xrange(n_parameters)]
                param_values = [numpy.arange(4) for k in
                                xrange(n_parameters)]
                for var, val in zip(shared_vars, original_shared_values):
                    var.set_value(val)
                theano_outs = my_f(*(input_values + state_values +
                                     param_values))
                args = ([_n_steps, go_backwards] +
                        input_values +
                        state_values +
                        param_values)
                rvals = execute_inner_graph(*args)
                numpy_outs, numpy_shared = rvals
                assert len(numpy_outs) == len(theano_outs)
                assert len(numpy_shared) == len(shared_vars)
                for th_out, num_out in zip(theano_outs, numpy_outs):
                    try:
                        assert numpy.allclose(th_out, num_out)
                    except Exception:
                        #import ipdb; ipdb.set_trace()
                        raise
                for th_out, num_out in zip(shared_vars, numpy_shared):
                    try:
                        assert numpy.allclose(th_out.get_value(), num_out)
                    except Exception:
                        #import ipdb; ipdb.set_trace()
                        raise
                # Scenario 2 : Loose fit (sequences longer then required)
                print('       Scenario 2. Loose shapes', file=sys.stderr)
                input_values = []
                for pos, info in enumerate(inputs_info):
                    taps = [x['tap'] for x in info]
                    offset = 0
                    if len([x for x in taps if x < 0]) > 0:
                        offset += abs(numpy.min([x for x in taps if x < 0]))
                    if len([x for x in taps if x > 0]) > 0:
                        offset += numpy.max([x for x in taps if x > 0])
                    if n_steps is not None:
                        # loose inputs make sense only when n_steps is
                        # defined
                        data = rng.uniform(
                                size=(abs(_n_steps) + offset + pos + 1, 4))
                    else:
                        data = rng.uniform(size=(abs(_n_steps) + offset, 4))
                    input_values.append(data)
                state_values = []
                for pos, info in enumerate(states_info):
                    taps = [x['tap'] for x in info]
                    offset = abs(numpy.min(taps))
                    if offset > 1:
                        data = rng.uniform(size=(offset + pos + 1, 4))
                    else:
                        data = rng.uniform(size=(4,))
                    state_values.append(data)
                param_values = [rng.uniform(size=(4,)) for k in
                                xrange(n_parameters)]
                for var, val in zip(shared_vars, original_shared_values):
                    var.set_value(val)
                theano_outs = my_f(*(input_values + state_values +
                                     param_values))
                args = ([_n_steps, go_backwards] +
                        input_values +
                        state_values +
                        param_values)
                rvals = execute_inner_graph(*args)
                numpy_outs, numpy_shared = rvals
                assert len(numpy_outs) == len(theano_outs)
                assert len(numpy_shared) == len(shared_vars)

                for th_out, num_out in zip(theano_outs, numpy_outs):
                    assert numpy.allclose(th_out, num_out)
                for th_out, num_out in zip(shared_vars, numpy_shared):
                    assert numpy.allclose(th_out.get_value(), num_out)
                # Scenario 3 : Less data then required
                print('       Scenario 2. Wrong shapes', file=sys.stderr)
                input_values = []
                for pos, info in enumerate(inputs_info):
                    taps = [x['tap'] for x in info]
                    offset = 0
                    if len([x for x in taps if x < 0]) > 0:
                        offset += abs(numpy.min([x for x in taps if x < 0]))
                    if len([x for x in taps if x > 0]) > 0:
                        offset += numpy.max([x for x in taps if x > 0])
                    data = rng.uniform(size=(abs(_n_steps) + offset - 1, 4))
                    input_values.append(data)
                state_values = []
                for pos, info in enumerate(states_info):
                    taps = [x['tap'] for x in info]
                    offset = abs(numpy.min(taps))
                    data = rng.uniform(size=(offset - 1, 4))
                    state_values.append(data)
                param_values = [rng.uniform(size=(4,)) for k in
                                xrange(n_parameters)]
                for var, val in zip(shared_vars, original_shared_values):
                    var.set_value(val)
                self.assertRaises(Exception, my_f,
                                  inputs + state_values + param_values)
コード例 #41
0
ファイル: test_scan.py プロジェクト: shivamvats/Theano
    def test001_generate_tests(self):
        rng = numpy.random.RandomState(utt.fetch_seed())
        all_inputs_info = [[]]
        possible_taps_use_pairs = [[dict(tap=0, use=True)],
                                   [dict(tap=0, use=False)],
                                   [dict(tap=-3, use=True),
                                        dict(tap=-1, use=True)],
                                   [dict(tap=-3, use=True),
                                        dict(tap=-1, use=False)],
                                   [dict(tap=-3, use=False),
                                        dict(tap=-1, use=False)],
                                   [dict(tap=-2, use=True),
                                        dict(tap=0, use=True)],
                                   [dict(tap=-2, use=False),
                                        dict(tap=0, use=True)],
                                   [dict(tap=-2, use=False),
                                        dict(tap=0, use=False)],
                                   [dict(tap=0, use=True),
                                        dict(tap=3, use=True)],
                                   [dict(tap=2, use=True),
                                        dict(tap=3, use=True)],
                                   [dict(tap=-2, use=True),
                                        dict(tap=3, use=True)]]
        test_nb = 0
        for n_ins in [1, 2]:
            # Randomly pick up 4*n_ins combinations of arguments
            for k in xrange(4 * n_ins):
                inp = []
                for inp_nb in xrange(n_ins):

                    pos = rng.randint(len(possible_taps_use_pairs))
                    inp.append(possible_taps_use_pairs[pos])
                all_inputs_info.append(inp)
        all_states_info = [[]]
        possible_taps_use_pairs = [[dict(tap=-1, use=True)],
                                   [dict(tap=-1, use=False)],
                                   [dict(tap=-3, use=True)],
                                   [dict(tap=-3, use=False)],
                                   [dict(tap=-3, use=True),
                                        dict(tap=-1, use=True)],
                                   [dict(tap=-3, use=True),
                                        dict(tap=-1, use=False)],
                                   [dict(tap=-3, use=False),
                                        dict(tap=-1, use=False)],
                                   [dict(tap=-4, use=True),
                                        dict(tap=-2, use=True)],
                                   [dict(tap=-4, use=False),
                                        dict(tap=-2, use=True)]]
        for n_ins in [1, 2]:
            # Randomly pick up 4*n_ins combinations of arguments
            for k in xrange(4 * n_ins):
                state = []
                for state_nb in xrange(n_ins):
                    pos = rng.randint(len(possible_taps_use_pairs))
                    state.append(possible_taps_use_pairs[pos])
                all_states_info.append(state)
        all_parameters_info = [[],
                           [dict(use=False)],
                           [dict(use=True)],
                           [dict(use=True), dict(use=True)],
                           [dict(use=True), dict(use=False)]]
        # This generates errors related to some unfixed bug in the current
        # version of scan
        # The test will also have to be changesd following some further
        # restriction of scan and reduction of the number of corner cases
        return
        for n_outputs in [0, 1, 2]:
            for n_shared_updates in [0, 1, 2]:
                for n_random_combinations in xrange(1):
                    pos_inp = rng.randint(len(all_inputs_info))
                    pos_st = rng.randint(len(all_states_info))
                    pos_param = rng.randint(len(all_parameters_info))
                    print(file=sys.stderr)
                    print('Test nb', test_nb, file=sys.stderr)
                    print(' inputs', all_inputs_info[pos_inp], file=sys.stderr)
                    print(' states', all_states_info[pos_st], file=sys.stderr)
                    print(' parameters', \
                            all_parameters_info[pos_param], file=sys.stderr)
                    print(' n_outputs', n_outputs, file=sys.stderr)
                    print(' n_shared_updates', n_shared_updates, file=sys.stderr)
                    test_nb += 1
                    self.new_run(inputs_info=all_inputs_info[pos_inp],
                             states_info=all_states_info[pos_st],
                             parameters_info=all_parameters_info[pos_param],
                             n_outputs=n_outputs,
                             n_shared_updates=n_shared_updates)
コード例 #42
0
ファイル: test_conv3d.py プロジェクト: yyq90/grammarVAE
    def setUp(self):
        super(TestConv3D, self).setUp()
        utt.seed_rng()
        self.rng = N.random.RandomState(utt.fetch_seed())

        mode = copy.copy(theano.compile.mode.get_default_mode())
        mode.check_py_code = False

        self.W = shared(N.ndarray(shape=(1, 1, 1, 1, 1), dtype=floatX))
        self.W.name = 'W'
        self.b = shared(N.zeros(1, dtype=floatX))
        self.b.name = 'b'
        self.rb = shared(N.zeros(1, dtype=floatX))
        self.rb.name = 'rb'
        self.V = shared(N.ndarray(shape=(1, 1, 1, 1, 1), dtype=floatX))
        self.V.name = 'V'
        self.d = shared(N.ndarray(shape=(3, ), dtype=int))
        self.d.name = 'd'

        self.H = conv3D(self.V, self.W, self.b, self.d)
        self.H.name = 'H'
        self.H_func = function([], self.H, mode=mode)
        self.H_shape_func = function([], self.H.shape, mode=mode)

        self.RShape = T.vector(dtype='int64')
        self.RShape.name = 'RShape'

        self.otherH = T.TensorType(floatX,
                        (False, False, False, False, False))(name='otherH')
        self.transp = convTransp3D(self.W, self.rb, self.d,
                                   self.otherH, self.RShape)
        self.transp.name = 'transp'
        self.transp_func = function([self.otherH, self.RShape],
                                    self.transp, mode=mode)

        self.R = convTransp3D(self.W, self.rb, self.d, self.H, self.RShape)
        self.R.name = 'R'
        self.R_func = function([self.RShape], self.R, mode=mode)
        self.R_shape_func = function([self.RShape], self.R.shape)

        diff = self.V - self.R
        diff.name = 'diff'
        sqr = T.sqr(diff)
        sqr.name = 'sqr'
        self.reconsObj = T.sum(sqr)
        self.reconsObj.name = 'reconsObj'
        self.reconsObjFunc = function([self.RShape], self.reconsObj, mode=mode)

        W_grad = T.grad(self.reconsObj, self.W)

        self.gradientsFunc = function([self.RShape],
                        [W_grad, T.grad(self.reconsObj,
                        self.H), T.grad(self.reconsObj, self.V),
                         T.grad(self.reconsObj, self.b)], mode=mode)

        self.check_c_against_python = function([self.RShape],
                        [T.grad(self.reconsObj, self.W), T.grad(self.reconsObj,
                        self.H), T.grad(self.reconsObj, self.V),
                         T.grad(self.reconsObj, self.b)], mode='DEBUG_MODE')

        self.dCdW_shape_func = function([self.RShape],
                        T.grad(self.reconsObj, self.W).shape, mode=mode)
コード例 #43
0
 def setUp(self):
     utt.seed_rng()
     self.rng = np.random.RandomState(seed=utt.fetch_seed())
コード例 #44
0
ファイル: test_nlinalg.py プロジェクト: vikkamath/Theano-1
 def setUp(self):
     super(test_MatrixInverse, self).setUp()
     self.op_class = MatrixInverse
     self.op = matrix_inverse
     self.rng = numpy.random.RandomState(utt.fetch_seed())
コード例 #45
0
    def test_shuffle_row_elements(self):
        """Test that RandomStreams.shuffle_row_elements generates the right results"""
        # Check over two calls to see if the random state is correctly updated.
        # On matrices, for each row, the elements of that row should be
        # shuffled.
        # Note that this differs from numpy.random.shuffle, where all the
        # elements of the matrix are shuffled.
        mm = Module()
        mm.random = RandomStreams(utt.fetch_seed())
        m_input = tensor.dmatrix()
        mm.f = Method([m_input], mm.random.shuffle_row_elements(m_input))
        mmade = mm.make()
        mmade.random.initialize()

        # Generate the elements to be shuffled
        val_rng = numpy.random.RandomState(utt.fetch_seed() + 42)
        in_mval = val_rng.uniform(-2, 2, size=(20, 5))
        fn_mval0 = mmade.f(in_mval)
        fn_mval1 = mmade.f(in_mval)
        print in_mval[0]
        print fn_mval0[0]
        print fn_mval1[0]
        assert not numpy.all(in_mval == fn_mval0)
        assert not numpy.all(in_mval == fn_mval1)
        assert not numpy.all(fn_mval0 == fn_mval1)

        rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30)
        rng = numpy.random.RandomState(int(rng_seed))
        numpy_mval0 = in_mval.copy()
        numpy_mval1 = in_mval.copy()
        for row in numpy_mval0:
            rng.shuffle(row)
        for row in numpy_mval1:
            rng.shuffle(row)

        assert numpy.all(numpy_mval0 == fn_mval0)
        assert numpy.all(numpy_mval1 == fn_mval1)

        # On vectors, the behaviour is the same as numpy.random.shuffle,
        # except that it does not work in place, but returns a shuffled vector.
        vm = Module()
        vm.random = RandomStreams(utt.fetch_seed())
        v_input = tensor.dvector()
        vm.f = Method([v_input], vm.random.shuffle_row_elements(v_input))
        vmade = vm.make()
        vmade.random.initialize()

        in_vval = val_rng.uniform(-3, 3, size=(12, ))
        fn_vval = vmade.f(in_vval)
        numpy_vval = in_vval.copy()
        vrng = numpy.random.RandomState(int(rng_seed))
        vrng.shuffle(numpy_vval)
        print in_vval
        print fn_vval
        print numpy_vval
        assert numpy.all(numpy_vval == fn_vval)

        # Trying to shuffle a vector with function that should shuffle
        # matrices, or vice versa, raises a TypeError
        self.assertRaises(TypeError, vmade.f, in_mval)
        self.assertRaises(TypeError, mmade.f, in_vval)
コード例 #46
0
ファイル: test_nlinalg.py プロジェクト: vikkamath/Theano-1
 def test_extract_diag_grad(self):
     rng = numpy.random.RandomState(utt.fetch_seed())
     x = rng.rand(5, 4).astype(self.floatX)
     tensor.verify_grad(extract_diag, [x], rng=rng)
コード例 #47
0
        def test_specify_shape(self):
            dtype = self.dtype
            if dtype is None:
                dtype = theano.config.floatX

            rng = np.random.RandomState(utt.fetch_seed())
            x1_1 = np.asarray(rng.uniform(1, 2, [4, 2]), dtype=dtype)
            x1_1 = self.cast_value(x1_1)
            x1_2 = np.asarray(rng.uniform(1, 2, [4, 2]), dtype=dtype)
            x1_2 = self.cast_value(x1_2)
            x2 = np.asarray(rng.uniform(1, 2, [4, 3]), dtype=dtype)
            x2 = self.cast_value(x2)

            # Test that we can replace with values of the same shape
            x1_shared = self.shared_constructor(x1_1)
            x1_specify_shape = tensor.specify_shape(x1_shared, x1_1.shape)
            x1_shared.set_value(x1_2)
            assert np.allclose(self.ref_fct(x1_shared.get_value(borrow=True)),
                               self.ref_fct(x1_2))
            shape_op_fct = theano.function([], x1_shared.shape)
            topo = shape_op_fct.maker.fgraph.toposort()
            if theano.config.mode != 'FAST_COMPILE':
                assert len(topo) == 3
                assert isinstance(topo[0].op, tensor.opt.Shape_i)
                assert isinstance(topo[1].op, tensor.opt.Shape_i)
                assert isinstance(topo[2].op, tensor.opt.MakeVector)

            # Test that we forward the input
            specify_shape_fct = theano.function([], x1_specify_shape)
            assert np.all(
                self.ref_fct(specify_shape_fct()) == self.ref_fct(x1_2))
            topo_specify = specify_shape_fct.maker.fgraph.toposort()
            assert len(topo_specify) == 2

            # Test that we put the shape info into the graph
            shape_constant_fct = theano.function([], x1_specify_shape.shape)
            assert np.all(shape_constant_fct() == shape_op_fct())
            topo_cst = shape_constant_fct.maker.fgraph.toposort()
            if theano.config.mode != 'FAST_COMPILE':
                assert len(topo_cst) == 1
                topo_cst[0].op == theano.compile.function_module.deep_copy_op

            # Test that we can take the grad.
            if (theano.sparse.enable_sparse and isinstance(
                    x1_specify_shape.type, theano.sparse.SparseType)):
                # SparseVariable don't support sum for now.
                assert not hasattr(x1_specify_shape, 'sum')
            else:
                shape_grad = tensor.grad(x1_specify_shape.sum(), x1_shared)
                shape_constant_fct_grad = theano.function([], shape_grad)
                # theano.printing.debugprint(shape_constant_fct_grad)
                shape_constant_fct_grad()

            # Test that we can replace with values of the different shape
            # but that will raise an error in some case, but not all
            specify_shape_fct()
            x1_shared.set_value(x2)
            self.assertRaises(AssertionError, specify_shape_fct)

            # No assertion will be raised as the Op is removed from the graph
            # when their is optimization
            if theano.config.mode not in [
                    'FAST_COMPILE', 'DebugMode', 'DEBUG_MODE'
            ]:
                shape_constant_fct()
            else:
                self.assertRaises(AssertionError, shape_constant_fct)
コード例 #48
0
def test_WEIRD_STUFF():
    n_vis = 3

    rng = N.random.RandomState(unittest_tools.fetch_seed(7722342))
    x = rng.randn(10,n_vis)
    y = rng.randn(10,n_vis)

    #set y to be like x with a lag of LAG
    LAG = 4
    y[LAG:] = x[:-LAG, 0:n_vis]

    minimizer_fn1 = sgd_minimizer(stepsize = 0.001, WEIRD_STUFF = False)
    minimizer_fn2 = sgd_minimizer(stepsize = 0.001, WEIRD_STUFF = True)
    rnn_module1 = ExampleRNN(n_vis, minimizer_fn1)
    rnn_module2 = ExampleRNN(n_vis, minimizer_fn2)
    rnn1 = rnn_module1.make(mode='FAST_RUN')
#    rnn2 = rnn_module1.make(mode='FAST_COMPILE')#work
#    rnn2 = rnn_module1.make(mode='FAST_RUN')#fail
    rnn2 = rnn_module2.make(mode=Mode('c|py', 'fast_run'))#fail
#    rnn2 = rnn_module1.make(mode=Mode('c|py', 'fast_run').excluding("inplace"))#work
#    rnn2 = rnn_module1.make(mode=Mode('c|py', 'fast_compile'))#work
#    rnn2 = rnn_module1.make(mode=Mode('py', 'fast_run_stable'))#work
#    rnn2 = rnn_module1.make(mode=Mode('py', 'merge'))#work
#    rnn2 = rnn_module1.make(mode=Mode('c|py', 'fast_run').excluding("inplace_opt"))#work
#    rnn2 = rnn_module1.make(mode=Mode('py', 'fast_run'))#fail
    m = Mode('py', 'fast_run')
#    for n in m.optimizer: print n.name

    if 0:
        topo1=rnn1.minimizer.step_cost.maker.fgraph.toposort()
        topo2=rnn2.minimizer.step_cost.maker.fgraph.toposort()
        for i in range(len(topo1)):
            print '1',i, topo1[i]
            print '2',i, topo2[i]
    if 0:
        topo1=rnn1.minimizer.step.maker.fgraph.toposort()
        topo2=rnn2.minimizer.step.maker.fgraph.toposort()
        for i in range(len(topo1)):
            print '1',i, topo1[i]
            print '2',i, topo2[i]
    import theano.printing

    #print len(rnn1.minimizer.step.maker.inputs)
    #print len(rnn2.minimizer.step.maker.inputs)
    #print rnn1.minimizer.step.maker.inputs
    #print rnn2.minimizer.step.maker.inputs



#    for i in range(1,len(rnn1.minimizer.step.maker.inputs)):
#        print "valid update:",theano.printing.pp(rnn1.minimizer.step.maker.inputs[i].update),
#        print rnn1.minimizer.step.maker.inputs[i].update.name
#        print "other update",theano.printing.pp(rnn2.minimizer.step.maker.inputs[i].update),
#        print rnn2.minimizer.step.maker.inputs[i].update.name
#    print dir(rnn1.minimizer.step.maker.inputs[5].update)
#    print dir(rnn2.minimizer.step.maker.inputs[5].update)



    niter=3
    for i in xrange(niter):
        #print rnn1.minimizer.step_cost(x, y)
        #print rnn2.minimizer.step_cost(x, y)

    #    assert rnn1.n_vis != rnn2.n_vis or slef.n_hid != rnn2.n_hid or rnn1.n_out != rnn2.n_out
        assert (N.abs(rnn1.z0-rnn2.z0)<1e-8).all()
        #print (N.abs(rnn1.w-rnn2.w)<1e-8).all()
        #print (N.abs(rnn1.w-rnn2.w))
        #print rnn1.w
        #print rnn2.w
        assert (N.abs(rnn1.w-rnn2.w)<1e-8).all()
コード例 #49
0
ファイル: test_nlinalg.py プロジェクト: vikkamath/Theano-1
 def setUp(self):
     super(test_Eig, self).setUp()
     self.rng = numpy.random.RandomState(utt.fetch_seed())
     self.A = theano.tensor.matrix(dtype=self.dtype)
     X = numpy.asarray(self.rng.rand(5, 5), dtype=self.dtype)
     self.S = X.dot(X.T)
コード例 #50
0
ファイル: test_raw_random.py プロジェクト: yubow/Theano
    def test_random_function_ndim_added(self):
        """Test that random_function helper function accepts ndim_added as
        keyword argument"""

        # If using numpy's uniform distribution, ndim_added should be 0,
        # because the shape provided as argument is the output shape.
        # Specifying a different ndim_added will change the Op's output ndim,
        # so numpy.uniform will produce a result of incorrect shape,
        # and a ValueError should be raised.
        def ndim_added_deco(ndim_added):
            def randomfunction(random_state,
                               size=(),
                               low=0.0,
                               high=0.0,
                               ndim=None):
                ndim, size, bcast = raw_random._infer_ndim_bcast(ndim, size)
                if ndim_added < 0:
                    bcast = bcast[:ndim_added]
                else:
                    bcast = bcast + ((False, ) * ndim_added)
                assert len(bcast) == ndim + ndim_added
                op = RandomFunction('uniform',
                                    tensor.TensorType(dtype='float64',
                                                      broadcastable=bcast),
                                    ndim_added=ndim_added)
                return op(random_state, size, low, high)

            return randomfunction

        uni_1 = ndim_added_deco(1)
        uni_0 = ndim_added_deco(0)
        uni_m1 = ndim_added_deco(-1)

        rng_R = random_state_type()

        p_uni11, uni11 = uni_1(rng_R, size=(4, ))
        p_uni12, uni12 = uni_1(rng_R, size=(3, 4))
        p_uni01, uni01 = uni_0(rng_R, size=(4, ))
        p_uni02, uni02 = uni_0(rng_R, size=(3, 4))
        p_unim11, unim11 = uni_m1(rng_R, size=(4, ))
        p_unim12, unim12 = uni_m1(rng_R, size=(3, 4))

        self.assertEqual(uni11.ndim, 2)
        self.assertEqual(uni12.ndim, 3)
        self.assertEqual(uni01.ndim, 1)
        self.assertEqual(uni02.ndim, 2)
        self.assertEqual(unim11.ndim, 0)
        self.assertEqual(unim12.ndim, 1)

        f11 = compile.function([
            compile.In(rng_R,
                       value=numpy.random.RandomState(utt.fetch_seed()),
                       update=p_uni11,
                       mutable=True)
        ], [uni11],
                               accept_inplace=True)
        f12 = compile.function([
            compile.In(rng_R,
                       value=numpy.random.RandomState(utt.fetch_seed()),
                       update=p_uni12,
                       mutable=True)
        ], [uni12],
                               accept_inplace=True)
        fm11 = compile.function([
            compile.In(rng_R,
                       value=numpy.random.RandomState(utt.fetch_seed()),
                       update=p_unim11,
                       mutable=True)
        ], [unim11],
                                accept_inplace=True)
        fm12 = compile.function([
            compile.In(rng_R,
                       value=numpy.random.RandomState(utt.fetch_seed()),
                       update=p_unim12,
                       mutable=True)
        ], [unim12],
                                accept_inplace=True)
        f0 = compile.function([
            compile.In(rng_R,
                       value=numpy.random.RandomState(utt.fetch_seed()),
                       update=p_uni02,
                       mutable=True)
        ], [uni01, uni02],
                              accept_inplace=True)
        self.assertRaises(ValueError, f11)
        self.assertRaises(ValueError, f12)
        self.assertRaises(ValueError, fm11)
        self.assertRaises(ValueError, fm12)
        u01, u02 = f0()
        print u01
        print u02
        self.assertTrue(numpy.allclose(u01, u02[0]))
コード例 #51
0
ファイル: test_nlinalg.py プロジェクト: vikkamath/Theano-1
 def test_alloc_diag_grad(self):
     rng = numpy.random.RandomState(utt.fetch_seed())
     x = rng.rand(5)
     tensor.verify_grad(alloc_diag, [x], rng=rng)
コード例 #52
0
ファイル: test_raw_random.py プロジェクト: yubow/Theano
    def test_infer_shape(self):
        rng_R = random_state_type()
        rng_R_val = numpy.random.RandomState(utt.fetch_seed())

        # no shape specified, default args
        post_r, out = uniform(rng_R)
        self._compile_and_check([rng_R], [out], [rng_R_val], RandomFunction)

        post_r, out = uniform(rng_R, size=None, ndim=2)
        self._compile_and_check([rng_R], [out], [rng_R_val], RandomFunction)
        """
        #infer_shape don't work for multinomial.
        #The parameter ndim_added is set to 1 and in this case, the infer_shape
        #inplementation don't know how to infer the shape
        post_r, out = multinomial(rng_R)

        self._compile_and_check([rng_R], [out], [rng_R_val],
                                RandomFunction)
        """

        # no shape specified, args have to be broadcasted
        low = tensor.TensorType(dtype='float64',
                                broadcastable=(False, True, True))()
        high = tensor.TensorType(dtype='float64',
                                 broadcastable=(True, True, True, False))()
        post_r, out = uniform(rng_R, size=None, ndim=2, low=low, high=high)
        low_val = [[[3]], [[4]], [[-5]]]
        high_val = [[[[5, 8]]]]
        self._compile_and_check([rng_R, low, high], [out],
                                [rng_R_val, low_val, high_val], RandomFunction)

        # multinomial, specified shape
        """
        #infer_shape don't work for multinomial
        n = iscalar()
        pvals = dvector()
        size_val = (7, 3)
        n_val = 6
        pvals_val = [0.2] * 5
        post_r, out = multinomial(rng_R, size=size_val, n=n, pvals=pvals,
                                  ndim=2)

        self._compile_and_check([rng_R, n, pvals], [out],
                                [rng_R_val, n_val, pvals_val],
                                RandomFunction)
        """

        # uniform vector low and high
        low = dvector()
        high = dvector()
        post_r, out = uniform(rng_R, low=low, high=1)
        low_val = [-5, .5, 0, 1]
        self._compile_and_check([rng_R, low], [out], [rng_R_val, low_val],
                                RandomFunction)

        low_val = [.9]
        self._compile_and_check([rng_R, low], [out], [rng_R_val, low_val],
                                RandomFunction)

        post_r, out = uniform(rng_R, low=low, high=high)
        low_val = [-4., -2]
        high_val = [-1, 0]
        self._compile_and_check([rng_R, low, high], [out],
                                [rng_R_val, low_val, high_val], RandomFunction)

        low_val = [-4.]
        high_val = [-1]
        self._compile_and_check([rng_R, low, high], [out],
                                [rng_R_val, low_val, high_val], RandomFunction)

        # uniform broadcasting low and high
        low = dvector()
        high = dcol()
        post_r, out = uniform(rng_R, low=low, high=high)
        low_val = [-5, .5, 0, 1]
        high_val = [[1.]]
        self._compile_and_check([rng_R, low, high], [out],
                                [rng_R_val, low_val, high_val], RandomFunction)

        low_val = [.9]
        high_val = [[1.], [1.1], [1.5]]
        self._compile_and_check([rng_R, low, high], [out],
                                [rng_R_val, low_val, high_val], RandomFunction)

        low_val = [-5, .5, 0, 1]
        high_val = [[1.], [1.1], [1.5]]
        self._compile_and_check([rng_R, low, high], [out],
                                [rng_R_val, low_val, high_val], RandomFunction)

        # uniform with vector slice
        low = dvector()
        high = dvector()
        post_r, out = uniform(rng_R, low=low, high=high)
        low_val = [.1, .2, .3]
        high_val = [1.1, 2.2, 3.3]
        size_val = (3, )
        self._compile_and_check([rng_R, low, high], [out],
                                [rng_R_val, low_val[:-1], high_val[:-1]],
                                RandomFunction)

        # uniform with explicit size and size implicit in parameters
        # NOTE 1: Would it be desirable that size could also be supplied
        # as a Theano variable?
        post_r, out = uniform(rng_R, size=size_val, low=low, high=high)
        self._compile_and_check([rng_R, low, high], [out],
                                [rng_R_val, low_val, high_val], RandomFunction)

        # binomial with vector slice
        n = ivector()
        prob = dvector()
        post_r, out = binomial(rng_R, n=n, p=prob)
        n_val = [1, 2, 3]
        prob_val = [.1, .2, .3]
        size_val = (3, )
        self._compile_and_check([rng_R, n, prob], [out],
                                [rng_R_val, n_val[:-1], prob_val[:-1]],
                                RandomFunction)

        # binomial with explicit size and size implicit in parameters
        # cf. NOTE 1
        post_r, out = binomial(rng_R, n=n, p=prob, size=size_val)
        self._compile_and_check([rng_R, n, prob], [out],
                                [rng_R_val, n_val, prob_val], RandomFunction)

        # normal with vector slice
        avg = dvector()
        std = dvector()
        post_r, out = normal(rng_R, avg=avg, std=std)
        avg_val = [1, 2, 3]
        std_val = [.1, .2, .3]
        size_val = (3, )
        self._compile_and_check([rng_R, avg, std], [out],
                                [rng_R_val, avg_val[:-1], std_val[:-1]],
                                RandomFunction)

        # normal with explicit size and size implicit in parameters
        # cf. NOTE 1
        post_r, out = normal(rng_R, avg=avg, std=std, size=size_val)
        self._compile_and_check([rng_R, avg, std], [out],
                                [rng_R_val, avg_val, std_val], RandomFunction)

        # multinomial with tensor-3 probabilities
        """
コード例 #53
0
ファイル: test_pool.py プロジェクト: adrn/Theano-PyMC
def test_pool2d():
    shps = [
        (1, 12),
        (1, 1, 12),
        (1, 1, 1, 12),
        (1, 1, 2, 2),
        (1, 1, 1, 1),
        (1, 1, 4, 4),
        (1, 1, 10, 11),
        (1, 2, 2, 2),
        (3, 5, 4, 4),
        (25, 1, 7, 7),
        (1, 1, 12, 12),
        (1, 1, 2, 14),
        (1, 1, 12, 14),
        (1, 1, 14, 14),
        (1, 1, 16, 16),
        (1, 1, 18, 18),
        (1, 1, 24, 24),
        (1, 6, 24, 24),
        (10, 1, 24, 24),
        (10, 6, 24, 24),
        (30, 6, 12, 12),
        (30, 2, 24, 24),
        (30, 6, 24, 24),
        (10, 10, 10, 11),
        (1, 1, 10, 1025),
        (1, 1, 10, 1023),
        (1, 1, 1025, 10),
        (1, 1, 1023, 10),
        (3, 2, 16, 16, 16),
        (3, 2, 6, 6, 6, 5),
        (3, 2, 6, 6, 6, 5, 7),
    ]

    np.random.RandomState(utt.fetch_seed()).shuffle(shps)
    test_ws = (2, 2), (3, 2), (1, 1)
    test_st = (2, 2), (3, 2), (1, 1)
    test_mode = ["max", "sum", "average_inc_pad", "average_exc_pad"]

    ref_mode = copy.copy(mode_without_gpu)
    ref_mode.check_py_code = False
    gpu_mode = mode_with_gpu.excluding("cudnn")
    gpu_mode.check_py_code = False

    for shp in shps:
        for mode, ws, st in itertools.product(test_mode, test_ws, test_st):
            if ws[0] > shp[-2] or ws[1] > shp[-1]:
                continue
            for ignore_border, pad in zip((True, False), [(1, 1), (0, 0)]):
                if pad[0] >= ws[0] or pad[1] >= ws[1]:
                    continue
                if mode == "average_exc_pad" and (pad[0] > 0 or pad[1] > 0):
                    continue
                # print('test_pool2d', shp, ws, st, pad, mode, ignore_border)
                ds_op = Pool(ndim=len(ws), mode=mode, ignore_border=ignore_border)

                a = theano.shared(rand(*shp), "a")
                a_pooled = ds_op(tensor.as_tensor_variable(a), ws, st, pad)

                f = theano.function([], a_pooled, mode=gpu_mode)
                f2 = theano.function([], a_pooled, mode=ref_mode)

                assert any(
                    [isinstance(node.op, GpuPool) for node in f.maker.fgraph.toposort()]
                )
                assert any(
                    [isinstance(node.op, Pool) for node in f2.maker.fgraph.toposort()]
                )
                assert np.allclose(f(), f2()), (shp, ws, st, pad, mode, ignore_border)

                a_pooled_grad = tensor.grad(a_pooled.sum(), a)

                g = theano.function([], a_pooled_grad, mode=gpu_mode)
                g2 = theano.function([], a_pooled_grad, mode=ref_mode)

                if mode == "max":
                    gop = GpuMaxPoolGrad
                    gop2 = MaxPoolGrad
                else:
                    gop = GpuAveragePoolGrad
                    gop2 = AveragePoolGrad
                assert any(
                    [isinstance(node.op, gop) for node in g.maker.fgraph.toposort()]
                )
                assert any(
                    [isinstance(node.op, gop2) for node in g2.maker.fgraph.toposort()]
                )

                assert np.allclose(g(), g2()), (shp, ws, st, pad, mode, ignore_border)

                # test rop and grad grad for max pooling
                # for average pooling grad grad is just average pooling grad
                if mode != "max":
                    continue

                ea = theano.shared(rand(*shp), "ea")

                gr = theano.function([], tensor.Rop(a_pooled, a, ea), mode=gpu_mode)
                gr2 = theano.function([], tensor.Rop(a_pooled, a, ea), mode=ref_mode)

                assert any(
                    [
                        isinstance(node.op, GpuDownsampleFactorMaxGradGrad)
                        for node in gr.maker.fgraph.toposort()
                    ]
                )
                assert any(
                    [
                        isinstance(node.op, DownsampleFactorMaxGradGrad)
                        for node in gr2.maker.fgraph.toposort()
                    ]
                )
                assert np.allclose(gr(), gr2()), (shp, ws, st, pad, mode, ignore_border)

                ggf = gradient.Lop(tensor.grad((a_pooled ** 2).sum(), a), a, a)

                gg = theano.function([], ggf, mode=gpu_mode)
                gg2 = theano.function([], ggf, mode=ref_mode)

                assert any(
                    [
                        isinstance(node.op, GpuDownsampleFactorMaxGradGrad)
                        for node in gg.maker.fgraph.toposort()
                    ]
                )
                assert any(
                    [
                        isinstance(node.op, DownsampleFactorMaxGradGrad)
                        for node in gg2.maker.fgraph.toposort()
                    ]
                )
                assert np.allclose(gg(), gg2()), (shp, ws, st, pad, mode, ignore_border)
コード例 #54
0
ファイル: test_sort.py プロジェクト: adrn/Theano-PyMC
 def setup_method(self):
     self.rng = np.random.RandomState(seed=utt.fetch_seed())
     self.m_val = self.rng.rand(3, 2)
     self.v_val = self.rng.rand(4)
コード例 #55
0
ファイル: test_sharedvar.py プロジェクト: adrn/Theano-PyMC
        def test_specify_shape_partial(self):
            dtype = self.dtype
            if dtype is None:
                dtype = theano.config.floatX

            rng = np.random.RandomState(utt.fetch_seed())
            x1_1 = np.asarray(rng.uniform(1, 2, [4, 2]), dtype=dtype)
            x1_1 = self.cast_value(x1_1)
            x1_2 = np.asarray(rng.uniform(1, 2, [4, 2]), dtype=dtype)
            x1_2 = self.cast_value(x1_2)
            x2 = np.asarray(rng.uniform(1, 2, [5, 2]), dtype=dtype)
            x2 = self.cast_value(x2)

            # Test that we can replace with values of the same shape
            x1_shared = self.shared_constructor(x1_1)
            x1_specify_shape = tensor.specify_shape(
                x1_shared,
                (tensor.as_tensor_variable(x1_1.shape[0]), x1_shared.shape[1]),
            )
            x1_shared.set_value(x1_2)
            assert np.allclose(
                self.ref_fct(x1_shared.get_value(borrow=True)), self.ref_fct(x1_2)
            )
            shape_op_fct = theano.function([], x1_shared.shape)
            topo = shape_op_fct.maker.fgraph.toposort()
            shape_op_fct()
            if theano.config.mode != "FAST_COMPILE":
                assert len(topo) == 3
                assert isinstance(topo[0].op, tensor.opt.Shape_i)
                assert isinstance(topo[1].op, tensor.opt.Shape_i)
                assert isinstance(topo[2].op, tensor.opt.MakeVector)

            # Test that we forward the input
            specify_shape_fct = theano.function([], x1_specify_shape)
            specify_shape_fct()
            # theano.printing.debugprint(specify_shape_fct)
            assert np.all(self.ref_fct(specify_shape_fct()) == self.ref_fct(x1_2))
            topo_specify = specify_shape_fct.maker.fgraph.toposort()
            if theano.config.mode != "FAST_COMPILE":
                assert len(topo_specify) == 4

            # Test that we put the shape info into the graph
            shape_constant_fct = theano.function([], x1_specify_shape.shape)
            # theano.printing.debugprint(shape_constant_fct)
            assert np.all(shape_constant_fct() == shape_op_fct())
            topo_cst = shape_constant_fct.maker.fgraph.toposort()
            if theano.config.mode != "FAST_COMPILE":
                assert len(topo_cst) == 2

            # Test that we can replace with values of the different shape
            # but that will raise an error in some case, but not all
            x1_shared.set_value(x2)
            with pytest.raises(AssertionError):
                specify_shape_fct()

            # No assertion will be raised as the Op is removed from the graph
            if theano.config.mode not in ["FAST_COMPILE", "DebugMode", "DEBUG_MODE"]:
                shape_constant_fct()
            else:
                with pytest.raises(AssertionError):
                    shape_constant_fct()
コード例 #56
0
ファイル: test_scan.py プロジェクト: wolverineq/Theano
    def test_one_sequence_one_output_weights_gpu1(self):
        def f_rnn(u_t, x_tm1, W_in, W):
            return u_t * W_in + x_tm1 * W

        u = theano.tensor.fvector('u')
        x0 = theano.tensor.fscalar('x0')
        W_in = theano.tensor.fscalar('win')
        W = theano.tensor.fscalar('w')

        mode = mode_with_gpu.excluding('InputToGpuOptimizer')
        output, updates = theano.scan(f_rnn,
                                      u,
                                      x0, [W_in, W],
                                      n_steps=None,
                                      truncate_gradient=-1,
                                      go_backwards=False,
                                      mode=mode)

        output = gpu_from_host(output)
        f2 = theano.function([u, x0, W_in, W],
                             output,
                             updates=updates,
                             allow_input_downcast=True,
                             mode=mode)

        rng = numpy.random.RandomState(utt.fetch_seed())
        v_u = rng.uniform(size=(4, ), low=-5., high=5.)
        v_x0 = rng.uniform()
        W = rng.uniform()
        W_in = rng.uniform()

        v_u = numpy.asarray(v_u, dtype='float32')
        v_x0 = numpy.asarray(v_x0, dtype='float32')
        W = numpy.asarray(W, dtype='float32')
        W_in = numpy.asarray(W_in, dtype='float32')

        # compute the output in numpy
        v_out = numpy.zeros((4, ))
        v_out[0] = v_u[0] * W_in + v_x0 * W
        for step in xrange(1, 4):
            v_out[step] = v_u[step] * W_in + v_out[step - 1] * W

        theano_values = f2(v_u, v_x0, W_in, W)
        utt.assert_allclose(theano_values, v_out)

        # TO DEL
        topo = f2.maker.fgraph.toposort()
        scan_node = [
            node for node in topo
            if isinstance(node.op, theano.scan_module.scan_op.Scan)
        ]
        assert len(scan_node) == 1
        scan_node = scan_node[0]

        topo = f2.maker.fgraph.toposort()
        assert sum([isinstance(node.op, HostFromGpu) for node in topo]) == 0
        assert sum([isinstance(node.op, GpuFromHost) for node in topo]) == 4

        scan_node = [
            node for node in topo
            if isinstance(node.op, theano.scan_module.scan_op.Scan)
        ]
        assert len(scan_node) == 1
        scan_node = scan_node[0]
        scan_node_topo = scan_node.op.fn.maker.fgraph.toposort()

        # check that there is no gpu transfer in the inner loop.
        assert any(
            [isinstance(node.op, GpuElemwise) for node in scan_node_topo])
        assert not any(
            [isinstance(node.op, HostFromGpu) for node in scan_node_topo])
        assert not any(
            [isinstance(node.op, GpuFromHost) for node in scan_node_topo])
コード例 #57
0
ファイル: test_scan_opt.py プロジェクト: zuiwufenghua/Theano
 def setUp(self):
     self.rng = np.random.RandomState(utt.fetch_seed())
コード例 #58
0
def test_downsample():
    shps = [
        (1, 1, 1, 12),
        (1, 1, 2, 2),
        (1, 1, 1, 1),
        (1, 1, 4, 4),
        (1, 1, 10, 11),
        (1, 2, 2, 2),
        (3, 5, 4, 4),
        (25, 1, 7, 7),
        (1, 1, 12, 12),
        (1, 1, 2, 14),
        (1, 1, 12, 14),
        (1, 1, 14, 14),
        (1, 1, 16, 16),
        (1, 1, 18, 18),
        (1, 1, 24, 24),
        (1, 6, 24, 24),
        (10, 1, 24, 24),
        (10, 6, 24, 24),
        (30, 6, 12, 12),
        (30, 2, 24, 24),
        (30, 6, 24, 24),
        (10, 10, 10, 11),
        (1, 1, 10, 1025),
        (1, 1, 10, 1023),
        (1, 1, 1025, 10),
        (1, 1, 1023, 10),
        (65536, 1, 10, 10),
        (1, 65536, 10, 10),
    ]

    numpy.random.RandomState(unittest_tools.fetch_seed()).shuffle(shps)

    for shp in shps:
        for ds in (2, 2), (3, 2), (1, 1):
            if ds[0] > shp[2]:
                continue
            if ds[1] > shp[3]:
                continue
            # GpuDownsampleFactorMax doesn't like having more than 512 columns
            # in the output tensor.
            if float(shp[3]) / ds[1] > 512:
                continue
            for ignore_border in (True, False):
                # print 'test_downsample', shp, ds, ignore_border
                ds_op = Pool(ignore_border=ignore_border)

                a = tcn.shared_constructor(my_rand(*shp), 'a')
                f = pfunc([],
                          ds_op(tensor.as_tensor_variable(a), ds),
                          mode=mode_with_gpu.excluding('cudnn'))
                f2 = pfunc([],
                           ds_op(tensor.as_tensor_variable(a), ds),
                           mode=mode_without_gpu)
                assert any([
                    isinstance(node.op, tcn.blas.GpuDownsampleFactorMax)
                    for node in f.maker.fgraph.toposort()
                ])
                assert any([
                    isinstance(node.op, Pool)
                    for node in f2.maker.fgraph.toposort()
                ])
                assert numpy.allclose(f(), f2())

                # The grad is too slow on GT220 GPU
                # This cause the computer to freeze...
                # Remove this when it gets optimized enough
                # This only bypass the last 2 checks
                # Those tests where passing in all Mode on a GTX470
                if shp[0] > 30000 or shp[1] > 30000:
                    continue

                g = pfunc([],
                          tensor.grad(
                              ds_op(tensor.as_tensor_variable(a), ds).sum(),
                              a),
                          mode=mode_with_gpu.excluding('cudnn'))
                g2 = pfunc([],
                           tensor.grad(
                               ds_op(tensor.as_tensor_variable(a), ds).sum(),
                               a),
                           mode=mode_without_gpu)
                assert any([
                    isinstance(node.op, tcn.blas.GpuDownsampleFactorMaxGrad)
                    for node in g.maker.fgraph.toposort()
                ])
                assert any([
                    isinstance(node.op, PoolGrad)
                    for node in g2.maker.fgraph.toposort()
                ])
                assert numpy.allclose(g(), g2()), shp

                ggf = gradient.Lop(
                    tensor.grad((ds_op(tensor.as_tensor_variable(a),
                                       ds)**2).sum(), a), a, a)

                ref_mode = copy.copy(mode_without_gpu)
                ref_mode.check_py_code = False
                gpu_mode = copy.copy(mode_with_gpu)
                gpu_mode.check_py_code = False
                gg = pfunc([], ggf, mode=gpu_mode)
                gg2 = pfunc([], ggf, mode=ref_mode)

                assert any([
                    isinstance(node.op,
                               tcn.blas.GpuDownsampleFactorMaxGradGrad)
                    for node in gg.maker.fgraph.toposort()
                ])
                assert any([
                    isinstance(node.op, DownsampleFactorMaxGradGrad)
                    for node in gg2.maker.fgraph.toposort()
                ])
                assert numpy.allclose(gg(), gg2()), shp
コード例 #59
0
def test_naacl_model(iters_per_unsup=3, iters_per_sup=3,
        optimizer=None, realistic=False):
    #print "BUILDING MODEL"
    import time
    t = time.time()

    if optimizer:
        mode = theano.Mode(linker='c|py', optimizer=optimizer)
    else:
        mode = get_default_mode()

    if mode.__class__.__name__ == 'DebugMode':
        iters_per_unsup = 1
        iters_per_sup = 1

    if realistic:
        m = create_realistic(compile_mode=mode)
    else:
        m = create(compile_mode=mode)

    #print 'BUILD took %.3fs'%(time.time() - t)
    prog_str = []
    idx_of_node = {}
    for i, node in enumerate(m.pretraining_update.maker.fgraph.toposort()):
        idx_of_node[node] = i
        if False and i > -1:
            print '   ', i, node, [(ii, idx_of_node.get(ii.
                owner, 'IN')) for ii in node.inputs]
        prog_str.append(str(node))
    #print input_pretraining_gradients[4].owner.inputs
    #print input_pretraining_gradients[4].owner.inputs[1].owner.inputs
    #sys.exit()

    #print "PROGRAM LEN %i HASH %i"% (len(m.pretraining_update.maker.fgraph.apply_nodes), reduce(lambda a, b: hash(a) ^ hash(b),prog_str))

    rng = N.random.RandomState(unittest_tools.fetch_seed(23904))

    inputs = [rng.rand(10, m.input_size) for i in 1, 2, 3]
    targets = N.asarray([0, 3, 4, 2, 3, 4, 4, 2, 1, 0])
    #print inputs

    #print 'UNSUPERVISED PHASE'
    t = time.time()
    for i in xrange(3):
        for j in xrange(iters_per_unsup):
            try:
                known_fail = False
                m.pretraining_update(*inputs)
            except ValueError:
                known_fail = True
            except TypeError:
                known_fail = True
            if known_fail:
                raise KnownFailureTest("Deprecated compile.module fails to "
                    "give a sensible warning when updates to a variable "
                    "have the wrong type")
        s0, s1 = [str(j) for j in m.pretraining_update(*inputs)]
        #print 'huh?', i, iters_per_unsup, iters_per_unsup * (i+1), s0, s1
    if iters_per_unsup == 3:
        assert s0.startswith('0.927793')  # '0.403044')
        assert s1.startswith('0.068035')  # '0.074898')
    #print 'UNSUPERVISED took %.3fs'%(time.time() - t)

    #print 'FINETUNING GRAPH'
    #print 'SUPERVISED PHASE COSTS (%s)'%optimizer
    t = time.time()
    for i in xrange(3):
        for j in xrange(iters_per_unsup):
            m.finetuning_update(*(inputs + [targets]))
        s0 = str(m.finetuning_update(*(inputs + [targets])))
        #print iters_per_sup * (i+1), s0
    if iters_per_sup == 10:
        s0f = float(s0)
        assert 19.7042 < s0f and s0f < 19.7043
コード例 #60
0
    def test_DownsampleFactorMaxStride(self):
        rng = numpy.random.RandomState(utt.fetch_seed())
        maxpoolshps = ((1, 1), (3, 3), (5, 3), (16, 16))
        stridesizes = (
            (1, 1),
            (3, 3),
            (5, 7),
        )
        # generate random images
        imval = rng.rand(4, 10, 16, 16)
        # The same for each mode
        outputshps = (
            (4, 10, 16, 16),
            (4, 10, 6, 6),
            (4, 10, 4, 3),
            (4, 10, 16, 16),
            (4, 10, 6, 6),
            (4, 10, 4, 3),
            (4, 10, 14, 14),
            (4, 10, 5, 5),
            (4, 10, 3, 2),
            (4, 10, 14, 14),
            (4, 10, 6, 6),
            (4, 10, 4, 3),
            (4, 10, 12, 14),
            (4, 10, 4, 5),
            (4, 10, 3, 2),
            (4, 10, 12, 14),
            (4, 10, 5, 6),
            (4, 10, 4, 3),
            (4, 10, 1, 1),
            (4, 10, 1, 1),
            (4, 10, 1, 1),
            (4, 10, 1, 1),
            (4, 10, 1, 1),
            (4, 10, 1, 1),
        )

        images = tensor.dtensor4()
        indx = 0
        for mode, maxpoolshp, ignore_border in product(
            ['max', 'sum', 'average_inc_pad', 'average_exc_pad'], maxpoolshps,
            [True, False]):
            for stride in stridesizes:
                outputshp = outputshps[indx % len(outputshps)]
                indx += 1
                # Pool op
                numpy_output_val = \
                    self.numpy_max_pool_2d_stride(imval, maxpoolshp,
                                                  ignore_border, stride,
                                                  mode)
                assert numpy_output_val.shape == outputshp, (
                    "outshape is %s, calculated shape is %s" %
                    (outputshp, numpy_output_val.shape))
                maxpool_op = \
                    Pool(maxpoolshp,
                         ignore_border=ignore_border,
                         st=stride, mode=mode)(images)
                f = function([images], maxpool_op)
                output_val = f(imval)
                utt.assert_allclose(output_val, numpy_output_val)