On this page
tf.keras.backend.set_value
Sets the value of a variable, from a Numpy array.
tf.keras.backend.set_value(
x, value
)
backend.set_value
is the compliment of backend.get_value
, and provides a generic interface for assigning to variables while abstracting away the differences between TensorFlow 1.x and 2.x semantics.
K = tf.keras.backend # Common keras convention
v = K.variable(1.)
# reassign
K.set_value(v, 2.)
print(K.get_value(v))
2.0
# increment
K.set_value(v, K.get_value(v) + 1)
print(K.get_value(v))
3.0
Variable semantics in TensorFlow 2 are eager execution friendly. The above code is roughly equivalent to:
v = tf.Variable(1.)
v.assign(2.)
print(v.numpy())
2.0
v.assign_add(1.)
print(v.numpy())
3.0
Arguments | |
---|---|
x |
Variable to set to a new value. |
value |
Value to set the tensor to, as a Numpy array (of the same shape). |
© 2020 The TensorFlow Authors. All rights reserved.
Licensed under the Creative Commons Attribution License 3.0.
Code samples licensed under the Apache 2.0 License.
https://www.tensorflow.org/versions/r2.3/api_docs/python/tf/keras/backend/set_value