<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/util/deferred_tensor.py#L425-L594) |

Variable tracking object which applies a bijector upon `convert_to_tensor`.

Inherits From: [`DeferredTensor`](https://www.tensorflow.org/probability/api_docs/python/tfp/util/DeferredTensor)

    tfp.util.TransformedVariable(
        initial_value, bijector, dtype=None, name=None, **kwargs
    )

### Used in the notebooks

| Used in the tutorials |
|---|
| - [Learnable Distributions Zoo](https://www.tensorflow.org/probability/examples/Learnable_Distributions_Zoo) - [Gaussian Process Regression in TensorFlow Probability](https://www.tensorflow.org/probability/examples/Gaussian_Process_Regression_In_TFP) - [Linear Mixed Effects Models](https://www.tensorflow.org/probability/examples/Linear_Mixed_Effects_Models) - [Bayesian Modeling with Joint Distribution](https://www.tensorflow.org/probability/examples/Modeling_with_JointDistribution) - [Probabilistic PCA](https://www.tensorflow.org/probability/examples/Probabilistic_PCA) |

#### Example

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

    positive_variable = tfp.util.TransformedVariable(1., bijector=tfb.Exp())

    positive_variable
    # ==> <TransformedVariable: dtype=float32, shape=[], fn=exp>

    # Note that the initial value corresponds to the transformed output.
    tf.convert_to_tensor(positive_variable)
    # ==> 1.

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

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

    # It is also possible to assign values to a TransformedVariable
    with tf.control_dependencies([positive_variable.assign_add(2.)]):
      positive_variable
    # ==> 3.

    A common use case for the `TransformedVariable` is to fit constrained
    parameters. E.g.:

    ```python
    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>

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

    opt = tf_keras.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

<br />

<br />

<br />

| ## Args ||
|---|---|
| `initial_value` | A `Tensor`, or Python object convertible to a `Tensor`, which is the initial value for the `TransformedVariable`. The underlying untransformed `tf.Variable` will be initialized with `bijector.inverse(initial_value)`. Can also be a callable with no argument that returns the initial value when called. |
| `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`. |

<br />

<br />

<br />

<br />

| ## Attributes ||
|---|---|
| `also_track` | Additional variables tracked by tf.Module in self.trainable_variables. |
| `bijector` | <br /> <br /> |
| `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. |
| `non_trainable_variables` | Sequence of non-trainable variables owned by this module and its submodules. <br /> > [!NOTE] > **Note:** this method uses reflection to find variables on the current instance and submodules. For performance reasons you may wish to cache the result of calling this method if you don't expect the return value to change. |
| `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 <br /> |
| `trainable_variables` | Sequence of trainable variables owned by this module and its submodules. <br /> > [!NOTE] > **Note:** this method uses reflection to find variables on the current instance and submodules. For performance reasons you may wish to cache the result of calling this method if you don't expect the return value to change. |
| `transform_fn` | Function which characterizes the `Tensor`ization of this object. |
| `variables` | Sequence of variables owned by this module and its submodules. > [!NOTE] > **Note:** this method uses reflection to find variables on the current instance and submodules. For performance reasons you may wish to cache the result of calling this method if you don't expect the return value to change. |

## Methods

### `assign`

    assign(
        value, use_locking=False, name=None, read_value=True
    )

Assigns a new value to the variable.

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

<br />

<br />

<br />

| 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. |

<br />

<br />

<br />

<br />

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

<br />

### `assign_add`

    assign_add(
        delta, use_locking=False, name=None, read_value=True
    )

Adds a value to this variable.

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

<br />

<br />

<br />

| 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. |

<br />

<br />

<br />

<br />

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

<br />

### `assign_sub`

    assign_sub(
        delta, use_locking=False, name=None, read_value=True
    )

Subtracts a value from this variable.

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

<br />

<br />

<br />

| 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. |

<br />

<br />

<br />

<br />

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

<br />

### `numpy`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/util/deferred_tensor.py#L312-L318)

    numpy()

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

### `set_shape`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/util/deferred_tensor.py#L320-L336)

    set_shape(
        shape
    )

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.

<br />

<br />

<br />

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

<br />

<br />

<br />

<br />

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

<br />

### `with_name_scope`

    @classmethod
    with_name_scope(
        method
    )

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.Variable`s and `tf.Tensor`s 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)>

<br />

<br />

<br />

| Args ||
|---|---|
| `method` | The method to wrap. |

<br />

<br />

<br />

<br />

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

<br />

### `__abs__`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/util/deferred_tensor.py#L74-L77)

    __abs__(
        *args, **kwargs
    )

### `__add__`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/util/deferred_tensor.py#L74-L77)

    __add__(
        *args, **kwargs
    )

### `__and__`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/util/deferred_tensor.py#L74-L77)

    __and__(
        *args, **kwargs
    )

### `__array__`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/util/deferred_tensor.py#L373-L378)

    __array__(
        dtype=None
    )

### `__bool__`

    __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.
      # ...

<br />

<br />

<br />

| Raises ||
|---|---|
| `TypeError`. ||

<br />

### `__div__`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/util/deferred_tensor.py#L74-L77)

    __div__(
        *args, **kwargs
    )

### `__floordiv__`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/util/deferred_tensor.py#L74-L77)

    __floordiv__(
        *args, **kwargs
    )

### `__ge__`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/util/deferred_tensor.py#L74-L77)

    __ge__(
        *args, **kwargs
    )

### `__getitem__`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/util/deferred_tensor.py#L74-L77)

    __getitem__(
        *args, **kwargs
    )

### `__gt__`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/util/deferred_tensor.py#L74-L77)

    __gt__(
        *args, **kwargs
    )

### `__invert__`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/util/deferred_tensor.py#L74-L77)

    __invert__(
        *args, **kwargs
    )

### `__iter__`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/util/deferred_tensor.py#L74-L77)

    __iter__(
        *args, **kwargs
    )

### `__le__`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/util/deferred_tensor.py#L74-L77)

    __le__(
        *args, **kwargs
    )

### `__lt__`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/util/deferred_tensor.py#L74-L77)

    __lt__(
        *args, **kwargs
    )

### `__matmul__`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/util/deferred_tensor.py#L74-L77)

    __matmul__(
        *args, **kwargs
    )

### `__mod__`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/util/deferred_tensor.py#L74-L77)

    __mod__(
        *args, **kwargs
    )

### `__mul__`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/util/deferred_tensor.py#L74-L77)

    __mul__(
        *args, **kwargs
    )

### `__neg__`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/util/deferred_tensor.py#L74-L77)

    __neg__(
        *args, **kwargs
    )

### `__nonzero__`

    __nonzero__()

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

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

<br />

<br />

<br />

| Raises ||
|---|---|
| `TypeError`. ||

<br />

### `__or__`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/util/deferred_tensor.py#L74-L77)

    __or__(
        *args, **kwargs
    )

### `__pow__`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/util/deferred_tensor.py#L74-L77)

    __pow__(
        *args, **kwargs
    )

### `__radd__`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/util/deferred_tensor.py#L74-L77)

    __radd__(
        *args, **kwargs
    )

### `__rand__`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/util/deferred_tensor.py#L74-L77)

    __rand__(
        *args, **kwargs
    )

### `__rdiv__`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/util/deferred_tensor.py#L74-L77)

    __rdiv__(
        *args, **kwargs
    )

### `__rfloordiv__`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/util/deferred_tensor.py#L74-L77)

    __rfloordiv__(
        *args, **kwargs
    )

### `__rmatmul__`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/util/deferred_tensor.py#L74-L77)

    __rmatmul__(
        *args, **kwargs
    )

### `__rmod__`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/util/deferred_tensor.py#L74-L77)

    __rmod__(
        *args, **kwargs
    )

### `__rmul__`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/util/deferred_tensor.py#L74-L77)

    __rmul__(
        *args, **kwargs
    )

### `__ror__`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/util/deferred_tensor.py#L74-L77)

    __ror__(
        *args, **kwargs
    )

### `__rpow__`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/util/deferred_tensor.py#L74-L77)

    __rpow__(
        *args, **kwargs
    )

### `__rsub__`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/util/deferred_tensor.py#L74-L77)

    __rsub__(
        *args, **kwargs
    )

### `__rtruediv__`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/util/deferred_tensor.py#L74-L77)

    __rtruediv__(
        *args, **kwargs
    )

### `__rxor__`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/util/deferred_tensor.py#L74-L77)

    __rxor__(
        *args, **kwargs
    )

### `__sub__`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/util/deferred_tensor.py#L74-L77)

    __sub__(
        *args, **kwargs
    )

### `__truediv__`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/util/deferred_tensor.py#L74-L77)

    __truediv__(
        *args, **kwargs
    )

### `__xor__`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/util/deferred_tensor.py#L74-L77)

    __xor__(
        *args, **kwargs
    )