来自两个一维张量的不同索引对

时间:2019-03-06 08:43:31

标签: python tensorflow

我有两个离散的概率分布。这些表示为TensorFlow 1D张量p1p2,每个张量的长度为len。我想生成一对索引(i, j),其中i是从第一个概率分布生成的,而j是从第二个概率分布生成的。我要生成很多对,直到总共有len个不同的对。如何使用while循环或扫描在TensorFlow中实现此目标?

1 个答案:

答案 0 :(得分:0)

要删除重复项,可能首先要从1d张量p1和p2中删除重复项。

p1 = tf.unique(tf.constant([1, 2, 3]))
p2 = tf.unique(tf.constant([3, 4, 5]))

计算对

p1 = tf.constant([1, 2, 3])
p2 = tf.constant([3, 4, 5])

# adding zeros columns
t1 = tf.stack([p1, tf.zeros([a.shape[0]], dtype="int32")], axis=1)
t2 = tf.stack([tf.zeros([b.shape[0]], dtype="int32"), p2], axis=1)
x = tf.expand_dims(t1, 0)
y = tf.expand_dims(t2, 1)

# broadcasting with addition to concatenate the two tensors
# reshaping to have the 2d tensor
c = tf.reshape(tf.add(x, y), [-1, 2])