Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

removing variables from Tensorflow

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?

like image 451
codebomb Avatar asked Sep 04 '17 14:09

codebomb


People also ask

How do I delete a variable in Jupyter?

You can clear variables by using %reset-f function. If you want to delete any specific variable then use %reset-f.

What is a variables in TensorFlow?

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.

What is the difference between tf constant and tf variable?

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.

How do you initialize a TensorFlow variable?

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.


1 Answers

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())
like image 129
J. P. Petersen Avatar answered Sep 29 '22 01:09

J. P. Petersen