Created
September 29, 2017 18:46
-
-
Save felipessalvatore/55ec56e5cd4245d546409f704a6a6751 to your computer and use it in GitHub Desktop.
tf_gist: strided_slice
This file contains hidden or 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
# strided_slice( | |
# input_, | |
# begin, | |
# end, | |
# strides=None, | |
# begin_mask=0, | |
# end_mask=0, | |
# ellipsis_mask=0, | |
# new_axis_mask=0, | |
# shrink_axis_mask=0, | |
# var=None, | |
# name=None | |
# ) | |
# If you're familiar with numpy arrays, you'll know that you can make slices | |
# via input[start1:end1:step1, start2:end2:step2, ... startN:endN:stepN]. | |
# Basically, a very succinct way of writing for loops | |
# to get certain elements of the array. | |
# Well, strided_slice just allows you to do this fancy indexing without | |
# the syntactic sugar. The numpy example from above just becomes | |
# input[start1:end1:step1, start2:end2:step2, ... startN:endN:stepN] | |
# >>>>>>>>>>>>>>>>>>> | |
# tf.strided_slice(input, | |
# [start1, start2, ..., startN], | |
# [end1, end2, ..., endN], | |
# [step1, step2, ..., stepN]) | |
import tensorflow as tf | |
import numpy as np | |
my_input = np.array([[1, 2, 3], | |
[4, 5, 6], | |
[7, 8, 9]]) | |
result1 = my_input[0:3:1, 0:3:1] | |
result2 = my_input[0:3:2, 0:3:2] | |
tf_result1 = tf.strided_slice(my_input, [0, 0], [3, 3], [1, 1]) | |
tf_result2 = tf.strided_slice(my_input, [0, 0], [3, 3], [2, 2]) | |
sess = tf.InteractiveSession() | |
print(result1) | |
print() | |
print(sess.run(tf_result1)) | |
print() | |
print() | |
print(result2) | |
print() | |
print(sess.run(tf_result2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment