Save the date! Google I/O returns May 18-20 Register now

tfp.util.TransformedVariable

Variable tracking object which applies a bijector upon convert_to_tensor.

Inherits From: DeferredTensor

Used in the notebooks

Used in the tutorials

Example

import tensorflow.compat.v2 as tf
import tensorflow_probability as tfp
tfb = tfp.bijectors
tfd = tfp.distributions

trainable_normal = tfd.Normal(
    loc=tf.Variable(0.),
    scale=tfp.util.TransformedVariable(1., bijector=tfb.Exp()))

trainable_normal.loc
# ==> <tf.Variable 'Variable:0' shape=() dtype=float32, numpy=0.0>

trainable_normal.scale
# ==> <TransformedVariable: dtype=float32, shape=[], fn=exp>

tf.convert_to_tensor(trainable_normal.scale)
# ==> 1.

# Operators work with `TransformedVariable`.
trainable_normal.scale + 1.
# ==> 2.

with tf.GradientTape() as tape:
  negloglik = -trainable_normal.log_prob(0.5)
g = tape.gradient(negloglik, trainable_normal.trainable_variables)
# ==> (-0.5, 0.75)

Which we could then fit as:

opt = tf.optimizers.Adam(learning_rate=0.05)
loss = tf.function(lambda: -trainable_normal.log_prob(0.5))
for _ in range(int(1e3)):
  opt.minimize(loss, trainable_normal.trainable_variables)
trainable_normal.mean()
# ==> 0.5
trainable_normal.stddev()
# ==> (approximately) 0.0075

It is also possible to assign values to a TransformedVariable, e.g.,

d = tfd.Normal(
    loc=tf.Variable(0.),
    scale=tfp.util.TransformedVariable([1., 2.], bijector=tfb.Softplus()))
d.stddev()
# ==> [1., 2.]
with tf.control_dependencies([x.scale.assign_add([0.5, 1.])]):
  d.stddev()
  # ==> [1.5, 3.]

initial_value A Tensor, or Python object convertible to a Tensor, which is the initial value for the Variable. Can also be a callable with no argument that returns the initial value when called. Note: if initial_value is a TransformedVariable then the instantiated object does not create a new tf.Variable, but rather points to the underlying Variable and chains the bijector arg with the underlying bijector as tfb.Chain([bijector, initial_value.bijector]).
bijector A Bijector-like instance which defines the transformations applied to the underlying tf.Variable.
dtype tf.dtype.DType instance or otherwise valid dtype value to tf.convert_to_tensor(..., dtype). Default value: None (i.e., bijector.dtype).
name Python str representing the underlying tf.Variable's name. Default value: None.
**kwargs Keyword arguments forward to tf.Variable.

also_track Additional variables tracked by tf.Module in self.trainable_variables.
bijector

dtype Represents the type of the elements in a Tensor.
initializer The initializer operation for the underlying variable.
name The string name of this object.
name_scope Returns a tf.name_scope instance for this class.
pretransformed_input Input to transform_fn.
shape Represents the shape of a Tensor.
submodules Sequence of all sub-modules.

Submodules are modules which are properties of this module, or found as properties of modules which are properties of this module (and so on).

a = tf.Module()
b = tf.Module()
c = tf.Module()
a.b = b
b.c = c
list(a.submodules) == [b, c]
True
list(b.submodules) == [c]
True
list(c.submodules) == []
True

trainable_variables Sequence of trainable variables owned by this module and its submodules.

transform_fn Function which characterizes the Tensorization of this object.
variables Sequence of variables owned by this module and its submodules.

Methods

assign

Assigns a new value to the variable.

This is essentially a shortcut for assign(self, value).

Args
value A Tensor. The new value for this variable.
use_locking If True, use locking during the assignment.
name The name of the operation to be created
read_value if True, will return something which evaluates to the new value of the variable; if False will return the assign op.

Returns
The updated variable. If read_value is false, instead returns None in Eager mode and the assign op in graph mode.

assign_add

Adds a value to this variable.

This is essentially a shortcut for assign_add(self, delta).

Args
delta A Tensor. The value to add to this variable.
use_locking If True, use locking during the operation.
name The name of the operation to be created
read_value if True, will return something which evaluates to the new value of the variable; if False will return the assign op.

Returns
The updated variable. If read_value is false, instead returns None in Eager mode and the assign op in graph mode.

assign_sub

Subtracts a value from this variable.

This is essentially a shortcut for assign_sub(self, delta).

Args
delta A Tensor. The value to subtract from this variable.
use_locking If True, use locking during the operation.
name The name of the operation to be created
read_value if True, will return something which evaluates to the new value of the variable; if False will return the assign op.

Returns
The updated variable. If read_value is false, instead returns None in Eager mode and the assign op in graph mode.

get_shape

View source

Legacy means of getting Tensor shape, for compat with 2.0.0 LinOp.

numpy

View source

Returns (copy of) deferred values as a NumPy array or scalar.

set_shape

View source

Updates the shape of this pretransformed_input.

This method can be called multiple times, and will merge the given shape with the current shape of this object. It can be used to provide additional information about the shape of this object that cannot be inferred from the graph alone.

Args
shape A TensorShape representing the shape of this pretransformed_input, a TensorShapeProto, a list, a tuple, or None.

Raises
ValueError If shape is not compatible with the current shape of this pretransformed_input.

with_name_scope

Decorator to automatically enter the module name scope.

class MyModule(tf.Module):
  @tf.Module.with_name_scope
  def __call__(self, x):
    if not hasattr(self, 'w'):
      self.w = tf.Variable(tf.random.normal([x.shape[1], 3]))
    return tf.matmul(x, self.w)

Using the above module would produce tf.Variables and tf.Tensors whose names included the module name:

mod = MyModule()
mod(tf.ones([1, 2]))
<tf.Tensor: shape=(1, 3), dtype=float32, numpy=..., dtype=float32)>
mod.w
<tf.Variable 'my_module/Variable:0' shape=(2, 3) dtype=float32,
numpy=..., dtype=float32)>

Args
method The method to wrap.

Returns
The original method wrapped such that it enters the module's name scope.

__abs__

View source

__add__

View source

__and__

View source

__bool__

Dummy method to prevent a tensor from being used as a Python bool.

This overload raises a TypeError when the user inadvertently treats a Tensor as a boolean (most commonly in an if or while statement), in code that was not converted by AutoGraph. For example:

if tf.constant(True):  # Will raise.
  # ...

if tf.constant(5) < tf.constant(7):  # Will raise.
  # ...

Raises
TypeError.

__div__

View source

__floordiv__

View source

__ge__

View source

__getitem__

View source

__gt__

View source

__invert__

View source

__iter__

View source

__le__

View source

__lt__

View source

__matmul__

View source

__mod__

View source

__mul__

View source

__neg__

View source

__nonzero__

Dummy method to prevent a tensor from being used as a Python bool.

This is the Python 2.x counterpart to __bool__() above.

Raises
TypeError.

__or__

View source

__pow__

View source

__radd__

View source

__rand__

View source

__rdiv__

View source

__rfloordiv__

View source

__rmatmul__

View source

__rmod__

View source

__rmul__

View source

__ror__

View source

__rpow__

View source

__rsub__

View source

__rtruediv__

View source

__rxor__

View source

__sub__

View source

__truediv__

View source

__xor__

View source