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))
def test_infer_shape_matrix(self): if not self.expect_success: raise utt.SkipTest('Requires numpy >= 1.13') # Testing the infer_shape with a matrix. x = theano.tensor.matrix() ops = [ self.op_class(*args, **kwargs) for args, kwargs in self.ops_pars ] for op in ops: if not op.return_inverse: continue if op.return_index: f = op(x)[2] else: f = op(x)[1] self._compile_and_check([x], [f], [ np.asarray(np.array([[2, 1], [3, 2], [2, 1]]), dtype=config.floatX) ], self.op_class)
def test_choice(self): """Test that RandomStreams.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. random = RandomStreams(utt.fetch_seed()) fn = function([], random.choice((11, 8), 10, 1, 0)) fn_val0 = fn() fn_val1 = 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.choice(10, (11, 8), True, None) numpy_val1 = rng.choice(10, (11, 8), True, None) assert numpy.all(fn_val0 == numpy_val0) assert numpy.all(fn_val1 == numpy_val1)
def test_basic_matrix(self): if not self.expect_success: raise utt.SkipTest('Requires numpy >= 1.13') # Basic test for a matrix. # Done by using the op and checking that it returns the right # answer. x = theano.tensor.matrix() ops = [ self.op_class(*args, **kwargs) for args, kwargs in self.ops_pars ] inp = np.asarray([[2, 1], [3, 2], [2, 1]], dtype=config.floatX) list_outs_expected = [[np.unique(inp, **kwargs)] if len(args) == 0 else np.unique(inp, *args, **kwargs) for args, kwargs in self.ops_pars] for op, outs_expected in zip(ops, list_outs_expected): f = theano.function(inputs=[x], outputs=op(x, return_list=True)) outs = f(inp) # Compare the result computed to the expected value. for out, out_exp in zip(outs, outs_expected): utt.assert_allclose(out, out_exp)