示例#1
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)
    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)
    def test_setitem(self):

        m = Module()
        m.random = RandomStreams(234)
        out = m.random.uniform((2, 2))
        m.fn = Method([], out)
        made = m.make()

        #as a distraction, install various seeds
        made.random.initialize(seed=789)
        made.random.seed(888)

        # then replace the rng of the stream we care about via setitem
        realseed = utt.fetch_seed()
        rng = numpy.random.RandomState(realseed)
        made.random[out.rng] = numpy.random.RandomState(realseed)

        print made.fn()
        print rng.uniform(size=(2, 2))

        fn_val0 = made.fn()
        fn_val1 = made.fn()
        numpy_val0 = rng.uniform(size=(2, 2))
        numpy_val1 = rng.uniform(size=(2, 2))
        assert numpy.allclose(fn_val0, numpy_val0)
        assert numpy.allclose(fn_val1, numpy_val1)
示例#4
0
    def test_setitem(self):

        m = Module()
        m.random = RandomStreams(234)
        out = m.random.uniform((2,2))
        m.fn = Method([], out)
        made = m.make()

        #as a distraction, install various seeds
        made.random.initialize(seed=789)
        made.random.seed(888)

        # then replace the rng of the stream we care about via setitem
        realseed = utt.fetch_seed()
        rng = numpy.random.RandomState(realseed)
        made.random[out.rng] = numpy.random.RandomState(realseed)

        print made.fn()
        print rng.uniform(size=(2,2))

        fn_val0 = made.fn()
        fn_val1 = made.fn()
        numpy_val0 = rng.uniform(size=(2,2))
        numpy_val1 = rng.uniform(size=(2,2))
        assert numpy.allclose(fn_val0, numpy_val0)
        assert numpy.allclose(fn_val1, numpy_val1)
    def test_multiple(self):
        M = Module()
        M.random = RandomStreams(utt.fetch_seed())
        out = M.random.uniform((2, 2))
        M.m2 = Module()
        M.m2.random = M.random
        out2 = M.m2.random.uniform((2, 2))
        M.fn = Method([], out)
        M.m2.fn2 = Method([], out2)
        m = M.make()
        m.random.initialize()
        m.m2.initialize()

        assert m.random is m.m2.random
示例#6
0
    def test_multiple(self):
        M = Module()
        M.random = RandomStreams(utt.fetch_seed())
        out = M.random.uniform((2,2))
        M.m2 = Module()
        M.m2.random = M.random
        out2 = M.m2.random.uniform((2,2))
        M.fn = Method([], out)
        M.m2.fn2 = Method([], out2)
        m = M.make()
        m.random.initialize()
        m.m2.initialize()

        assert m.random is m.m2.random
    def test_multinomial(self):
        """Test that RandomStreams.multinomial generates the same results as numpy"""
        # Check over two calls to see if the random state is correctly updated.
        m = Module()
        m.random = RandomStreams(utt.fetch_seed())
        m.fn = Method([], m.random.multinomial((20, 20), 1, [0.1] * 10))

        made = m.make()
        made.random.initialize()
        fn_val0 = made.fn()
        fn_val1 = made.fn()

        rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30)
        rng = numpy.random.RandomState(int(rng_seed))  #int() is for 32bit
        numpy_val0 = rng.multinomial(1, [0.1] * 10, size=(20, 20))
        numpy_val1 = rng.multinomial(1, [0.1] * 10, size=(20, 20))

        assert numpy.all(fn_val0 == numpy_val0)
        assert numpy.all(fn_val1 == numpy_val1)
    def test_normal(self):
        """Test that RandomStreams.normal generates the same results as numpy"""
        # Check over two calls to see if the random state is correctly updated.
        m = Module()
        m.random = RandomStreams(utt.fetch_seed())
        m.fn = Method([], m.random.normal((2, 2), -1, 2))

        made = m.make()
        made.random.initialize()
        fn_val0 = made.fn()
        fn_val1 = made.fn()

        rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30)
        rng = numpy.random.RandomState(int(rng_seed))  #int() is for 32bit
        numpy_val0 = rng.normal(-1, 2, size=(2, 2))
        numpy_val1 = rng.normal(-1, 2, size=(2, 2))

        assert numpy.allclose(fn_val0, numpy_val0)
        assert numpy.allclose(fn_val1, numpy_val1)
示例#9
0
    def test_multinomial(self):
        """Test that RandomStreams.multinomial generates the same results as numpy"""
        # Check over two calls to see if the random state is correctly updated.
        m = Module()
        m.random = RandomStreams(utt.fetch_seed())
        m.fn = Method([], m.random.multinomial((20,20), 1, [0.1]*10))

        made = m.make()
        made.random.initialize()
        fn_val0 = made.fn()
        fn_val1 = made.fn()

        rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30)
        rng = numpy.random.RandomState(int(rng_seed)) #int() is for 32bit
        numpy_val0 = rng.multinomial(1, [0.1]*10, size=(20,20))
        numpy_val1 = rng.multinomial(1, [0.1]*10, size=(20,20))

        assert numpy.all(fn_val0 == numpy_val0)
        assert numpy.all(fn_val1 == numpy_val1)
示例#10
0
    def test_normal(self):
        """Test that RandomStreams.normal generates the same results as numpy"""
        # Check over two calls to see if the random state is correctly updated.
        m = Module()
        m.random = RandomStreams(utt.fetch_seed())
        m.fn = Method([], m.random.normal((2,2), -1, 2))

        made = m.make()
        made.random.initialize()
        fn_val0 = made.fn()
        fn_val1 = made.fn()

        rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30)
        rng = numpy.random.RandomState(int(rng_seed)) #int() is for 32bit
        numpy_val0 = rng.normal(-1, 2, size=(2,2))
        numpy_val1 = rng.normal(-1, 2, size=(2,2))

        assert numpy.allclose(fn_val0, numpy_val0)
        assert numpy.allclose(fn_val1, numpy_val1)
    def test_getitem(self):

        m = Module()
        m.random = RandomStreams(234)
        out = m.random.uniform((2, 2))
        m.fn = Method([], out)
        made = m.make()
        made.random.initialize(seed=789)

        made.random.seed(utt.fetch_seed())

        rng = numpy.random.RandomState()
        rng.set_state(made.random[out.rng].get_state())

        fn_val0 = made.fn()
        fn_val1 = made.fn()
        numpy_val0 = rng.uniform(size=(2, 2))
        numpy_val1 = rng.uniform(size=(2, 2))
        assert numpy.allclose(fn_val0, numpy_val0)
        assert numpy.allclose(fn_val1, numpy_val1)
    def test_seed_in_initialize(self):
        m = Module()
        m.random = RandomStreams(234)
        m.fn = Method([], m.random.uniform((2, 2)))
        made = m.make()
        made.random.initialize(seed=utt.fetch_seed())

        fn_val0 = made.fn()
        fn_val1 = made.fn()

        rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30)
        rng = numpy.random.RandomState(int(rng_seed))  #int() is for 32bit

        #print fn_val0
        numpy_val0 = rng.uniform(size=(2, 2))
        numpy_val1 = rng.uniform(size=(2, 2))
        #print numpy_val0

        assert numpy.allclose(fn_val0, numpy_val0)
        assert numpy.allclose(fn_val1, numpy_val1)
示例#13
0
    def test_getitem(self):

        m = Module()
        m.random = RandomStreams(234)
        out = m.random.uniform((2,2))
        m.fn = Method([], out)
        made = m.make()
        made.random.initialize(seed=789)

        made.random.seed(utt.fetch_seed())

        rng = numpy.random.RandomState()
        rng.set_state(made.random[out.rng].get_state())

        fn_val0 = made.fn()
        fn_val1 = made.fn()
        numpy_val0 = rng.uniform(size=(2,2))
        numpy_val1 = rng.uniform(size=(2,2))
        assert numpy.allclose(fn_val0, numpy_val0)
        assert numpy.allclose(fn_val1, numpy_val1)
示例#14
0
    def test_seed_in_initialize(self):
        m = Module()
        m.random = RandomStreams(234)
        m.fn = Method([], m.random.uniform((2,2)))
        made = m.make()
        made.random.initialize(seed=utt.fetch_seed())

        fn_val0 = made.fn()
        fn_val1 = made.fn()

        rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30)
        rng = numpy.random.RandomState(int(rng_seed))  #int() is for 32bit

        #print fn_val0
        numpy_val0 = rng.uniform(size=(2,2))
        numpy_val1 = rng.uniform(size=(2,2))
        #print numpy_val0

        assert numpy.allclose(fn_val0, numpy_val0)
        assert numpy.allclose(fn_val1, numpy_val1)
    def test_permutation(self):
        """Test that RandomStreams.permutation generates the same results as numpy"""
        # Check over two calls to see if the random state is correctly updated.
        m = Module()
        m.random = RandomStreams(utt.fetch_seed())
        m.fn = Method([], m.random.permutation((20, ), 10))

        made = m.make()
        made.random.initialize()
        fn_val0 = made.fn()
        fn_val1 = made.fn()

        rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30)
        rng = numpy.random.RandomState(int(rng_seed))  #int() is for 32bit

        # rng.permutation outputs one vector at a time, so we iterate.
        numpy_val0 = numpy.asarray([rng.permutation(10) for i in range(20)])
        numpy_val1 = numpy.asarray([rng.permutation(10) for i in range(20)])

        assert numpy.all(fn_val0 == numpy_val0)
        assert numpy.all(fn_val1 == numpy_val1)
示例#16
0
    def test_permutation(self):
        """Test that RandomStreams.permutation generates the same results as numpy"""
        # Check over two calls to see if the random state is correctly updated.
        m = Module()
        m.random = RandomStreams(utt.fetch_seed())
        m.fn = Method([], m.random.permutation((20,), 10))

        made = m.make()
        made.random.initialize()
        fn_val0 = made.fn()
        fn_val1 = made.fn()

        rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30)
        rng = numpy.random.RandomState(int(rng_seed)) #int() is for 32bit

        # rng.permutation outputs one vector at a time, so we iterate.
        numpy_val0 = numpy.asarray([rng.permutation(10) for i in range(20)])
        numpy_val1 = numpy.asarray([rng.permutation(10) for i in range(20)])

        assert numpy.all(fn_val0 == numpy_val0)
        assert numpy.all(fn_val1 == numpy_val1)