Tensorflow with tf.Session The usage of () as sess

The

Session provides the environment for Operation execution and Tensor evaluation. As shown below,

import tensorflow as tf

# Build a graph.
a = tf.constant([1.0, 2.0])
b = tf.constant([3.0, 4.0])
c = a * b

# Launch the graph in a session.
sess = tf.Session()

# Evaluate the tensor 'c'.
print sess.run(c)
sess.close()

# result: [3., 8.]

a Session might have some resources, such as Variable or Queue. When the session is no longer needed, these resources need to be released. There are two ways to do it,

  1. calls session.close() method;

  2. USES with tf.session () to create the Context (Context) for execution, which is automatically released when the Context exits.

import tensorflow as tf

# Build a graph.
a = tf.constant([1.0, 2.0])
b = tf.constant([3.0, 4.0])
c = a * b

with tf.Session() as sess:
    print sess.run(c)


https://www.cnblogs.com/lienhua34/p/5998853.html


https://blog.csdn.net/qq_36666115/article/details/80017050


Read More: