使用 "with tf.Session()" 的目的?
Purpose of using "with tf.Session()"?
我正在练习称为连接的 keras 方法。
在这个例子中使用 with 语句让我想到了这个语句的目的
示例代码如下所示:
import numpy as np
import keras.backend as K
import tensorflow as tf
t1 = K.variable(np.array([ [[1, 2], [2, 3]], [[4, 4], [5, 3]]]))
t2 = K.variable(np.array([[[7, 4], [8, 4]], [[2, 10], [15, 11]]]))
d0 = K.concatenate([t1 , t2] , axis=-2)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
print(sess.run(d0))
然后我检查文档来自:tensorflow
并说:
A session may own resources, such as tf.Variable, tf.QueueBase, and tf.ReaderBase. 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.
我相信已经解释了所有这些,但是谁能给我更直观的解释。
在此先致谢,祝您有愉快的一天!
tf.Session()
启动一个 TensorFlow Graph 对象,其中张量通过操作(或 ops)进行处理。 with
块在操作完成后立即终止会话。因此,不需要调用 Session.close
。此外,会话包含变量、全局变量、占位符和操作。这些必须在会话创建后启动。因此我们称 tf.global_variables_initializer().run()
A graph contains tensors and operations. To initiate a graph, a session is created which runs the graph. In other words, graph provides a schema whereas a session processes a graph to compute values( tensors ).
tensorflow documentation 对此非常具体。
Since a tf.Session owns physical resources (such as GPUs and network connections), it is typically used as a context manager (in a with
block) that automatically closes the session when you exit the block.
It is also possible to create a session without using a with
block, but you should explicitly call tf.Session.close when you are finished with it to free the resources.
我正在练习称为连接的 keras 方法。
在这个例子中使用 with 语句让我想到了这个语句的目的
示例代码如下所示:
import numpy as np
import keras.backend as K
import tensorflow as tf
t1 = K.variable(np.array([ [[1, 2], [2, 3]], [[4, 4], [5, 3]]]))
t2 = K.variable(np.array([[[7, 4], [8, 4]], [[2, 10], [15, 11]]]))
d0 = K.concatenate([t1 , t2] , axis=-2)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
print(sess.run(d0))
然后我检查文档来自:tensorflow 并说:
A session may own resources, such as tf.Variable, tf.QueueBase, and tf.ReaderBase. 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.
我相信已经解释了所有这些,但是谁能给我更直观的解释。
在此先致谢,祝您有愉快的一天!
tf.Session()
启动一个 TensorFlow Graph 对象,其中张量通过操作(或 ops)进行处理。 with
块在操作完成后立即终止会话。因此,不需要调用 Session.close
。此外,会话包含变量、全局变量、占位符和操作。这些必须在会话创建后启动。因此我们称 tf.global_variables_initializer().run()
A graph contains tensors and operations. To initiate a graph, a session is created which runs the graph. In other words, graph provides a schema whereas a session processes a graph to compute values( tensors ).
tensorflow documentation 对此非常具体。
Since a tf.Session owns physical resources (such as GPUs and network connections), it is typically used as a context manager (in a
with
block) that automatically closes the session when you exit the block.It is also possible to create a session without using a
with
block, but you should explicitly call tf.Session.close when you are finished with it to free the resources.