Store weights and other values updated by an optimizer.
Model parameters
TensorFlow · Class
tf.Variable represents mutable TensorFlow state backed by a tensor. Its initial value establishes the variable's dtype and shape, and assign methods update its value.
tf.Variable stores mutable TensorFlow state. Its initial value establishes the variable's dtype and shape; later updates use assignment methods, while TensorFlow can track trainable variables for optimization and capture variables inside tf.function.
tf.Variable(
initial_value=None,
trainable=None,
validate_shape=True,
caching_device=None,
name=None,
variable_def=None,
dtype=None,
import_scope=None,
constraint=None,
synchronization=tf.VariableSynchronization.AUTO,
aggregation=tf.compat.v1.VariableAggregation.NONE,
shape=None,
experimental_enable_variable_lifting=True
)| Name | Type | Description |
|---|---|---|
initial_value | Tensor or Python object convertible to Tensor | Initial Tensor or Python-convertible value; a zero-argument callable is also accepted. |
trainable | bool or None | Whether the variable is trainable; defaults to True. |
validate_shape | bool | Whether to validate the initial shape; defaults to True. |
name | str or None | Optional variable name, automatically uniquified when omitted. |
dtype | tf.dtypes.DType or None | Optional dtype to which the initial value is converted. |
shape | tf.TensorShape or None | Optional variable shape; when omitted, the initial value's shape is used. |
synchronization | tf.VariableSynchronization | Synchronization policy for distributed variables. |
aggregation | tf.compat.v1.VariableAggregation | Aggregation policy for distributed updates. |
A tf.Variable instance representing mutable TensorFlow state.
import tensorflow as tf
step = tf.Variable(0, dtype=tf.int32, trainable=False)
step.assign_add(1)
print(step.numpy())Creates non-trainable integer state, updates it in place, and reads its current value.
Model parameters
Persistent counters
Compiled computation
A tf.Variable is mutable TensorFlow state backed by a tensor. Its constructor's initial value determines its dtype and shape, and assign-style methods change the stored value.
Follow-up: How can a variable participate in optimization? Mark it trainable, allowing higher-level training code to treat it as a trainable model parameter.
The initial_value argument accepts a Tensor or a Python object convertible to a Tensor. It may also be a zero-argument callable; in that case, dtype must be provided.
Follow-up: What happens if validation is disabled? An initial value may omit a specified shape, but assignments still need to satisfy the variable's configured shape rules.
trainable controls whether the variable is considered trainable, and the documented default is True. Variables are also commonly captured and manipulated by tf.function.
Follow-up: Why set trainable=False? Use it for persistent state such as counters or bookkeeping that should not be optimized as a model parameter.
tf.Variable.assigntf.Variable.assign_addtf.Variable.assign_subtf.Tensortf.functiontf.Module