Suppose you have a dictionary and an inverse_dict list with an index in the list that matches most common words:
vocab = {'hello': 0, 'world': 2, 'neural':1, 'networks':3}
inv_dict = ['hello', 'neural', 'world', 'networks']
Notice how the inverse_dict index matches the dictionary values. Now declare the embed matrix and get the values:
vocab_size = len(inv_dict)
emb_size = 300
embeddings = np.zeroes((vocab_size, emb_size))
from gensim.models.keyedvectors import KeyedVectors
model = KeyedVectors.load_word2vec_format('embeddings_file', binary=True)
for k, v in vocab.items():
embeddings[v] = model[k]
. . , : x = ['hello', 'world']. . :
x_train = []
for word in x:
x_train.append(vocab[word])
x_train = np.array(x_train)
x_model = tf.placeholder(tf.int32, shape=[None, input_size])
with tf.device("/cpu:0"):
embedded_x = tf.nn.embedding_lookup(embeddings, x_model)
embedded_x - . , , . ,