gpt4 book ai didi

python - 创建一个内容大于 2GB 的张量原型(prototype)

转载 作者:行者123 更新时间:2023-11-28 22:17:21 31 4
gpt4 key购买 nike

我创建了一个大小为 (2^22, 256) 的 ndarray (W),并尝试使用此数组作为权重矩阵的初始化:

w = tf.Variable(tf.convert_to_tensor(W))

然后,tensorflow 报错:ValueError:无法创建内容大于 2GB 的张量原型(prototype)。

我该如何解决这个问题?附言。我的权重矩阵必须使用该 (2^22, 256) 矩阵进行初始化。谢谢 :)

最佳答案

Protobuf 有一个 hard limit of 2GB . 2^22*256 float 是 4GB。您的问题是,您将通过以下方式将初始值嵌入图形原型(prototype)中

import tensorflow as tf
import numpy as np

w_init = np.random.randn(2**22, 256).astype(np.float32)
w = tf.Variable(tf.convert_to_tensor(w_init))
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print sess.run(tf.reduce_sum(w))

造成

ValueError: Cannot create a tensor proto whose content is larger than 2GB.

上面的图定义基本上是在说:“图有一个占用 4GB 的变量,这里是确切的值:...”

相反,你应该写

import tensorflow as tf
import numpy as np

w_init = np.random.randn(2**22, 256).astype(np.float32)
w_plhdr = tf.placeholder(dtype=tf.float32, shape=[2**22, 256])
w = tf.get_variable('w', [2**22, 256])
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
sess.run(w.assign(w_plhdr), {w_plhdr: w_init})
print sess.run(tf.reduce_sum(w))

这样,您的变量拥有 4GB 的值,但图只有知识:“嘿,有一个大小为 4GB 的变量。只是不要关心图定义中的确切值。因为有一个以后无论如何都要覆盖这些值的操作。”。

关于python - 创建一个内容大于 2GB 的张量原型(prototype),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51470991/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com