There are a few ways that you can use a pre-trained embedding in TensorFlow. Let’s say that you have the embedding in a NumPy array called embedding, with vocab_size rows and embedding_dim columns and you want to create a tensor W that can be used in a call to tf.nn.embedding_lookup().
-
Simply create
Was atf.constant()that takesembeddingas its value:W = tf.constant(embedding, name="W")This is the easiest approach, but it is not memory efficient because the value of a
tf.constant()is stored multiple times in memory. Sinceembeddingcan be very large, you should only use this approach for toy examples. -
Create
Was atf.Variableand initialize it from the NumPy array via atf.placeholder():W = tf.Variable(tf.constant(0.0, shape=[vocab_size, embedding_dim]), trainable=False, name="W") embedding_placeholder = tf.placeholder(tf.float32, [vocab_size, embedding_dim]) embedding_init = W.assign(embedding_placeholder) # ... sess = tf.Session() sess.run(embedding_init, feed_dict={embedding_placeholder: embedding})This avoid storing a copy of
embeddingin the graph, but it does require enough memory to keep two copies of the matrix in memory at once (one for the NumPy array, and one for thetf.Variable). Note that I’ve assumed that you want to hold the embedding matrix constant during training, soWis created withtrainable=False. -
If the embedding was trained as part of another TensorFlow model, you can use a
tf.train.Saverto load the value from the other model’s checkpoint file. This means that the embedding matrix can bypass Python altogether. CreateWas in option 2, then do the following:W = tf.Variable(...) embedding_saver = tf.train.Saver({"name_of_variable_in_other_model": W}) # ... sess = tf.Session() embedding_saver.restore(sess, "checkpoint_filename.ckpt")