tensorflow加载大型词嵌入模型(>2G)

tensorflow加载词嵌入模型

方法一

当词嵌入文件较小的时候,直接加载:

embedding = tf.get_variable(initializer=word_embeddings, name='label_embedding')
word_ebd = tf.nn.embedding_lookup(embedding, word_id)

方法二

当词嵌入文件较大的时候(>2G),加载方法如下:

embedding_weights = tf.Variable(tf.constant(0.0, shape=[embedding_vocab_size, EMBEDDING_DIM]), trainable=False, name='embedding_weights')
embedding_placeholder = tf.placeholder(tf.float32, [embedding_vocab_size, EMBEDDING_DIM])
embedding = embedding_weight.assign(embedding_placeholder)
word_ebd = tf.nn.embedding_lookup(embedding, word_id)

sess = tf.Session()
word_ebd = sess.run(word_ebd, feed_dict={embedding_placeholder: word_embeddings, word_id: word_id})

注意:当使用这种方法时会导致网络训练速度变慢!


版权声明:本文为a_123zxy原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。