简体   繁体   English

如何在没有ambuigity的情况下使用numpy.ndarray合并特定的轴

[英]How to merge specific axes without ambuigity with numpy.ndarray

Basically I want to reshape tensors represented by numpy.ndarray. 基本上我想重塑由numpy.ndarray代表的张量。

For example, I want to do something like this (latex notation) 例如,我想做这样的事情(乳胶符号)

A_{i,j,k,l,m,n,p} -> A_{i,jk,lm,np} A_ {i,j,k,l,m,n,p} - > A_ {i,jk,lm,np}

or 要么

A_{i,j,k,l,m,n,p} -> A_{ij,k,l,m,np} A_ {i,j,k,l,m,n,p} - > A_ {ij,k,l,m,np}

where A is an ndarray. 其中A是ndarray。 i,j,k,... denotes the original axes. i,j,k,...表示原始轴。

so the new axis 2 becomes the "flattened" version of axis 2 and 3, etc. If I simply use numpy.reshape, I don't think it knows what axes I want to merge, so it seems ambiguous and error prone. 所以新的轴2成为轴2和3等的“扁平”版本。如果我只是使用numpy.reshape,我不认为它知道我想要合并的轴,所以它看起来很模糊且容易出错。

Is there any neat way of doing this rather than creating another ndarray manually? 有没有任何巧妙的方法来做这个而不是手动创建另一个ndarray?

Using reshape is never ambiguous. 使用reshape永远不会模棱两可。 It doesn't change the memory-layout of the data. 它不会改变数据的内存布局。

Indexing is always done using the strides determined by the shape. 始终使用由形状确定的步幅完成索引。 The right-most axis has stride 1, while the axes to the left have strides given by the product of the sizes to their right. 最右边的轴有1步,而左边的轴有右边的尺寸乘积给出的步幅。

That means for you: as long as you collect neighboring axes, it will do the "right" thing. 这对你意味着:只要你收集相邻的轴,它就会做“正确”的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM