View source on GitHub |
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.
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 ) __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 )
View source on GitHub