Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Outer addition and subtraction in tensorflow

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.

like image 947
Hooked Avatar asked Jan 06 '23 16:01

Hooked


1 Answers

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

enter image description here

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

like image 113
Yaroslav Bulatov Avatar answered Jan 16 '23 04:01

Yaroslav Bulatov