Tensorflow,将张量中的索引值与if条件的整数进行比较

Tensorflow, compare an indexed value in a tensor with an integer for if condition

我正在使用 TensorFlow 进行类似于连续词袋 (CBOW) 模型的自定义嵌入训练。不过不像'CBOW'有定长滑动window,我的滑动window可以说是灵活的。问题是:

比方说,嵌入是词嵌入。对于单词 t,我有一个张量显示其上下文单词的索引:[-1, 1, 2, -1]。最大window大小为4,所以vector的长度为4。但有时我一个词没有4个上下文词,所以我用'-1'表示'no word in this position',其他的整数是单词的索引。我还有一个 'embedding' 张量,它是所有单词的嵌入。

我想要做的是获取上下文单词的平均嵌入以表示上下文。例如,如果上下文词是 [-1, 1, 2, -1],我会得到 (1 * (embedding for word 1) + 2 * (embedding for word 2) ) / 2。我只需要忽略所有-1.

所以在我的代码中,我尝试遍历上下文词张量以将每个值与 -1 进行比较,并使用 if 条件来控制我是否要添加该上下文词的嵌入。我为此尝试了不同的方法,但总是得到 'TypeError: Using a tf.Tensor as a Python bool is not allowed.'

有没有办法解决这个问题?或者更好的是,是否有更好的无词位置表示,以便我可以更有效地计算(尝试使用 NaN 但也遇到了很多麻烦...)?

非常感谢您的帮助,希望我能清楚地描述问题。

我认为了解如何从 Tensorflow 张量中获取值会有所帮助。查看已接受的答案 。请记住,Tensorflow 可能被视为一种符号语言,因此 tf.Tensor 是一种符号,它在会话中评估时表示某些值,而不是在定义或与其他操作叠加时。

如果要在图中进行比较,请使用张量流特殊函数进行张量比较,如 tf.equaltf.less 等。例如,我比较张量的第一个元素 a 与另一个常量张量 b:

# Here is an example of two tensors
a = tf.constant([[1.0, 0.0], [0.0, 1.0], [1.0, 0.0], [0.0, 1.0]])
b = tf.constant(1, dtype=tf.float32)

# generate a tensor representing a result of comparison
c = tf.equal(a[0, 0], b)
# Evaluate the output tensor in a session
print(tf.Session().run(c))

输出为真

此外,您可以尝试如下提取张量的值:

# Here is an example of the tensor
a = tf.constant([[1.0, 0.0], [0.0, 1.0], [1.0, 0.0], [0.0, 1.0]])

# Evaluate the tensor in a session
print(tf.Session().run(a))

这里所做的是将 tf 张量转换为 numpy 数组,您可以按任何需要的方式对其进行处理。