View source on GitHub |
Variable tracking object which applies a bijector upon convert_to_tensor
.
Inherits From: DeferredTensor
tfp.util.TransformedVariable(
initial_value, bijector, dtype=None, name=None, **kwargs
)
Used in the notebooks
Used in the tutorials |
---|
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.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
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)
.
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
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)
.
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
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)
.
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.
|
numpy
numpy()
Returns (copy of) deferred values as a NumPy array or scalar.
set_shape
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.
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
@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)>
Args | |
---|---|
method
|
The method to wrap. |
Returns | |
---|---|
The original method wrapped such that it enters the module's name scope. |
__abs__
__abs__(
*args, **kwargs
)
__add__
__add__(
*args, **kwargs
)
__and__
__and__(
*args, **kwargs
)
__array__
__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.
# ...
Raises | |
---|---|
TypeError .
|
__div__
__div__(
*args, **kwargs
)
__floordiv__
__floordiv__(
*args, **kwargs
)
__ge__
__ge__(
*args, **kwargs
)
__getitem__
__getitem__(
*args, **kwargs
)
__gt__
__gt__(
*args, **kwargs
)
__invert__
__invert__(
*args, **kwargs
)
__iter__
__iter__(
*args, **kwargs
)
__le__
__le__(
*args, **kwargs
)
__lt__
__lt__(
*args, **kwargs
)
__matmul__
__matmul__(
*args, **kwargs
)
__mod__
__mod__(
*args, **kwargs
)
__mul__
__mul__(
*args, **kwargs
)
__neg__
__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.
Raises | |
---|---|
TypeError .
|
__or__
__or__(
*args, **kwargs
)
__pow__
__pow__(
*args, **kwargs
)
__radd__
__radd__(
*args, **kwargs
)
__rand__
__rand__(
*args, **kwargs
)
__rdiv__
__rdiv__(
*args, **kwargs
)
__rfloordiv__
__rfloordiv__(
*args, **kwargs
)
__rmatmul__
__rmatmul__(
*args, **kwargs
)
__rmod__
__rmod__(
*args, **kwargs
)
__rmul__
__rmul__(
*args, **kwargs
)
__ror__
__ror__(
*args, **kwargs
)
__rpow__
__rpow__(
*args, **kwargs
)
__rsub__
__rsub__(
*args, **kwargs
)
__rtruediv__
__rtruediv__(
*args, **kwargs
)
__rxor__
__rxor__(
*args, **kwargs
)
__sub__
__sub__(
*args, **kwargs
)
__truediv__
__truediv__(
*args, **kwargs
)
__xor__
__xor__(
*args, **kwargs
)