python - 无论维度如何,如何从张量中选择选定的行?
问题描述
我有一个张tensorflow.Tensor
量(A
A
A[i,:,:,...,:]
i
问题是我事先不知道A
有多少轴。那么我该如何编写这个操作呢?
解决方案
这正是tf.gather()
它的用途。请参见下面的示例代码:
x = tf.reshape(tf.constant([1, 2, 3, 4, 5, 6, 7, 8]), [2, 2, 2])
# This is using tf.gather() on a 3D tensor.
print(tf.gather(x, [1]))
结果是:
<tf.Tensor: shape=(1, 2, 2), dtype=int32, numpy=
array([[[5, 6],
[7, 8]]], dtype=int32)>
x = tf.reshape(tf.constant([1, 2, 3, 4, 5, 6, 7, 8]), [2, 4])
# This is using tf.gather() on a 2D tensor.
print(tf.gather(x, [1]))
结果是:
tf.Tensor([[5 6 7 8]], shape=(1, 4), dtype=int32)
推荐阅读
- amazon-web-services - AWS Quicksight,Redshift“引用嵌套表的子查询不能包含 WINDOW 操作”
- javascript - 替换 Javascript 中的正则表达式匹配项
- javascript - 动态加载数据并执行js的jQuery函数?
- python - 为什么我的对象显示错误的值?
- flutter - (Flutter) pub 聊天失败取决于 charts_flutter >=0.0.1+1 <0.4.0 这需要 SDK 版本 >=1.23.0 <2.0.0 退出代码 1
- request - 分析 gzip 请求
- excel - 甘特图创建导致程序错误
- excel - Excel自动更改数值
- c - 为什么在 printf 中格式化指针时整数需要和号,而字符数组在 C 中不需要?
- facebook - Facebook 点赞数问题