網(wǎng)站首頁 編程語言 正文
作用相同的情況
在若兩個array的維度均為兩維的情況下,兩個函數(shù)的結(jié)果是相同的,例如:
a = np.array([i for i in range(6)]).reshape([2,3])
b = np.array([i for i in range(6)]).reshape([3,2])
"""
a
[[0 1 2]
[3 4 5]]
b
[[0 1]
[2 3]
[4 5]]
"""
>>> np.dot(a,b)
array([[10, 13],
[28, 40]])
>>> np.matmul(a,b)
array([[10, 13],
[28, 40]])
作用不同的情況
在三維的情況下,假設(shè)
a = np.array([i for i in range(12)]).reshape([2,2,3])
b = np.array([i for i in range(12)]).reshape([2,3,2])
"""
a
[[[ 0 ?1 ?2]
? [ 3 ?4 ?5]]
?[[ 6 ?7 ?8]
? [ 9 10 11]]]
b
[[[ 0 ?1]
? [ 2 ?3]
? [ 4 ?5]]
?[[ 6 ?7]
? [ 8 ?9]
? [10 11]]]
"""
>>> np.matmul(a,b)
array([[[ 10, ?13],
? ? ? ? [ 28, ?40]],
? ? ? ?[[172, 193],
? ? ? ? [244, 274]]])
>>> np.matmul(a,b).shape
(2, 2, 2)
這是因為matmul將最后兩維作為矩陣的兩維,相當(dāng)于有2個2 ? 2 2*22?2的矩陣,因此通過對應(yīng)位置矩陣進行矩陣乘法,會得到2個2 ? 2 2*22?2的結(jié)果
>>> np.dot(a,b)
array([[[[ 10, ?13],
? ? ? ? ?[ 28, ?31]],
? ? ? ? [[ 28, ?40],
? ? ? ? ?[100, 112]]],
? ? ? ?[[[ 46, ?67],
? ? ? ? ?[172, 193]],
? ? ? ? [[ 64, ?94],
? ? ? ? ?[244, 274]]]])
>>> np.dot(a,b).shape
(2, 2, 2, 2)
可以看到其結(jié)果與matmul不同并且結(jié)果是四維的,這是因為dot將a數(shù)組的最后一維作為向量,并將b數(shù)組的倒數(shù)第二維作為了另一個向量,因此a中可以看成有2 ? 2 2*22?2個向量,b中有2 ? 2 2*22?2個向量,dot會將a的向量與b的向量全部組合在一起,因此會有( 2 ? 2 ) ? ( 2 ? 2 ) (2*2)*(2*2)(2?2)?(2?2)種結(jié)果。
原文鏈接:https://blog.csdn.net/ACTerminate/article/details/96151132
相關(guān)推薦
- 2022-03-23 C語言新手初階教程之三子棋實現(xiàn)_C 語言
- 2023-12-07 WARNING: Access control is not enabled for the dat
- 2022-09-23 WPF仿LiveCharts實現(xiàn)餅圖的繪制_C#教程
- 2022-04-10 C#實現(xiàn)簡單的計算器小功能_C#教程
- 2023-04-27 React中使用Axios發(fā)起POST請求提交文件方式_React
- 2022-07-10 數(shù)組的遍歷方法有哪些
- 2022-05-09 Docker?Overlay2磁盤空間占用過大清理的方法實現(xiàn)_docker
- 2022-05-17 configure: error: Building GCC requires GMP 4.2+,
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支