def test_gbm_euler_step_nd_is_deterministic(self): drift = np.asarray([0.1, 0.3, -0.05], dtype=np.float32) vol_matrix = 0.2 * np.asarray( [[1.5, 0.2, 0.3], [0.2, 1.1, -0.1], [0.3, -0.1, 0.8]], dtype=np.float32) t = 0.3 dt = 0.01 num_samples = 8 num_dims = drift.shape[0] key = 42 states = tf.ones([num_samples, num_dims]) eps_t = contrib_stateless.stateless_random_normal( shape=[num_samples, num_dims], seed=[key, int(t / dt)]) next_states = dynamics.gbm_euler_step_nd( states, drift, vol_matrix, t, dt, random_normal_op=lambda: eps_t) next_states_bis = dynamics.gbm_euler_step_nd(states, drift, vol_matrix, t, dt, key=key) with self.session() as session: next_states_eval, next_states_bis_eval = session.run( (next_states, next_states_bis)) self.assertEqual(next_states_eval.shape, (num_samples, num_dims)) self.assertEqual(next_states_bis_eval.shape, (num_samples, num_dims)) self.assertAllClose(next_states_eval, next_states_bis_eval)
def test_gbm_log_euler_step_is_deterministic(self): drift = 0.2 vol = 0.1 t = 0.0 dt = 0.01 num_samples = 8 key = 13 log_states = tf.zeros([num_samples]) eps_t = contrib_stateless.stateless_random_normal( shape=[num_samples], seed=[key, int(t / dt)]) next_log_states = dynamics.gbm_log_euler_step( log_states, drift, vol, t, dt, random_normal_op=lambda: eps_t) next_log_states_bis = dynamics.gbm_log_euler_step(log_states, drift, vol, t, dt, key=key) with self.session() as session: next_log_states_eval, next_log_states_bis_eval = session.run( (next_log_states, next_log_states_bis)) self.assertEqual(next_log_states_eval.shape, (num_samples, )) self.assertEqual(next_log_states_bis_eval.shape, (num_samples, )) self.assertAllClose(next_log_states_eval, next_log_states_bis_eval)
def test_gbm_euler_step_running_sum_is_deterministic(self): drift = 0.2 vol = 0.1 t = 0.2 dt = 0.01 num_samples = 8 key = 1337 states_and_sums = [tf.ones([num_samples])] * 2 eps_t = contrib_stateless.stateless_random_normal( shape=[num_samples], seed=[key, int(t / dt)]) next_states_and_sums = dynamics.gbm_euler_step_running_sum( states_and_sums, drift, vol, t, dt, random_normal_op=lambda: eps_t) next_states_and_sums_bis = dynamics.gbm_euler_step_running_sum( states_and_sums, drift, vol, t, dt, key=key) with self.session() as session: next_states_and_sums_eval, next_states_and_sums_bis_eval = session.run( (next_states_and_sums, next_states_and_sums_bis)) next_states_eval, next_sums_eval = next_states_and_sums_eval next_states_bis_eval, next_sums_bis_eval = next_states_and_sums_bis_eval self.assertEqual(next_states_eval.shape, (num_samples, )) self.assertEqual(next_states_bis_eval.shape, (num_samples, )) self.assertEqual(next_sums_eval.shape, (num_samples, )) self.assertEqual(next_sums_bis_eval.shape, (num_samples, )) self.assertAllClose(next_states_eval, next_states_bis_eval) self.assertAllClose(next_sums_eval, next_sums_bis_eval)
def testRandomNormalIsFinite(self): with self.cached_session() as sess, self.test_scope(): for dtype in self._random_types(): seed_t = array_ops.placeholder(dtypes.int32, shape=[2]) x = stateless.stateless_random_normal( shape=[10000], seed=seed_t, dtype=dtype) y = sess.run(x, {seed_t: [0x12345678, 0xabcdef12]}) self.assertTrue(np.all(np.isfinite(y)))
def _draw_n_gaussian_samples(n, seed=None): """ Draws n gaussian samples. """ if seed is None: return tf.random_normal([n], dtype=settings.float_type) else: return stateless.stateless_random_normal([n], dtype=settings.float_type, seed=seed)
def testDistributionOfStatelessRandomNormal(self): """Use Anderson-Darling test to test distribution appears normal.""" with self.test_session() as sess, self.test_scope(): for dtype in self._random_types(): seed_t = array_ops.placeholder(dtypes.int32, shape=[2]) n = 1000 x = stateless.stateless_random_normal( shape=[n], seed=seed_t, dtype=dtype) y = sess.run(x, {seed_t: [25252, 314159]}) # The constant 2.492 is the 5% critical value for the Anderson-Darling # test where the mean and variance are known. This test is probabilistic # so to avoid flakiness the seed is fixed. self.assertTrue(self._anderson_darling(y) < 2.492)
def distort_image(images, seed): """Perform random distortions on a batch of images. Args: images: A float32 Tensor of shape [batch_size, height, width, 3] with values in [0, 1). seed: Tensor (scalar) for seeding the order of the random pertubations. Returns: distorted_image: A float32 Tensor of shape [height, width, 3] with values in [0, 1]. """ color_ordering = contrib_stateless.stateless_random_normal( shape=images.shape[0:1], seed=tf.cast(tf.stack([0, seed]), tf.int32), dtype=tf.float32) # random flip doesn't work on a batch, and running it inside of a map_fn # triggers a memory error; skip it for now: we could alternatively move it # to the CPU. # # image = tf.image.random_flip_left_right(image) with tf.name_scope("distort_color", values=[images]): def _a(image): image = tf.image.random_brightness(image, max_delta=32. / 255.) image = tf.image.random_saturation(image, lower=0.5, upper=1.5) image = tf.image.random_hue(image, max_delta=0.032) image = tf.image.random_contrast(image, lower=0.5, upper=1.5) return image def _b(image): image = tf.image.random_brightness(image, max_delta=32. / 255.) image = tf.image.random_contrast(image, lower=0.5, upper=1.5) image = tf.image.random_saturation(image, lower=0.5, upper=1.5) image = tf.image.random_hue(image, max_delta=0.032) return image images = tf.where( tf.less(color_ordering, 0), _a(images), _b(images) # tf.map_fn(_a, images), # tf.map_fn(_b, images) ) # The random_* ops do not necessarily clamp. images = tf.clip_by_value(images, 0.0, 1.0) return images
def _draw_n_sparse_gaussian_samples(n, s, seed=None): """ Draws n sparse gaussian samples, that is with P(X = N(0,1)) = 1/s, P(X = 0) = 1 - 1/s. """ s = tf.cast(s, settings.float_type) if seed is None: return tf.where( tf.random_uniform([n], dtype=settings.float_type) <= 1. / s, tf.random_normal([n], dtype=settings.float_type), tf.zeros([n], dtype=settings.float_type)) else: return tf.where( stateless.stateless_random_uniform( [n], dtype=settings.float_type, seed=seed) <= 1. / s, stateless.stateless_random_normal([n], dtype=settings.float_type, seed=seed), tf.zeros([n], dtype=settings.float_type))
def _random_normal(shape, i, key=0): return contrib_stateless.stateless_random_normal(shape=shape, seed=_prng_key(i, key))