Exemple #1
0
 def f(x1, x2):
     if x1.dtype == dtypes.bool:
         assert x2.dtype == dtypes.bool
         float_ = np_dtypes.default_float_type()
         x1 = math_ops.cast(x1, float_)
         x2 = math_ops.cast(x2, float_)
     if not np_dtypes.is_allow_float64():
         # math_ops.truediv in Python3 produces float64 when both inputs are int32
         # or int64. We want to avoid that when is_allow_float64() is False.
         x1, x2 = _avoid_float64(x1, x2)
     return math_ops.truediv(x1, x2)
Exemple #2
0
 def _test(self, *args, **kw_args):
     onp_dtype = kw_args.pop('onp_dtype', None)
     allow_float64 = kw_args.pop('allow_float64', True)
     old_allow_float64 = np_dtypes.is_allow_float64()
     np_dtypes.set_allow_float64(allow_float64)
     old_func = getattr(self, 'onp_func', None)
     # TODO(agarwal): Note that onp can return a scalar type while np returns
     # ndarrays. Currently np does not support scalar types.
     self.onp_func = lambda *args, **kwargs: onp.asarray(  # pylint: disable=g-long-lambda
         old_func(*args, **kwargs))
     np_out = self.np_func(*args, **kw_args)
     onp_out = onp.asarray(self.onp_func(*args, **kw_args))
     if onp_dtype is not None:
         onp_out = onp_out.astype(onp_dtype)
     self.assertEqual(np_out.shape, onp_out.shape)
     self.assertEqual(np_out.dtype, onp_out.dtype)
     np_dtypes.set_allow_float64(old_allow_float64)
Exemple #3
0
        def run_test(*args):
            num_samples = 1000
            tol = 0.1  # High tolerance to keep the # of samples low else the test
            # takes a long time to run.
            np_random.seed(10)
            outputs = [np_random.randn(*args) for _ in range(num_samples)]

            # Test output shape.
            for output in outputs:
                self.assertEqual(output.shape, tuple(args))
                default_dtype = (np.float64 if np_dtypes.is_allow_float64()
                                 else np.float32)
                self.assertEqual(output.dtype.as_numpy_dtype, default_dtype)

            if np.prod(args):  # Don't bother with empty arrays.
                outputs = [output.tolist() for output in outputs]

                # Test that the properties of normal distribution are satisfied.
                mean = np.mean(outputs, axis=0)
                stddev = np.std(outputs, axis=0)
                self.assertAllClose(mean, np.zeros(args), atol=tol)
                self.assertAllClose(stddev, np.ones(args), atol=tol)

                # Test that outputs are different with different seeds.
                np_random.seed(20)
                diff_seed_outputs = [
                    np_random.randn(*args).tolist() for _ in range(num_samples)
                ]
                self.assertNotAllClose(outputs, diff_seed_outputs)

                # Test that outputs are the same with the same seed.
                np_random.seed(10)
                same_seed_outputs = [
                    np_random.randn(*args).tolist() for _ in range(num_samples)
                ]
                self.assertAllClose(outputs, same_seed_outputs)