Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

initialising Seq2seq embedding with pretrained word2vec

Tags:

tensorflow

I am interested in initialising tensorflow seq2seq implementation with pretrained word2vec.

I have seen the code. It seems embedding is initialized

with tf.variable_scope(scope or "embedding_attention_decoder"):
with tf.device("/cpu:0"):
embedding = tf.get_variable("embedding", [num_symbols, cell.input_size])

how do I change this to initialise with pretrained word2vec??

like image 640
skw Avatar asked Nov 22 '15 05:11

skw


1 Answers

I think you've gotten your answer in the mailing list but I am putting it here for posterity.

https://groups.google.com/a/tensorflow.org/forum/#!topic/discuss/bH6S98NpIJE

You can initialize it randomly and afterwards do: session.run(embedding.assign(my_word2vec_matrix))

This will override the init values.

This seems to work for me. I believe trainable=False is needed to keep the values fixed?

# load word2vec model (say from gensim)
model = load_model(FILENAME, binary=True)

# embedding matrix
X = model.syn0
print(type(X)) # numpy.ndarray
print(X.shape) # (vocab_size, embedding_dim)

# start interactive session
sess = tf.InteractiveSession()

# set embeddings
embeddings = tf.Variable(tf.random_uniform(X.shape, minval=-0.1, maxval=0.1), trainable=False)

# initialize
sess.run(tf.initialize_all_variables())

# override inits
sess.run(embeddings.assign(X))
like image 120
tokestermw Avatar answered Nov 13 '22 08:11

tokestermw