I see a problem when using sess.run() and not using the close() afterwords in general case.
A session may own resources, such as tf.Variable and it is important to release these resources when they are no longer required. To do this, either invoke the tf.Session.close() method on the session, or use the session as a context manager.
The following two examples are equivalent:
# Using the `close()` method.
sess = tf.Session()
sess.run(...)
sess.close()
# Using the context manager.
with tf.Session() as sess:
sess.run(...)
On calling the session.run() to often...
The Sessionclass is for running TensorFlow operations and the session object encapsulates the environment in which Operation objects are executed, and Tensor objects are evaluated.
If you can achieve your computations without creating the environment you can say it would be smarter to go without the session.run().
Note: Session.run() method runs one "step" of TensorFlow computation, by running the necessary graph fragment to execute every Operation and evaluate every Tensor in fetches, substituting the values in feed_dict for the corresponding input values.
The fetches argument may be a single graph element, or an arbitrarily nested list, tuple, namedtuple, dict, or OrderedDict containing graph elements at its leaves. A graph element can be one of the following types:
- An
tf.Operation. The corresponding fetched value will be None.
- A
tf.Tensor. The corresponding fetched value will be a numpy ndarray containing the
value of that tensor.
- A
tf.SparseTensor. The corresponding fetched value will be a tf.SparseTensorValue containing the value of that sparse tensor.
- A
get_tensor_handle op. The corresponding fetched value will be a
numpy ndarray containing the handle of that tensor.
- A
string which is the name of a tensor or operation in the graph.
Update: Interactive session will not help you either. The only difference comparing to the normal Session, InteractiveSession makes itself the default session so you can call run() or eval() on variables explicitly using the session object.
#start the session
sess = tf.InteractiveSession()
# stop the session
sess.stop()
ops.reset_default_graph()