我正在尝试使用预训练的维基模型来比较Glove,Fasttext,Bert,Elmo在两个单词之间的相似度。Glove和Fasttext有预训练的模型,可以很容易地与python中的gensim word2vec一起使用。Elmo和Bert有这样的模型吗?
发布于 2019-09-20 20:32:52
以下是python代码,用于在Tensorflow上使用可用的Elmo模型,并比较两个字符串之间的相似性:
import tensorflow_hub as hub
import tensorflow as tf
elmo = hub.Module("https://tfhub.dev/google/elmo/2", trainable=True)
def elmo_vectors(x):
embeddings = elmo(x, signature="default", as_dict=True)["elmo"]
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
sess.run(tf.tables_initializer())
# return average of ELMo features
return sess.run(tf.reduce_mean(embeddings,1))
dist=spatial.distance.cosine(elmo_vectors(["partner"]),elmo_vectors(["vendor"]))
https://stackoverflow.com/questions/57943303
复制相似问题