【问题标题】:How to find a value in tensor from other tensor in Tensorflow如何从 Tensorflow 中的其他张量中找到张量中的值
【发布时间】:2018-08-30 17:41:57
【问题描述】:

我在从其他张量中查找值时遇到问题。

这个问题的描述如下。 例如,

Input Tensor
s_idx = ( 1, 3, 5, 7)
e_idx = ( 3, 4, 5, 8)


label_s_idx = (2, 2, 3, 6)
label_e_idx = (2, 3, 4, 8)

上图中,s_idx[1]的值等于label_s_idx[2]e_idx[1]的值> 等于 label_e_idx[2]

换句话说,问题是如果条件 s_idx[i] == label_s_idx[i]output[i] 值 1并且 e_idx[i] == label_s_idx[j] 满足 label_s_idx 长度(== label_e_idx 长度)范围内的一些 j。

因此,在上面的例子中,输出张量是

output = ( 0, 1, 0, 0)

如何在 Python 中的 Tensorflow 上进行这样的编码?

【问题讨论】:

  • 我想tf.equal (tensorflow.org/api_docs/python/tf/equal) 和tf.logical_and (tensorflow.org/api_docs/python/tf/logical_and) 的组合可以做到这一点。但是我很困惑,因为您在示例中比较索引 1 和 2,而您在描述条件时说 s_idx[i] == label_s_idx[i]。更多说明会有所帮助。
  • 感谢您的评论。我的意思是 s_idx[i] == label_s_idx[j] and e_idx[i] == label_e_idx[j] 对于一些 j。所以,如果(s_idx[i], e_idx[i]) is contained in [ (label_s_idx[0], label_e_idx[0]), (label_s_idx[1], label_e_idx[1]), ... ,(label_s_idx[x], label_e_idx[x]) ],那么output[i]就变成1

标签: python tensorflow


【解决方案1】:

我找不到为此操作设计的函数。您可以使用下面的矩阵运算来实现它。

import tensorflow as tf

s_idx = [1, 3, 5, 7]
e_idx = [3, 4, 5, 8]
label_s_idx = [2, 2, 3, 6]
label_e_idx = [2, 3, 4, 8]

# convert the variables to one-hot encoding
# s_oh[i,j] = 1 if and only if s_idx[i] == j
# analogous for e_oh
s_depth = tf.reduce_max([s_idx, label_s_idx])
s_oh = tf.one_hot(s_idx, s_depth)
label_s_oh = tf.one_hot(label_s_idx, s_depth)

e_depth = tf.reduce_max([e_idx, label_e_idx])
e_oh = tf.one_hot(e_idx, e_depth)
label_e_oh = tf.one_hot(label_e_idx, e_depth)

# s_mult[i,j] == 1 if and only if s_idx[i] == label_s_idx[j]
# analogous for e_mult
s_mult = tf.matmul(s_oh, label_s_oh, transpose_b=True)
e_mult = tf.matmul(e_oh, label_e_oh, transpose_b=True)

# s_included[i] == 1 if and only if s_idx[i] is included in label_s_idx
# analogous for e_included
s_included = tf.reduce_max(s_mult, axis=1)
e_included = tf.reduce_max(e_mult, axis=1)

# output[i] == 1 if and only if s_idx[i] is included in label_s_idx
# and e_idx[i] is included in label_e_idx
output = tf.multiply(s_included, e_included)

with tf.Session() as sess:
    print(sess.run(output))
# [0. 1. 0. 0.]

【讨论】:

  • 我还有一个问题,例如 s_idx = [1, 3, 5, 7] e_idx = [3, 4, 5, 8] label_s_idx = [2, 2, 3, 6] label_e_idx = [2, 3, 4, 8] label_score = [1, 2, 3, 2] 然后,我想通过提取 label_score 的值来输出 [0, 3, 0, 0] 如果我包含在标签中,然后 output[i] = label_score[j],我该如何解决这个问题的代码?
  • 嗯。如果有多个匹配怎么办?例如,让label_s_idx = [2, 2, 3, 3]label_e_idx = [2, 3, 4, 4]。然后会有多个“j”。顺便说一句,回顾一下代码,我的回答可能与您的意图 100% 不一致。我的代码找到“i”,使得s_idx[i]s_label_idx 中,e_idx[i]e_label_idx 中。但是您真的想要求在同一位置(相同的“j”)找到匹配的值吗?如果是这样,您能否制作另一个示例和预期输出来阐明您的意图?然后我将编辑我的答案。
  • 非常感谢您的评论。另一个问题是当我添加其他变量label_score 时(它的长度将与label_s_idx 的长度和label_e_idx 的长度相同)。在上一题中,输出表示(label_s_idx, label_e_idx) 中存在(s_idx, e_idx)。但我打算稍微改变一下这个问题。
  • 首先,没有相同的(label_s_idx[x], label_e_idx[x])。所以没有label_s_idx = [2, 2, 3, 3]label_e_idx = [2,3,4,4]这样的例子,因为(label_s_idx[2], label_e_idx[2])(label_s_idx[3], label_e_idx[3])具有相同的值。
  • 另外,如果没有label_score值导入输出,当(s_idx[i], e_idx[i])不包含在(label_s_idx, label_e_idx)中,那么我想提取0来导入输出值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-22
  • 2016-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多