# Stochastic Tensor Value Types

### class tf.contrib.bayesflow.stochastic_tensor.SampleValue

Draw n samples along a new outer dimension.

This ValueType draws n samples from StochasticTensors run within its context, increasing the rank by one along a new outer dimension.

Example:

mu = tf.zeros((2,3))
sigma = tf.ones((2, 3))
with sg.value_type(sg.SampleValue(n=4)):
dt = sg.DistributionTensor(
distributions.Normal, mu=mu, sigma=sigma)
# draws 4 samples each with shape (2, 3) and concatenates
assertEqual(dt.value().get_shape(), (4, 2, 3))


#### tf.contrib.bayesflow.stochastic_tensor.SampleValue.__init__(n=1, stop_gradient=False) {:#SampleValue.init}

Sample n times and concatenate along a new outer dimension.

##### Args:
• n: A python integer or int32 tensor. The number of samples to take.
• stop_gradient: If True, StochasticTensors' values are wrapped in stop_gradient, to avoid backpropagation through.

### class tf.contrib.bayesflow.stochastic_tensor.SampleAndReshapeValue

Ask the StochasticTensor for n samples and reshape the result.

Sampling from a StochasticTensor increases the rank of the value by 1 (because each sample represents a new outer dimension).

This ValueType requests n samples from StochasticTensors run within its context that the outer two dimensions are reshaped to intermix the samples with the outermost (usually batch) dimension.

Example:

# mu and sigma are both shaped (2, 3)
mu = [[0.0, -1.0, 1.0], [0.0, -1.0, 1.0]]
sigma = tf.constant([[1.1, 1.2, 1.3], [1.1, 1.2, 1.3]])

with sg.value_type(sg.SampleAndReshapeValue(n=2)):
dt = sg.DistributionTensor(
distributions.Normal, mu=mu, sigma=sigma)

# sample(2) creates a (2, 2, 3) tensor, and the two outermost dimensions
# are reshaped into one: the final value is a (4, 3) tensor.
dt_value = dt.value()
assertEqual(dt_value.get_shape(), (4, 3))

dt_value_val = sess.run([dt_value])[0]  # or e.g. run([tf.identity(dt)])[0]
assertEqual(dt_value_val.shape, (4, 3))


#### tf.contrib.bayesflow.stochastic_tensor.SampleAndReshapeValue.__init__(n=1, stop_gradient=False) {:#SampleAndReshapeValue.init}

Sample n times and reshape the outer 2 axes so rank does not change.

##### Args:
• n: A python integer or int32 tensor. The number of samples to take.
• stop_gradient: If True, StochasticTensors' values are wrapped in stop_gradient, to avoid backpropagation through.

### tf.contrib.bayesflow.stochastic_tensor.value_type(dist_value_type)

Creates a value type context for any StochasticTensor created within.

Typical usage:

with sg.value_type(sg.MeanValue(stop_gradients=True)):
dt = sg.DistributionTensor(distributions.Normal, mu=mu, sigma=sigma)


In the example above, dt.value() (or equivalently, tf.identity(dt)) will be the mean value of the Normal distribution, i.e., mu (possibly broadcasted to the shape of sigma). Furthermore, because the MeanValue was marked with stop_gradients=True, this value will have been wrapped in a stop_gradients call to disable any possible backpropagation.

##### Args:
• dist_value_type: An instance of MeanValue, SampleAndReshapeValue, or any other stochastic value type.
##### Yields:

A context for StochasticTensor objects that controls the value created when they are initialized.

##### Raises:
• TypeError: if dist_value_type is not an instance of a stochastic value type.