2

我正在尝试使用 Elmo 模型计算 wordsim 集的余弦相似度。这可能没有意义,因为它是为句子词嵌入而设计的,但我想看看模型在这种情况下的表现如何。我使用的 Elmo 来自:

https://tfhub.dev/google/elmo/3

如果我运行以下代码(它是从文档页面修改以符合 TF 2.0),它将生成单词的张量表示。

import tensorflow_hub as hub
import tensorflow as tf


elmo = hub.load("https://tfhub.dev/google/elmo/3")
tensor_of_strings = tf.constant(["Gray",
                                 "Quick",
                                 "Lazy"])
elmo.signatures['default'](tensor_of_strings)

如果我尝试直接计算余弦相似度,我会得到错误, NotImplementedError: Cannot convert a symbolic Tensor (strided_slice_59:0) to a numpy array. This error may indicate that you're trying to pass a Tensor to a NumPy call, which is not supported . 我不确定如何将张量直接转换为 numpy 数组,或者是否有更好的张量评估器而不是余弦相似度?

编辑:这就是我为计算余弦相似度所做的

def cos_sim(a, b):
    return np.inner(a, b) / (np.linalg.norm(a) * (np.linalg.norm(b)))

print("ELMo:", cos_sim(elmo.signatures['default'](tensor_of_strings)['word_emb'][0], elmo.signatures['default'](tensor_of_strings)['word_emb'][1]))
4

1 回答 1

1

在此线程中:NotImplementedError:无法将符号张量 (lstm_2/strided_slice:0) 转换为 numpy 数组。吨。解决方案是更改numpy版本(1.19.5可能是合适的版本)。

我认为提供 (Python + TensorFlow + NumPy) 的所有版本很重要。

此外,就像评论中提到的@Edwin Cheong 一样,您很可能在损失函数中混合了 numpy 和 Tensorflow 代码。向我们提供这些信息也很重要,这里的问题是损失函数计算/创建:NotImplementedError: Cannot convert a symbolic Tensor (2nd_target:0) to a numpy array

于 2021-05-20T13:16:54.723 回答