Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yaroslavvb/432de370d9d0f3878ea754af39134b50 to your computer and use it in GitHub Desktop.
Save yaroslavvb/432de370d9d0f3878ea754af39134b50 to your computer and use it in GitHub Desktop.
Example of dynamic stitch to add vector to first row of matrix
a = tf.constant([[1,2],[3,4]])
row_to_add = tf.constant([1, 1])
original_row = a[0]
updated_row = original_row + row_to_add
unchanged_indices = tf.range(tf.size(a))
changed_indices = tf.range(a.get_shape()[0])
a_flat = tf.reshape(a, [-1])
updated_a_flat = tf.dynamic_stitch([unchanged_indices, changed_indices], [a_flat, updated_row])
updated_a = tf.reshape(updated_a_flat, a.get_shape())
print sess.run(updated_a)
# [[2 3]
# [3 4]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment