View source on GitHub
|
Represents a backend-agnostic variable in Keras.
tf.keras.Variable(
initializer,
shape=None,
dtype=None,
trainable=True,
autocast=True,
aggregation='none',
synchronization='auto',
name=None,
layout=None,
**kwargs
)
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.
|
autocast
|
Optional. Boolean indicating whether the variable supports
autocasting. If True, the layer may first convert the variable
to the compute data type when accessed. Defaults to True.
|
aggregation
|
Optional string, one of None, "none", "mean",
"sum" or "only_first_replica" specifying how a distributed
variable will be aggregated. This serves as a semantic annotation,
to be taken into account by downstream backends or users. Defaults
to "none".
|
name
|
Optional. A unique name for the variable. Automatically generated if not set. |
layout
|
Optional. Sharding layout for the variable. Can be a
keras.distribution.TensorLayout or a backend-specific layout.
|
kwargs
|
Additional backend-specific keyword arguments. |
Examples:
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
)
Methods
assign
assign(
value
)
assign_add
assign_add(
value
)
assign_sub
assign_sub(
value
)
numpy
numpy()
__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
)
Return self|value.
__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
)
Return value|self.
__rpow__
__rpow__(
other
)
__rsub__
__rsub__(
other
)
__rtruediv__
__rtruediv__(
other
)
__rxor__
__rxor__(
other
)
__sub__
__sub__(
other
)
__truediv__
__truediv__(
other
)
__xor__
__xor__(
other
)
View source on GitHub