TensorFlow:如何切片/收集所有可能的配置?
我有一个带有形状的张量(批处理大小,序列长度,2,n,k)
(在我的特殊情况下,2表示(x,y)空间位置)。 n
表示n变量,k
是每个变量所占的值。
我想生成带有shape 的张量
n
“变量”采取了其每个k
可能的值。
如何使用切片或聚集在张量流中有效地进行此操作?
让我提供一个示例。出于插图的目的,我将省略批处理大小和序列长度的两个领先维度。
假设这是形状的3D张量x
(2,n = 3,k = 4):
第一个配置将是(滥用稍微表示符号),将切片如x [:,(0,1,2),(0,0,0)]
;在这里,n = 3
变量都采用其第一个值。第二个配置是将切片如x [:,(0,1,2),(0,0,1)]]
;在这里,前两个变量正在采用其第一个值,第三个变量正在采用其第二个值。这会持续到4^3 = 64可能的配置,最后一个为x [:,(0,1,2),(3,3,3)]
。
如果我将所有这些都堆叠在一起,则结果将是带状(2,3,4^3)
的张量。
I have a tensor with shape (batch size, sequence length, 2, N, K)
(in my particular case, the 2 represents the (x, y) spatial position). N
represents N variables and K
is the number of values each variable can take.
I want to generate a tensor with shape (batch size, sequence length, 2, N, K^N)
, where K^N
arises from all possible configurations of each of the N
"variables" taking on each of their K
possible values.
How can I efficiently do this in Tensorflow using slicing or gathering?
Let me offer a pictorial example. For the purposes of illustration, I'm going to omit the 2 leading dimensions of batch size and sequence length.
Suppose this is a 3D tensor x
of shape (2, N=3, K=4)
:
The first configuration would be (abusing notation slightly) to take a slice like x[:, (0, 1, 2), (0, 0, 0)]
; here, the N=3
variables are all taking on their first values. The second configuration would be to take a slice like x[:, (0, 1, 2), (0, 0, 1)]
; here, the first two variables are taking on their first values and the third variable is taking on its second value. This continues on, up to the 4^3=64 possible configurations, with the last being x[:, (0, 1, 2), (3, 3, 3)]
.
If I stacked all these up, the result would be a tensor with shape (2, 3, 4^3)
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
iiuc,如果仍然相关,这是您可以用
tensorflow
纯粹解决问题的一种方法(请注意,我与3D张量一起使用并省略了前两个领先维度):对于arbitary
n <
n < /代码>,尝试以下操作:
IIUC and if it is still relevant, here's one way you can solve your problem purely with
Tensorflow
(note I worked with the 3D tensor and omitted the first two leading dimensions):For arbitary
N
, try this: