title: Packing array into lower triangular of a tensor
body: <p>I would like to pack an array of shape <code>(..., n * (n - 1) / 2)</code> into the lower triangular part of a tensor with shape <code>(..., n, n)</code> where <code>...</code> denotes an arbitrary shape. In numpy, I would implement it as</p>
  
  <pre><code>import numpy as np
  
  # Create the array to store data in
  arbitrary_shape = (10, 11, 12)
  n = 5
  target = np.zeros(arbitrary_shape + (n, n))
  # Create the source array
  source = np.random.normal(0, 1, arbitrary_shape + (n * (n - 1) / 2,))
  # Create indices and set values
  u, v = np.tril_indices(n, -1)
  target[..., u, v] = source
  # Check that everything went ok
  print target[0, 0, 0]
  </code></pre>
  
  <p>So far, I've been able to achieve something similar in tensorflow using a combination of <code>transpose</code>, <code>reshape</code> and <code>scatter_update</code> but it feels clumsy.</p>
  
  <pre><code>import tensorflow as tf
  
  # Create the source array
  source = np.random.normal(0, 1, (n * (n - 1) / 2,) + arbitrary_shape)
  
  sess = tf.InteractiveSession()
  
  # Create a flattened representation
  target = tf.Variable(np.zeros((n * n,) + arbitrary_shape))
  # Assign the values
  target = tf.scatter_update(target, u * n + v, source)
  # Reorder the axes and reshape into a square matrix along the last dimension
  target = tf.transpose(target, (1, 2, 3, 0))
  target = tf.reshape(target, arbitrary_shape + (n, n))
  
  # Initialise variables and check results
  sess.run(tf.initialize_all_variables())
  print target.eval()[0, 0, 0]
  
  sess.close()
  </code></pre>
  
  <p>Is there a better way to achieve this?</p>