我正在练习名为 concatenate 的 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))
Run Code Online (Sandbox Code Playgroud)
然后我检查文档:tensorflow 并说:
会话可能拥有资源,例如 tf.Variable、tf.QueueBase 和 tf.ReaderBase。当不再需要这些资源时,释放这些资源很重要。为此,请在会话上调用 tf.Session.close 方法,或将会话用作上下文管理器。
我相信这已经解释了所有这些,但有人可以给我更直观的解释。
提前致谢,祝您有美好的一天!
我收到这条消息:
Hint: If you want to see a list of allocated tensors when OOM happens, add report_tensor_allocations_upon_oom to RunOptions for current allocation info.
我如何在 Tensorflow 2.3 中做到这一点?
在过去的几天里,结果证明这是一个令人惊讶的令人沮丧的问题。似乎没有关于如何在 TF2 中执行此操作的工作示例。