Python tensorflow中的输出数据[output\u ID==i]=输入数据[input\u ID==i]
在numpy代码中,如果希望相同的id获得相同的值,可以执行以下操作,例如:Python tensorflow中的输出数据[output\u ID==i]=输入数据[input\u ID==i],python,tensorflow,Python,Tensorflow,在numpy代码中,如果希望相同的id获得相同的值,可以执行以下操作,例如: input_data = np.array([0.1, 0.2, 0.3]) input_ids = np.array([0, 1, 2]) output_ids = np.array([2, 0, 1, 0]) output_data = np.array([0.1, 0.1, 0.1, 0.1]) for i in input_ids: output_data[output_ids == i] = inpu
input_data = np.array([0.1, 0.2, 0.3])
input_ids = np.array([0, 1, 2])
output_ids = np.array([2, 0, 1, 0])
output_data = np.array([0.1, 0.1, 0.1, 0.1])
for i in input_ids:
output_data[output_ids == i] = input_data[input_ids == i]
print(output_data)
输出:[0.30.1 0.2 0.1]
注意:input_id=unique(input_id),它在开始时是唯一的
在tensorflow中,如何执行此类代码,应使用哪种功能。有类似的例子吗
- 输入数据:一个张量,可以是浮点64,浮点32
- 输出数据:张量,与输入数据类型相同
- 输入ID:张量,必须为int32或int64
- 输出ID:张量,必须为int32或int64
input_id
始终是从0
开始的整数序列,对应于input_data
的索引([0,1,2,…])
)。在这种情况下,您可以简单地执行以下操作:
import tensorflow as tf
with tf.Graph().as_default(), tf.Session() as sess:
input_data = tf.constant([0.1, 0.2, 0.3])
output_ids = tf.constant([2, 0, 1, 0])
output_data = tf.gather(input_data, output_ids)
print(sess.run(output_data))
# [0.3 0.1 0.2 0.1]
如果input\u id
与input\u data
的索引不对应,但仍按升序排序,则可以执行以下操作:
import tensorflow as tf
with tf.Graph().as_default(), tf.Session() as sess:
input_data = tf.constant([0.1, 0.2, 0.3])
input_ids = tf.constant([-2, 0, 4])
output_ids = tf.constant([4, -2, 0, -2])
output_idx = tf.searchsorted(input_ids, output_ids)
output_data = tf.gather(input_data, output_idx)
print(sess.run(output_data))
# [0.3 0.1 0.2 0.1]
import tensorflow as tf
with tf.Graph().as_default(), tf.Session() as sess:
input_data = tf.constant([0.1, 0.2, 0.3])
input_ids = tf.constant([3, 1, 6])
output_ids = tf.constant([6, 3, 1, 3])
# From TF v1.13
s = tf.argsort(input_ids)
# Before TF v1.13
s = tf.contrib.framework.argsort(input_ids)
output_idx_s = tf.searchsorted(tf.gather(input_ids, s), output_ids)
output_data = tf.gather(input_data, tf.gather(s, output_idx_s))
print(sess.run(output_data))
# [0.3 0.1 0.2 0.1]
最常见的情况是,input\u id
是未排序的整数数组。在这种情况下,您可以执行以下操作:
import tensorflow as tf
with tf.Graph().as_default(), tf.Session() as sess:
input_data = tf.constant([0.1, 0.2, 0.3])
input_ids = tf.constant([-2, 0, 4])
output_ids = tf.constant([4, -2, 0, -2])
output_idx = tf.searchsorted(input_ids, output_ids)
output_data = tf.gather(input_data, output_idx)
print(sess.run(output_data))
# [0.3 0.1 0.2 0.1]
import tensorflow as tf
with tf.Graph().as_default(), tf.Session() as sess:
input_data = tf.constant([0.1, 0.2, 0.3])
input_ids = tf.constant([3, 1, 6])
output_ids = tf.constant([6, 3, 1, 3])
# From TF v1.13
s = tf.argsort(input_ids)
# Before TF v1.13
s = tf.contrib.framework.argsort(input_ids)
output_idx_s = tf.searchsorted(tf.gather(input_ids, s), output_ids)
output_data = tf.gather(input_data, tf.gather(s, output_idx_s))
print(sess.run(output_data))
# [0.3 0.1 0.2 0.1]
当然,在所有情况下,您都可以使用二次解决方案,将input\u id
中的每个值与output\u id
中的每个值进行比较。我将在下面写它作为参考,但它在时间和内存方面不如前几篇,所以没有理由喜欢它
import tensorflow as tf
with tf.Graph().as_default(), tf.Session() as sess:
input_data = tf.constant([0.1, 0.2, 0.3])
input_ids = tf.constant([3, 1, 6])
output_ids = tf.constant([6, 3, 1, 3])
eq = tf.equal(tf.expand_dims(output_ids, 1), input_ids)
output_idx = tf.argmax(tf.cast(eq, tf.int8), axis=1)
output_data = tf.gather(input_data, output_idx)
print(sess.run(output_data))
# [0.3 0.1 0.2 0.1]
编辑:正如所指出的,也可能存在这样的情况,output\u id
中的值并非都在input\u id
中。在这种情况下,将使用输出数据
的初始值。您可以通过以下方式实现:
import tensorflow as tf
with tf.Graph().as_default(), tf.Session() as sess:
input_data = tf.constant([0.1, 0.2, 0.3])
input_ids = tf.constant([3, 1, 6])
output_data = tf.constant([0., 0., 0., 0., 0.])
output_ids = tf.constant([6, 3, 1, 3, 0])
# From TF v1.13
s = tf.argsort(input_ids)
# Before TF v1.13
s = tf.contrib.framework.argsort(input_ids)
input_ids_s = tf.gather(input_ids, s)
n = tf.size(input_ids)
output_idx_s = tf.minimum(tf.searchsorted(input_ids_s, output_ids), n - 1)
output_data = tf.where(tf.equal(output_ids, tf.gather(input_ids_s, output_idx_s)),
tf.gather(input_data, tf.gather(s, output_idx_s)),
output_data)
print(sess.run(output_data))
# [0.3 0.1 0.2 0.1 0. ]
output\u data
中的初始值不会以任何方式用于结果,对吗?另外,输入\u id
是否总是这样排序?也就是说,它是否总是像[0,1,2,…]
?@jdehesa不,它不是指定要排序的。我也关心这个问题。你的答案非常完美。我想问一下,如果输出\u ID
中有一个ID未包含在输入\u ID
中,该怎么办?也许我应该问一个新问题?@giser_yugang这是一个很好的观点,是的,这也是一个可能的情况。我增加了另一种选择。如果你觉得这个问题值得提出,请随意发布,如果你想在自我回答中使用上面的代码。似乎应该使用tf.size(input_id)-1
而不是tf.size(input_id)
当output_id=tf.constant([6,3,1,3,8])
时。你完美地回答了我的问题。它像一个符咒一样工作。@giser_yugang这是正确的,谢谢你指出错误。@jdehesa THX!!现在我正在测试代码。tf.searchsort()是否仅适用于1.13?。所以我想我应该为tensorflow 1.13设置一个conda env?