Represents a backend-agnostic variable in Keras.
tf.keras.Variable(
initializer,
shape=None,
dtype=None,
trainable=True,
autocast=True,
aggregation='mean',
name=None
)
A Variable acts as a container for state. It holds a tensor value and can be updated. With the JAX backend, variables are used to implement "functionalization", the pattern of lifting stateful operations out of a piece of computation to turn it into a stateless function.
| Args | |
|---|---|
initializer | Initial value or callable for initialization. If a callable is used, it should take the arguments shape and dtype. |
shape | Optional. Tuple for the variable's shape. Required if initializer is a callable. |
dtype | Optional. Data type of the variable. Defaults to the global float dtype type ("float32" if never configured). |
trainable | Optional. Boolean indicating if variable is trainable. Defaults to True. |
name | Optional. A unique name for the variable. Automatically generated if not set. |
Initializing a Variable with a NumPy array:
import numpy as np import keras initial_array = np.ones((3, 3)) variable_from_array = keras.Variable(initializer=initial_array)
Using a Keras initializer to create a Variable:
from keras.src.initializers import Ones
variable_from_initializer = keras.Variable(
initializer=Ones(), shape=(3, 3), dtype="float32"
)
Updating the value of a Variable:
new_value = np.zeros((3, 3), dtype="float32") variable_from_array.assign(new_value)
Marking a Variable as non-trainable:
non_trainable_variable = keras.Variable(
initializer=np.ones((3, 3), dtype="float32"), trainable=False
)
| Attributes | |
|---|---|
name | The name of the variable (string). |
path | The path of the variable within the Keras model or layer (string). |
dtype | The data type of the variable (string). |
shape | The shape of the variable (tuple of integers). |
ndim | The number of dimensions of the variable (integer). |
trainable | Whether the variable is trainable (boolean). |
value | The current value of the variable (NumPy array or tensor). |
aggregation | |
constraint | |
handle | |
overwrite_with_gradient | Whether this variable should be overwritten by the gradient. This property is designed for a special case where we want to overwrite the variable directly with its computed gradient. For example, in float8 training, new |
regularizer | |
assignassign(
value
)
assign_addassign_add(
value
)
assign_subassign_sub(
value
)
numpynumpy()
__abs____abs__()
__add____add__(
other
)
__and____and__(
other
)
__array____array__(
dtype=None
)
__bool____bool__()
__eq____eq__(
other
)
Return self==value.
__floordiv____floordiv__(
other
)
__ge____ge__(
other
)
Return self>=value.
__getitem____getitem__(
idx
)
__gt____gt__(
other
)
Return self>value.
__invert____invert__()
__le____le__(
other
)
Return self<=value.
__lt____lt__(
other
)
Return self<value.
__matmul____matmul__(
other
)
__mod____mod__(
other
)
__mul____mul__(
other
)
__ne____ne__(
other
)
Return self!=value.
__neg____neg__()
__or____or__(
other
)
__pos____pos__()
__pow____pow__(
other
)
__radd____radd__(
other
)
__rand____rand__(
other
)
__rfloordiv____rfloordiv__(
other
)
__rmatmul____rmatmul__(
other
)
__rmod____rmod__(
other
)
__rmul____rmul__(
other
)
__ror____ror__(
other
)
__rpow____rpow__(
other
)
__rsub____rsub__(
other
)
__rtruediv____rtruediv__(
other
)
__rxor____rxor__(
other
)
__sub____sub__(
other
)
__truediv____truediv__(
other
)
__xor____xor__(
other
)
© 2022 The TensorFlow Authors. All rights reserved.
Licensed under the Creative Commons Attribution License 4.0.
Code samples licensed under the Apache 2.0 License.
https://www.tensorflow.org/api_docs/python/tf/keras/Variable