首页 > 解决方案 > elmo 预训练模型的输出

问题描述

我正在做情绪分析。我正在使用 elmo 方法来获取词嵌入。但我对这种方法给出的输出感到困惑。考虑张量流网站中给出的代码:

 elmo = hub.Module("https://tfhub.dev/google/elmo/2", trainable=True)
    embeddings = elmo(["the cat is on the mat", "dogs are in the fog"],
    signature="default",as_dict=True)["elmo"]

特定句子的嵌入向量根据您提供的字符串数量而有所不同。详细解释让

 x = "the cat is on the mat"
 y = "dogs are in the fog"
 x1 = elmo([x],signature="default",as_dict=True)["elmo"]
 z1 = elmo([x,y] ,signature="default",as_dict=True)["elmo"] 

所以x1[0]不会等于z1[0]。这会随着您更改字符串的输入列表而改变。为什么一个句子的输出取决于另一个。我没有训练数据。我只使用现有的预训练模型。在这种情况下,我很困惑如何将我的评论文本转换为嵌入并用于情绪分析。请解释。
注意:要获取嵌入向量,我使用以下代码:

 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(x1,1))

标签: tensorflowsentiment-analysisword-embeddingtensorflow-hubelmo

解决方案


当我运行您的代码时,x1[0] 和 z1[0] 是相同的。然而,z1[1] 不同于

y1 = elmo([y],signature="default",as_dict=True)["elmo"]
return sess.run(tf.reduce_mean(y1,1))

因为 y 的 token 比 x 少,并且盲目地减少结束后的输出会捡起垃圾。

我建议使用“默认”输出而不是“elmo”,它可以实现预期的减少。请参阅模块文档。


推荐阅读