I'm trying some deep learning experiments with various hyper parameters. I build model for each hyper parameter settings separately. After first hyperparameter setting is trained and evaluated, when I try to build a new model with second setting, it gives me an error related to variable reused and stuff.
So I want to reset my session after each experiment. How can I do that?
I've tried tf.reset_default_graph()
, but when I call sess.run(tf.global_variables_initializer())
, it gives me following error:
ValueError: Fetch argument cannot be interpreted as a Tensor. (Operation name: "init" op: "NoOp" input: "^v/Assign" is not an element of this graph.)
How can I remove all variables and reset my session peacefully?
You can clear variables by using %reset-f function. If you want to delete any specific variable then use %reset-f.
Tensorflow is a high-level library. A variable is a state or value that can be modified by performing operations on it. In TensorFlow variables are created using the Variable() constructor. The Variable() constructor expects an initial value for the variable, which can be any kind or shape of Tensor.
In TensorFlow the differences between constants and variables are that when you declare some constant, its value can't be changed in the future (also the initialization should be with a value, not with operation). Nevertheless, when you declare a Variable, you can change its value in the future with tf.
To initialize a new variable from the value of another variable use the other variable's initialized_value() property. You can use the initialized value directly as the initial value for the new variable, or you can use it as any other tensor to compute a value for the new variable.
After you have reset the default graph, you will also need to create a new session. A small example:
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
tf.reset_default_graph()
sess = tf.Session() # Create new session
sess.run(tf.global_variables_initializer())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With