I'm going to assume that both a
and b
are one-dimensional tensors.
To get the Cartesian product of two, I would use a combination of tf.expand_dims
and tf.tile
:
a = tf.constant([1,2,3])
b = tf.constant([4,5,6,7])
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)
cartesian_product = tf.concat([tile_a, tile_b], axis=2)
cart = tf.Session().run(cartesian_product)
print(cart.shape)
print(cart)
As a result, you get a tensor len (a) * len (b ) * 2, where each combination of elements a
, and b
is represented in the last measurement.
source
share