tf.stride_slice(data, begin, end)
tf.slice(data, begin, end)
和tf.slice的区别是slice的end索引是闭区间,stride_slice的end索引是开区间,所以一个截掉最后一列(remove the last element or column of each line)的小技巧是用stride_slice(data, [0, 0], [rows, -1]),但是如果是用slice(data, [0, 0], [rows, -1])则仍是原矩阵。
tf.slice(data, begin, end)
和tf.slice的区别是slice的end索引是闭区间,stride_slice的end索引是开区间,所以一个截掉最后一列(remove the last element or column of each line)的小技巧是用stride_slice(data, [0, 0], [rows, -1]),但是如果是用slice(data, [0, 0], [rows, -1])则仍是原矩阵。
import tensorflow as tf
data = [1,2,3,4,5,6,7,8]
x = tf.strided_slice(data,[0],[4])
y = tf.strided_slice(data,[1],[5])
with tf.Session() as sess:
print(sess.run(x))
print(sess.run(y))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
结果
[1 2 3 4]
[2 3 4 5]
import tensorflow as tf
data = [[1,2,3,4,5,6,7,8],[11,12,13,14,15,16,17,18]]
x = tf.strided_slice(data,[0,0],[1,4])
y = tf.strided_slice(data,[1,1],[2,5])
with tf.Session() as sess:
print(sess.run(x))
print(sess.run(y))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
结果
[[1 2 3 4]]
[[12 13 14 15]]