问题描述
限时送ChatGPT账号..我正在尝试使用预训练的 word2vec
嵌入初始化 tensorflow Variable
.
I am trying to initialize a tensorflow Variable
with pre-trained word2vec
embeddings.
我有以下代码:
import tensorflow as tf
from gensim import models
model = models.Word2Vec.load_word2vec_format('GoogleNews-vectors-negative300.bin', binary=True)
X = model.syn0
embeddings = tf.Variable(tf.random_uniform(X.shape, minval=-0.1, maxval=0.1), trainable=False)
sess.run(tf.initialize_all_variables())
sess.run(embeddings.assign(X))
我收到以下错误:
ValueError: Cannot create an Operation with a NodeDef larger than 2GB.
我尝试分配的数组 (X
) 的形状为 (3000000, 300)
,大小为 3.6GB.
The array (X
) I am trying to assign is of shape (3000000, 300)
and its size is 3.6GB.
如果我也尝试 tf.convert_to_tensor(X)
,我也会遇到同样的错误.
I am getting the same error if I try tf.convert_to_tensor(X)
as well.
我知道它失败是因为数组大于 2GB.但是,我不知道如何将大于 2GB 的数组分配给 tensorflow Variable
I know that it fails due to the fact that the array is larger than 2GB. However, I do not know how to assign an array larger than 2GB to a tensorflow Variable
推荐答案
最简单的解决方案是将其 feed_dict'ing 到一个占位符节点,用于 tf.assign 给变量.
The easiest solution is to feed_dict'ing it into a placeholder node that you use to tf.assign to the variable.
X = tf.Variable([0.0])
place = tf.placeholder(tf.float32, shape=(3000000, 300))
set_x = X.assign(place)
# set up your session here....
sess.run(set_x, feed_dict={place: model.syn0})
正如 Joshua Little 在单独的回答中指出的那样,您也可以在初始化程序中使用它:
As Joshua Little noted in a separate answer, you can also use it in the initializer:
X = tf.Variable(place) # place as defined above
...
init = tf.initialize_all_variables()
... create sess ...
sess.run(init, feed_dict={place: model.syn0})
这篇关于使用大于 2GB 的数组初始化 tensorflow 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论