×

# tf.strided_slice使用简介

### Update

tf.stride_slice(data, begin, end)
tf.slice(data, begin, end)

``````import tensorflow as tf
data = [[[1, 1, 1], [2, 2, 2]],
[[3, 3, 3], [4, 4, 4]],
[[5, 5, 5], [6, 6, 6]]]
x = tf.strided_slice(data,[0,0,0],[1,1,1])
with tf.Session() as sess:
print(sess.run(x))
``````

[[[1]]]

``````x = tf.strided_slice(data,[0,0,0],[2,2,2])
with tf.Session() as sess:
print(sess.run(x))
``````

[[[1 1]
[2 2]]

[[3 3]
[4 4]]]

begin和end指定了位于[begin, end)的一小块切片。注意end中的索引是开区间。

``````x = tf.strided_slice(data,[0,0,0],[2,2,2],[1,1,1])
with tf.Session() as sess:
print(sess.run(x))
``````

[[[1 1]
[2 2]]

[[3 3]
[4 4]]]

``````x = tf.strided_slice(data,[0,0,0],[2,2,2],[1,2,1])
with tf.Session() as sess:
print(sess.run(x))
``````

[[[1 1]]

[[3 3]]]

``````x = tf.strided_slice(data,[0,0,0],[2,2,3],[-1,1,2])
with tf.Session() as sess:
print(sess.run(x))
``````

[]

``````x = tf.strided_slice(input, [1, -1, 0], [2, -3, 3], [1, -1, 1])
with tf.Session() as sess:
print(sess.run(x))
``````

[[[4 4 4]
[3 3 3]]]