gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 21:11:47 25 4
gpt4 key购买 nike

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

最佳答案

要删除重复项,可以首先从一维张量 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])

关于python - 来自两个一维张量的不同索引对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55018821/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com