tensorflow - map_fn对两个张量的每个可能组合进行计算

时间:2017-12-13 13:45:51

标签: tensorflow tensor

有没有人知道如何使用map_fn或任何其他tensorflow-func对两个输入张量的每个组合进行计算?

所以我想要的是这样的: 具有两个阵列([1,2][4,5])我想要在两个阵列的每个可能组合上具有计算输出的矩阵(例如add)。结果将是:

[[5,6],
 [6,7]]

我使用了map_fn,但这只采用索引方式的元素:

[[5]
 [7]]

有谁知道如何实现这个?

由于

1 个答案:

答案 0 :(得分:2)

您可以为每个Tensor添加新的单位维度,然后依赖广播添加:

import tensorflow as tf
import tensorflow.contrib.eager as tfe
tfe.enable_eager_execution()
first = tf.constant([1, 2])
second = tf.constant([4, 5])
print(first[None, :] + second[:, None])

打印:

tf.Tensor(
[[5 6]
 [6 7]], shape=(2, 2), dtype=int32)