首页 > 解决方案 > 如何拥有 tf.zero 形状的大矩阵

问题描述

print("sequences",len(sequences))
print("seq_length",(seq_length))
print("vocab size",(vocab_size))
X = tf.zeros((len(sequences), seq_length, vocab_size), dtype=tf.bool)
y = tf.zeros((len(sequences), vocab_size), dtype=tf.bool)

输出

sequences 30373553
seq_length 30
vocab size 1290174

ResourceExhaustedError                    Traceback (most recent call last)
<ipython-input-35-1bd9b1544ba0> in <module>()
      2 print("seq_length",(seq_length))
      3 print("vocab size",(vocab_size))
----> 4 X = tf.zeros((len(sequences), seq_length, vocab_size), dtype=tf.bool)
      5 y = tf.zeros((len(sequences), vocab_size), dtype=tf.bool)
      6 

3 frames
/usr/local/lib/python3.6/dist-packages/six.py in raise_from(value, from_value)

ResourceExhaustedError: OOM when allocating tensor with shape[30373553,30,1290174] and type bool on /job:localhost/replica:0/task:0/device:CPU:0 by allocator cpu [Op:Fill] name: zeros/

在 tensorflow 2.0 上工作
我想制作一个形状为零的矩阵 [30373553,30,1290174]
在 TensorFlow 1.5 上运行相同的代码时没有这样的错误,但是在使用 Tensorflow 2.0 时给出了这个错误

标签: python-3.xtensorflow2.0

解决方案


假设每个bool元素使用 1 字节的内存,您的形状张量[30373553, 30, 1290174]将需要大约 1200 TB 的内存才能实现。这么多内存...

我猜这在 TensorFlow 1.5 中并没有出错,因为旧的延迟执行范例,你可以tf.zeros([30373553, 30, 1290174])毫无问题地调用,因为调用返回的符号张量在你调用之前不会实际分配到内存tf.Session.run()中包含张量的 tf.Graph。然而,在 TensorFlow 2.0 中,Eager Execution 将在调用后立即执行内存分配。


推荐阅读