Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
227 views
in Technique[技术] by (71.8m points)

python - Replicating Matrix-Matrix Multiplication using Tensordot

In PyKeops package, there is no available formula for Matrix-Matrix multiplication. Instead, they have implemented something similar to numpy.tensordot. I have two matrices A,B of size m x n and n x n. Is there any way to replicate A @ B using numpy.tensordot.

import numpy as np
m,n = 10, 20
A = np.random.random((m,n))
B = np.random.random((n,n))

result1 = A @ B
result2 = np.tensordot(A,B,(1,1))

print(result1 == result2)

# =======================
# array([[False, False, False, False, False, False, False, False, 
# ....
question from:https://stackoverflow.com/questions/65941480/replicating-matrix-matrix-multiplication-using-tensordot

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Okay I found an answer to this, we can fix this using the following parameters given to tensordot:

import numpy as np
m,n = 10, 20
A = np.random.random((m,n))
B = np.random.random((n,n))

result1 = A @ B
result2 = np.tensordot(A,B,[(1,),(0,)])

print(result1 == result2)

# =======================
# array([[True,  True,  True,  True,  True,  True,  True,  True,  True, 
# ....

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...