【发布时间】:2020-08-16 01:08:36
【问题描述】:
我们得到了这个 3D input_tensor,它是一个代表 (batch_size, N, 2) 的张量。
- 在哪里,
batch_size = total batches-
N = total predictions, 2 = (label, score)
我想添加每个批次的标签(第一列元素)相同的分数值(第二列元素)。例如,给定这个具有 3 个批次、每批次 4 个预测和 2 个元素的张量;我想要required_output_tensor 作为结果。
条件:没有for loops 或tf.map_fn() 这个答案。原因,tf.map_fn() 在 TF2.X 的 GPU 上运行缓慢。 You can take a look at my sample code here that is working on 2d tensor and I can use the same with tf.map_fn().
input_tensor = tf.constant([
[
[2., 0.7],
[1., 0.1],
[3., 0.4],
[2., 0.8],
],
[
[2., 0.7],
[1., 0.1],
[1., 0.4],
[4., 0.8],
],
[
[3., 0.7],
[1., 0.1],
[3., 0.4],
[4., 0.8],
]
])
required_output_tensor = [
[
[2., 1.5],
[1., 0.1],
[3., 0.4],
],
[
[2., 0.7],
[1., 0.5],
[4., 0.8],
],
[
[3., 1.1],
[1., 0.1],
[4., 0.8],
]
]
编辑:我可以看到我们将如何以不规则的张量结束。在这种情况下,我可以选择每批次的前 k 个元素,其中 k=min(size(smallest_batch)),或者可以硬编码为 topk=2。
编辑 2:添加额外的输入来试用建议的解决方案:
additional_input_tensor = tf.constant([
[
[2., 0.5],
[1., 0.1],
[3., 0.4],
[2., 0.5],
],
[
[22., 0.7],
[11., 0.2],
[11., 0.3],
[44., 0.8],
],
[
[3333., 0.7],
[1111., 0.1],
[4444., 0.4],
[5555., 0.8],
],
[
[2., 0.9],
[1., 0.2],
[5., 0.3],
[2., 0.9],
]
])
【问题讨论】:
-
这个问题一般没有很好的定义,除非你使用不规则张量,因为你可以在每组上有不同数量的重复值,这会导致每组的聚合结果数量不同.一种可能的解决方案是在所有组上都有所有标签,并用零填充缺失标签的分数。
-
@jdehesa 是的,我可以看到。我忘了包括我们最终得到参差不齐的张量的情况。在这种情况下,我可以选择每批说
topk元素。我已在原始问题中添加了澄清 cmets。谢谢! -
@Snehal 你有多少课? (4?)
-
@MarcoCerliani:不幸的是,总类是任意的。如果我给你一个可靠的数字会有帮助吗?如果您有解决方案,那么您可以在答案中使用
total_classes=50。
标签: python tensorflow tensorflow2.0