Created
October 18, 2018 22:26
-
-
Save necronet/e598ffa67cf08ad13f667318209d17e1 to your computer and use it in GitHub Desktop.
Working with slice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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