Skip to content

Instantly share code, notes, and snippets.

@tansey
Created February 10, 2017 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tansey/4942718c2a924c53fc725609a790c58e to your computer and use it in GitHub Desktop.
Save tansey/4942718c2a924c53fc725609a790c58e to your computer and use it in GitHub Desktop.
Unpack a lower triangular Cholesky matrix from a neural network output in Tensorflow
import tensorflow as tf
def unpack_cholesky(q, ndims):
# Build the lower-triangular Cholesky from the flat buffer (assumes q shape is [batchsize, cholsize])
chol_diag = tf.nn.softplus(q[:,:ndims])
chol_offdiag = q[:,ndims:]
chol_rows = []
chol_start = 0
chol_end = 1
for i in xrange(ndims):
pieces = []
if i > 0:
pieces.append(chol_offdiag[:,chol_start:chol_end])
chol_start = chol_end
chol_end += i+1
pieces.append(chol_diag[:,i:i+1])
if i < (ndims-1):
pieces.append(tf.zeros([tf.shape(chol_diag)[0], ndims-i-1]))
print i, pieces
chol_rows.append(tf.concat(1, pieces))
return tf.reshape(tf.concat(1, chol_rows), [tf.shape(chol_diag)[0], ndims, ndims])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment