【问题标题】:Creating all possible combinations from vectors in Tensorflow在 Tensorflow 中从向量创建所有可能的组合
【发布时间】:2018-06-20 14:59:16
【问题描述】:

假设我有 3 个一维张量

x = tf.constant([0,1,2])
y = tf.constant([0,1])
x = tf.constant([0])

我正在尝试得到这个输出

[[0,0,0],
 [0,1,0],
 [1,0,0],
 [1,1,0],
 [2,0,0],
 [2,1,0]]

是否可以在不使用 tf.while_loop 的情况下做到这一点?

【问题讨论】:

标签: tensorflow combinations


【解决方案1】:
import tensorflow as tf

a = tf.constant([1,2,3]) 
b = tf.constant([4,5,6,7])
c = tf.constant([8,9,10])

def cart_prod(a,b,c):
    tile_a = tf.tile(tf.expand_dims(a, 1), [1, tf.shape(b)[0]])  
    tile_a = tf.expand_dims(tile_a, 2) 
    tile_b = tf.tile(tf.expand_dims(b, 0), [tf.shape(a)[0], 1]) 
    tile_b = tf.expand_dims(tile_b, 2) 
    cart = tf.concat([tile_a, tile_b], axis=2) 
    cart = tf.reshape(cart,[-1,2])

    tile_c = tf.tile(tf.expand_dims(c, 1), [1, tf.shape(cart)[0]])  
    tile_c = tf.expand_dims(tile_c, 2) 
    tile_c = tf.reshape(tile_c, [-1,1])

    cart = tf.tile(cart,[tf.shape(c)[0],1])
    cart = tf.concat([cart, tile_c], axis=1) 
    return cart

with tf.Session() as sess:
    cart = tf.Session().run(cart_prod(a,b,c)) 
    print(cart) 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-08
    • 2020-02-29
    • 1970-01-01
    相关资源
    最近更新 更多