tensorflow - 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))
解决方案
当我运行您的代码时,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”,它可以实现预期的减少。请参阅模块文档。
推荐阅读
- python - 获取段落的句子中的最大单词数Python
- c - 尝试对负数执行按位运算以在 C 中的第 31-16 位(32 位数)中生成 0
- coq - 目标中的一个假设与我之前定义的函数完全相同。我怎么能告诉 Coq 它们确实是一样的呢?
- python - 如何在python中求解这个齐次一阶微分方程?x*∂y/∂x=y+2x^3*sin^2(y/x)
- css - 阻止元素在窗口调整大小时移动
- php - 为什么 Ajax 数据表不按当前连接数据库显示数据?
- python - Tensorflow自定义梯度没有给出预期的答案
- angular - 绑定 [禁用] 到属性
- android - 如何从包含异步功能的 firestore 数据库中正确获取结果?- Android - Kotlin - MVVM
- java - 盈透证券 Java API