Outer addition and subtraction in tensorflow
Question: 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`](https://www.tensorflow.org/versions/r0.9/api_docs/python/math_ops.html#sub)
seems to only act elementwise.


Answer: 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](http://i.stack.imgur.com/nHVSb.png)](http://i.stack.imgur.com/nHVSb.png)

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 with`1'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)]`

