当前位置: 首页 > 新闻动态 > 软件编程

tensorflow中tf.slice和tf.gather切片函数的使用

作者:用户投稿 浏览: 发布日期:2026-01-11
[导读]:今天小编就为大家分享一篇tensorflow中tf.slice和tf.gather切片函数的使用,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

tf.slice(input_, begin, size, name=None):按照指定的下标范围抽取连续区域的子集

tf.gather(params, indices, validate_indices=None, name=None):按照指定的下标集合从axis=0中抽取子集,适合抽取不连续区域的子集

输出:

input = [[[1, 1, 1], [2, 2, 2]],
   [[3, 3, 3], [4, 4, 4]],
   [[5, 5, 5], [6, 6, 6]]]
tf.slice(input, [1, 0, 0], [1, 1, 3]) ==> [[[3, 3, 3]]]
tf.slice(input, [1, 0, 0], [1, 2, 3]) ==> [[[3, 3, 3],
           [4, 4, 4]]]
tf.slice(input, [1, 0, 0], [2, 1, 3]) ==> [[[3, 3, 3]],
           [[5, 5, 5]]]
           
tf.gather(input, [0, 2]) ==> [[[1, 1, 1], [2, 2, 2]],
        [[5, 5, 5], [6, 6, 6]]]

假设我们要从input中抽取[[[3, 3, 3]]],这个输出在inputaxis=0的下标是1,axis=1的下标是0,axis=2的下标是0-2,所以begin=[1,0,0],size=[1,1,3]。

假设我们要从input中抽取[[[3, 3, 3], [4, 4, 4]]],这个输出在inputaxis=0的下标是1,axis=1的下标是0-1,axis=2的下标是0-2,所以begin=[1,0,0],size=[1,2,3]。

假设我们要从input中抽取[[[3, 3, 3], [5, 5, 5]]],这个输出在inputaxis=0的下标是1-2,axis=1的下标是0,axis=2的下标是0-2,所以begin=[1,0,0],size=[2,1,3]。

假设我们要从input中抽取[[[1, 1, 1], [2, 2, 2]],[[5, 5, 5], [6, 6, 6]]],这个输出在input的axis=0的下标是[0, 2],不连续,可以用tf.gather抽取。input[0]和input[2]

以上这篇tensorflow中tf.slice和tf.gather切片函数的使用就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

免责声明:转载请注明出处:http://www.sczxchw.cn/news/504199.html

扫一扫高效沟通

多一份参考总有益处

免费领取网站策划SEO优化策划方案

请填写下方表单,我们会尽快与您联系
感谢您的咨询,我们会尽快给您回复!