如何使用 Tensorflow 执行笛卡尔积? [英] How to perform cartesian product with Tensorflow?

查看:40
本文介绍了如何使用 Tensorflow 执行笛卡尔积?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to cross stack two tensors, for example

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

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

Does anybody know which function can do that?

推荐答案

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

I think this does what you need:

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]]

这篇关于如何使用 Tensorflow 执行笛卡尔积?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆