tf.gather( )的详细解析

tf.gather()函数

tf.gather()
该接口的作用:就是抽取出params的第axis维度上在indices里面所有的index
 
tf.gather(
    params,
    indices,
    validate_indices=None,
    name=None,
    axis=0
)
 
'''
Args:
    params: A Tensor. The tensor from which to gather values. Must be at least rank axis + 1.
    indices: A Tensor. Must be one of the following types: int32, int64. Index tensor. Must be in range [0, params.shape[axis]).
    axis: A Tensor. Must be one of the following types: int32, int64. The axis in params to gather indices from. Defaults to the first dimension. Supports negative indexes.
    name: A name for the operation (optional).
Returns:
    A Tensor. Has the same type as params.
'''

参数说明:

params: A Tensor.
indices: A Tensor. types必须是: int32, int64. 里面的每一个元素大小必须在 [0, params.shape[axis])范围内.
axis: 维度。沿着params的哪一个维度进行抽取indices
返回的是一个tensor

示例

1.当axis参数省略时,即axis默认为axis=0

temp=np.arange(7)*7*2+tf.constant(1,shape=[7])
print(temp) #temp是一个tf张量,直接打印只显示形状和类型,需要创建会话机制

temp1=tf.gather(temp,[0,2,4]) #取temp一维张量中第0,第2,第4的值形成一个tf张量

with tf.Session() as sess:
    print(sess.run(temp))
    print(sess.run(temp1))

结果:

Tensor("add:0", shape=(7,), dtype=int32)
[ 1 15 29 43 57 71 85]
[ 1 29 57]

2.当temp为多维时,这里是4维张量,indices=[0,2],axis=0

input =[ [[[1, 1, 1], [2, 2, 2]],
         [[3, 3, 3], [4, 4, 4]],
         [[5, 5, 5], [6, 6, 6]]],
 
         [[[7, 7, 7], [8, 8, 8]],
         [[9, 9, 9], [10, 10, 10]],
         [[11, 11, 11], [12, 12, 12]]],
 
        [[[13, 13, 13], [14, 14, 14]],
         [[15, 15, 15], [16, 16, 16]],
         [[17, 17, 17], [18, 18, 18]]]
         ]
 
print(tf.shape(input))
with tf.Session() as sess:
    output=tf.gather(input, [0,2],axis=0)#其实默认axis=0
    print(sess.run(output))
 

结果:

Tensor("Shape:0", shape=(4,), dtype=int32)
[[[[ 1  1  1]
   [ 2  2  2]]

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

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


 [[[13 13 13]
   [14 14 14]]

  [[15 15 15]
   [16 16 16]]

  [[17 17 17]
   [18 18 18]]]]

解释:


第一个[ 是列表语法需要的括号,剩下的最里面的三个[[[是axis=0需要搜寻的中括号。这里一共有3个[[[。
indices的[0,2]即取第0个[[[和第2个[[[,也就是第0个和第2个三维立体。

3.当indices=[0,2],axis=1

input =[ [[[1, 1, 1], [2, 2, 2]],
         [[3, 3, 3], [4, 4, 4]],
         [[5, 5, 5], [6, 6, 6]]],
 
         [[[7, 7, 7], [8, 8, 8]],
         [[9, 9, 9], [10, 10, 10]],
         [[11, 11, 11], [12, 12, 12]]],
 
        [[[13, 13, 13], [14, 14, 14]],
         [[15, 15, 15], [16, 16, 16]],
         [[17, 17, 17], [18, 18, 18]]]
         ]
print(tf.shape(input))
with tf.Session() as sess:
    output=tf.gather(input, [0,2],axis=1)#默认axis=0
    print(sess.run(output))

结果:

Tensor("Shape:0", shape=(4,), dtype=int32)
[[[[ 1  1  1]
   [ 2  2  2]]

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


 [[[ 7  7  7]
   [ 8  8  8]]

  [[11 11 11]
   [12 12 12]]]


 [[[13 13 13]
   [14 14 14]]

  [[17 17 17]
   [18 18 18]]]]

解释:

第一个[ 是列表语法需要的括号,先把这个干扰去掉,剩下的每个[[[中所有内侧的 [[ 是axis=1搜索的中括号。
然后[0,2]即再取每个[[[体内的第0个[[和第2个[[,也就是去每个三维体的第0个面和第2个面

  • 7
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
`tf.gather`函数是 TensorFlow 中的一个操作,用于根据索引从张量中收集元素。它的语法如下: ```python tf.gather(params, indices, axis=None, batch_dims=0, name=None) ``` 参数说明: - `params`: 要从中收集元素的张量,可以是任何形状的张量。 - `indices`: 一个张量,指定要收集哪些元素。它可以是任何形状的整数张量。 - `axis`: 一个可选的整数,指定从哪个轴收集元素。默认为`None`,表示要将`indices`解释为一维向量。如果指定`axis`,则`indices`必须是具有相同形状的张量。 - `batch_dims`: 一个可选的整数,指定批次维度的数量。默认为0,表示没有批次维度。例如,如果`params`的形状是`(batch_size, height, width, channels)`,则`batch_dims`可以设置为1,表示`indices`的形状是`(batch_size, num_elements)`。 - `name`: (可选)操作的名称。 `tf.gather`函数的返回值是一个张量,其中包含来自`params`的元素,其索引由`indices`指定。 下面是一个使用`tf.gather`函数的示例代码: ```python import tensorflow as tf # 创建一个3x3的矩阵 x = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # 从第1个轴(即行)收集第0行和第2行 indices = tf.constant([0, 2]) y = tf.gather(x, indices, axis=0) print(y.numpy()) # 输出 [[1 2 3] [7 8 9]] ``` 在这个例子中,我们创建了一个3x3的矩阵`x`,然后使用`tf.gather`函数从第一个轴(即行)收集第0行和第2行,得到了形状为`(2, 3)`的矩阵`y`。注意,`indices`的形状是`(2,)`,因为我们没有指定`axis`参数。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值