tensorflow学习:tf.strided_slice函数

 

关于tf.strided_slice函数tensorflow官方文档介绍链接https://www.tensorflow.org/api_docs/python/tf/strided_slice过于简单,本文使用简短的测试用例补充说明strided_slice的详细用法。首先给出接口定义:

tf.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
)

其中input表示一个tensortf.strided_slice完成对该tensor的切片操作,begin,end 以及strides参数是长度为N的向量(N等于input的维度)。begin[i]end[i]表示对应的维度的切片开始下标和结束下标。使用python测试用例说明如下:

import tensorflow as tf
import numpy as np
data_a = [
        [[1, 1, 1], [2, 2, 2]], 
        [[3, 3, 3], [4, 4, 4]], 
        [[5, 5, 5], [6, 6, 6]]
        ]
print np.shape(data_a)
c   = tf.strided_slice(data_a, [0,0,0],[3,2,3],[1,1,1], 0, 0, 0, 0, 0)
c1  = tf.strided_slice(data_a, [1,0,0],[3,2,3],[1,1,1], 0, 0, 0, 0, 0)
c2  = tf.strided_slice(data_a, [0,0,0],[2,2,3],[1,1,1], 0, 0, 0, 0)
c3  = tf.strided_slice(data_a, [0,0,0],[3,2,3],[2,1,1], 0, 0, 0, 0)
with tf.Session() as session:
  session.run(tf.global_variables_initializer())
  out = session.run(c)
  out1 = session.run(c1)
  out2 = session.run(c2)
  out3 = session.run(c3)
print 'c shape:', np.shape(out)
print out
print 'c1 shape:', np.shape(out1)
print out1
print 'c2 shape:', np.shape(out2)
print out2
print 'c3 shape:', np.shape(out3)
print out3

结果分析:

#c作为参考结果,beginend均跟原始shape一致,stride步长为1,结果为原始输入不做切片:

c shape: (3, 2, 3)

[

    [[1 1 1] [2 2 2]] 

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

    [[5 5 5] [6 6 6]]

]

#c1begin[0] = 1表示第一个维度从下标1开始切分数据:

c1 shape: (2, 2, 3)

[

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

    [[5 5 5] [6 6 6]]

]

#c2end[0]=2,表示第一个维度切分出索引为01的数据,索引为2及以后的数据均被切除:

c2 shape: (2, 2, 3)

[

    [[1 1 1] [2 2 2]]

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

]

#c3stride[0]=2,表示第一个维度按步长为2进行切分,所以中间的[[3 3 3] [4 4 4]]给切掉了:

c3 shape: (1, 2, 3)

[

    [[1 1 1] [2 2 2]]

    [[5 5 5] [6 6 6]]

]

1.begin_mask掩码:使用二进制flaginput tensor不同维度进行标志,第i位设置为1begin[i]参数对应的第i维度设置无效,表示该维度的起始索引从0开始。

参考:c  = tf.strided_slice(data_a, [0,0,2],[3,2,3],[1,1,1], 0, 0, 0, 0, 0)

结果:shape: (3, 2, 1)

[

    [[1][2]]

    [[3][4]]

    [[5][6]]

]

测试1c  = tf.strided_slice(data_a, [0,0,2],[3,2,3],[1,1,1], 0b100, 0, 0, 0, 0)  

结果:shape: (3, 2, 3)

[

    [[1 1 1] [2 2 2]]

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

    [[5 5 5] [6 6 6]]

]

测试2c  = tf.strided_slice(data_a, [0,0,2],[3,2,3],[1,1,1], 0b110, 0, 0, 0, 0)

结果:shape: (3, 2, 3)

[

      [[1 1 1] [2 2 2]]

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

      [[5 5 5] [6 6 6]]

]

2.end_mask掩码:功能类似begin_mask。使用二进制flaginput tensor不同维度进行标志,第i位设置为1end参数对应的该维度设置无效,表示该维度切分的结束索引到列表最后即切分到尽可能大的范围。

参考:c  = tf.strided_slice(data_a, [0,0,0],[3,2,1],[1,1,1], 0, 0, 0, 0, 0)

结果:shape: (3, 2, 2)

[

      [[1] [2]]

      [[3] [4]]

      [[5] [6]]

]

测试:c  = tf.strided_slice(data_a, [0,0,0],[3,2,1],[1,1,1], 0, 0b100, 0, 0, 0)

结果:shape: (3, 2, 3)

[

      [[1 1 1] [2 2 2]]

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

      [[5 5 5] [6 6 6]]

]

3.ellipsis_mask 掩码: 不为零的维度不需要进行切分操作(只允许出现一个非零位

举例:

c  = tf.strided_slice(data_a, [0,0,2],[3,2,3],[1,1,1], 0, 0, 0, 0, 0)

结果:shape: (3, 2, 2)

[

    [[1] [2]]

    [[3] [4]]

    [[5] [6]]

]

c  = tf.strided_slice(data_a, [0,0,2],[3,2,3],[1,1,1], 0, 0, 0b100, 0, 0)

结果:shape: (3, 2, 3)

[

    [[1 1 1] [2 2 2]]

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

    [[5 5 5] [6 6 6]]

]

4. new_axis_mask掩码如果第i位出现1begin, end, and stride对所有维度参数无效,并在第1位上增加一个大小为1的维度。

参考:c  = tf.strided_slice(data_a, [1,0,0],[3,2,3],[1,1,1], 0, 0, 0, 0b000, 0)

结果:shape: (2, 2, 3)

[     

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

[[5 5 5] [6 6 6]]

]

测试:c  = tf.strided_slice(data_a, [1,0,0],[3,2,3],[1,1,1], 0, 0, 0, 0b001, 0)

结果:shape: (1, 2, 2, 3)

[

[

            [[1 1 1] [2 2 2]]

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

]

]

5. shrink_axis_mask掩码:第i位设置为1则意味着第i维度缩小为1。

参考:c1  = tf.strided_slice(data_a, [0,0,0],[3,2,3],[1,1,1], 0, 0, 0, 0, 0)

结果:shape: (3, 2, 3)

[

    [[1 1 1] [2 2 2]]

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

    [[5 5 5] [6 6 6]]

]

测试:c1  = tf.strided_slice(data_a, [0,0,0],[3,2,3],[1,1,1], 0, 0, 0, 0, 0b100)

结果:shape: (3, 2)

[     

    [1 2]

    [3 4]

    [5 6]             

]

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值