Tensorflow中with tf.Session() as sess用法详解
Session提供了Operation执行和Tensor求值的环境。如下面所示,
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.]
一个Session可能会拥有一些资源,例如Variable或者Queue。当我们不再需要该session的时候,需要将这些资源进行释放。有两种方式,
-
调用session.close()方法;
-
使用with tf.Session()创建上下文(Context)来执行,当上下文退出时自动释放。
import tensorflow as tf# Build a graph.
a = tf.constant([1.0, 2.0])
b = tf.constant([3.0, 4.0])
c = a * bwith 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
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
