具有转置的4D数组的块状点积失败

具有转置的4D数组的块状点积失败

本文介绍了具有转置的4D数组的块状点积失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于尺寸为(60,64,2,2)的4D数组A,需要使用其转置A_t计算点积.

For a 4D array A with dimensions of (60,64,2,2), need to calculate the dot product with its transpose A_t.

A_t的尺寸为(2,2,64,60).下面是我的工作.

A_t is of dimension(2,2,64,60). Below is what I do.

A_t = np.transpose(A)
A_At = A_t.dot(A)

点积引发错误

ValueError: shapes (2,2,64,60) and (60,64,2,2) not aligned: 60 (dim 3) != 2 (dim 2)

我不正确地处理了移调吗?我也尝试过将单个数组转换为numpy矩阵(尽管不建议按照多个帖子),然后计算点积,但是却遇到了另一个错误.

Am I taking the transpose incorrectly? I have also tried converting the individual arrays to numpy matrices(even though not recommended as per several posts) and then computing the dot product but I get a different error.

还研究了诸如广播之类的麻木主题,但我找不到4D阵列的任何有用示例.

Have also researched numpy topics such as broadcasting but I could not find any useful example for 4D arrays.

任何输入将不胜感激.谢谢!

Any inputs would be grateful. Thanks!

注意:我使用的是python 2.7

Note: I'm using python 2.7

推荐答案

基于您的知识驱动,希望在末尾有2x2数组,那么使用 xarray.dot 即可完成此类任务.拿起您的A

On your knowledge-driven wish of having a 2x2 array at the end, what about using xarray.dot for that kind of task. With your A in hand

>>> A.shape
(60, 64, 2, 2)

你会做

>>> xA   = xr.DataArray(A, dims=['d1','d2','d3','d4'])
>>> xA_t = xA.T
>>> xr.dot(xA_t, xA, dims=['d1','d2']).shape
(2, 2)

这篇关于具有转置的4D数组的块状点积失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 23:05