使用大于 2GB 的数组初始化 tensorflow 变量
Initializing tensorflow Variable with an array larger than 2GB
我正在尝试使用预训练的 word2vec
嵌入来初始化张量流 Variable
。
我有以下代码:
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 的数组分配给 tensorflow Variable
最简单的解决方案是 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})
正如 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})
似乎唯一的选择是使用占位符。我能找到的最干净的方法是直接初始化为占位符:
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})
试试这个:
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)
我正在尝试使用预训练的 word2vec
嵌入来初始化张量流 Variable
。
我有以下代码:
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 的数组分配给 tensorflow Variable
最简单的解决方案是 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})
正如 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})
似乎唯一的选择是使用占位符。我能找到的最干净的方法是直接初始化为占位符:
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})
试试这个:
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)