Reputation: 88208
Is the an equivalent operation (or series of operations) that acts like the numpy outer functions?
import numpy as np
a = np.arange(3)
b = np.arange(5)
print np.subtract.outer(a,b)
[[ 0 -1 -2 -3 -4]
[ 1 0 -1 -2 -3]
[ 2 1 0 -1 -2]]
The obvious candidate tf.sub
seems to only act elementwise.
Upvotes: 4
Views: 1659
Reputation: 57963
Use broadcasting:
sess.run(tf.transpose([tf.range(3)]) - tf.range(5))
Output
array([[ 0, -1, -2, -3, -4],
[ 1, 0, -1, -2, -3],
[ 2, 1, 0, -1, -2]], dtype=int32)
To be more specific, given (3, 1)
and (1, 5)
arrays, broadcasting is mathematically equivalent to tiling the arrays into matching (3, 5)
shapes and doing operation pointwise
This tiling is internally implemented by looping over existing data, so no extra memory is needed. When given unequal ranks with shapes like (3, 1)
and (5)
, broadcasting will pad smaller shape with1's
on the left. This means that 1D list like tf.range(5)
is treated as a row-vector, and equivalent to [tf.range(5)]
Upvotes: 9