[tf]tensorflow embedding加载预训练好的

2019-05-11  本文已影响0人  VanJordan
embedding_model = gensim.models.KeyedVectors.load_word2vec_format(FLAGS.glove_path,binary=True)
embedding_numpy = np.zeros((self.vocab_size,300))
for i in self.vocab_size:
    this_word = self.vocab.id2word(i)
    try :
        this_vector = embedding_model[this_word]
    except KeyError:
        this_vector = None 
    if not this_vector:
        embedding_numpy[i] = this_vector
W = tf.get_variable(name="W", shape=embedding.shape, 
      initializer=tf.constant_initializer(embedding_numpy), 
      trainable=False)
上一篇下一篇

猜你喜欢

热点阅读