Updating TensorFlow variables within a session in Python
Python TensorFlow Basic: Exercise-11 with Solution
Write a Python program that demonstrates how to update a TensorFlow variable within a session.
Sample Solution:
Python Code:
Output:
Initial Variable: 2.0 Updated Variable: 7.0
Explanation:
In the exercise above -
- Create a TensorFlow variable named “variable_tensor” with an initial value of 3.0.
- Define a Python function “update_variable()” and decorate it with @tf.function. This decorator allows you to define a function containing TensorFlow operations.
- Within the “update_variable()” function, we use the assign method to update the variable's value with a new value.
- Call the "update_variable()" function with a new value of 5.0 to update the variable.
- Finally, we print the updated variable value.
Python Code Editor:
Previous: TensorFlow constant and variable operations in Python.
Next: Updating TensorFlow variables in Python.
What is the difficulty level of this exercise?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics