Skip to content

Instantly share code, notes, and snippets.

@necronet
Created October 18, 2018 22:26
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 necronet/e598ffa67cf08ad13f667318209d17e1 to your computer and use it in GitHub Desktop.
Save necronet/e598ffa67cf08ad13f667318209d17e1 to your computer and use it in GitHub Desktop.
Working with slice
# A example on how to use slice in tensorflow, simple but important to remember.
import tensorflow as tf
x = tf.constant([[[1., 2.], [3., 4. ], [5. , 6. ]],
[[7., 8.], [9., 10.], [11., 12.]]])
# All first elements
# [[[ 1.],[ 3.],[ 5.]], [[ 7.],[ 9.],[11.]]]
res = tf.slice(x, [0, 0, 0], [2, 3, 1])
# All elements of the second matrix
# [[[ 7, 8],[ 9, 10.],[11, 12]]]
res = tf.slice(x, [1, 0, 0], [1, 3, 2])
# Getting last elements of the second matrix
# [[[ 1.],[ 3.],[ 5.]], [[ 7.],[ 9.],[11.]]]
res = tf.slice(x, [0, 0, 0], [2, 3, 1])
# For printing purposes
with tf.Session() as sess:
sess.run(t)
print(res.eval())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment