def test_mixture_dev(self): mixture_weights = np.array([ [1.0/3, 1.0/3, 1.0/3], [0.750, 0.250, 0.000] ]) component_means = np.array([ [1.0, 1.0, 1.0], [-5, 0, 1.25] ]) component_devs = np.array([ [1.0, 1.0, 1.0], [0.01, 2.0, 0.1] ]) # The first case should trivially have a standard deviation of 1.0 because # all components are identical and have that standard deviation. # The second case was computed by hand. expected_devs = np.array([ 1.0, 2.3848637277 ]) weights_tf = tf.constant(mixture_weights) means_tf = tf.constant(component_means) sigmas_tf = tf.constant(component_devs) mix_dev = distribution_util.mixture_stddev(weights_tf, means_tf, sigmas_tf) self.assertAllClose(expected_devs, self.evaluate(mix_dev))
def test_reproduce_bug_169359285(self): mixture_weight_vector = [ [3.46246948e-10, 1.91437426e-08, 0.999999881, 7.18619435e-12, 2.2613047e-09, 1.99994091e-10, 1.10241352e-07, 3.18231095e-08]] mean_vector = [ [823.835449, -897.290955, 2554.36426, -2.47223473, 1082.69666, 504.688751, 1291.00989, 954.730896]] stddev_vector = [ [0.000924979395, 0.000693350448, 0.00416708598, 5.63207436, 0.000353494543, 0.00704913, 0.0127898213, 0.00763763487]] mixture_weight_vector = tf.convert_to_tensor(mixture_weight_vector) mean_vector = tf.convert_to_tensor(mean_vector) stddev_vector = tf.convert_to_tensor(stddev_vector) result = self.evaluate(distribution_util.mixture_stddev( mixture_weight_vector, mean_vector, stddev_vector)) self.assertAllClose( np.array([0.70164], dtype=np.float32), result, rtol=1e-5)
def _stddev(self): distribution_means = [d.mean() for d in self.components] distribution_devs = [d.stddev() for d in self.components] cat_probs = self._cat_probs(log_probs=False) stacked_means = tf.stack(distribution_means, axis=-1) stacked_devs = tf.stack(distribution_devs, axis=-1) cat_probs = [self._expand_to_event_rank(c_p) for c_p in cat_probs] broadcasted_cat_probs = (tf.stack(cat_probs, axis=-1) * tf.ones_like(stacked_means)) batched_dev = distribution_util.mixture_stddev( tf.reshape(broadcasted_cat_probs, [-1, len(self.components)]), tf.reshape(stacked_means, [-1, len(self.components)]), tf.reshape(stacked_devs, [-1, len(self.components)])) # I.e. re-shape to list(batch_shape) + list(event_shape). return tf.reshape(batched_dev, tf.shape(broadcasted_cat_probs)[:-1])
def _stddev(self): with tf.control_dependencies(self._assertions): distribution_means = [d.mean() for d in self.components] distribution_devs = [d.stddev() for d in self.components] cat_probs = self._cat_probs(log_probs=False) stacked_means = tf.stack(distribution_means, axis=-1) stacked_devs = tf.stack(distribution_devs, axis=-1) cat_probs = [self._expand_to_event_rank(c_p) for c_p in cat_probs] broadcasted_cat_probs = ( tf.stack(cat_probs, axis=-1) * tf.ones_like(stacked_means)) batched_dev = distribution_utils.mixture_stddev( tf.reshape(broadcasted_cat_probs, [-1, len(self.components)]), tf.reshape(stacked_means, [-1, len(self.components)]), tf.reshape(stacked_devs, [-1, len(self.components)])) # I.e. re-shape to list(batch_shape) + list(event_shape). return tf.reshape(batched_dev, tf.shape(broadcasted_cat_probs)[:-1])