tf.keras.backend.set_value
Stay organized with collections
Save and categorize content based on your preferences.
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).
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2020-10-01 UTC.
[null,null,["Last updated 2020-10-01 UTC."],[],[],null,["# tf.keras.backend.set_value\n\n\u003cbr /\u003e\n\n|------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------|\n| [TensorFlow 1 version](/versions/r1.15/api_docs/python/tf/keras/backend/set_value) | [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.2.0/tensorflow/python/keras/backend.py#L3336-L3371) |\n\nSets the value of a variable, from a Numpy array.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.keras.backend.set_value`](/api_docs/python/tf/keras/backend/set_value)\n\n\u003cbr /\u003e\n\n tf.keras.backend.set_value(\n x, value\n )\n\n[`backend.set_value`](../../../tf/keras/backend/set_value) is the compliment of [`backend.get_value`](../../../tf/keras/backend/get_value), and provides\na generic interface for assigning to variables while abstracting away the\ndifferences between TensorFlow 1.x and 2.x semantics. \n\n K = tf.keras.backend # Common keras convention\n v = K.variable(1.)\n\n # reassign\n K.set_value(v, 2.)\n print(K.get_value(v))\n 2.0\n\n # increment\n K.set_value(v, K.get_value(v) + 1)\n print(K.get_value(v))\n 3.0\n\nVariable semantics in TensorFlow 2 are eager execution friendly. The above\ncode is roughly equivalent to: \n\n v = tf.Variable(1.)\n\n _ = v.assign(2.)\n print(v.numpy())\n 2.0\n\n _ = v.assign_add(1.)\n print(v.numpy())\n 3.0\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Arguments --------- ||\n|---------|-------------------------------------------------------------------|\n| `x` | Variable to set to a new value. |\n| `value` | Value to set the tensor to, as a Numpy array (of the same shape). |\n\n\u003cbr /\u003e"]]