tensorflow中张量的提取值和赋值操作

本文详细介绍了在TensorFlow中如何使用tf.gather和tf.gather_nd从张量中提取值,以及如何通过tf.scatter_nd和tf.scatter_nd_update进行赋值操作。这两个函数提供了灵活的索引和更新机制,适用于各种场景,包括从张量中按索引获取值以及在特定位置更新张量的值。
摘要由CSDN通过智能技术生成

tf.gather和gather_nd从params中收集数值,tf.scatter_nd 和 tf.scatter_nd_update用updates更新某一张量。严格上说,tf.gather_nd和tf.scatter_nd_update互为逆操作。

已知数值的位置,从张量中提取数值:tf.gather, tf.gather_nd

tf.gather indices每个元素(标量)是params某个axis的索引,tf.gather_nd 中indices最后一个阶对应于索引值。

tf.gather函数

函数原型

gather(
    params,
    indices,
    validate_indices=None,
    name=None,
    axis=0
)

params是要查找的张量,indices是要查找值的索引(int32或int64),axis是查找轴,name是操作名。

如果indices是标量

output[a_0, ..., a_n, b_0, ..., b_n] = params[a_0, ..., a_n, indices, b_0, ..., b_n]

如果indices是向量

output[a_0, ..., a_n, i, b_0, ..., b_n] = params[a_0, ..., a_n, indices[i], b_0, ..., b_n]

如果indices是高阶张量

output[a_0, ..., a_n, i, ..., j, b_0, ... b_n] = params[a_0, ..., a_n, indices[i, ..., j], b_0, ..., b_n]

返回值:

该函数返回值类型与params相同,具体值是从params中收集过来的,形状为

  • 6
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值