谷歌colab内存问题
我试图理解为什么以下代码会导致我的 Colab 会话崩溃。
import numpy as np
import tensorflow as tf
x1 = np.random.rand(90000)
x2 = tf.random.uniform((90000,1)).numpy()
print(x1.shape, type(x1))
print(x2.shape, type(x2))
x1 - x2
我可以看到内存正在爆炸,导致崩溃,但我希望有人能准确解释为什么会发生这种情况。我也知道这与 numpy 中的广播数组有关,我只是想知道这是否是预期的行为,以便我将来可以避免它。
修复方法是 np.squeze(x2, axis=1) ,因此向量具有相同的形状,但显然我不明白 numpy 正在做什么引擎盖。欢迎任何建议和澄清。
I am trying to understand why the following code crashes my Colab session.
import numpy as np
import tensorflow as tf
x1 = np.random.rand(90000)
x2 = tf.random.uniform((90000,1)).numpy()
print(x1.shape, type(x1))
print(x2.shape, type(x2))
x1 - x2
I can see that memory is exploding which causes the crash but I was hoping someone can explain exactly why this is happening. I also understand that this has to do with broadcasting arrays in numpy
and I am just wondering if this is expected behavior so I can avoid it in the future.
The fix is to np.squeze(x2, axis=1)
so the vectors have the same shape but clearly there's something I don't understand about what numpy
is doing under the hood. Any suggestions and clarifications welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
x1
的形状为(90000,)
。x2
的形状为(90000, 1)
。在表达式x1 - x2
中,广播 发生(正如您所怀疑的),给出形状为(90000, 90000)
的结果。这样的浮点值数组需要90000*90000*8 = 64800000000
字节。x1
has shape(90000,)
.x2
has shape(90000, 1)
. In the expressionx1 - x2
, broadcasting occurs (as you suspected), giving a result that has shape(90000, 90000)
. Such an array of floating point values requires90000*90000*8 = 64800000000
bytes.