Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何检查张量的值是否包含在其他张量中?_Python_Tensorflow - Fatal编程技术网

Python 如何检查张量的值是否包含在其他张量中?

Python 如何检查张量的值是否包含在其他张量中?,python,tensorflow,Python,Tensorflow,我对从其他张量中找到一个值有问题 它类似于以下问题:(URL:) 前面的问题是询问输入张量x[i],y[i]是否包含在输入张量label\ux,label\uy 以下是前一个问题的示例: 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) 问题是给输出[i]一个值1 如果一些j的s_idx[i]==label_s_idx[j]和

我对从其他张量中找到一个值有问题

它类似于以下问题:(URL:)

前面的问题是询问输入张量x[i]y[i]是否包含在输入张量label\uxlabel\uy

以下是前一个问题的示例:

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)
问题是给输出[i]一个值1 如果一些j的s_idx[i]==label_s_idx[j]和e_idx[i]==label_s_idx[j]满足某些j的要求

因此,在上述示例中,输出张量为

output = (0, 1, 0, 0)
因为(s_idx[1]=3,e_idx[1]=4)与(label_s_idx[2]=3,label_e_idx[2]=4)相同

(s_idx,e_idx)没有重复值,而(label_s_idx,label_e_idx)有重复值

因此,假设以下输入示例不可能:

s_idx = (2, 2, 3, 3)
e_idx = (2, 3, 3, 3)
因为,(s_idx[2]=3,e_idx[2]=3)与(s_idx[3]=3,e_idx[3]=3)相同

在这个问题中,我想改变的是,给输入张量增加另一个值:

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)
label_score = (1, 3, 2, 3)
*标签\分数张量中没有0值

已更改问题中的任务定义如下:

问题是,如果满足某些j的s\u idx[i]==label\u s\u idx[j]e\u idx[i]==label\u idx[j],则给输出值[i]一个label\u score[j]的值

因此,输出_2应如下所示:

output = (0, 1, 0, 0)  // It is same as previous problem
output_2 = (0, 2, 0, 0)

如何在Python中的Tensorflow上编写这样的代码?

这可能会奏效。由于这是一项复杂的任务,请尝试更多的示例,看看是否获得了预期的结果

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]
label_score = [1, 3, 2, 3]

# convert to one-hot vector.
# make sure all have the same shape
max_idx = tf.reduce_max([s_idx, label_s_idx, e_idx, label_e_idx])
s_oh = tf.one_hot(s_idx, max_idx)
label_s_oh = tf.one_hot(label_s_idx, max_idx)
e_oh = tf.one_hot(e_idx, max_idx)
label_e_oh = tf.one_hot(label_e_idx, max_idx)

# make a matrix such that (i,j) element equals one if
# idx(i) = label(j)
s_mult = tf.matmul(s_oh, label_s_oh, transpose_b=True)
e_mult = tf.matmul(e_oh, label_e_oh, transpose_b=True)

# find i such that idx(i) = label(j) for s and e, with some j
# there is at most one such j by the uniqueness condition.
output = tf.reduce_max(s_mult * e_mult, axis=1)

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

# extract the label score at the corresponding j index
# and store in the index i
# then remove redundant dimension
output_2 = tf.matmul(
    s_mult * e_mult, 
    tf.cast(tf.expand_dims(label_score, -1), tf.float32))
output_2 = tf.squeeze(output_2)    

with tf.Session() as sess:
    print(sess.run(output_2))
    # [0. 2. 0. 0.]

以下是一个可能的解决方案:

import tensorflow as tf

s_idx = tf.placeholder(tf.int32, [None])
e_idx = tf.placeholder(tf.int32, [None])
label_s_idx = tf.placeholder(tf.int32, [None])
label_e_idx = tf.placeholder(tf.int32, [None])
label_score = tf.placeholder(tf.int32, [None])

# Stack inputs for comparison
se_idx = tf.stack([s_idx, e_idx], axis=1)
label_se_idx = tf.stack([label_s_idx, label_e_idx], axis=1)
# Compare every pair to each other and find matches
cmp = tf.equal(se_idx[:, tf.newaxis, :], label_se_idx[tf.newaxis, :, :])
matches = tf.reduce_all(cmp, axis=2)
# Find the position of the matches
match_pos = tf.argmax(tf.cast(matches, tf.int8), axis=1)
# For those positions where a match was found take the corresponding score
output = tf.where(tf.reduce_any(matches, axis=1),
                  tf.gather(label_score, match_pos),
                  tf.zeros_like(label_score))

# Test
with tf.Session() as sess:
    print(sess.run(output, feed_dict={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, 3, 2, 3]}))
# >>> [0 2 0 0]

它将每对值相互比较,因此成本是输入大小的二次方。此外,用于查找匹配位置的索引,如果有多个可能的索引,则它可能会不确定地返回其中任何一个。

label__idx=(2,2,3,6)
具有重复值2?这是可能的,因为
label_e_idx=(2,3,4,8)
,所以我们可以这样创建元组:
(label_idx,label_idx)=((2,2)、(2,3)、(3,4)、(6,8))
,在这种情况下,这里没有重复项,
j=2
,因此
output_2
应该是(0,2,0,0)`?为什么是3?你是对的。我将修改问题的示例。