下面的代码使用
tf.scan
.
tensorflow会分配一个6元素数组来保存6个部分和吗?
或者,tensorflow是否聪明到只分配一个1元素数组来保存部分和?
import tensorflow as tf
import numpy as np
elems = np.array([1, 2, 3, 4, 5, 6])
s = tf.scan(lambda a, x: a + x, elems)
# s == [1, 3, 6, 10, 15, 21]
final_s = s[-1] # final_s == 21
with tf.Session() as sess:
print(sess.run(final_s))
以下代码导致16 GB的计算机内存不足。
import tensorflow as tf
import numpy as np
elems = tf.ones(int(1e9), dtype=np.float32) # 4GB
s = tf.scan(lambda a, x: a + x, elems)
final_s = s[-1]
with tf.Session() as sess:
print(sess.run(final_s))