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]](https://i-blog.csdnimg.cn/blog_migrate/18864dd34bafe1b4b7c52930f05ae864.gif)
如果indices是向量
![output[a_0, ..., a_n, i, b_0, ..., b_n] = params[a_0, ..., a_n, indices[i], b_0, ..., b_n]](https://i-blog.csdnimg.cn/blog_migrate/8d11b3e78df38d323dab0cd3276696f7.gif)
如果indices是高阶张量
![output[a_0, ..., a_n, i, ..., j, b_0, ... b_n] = params[a_0, ..., a_n, indices[i, ..., j], b_0, ..., b_n]](https://i-blog.csdnimg.cn/blog_migrate/52be7cc12fcf30e6aa1fbabe64481030.gif)
返回值:
该函数返回值类型与params相同,具体值是从params中收集过来的,形状为

本文详细介绍了在TensorFlow中如何使用tf.gather和tf.gather_nd从张量中提取值,以及如何通过tf.scatter_nd和tf.scatter_nd_update进行赋值操作。这两个函数提供了灵活的索引和更新机制,适用于各种场景,包括从张量中按索引获取值以及在特定位置更新张量的值。
最低0.47元/天 解锁文章
3万+

被折叠的 条评论
为什么被折叠?



