我正在尝试使用预先训练的Variable
嵌入来初始化张量流word2vec
。
我有以下代码:
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。
如果我尝试tf.convert_to_tensor(X)
,我也会遇到同样的错误。
我知道由于阵列大于2GB而失败了。但是,我不知道如何将大于2GB的数组分配给张量流Variable
答案 0 :(得分:17)
似乎唯一的选择是使用占位符。我能找到的最简洁的方法是直接初始化为占位符:
X_init = tf.placeholder(tf.float32, shape=(3000000, 300))
X = tf.Variable(X_init)
# The rest of the setup...
sess.run(tf.initialize_all_variables(), feed_dict={X_init: model.syn0})
答案 1 :(得分:11)
最简单的解决方案是将feed_dict'转换为占位符节点,用于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在单独的回答中所指出的,你也可以在初始化器中使用它:
X = tf.Variable(place) # place as defined above
...
init = tf.initialize_all_variables()
... create sess ...
sess.run(init, feed_dict={place: model.syn0})
答案 2 :(得分:-1)
尝试一下:
import tensorflow as tf
from gensim import models
model = models.KeyedVectors.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 = tf.Session()
sess.run(tf.global_variables_initializer())
embeddings.load(model.syn0, sess)