【问题标题】:How to perform cartesian product with Tensorflow? [duplicate]如何使用 Tensorflow 执行笛卡尔积? [复制]
【发布时间】:2019-04-07 00:06:39
【问题描述】:

例如,我正在尝试交叉堆叠两个张量

[0,1,2],[2,3]->[[0,2],[0,3],[1,2],[1,3],[2,2],[2,3]]

有人知道哪个函数可以做到这一点吗?

【问题讨论】:

  • 如果有这样的功能,很有可能在其描述中提到“笛卡尔积”(或者可能是直积)。

标签: python tensorflow


【解决方案1】:

我认为这可以满足您的需要:

import tensorflow as tf

a = tf.constant([0, 1, 2])
b = tf.constant([2, 3])
c = tf.stack(tf.meshgrid(a, b, indexing='ij'), axis=-1)
c = tf.reshape(c, (-1, 2))
with tf.Session() as sess:
    print(sess.run(c))

输出:

[[0 2]
 [0 3]
 [1 2]
 [1 3]
 [2 2]
 [2 3]]

【讨论】:

    猜你喜欢
    • 2018-04-18
    • 2018-03-09
    • 1970-01-01
    • 2011-11-11
    • 2021-05-06
    • 2016-11-22
    • 2012-02-24
    • 2011-09-18
    • 2014-06-18
    相关资源
    最近更新 更多