本文介绍了python中的张量点操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个数组A=[1,2,3]
和B=[[1],[0],[1],[0]]
.问题是如何在python中执行其张量点积.我期望得到:
I have two arrays A=[1,2,3]
and B=[[1],[0],[1],[0]]
. The question how to perform their tensor dot product in python. I am expecting to get:
C=[[1,2,3],
[0,0,0],
[1,2,3],
[0,0,0]]
np.tensordot()函数返回有关数组形状的错误.
The function np.tensordot() returns an error concerning shapes of arrays.
这个问题的补充.如果矩阵的形状完全不同,该怎么做?
A little addition to this question. How to do such operation if matrix are totally different in shape, like:
A=[[1,1,1,1],
[1,1,1,1],
[2,2,2,2],
[3,3,3,3]]
B=[2,1]
C=[[[2,1],[2,1],[2,1],[2,1]],
[[2,1],[2,1],[2,1],[2,1]],
[[4,2],[4,2],[4,2],[4,2]],
[[6,3],[6,3],[6,3],[6,3]]]
推荐答案
尝试使用正确的numpy
数组:
>>> array([[1],[2],[3]]).dot(array([[1,0,1,0]]))
array([[1, 0, 1, 0],
[2, 0, 2, 0],
[3, 0, 3, 0]])
如果对齐方式不同,则使用a.transpose()
可以将其翻转:
If your alignment is different, using a.transpose()
can flip it:
>>> array([[1],[2],[3]]).dot(array([[1,0,1,0]])).transpose()
array([[1, 2, 3],
[0, 0, 0],
[1, 2, 3],
[0, 0, 0]])
如果(出于某种原因)必须使用tensordot()
,请尝试以下操作:
If you (for whatever reason) have to use tensordot()
, try this:
>>> numpy.tensordot([1,2,3], [1,0,1,0], axes=0)
array([[1, 0, 1, 0],
[2, 0, 2, 0],
[3, 0, 3, 0]])
这篇关于python中的张量点操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!