2016-02-14 124 views
14

我试图用预先训练的word2vec嵌入来初始化张量流Variable使用大于2GB的数组初始化tensorflow变量

我有以下代码:

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

回答

6

最简单的解决方法是将feed_dict转换为一个用于tf.assign变量的占位符节点。

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}) 

约书亚小在一个单独的回答指出,你也可以在初始化使用它:

X = tf.Variable(place) # place as defined above 
... 
init = tf.initialize_all_variables() 
... create sess ... 
sess.run(init, feed_dict={place: model.syn0}) 
+1

'X.assign(地方)'必须'tf.assign(X,地点,validate_shape = FALSE)',或TensorFlow会抱怨你正在改变张量的形状。除此之外,这工作。 –

+0

谢谢 - 通过设置占位符的形状更新了答案,在下面包含了+ mrry的评论。 – dga

+3

欲了解更多信息,有关如何在[预加载的数据]下的文档(https://www.tensorflow.org/versions/r0.7/how_tos/reading_data/index.html#preloaded-data )以及如何使用占位符和变量来预加载MNIST训练输入的完整示例[here](https://github.com/tensorflow/tensorflow/blob/r0.7/tensorflow/examples/how_tos/reading_data/) fully_connected_preloaded_var.py) – stefano

10

这似乎是唯一的选择就是使用的占位符。我能找到的最彻底的方法是初始化直接的占位符:

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

注意,当你调用'tf.placeholder()'时,你也可以设置可选的形状参数,然后你不需要'validate_shape = False'(并且你在你的程序的其他部分得到了更好的形状推理!)。 – mrry

+0

@ mrry,哦,没错。谢谢。我已经添加了答案。 –

+0

干净整洁的解决方案。 Upvoted。 – dga