[英]Why is numpy.dot() throwing a ValueError: shapes not aligned?
[英]numpy.dot - Shapes error - Neural Network
我正在嘗試將這些A1
和W2
矩陣相乘( Z2 = W2.dot(A1)
):
A1 : [[0.42940542]
[0.55013895]]
W2 : [[-0.4734037 -0.39642393 -0.05440914 -0.24011293 -0.03670913 -0.37523234]
[-0.45501004 0.23881832 0.21831658 0.32237388 0.25674681 0.27956714]]
但是我得到這個錯誤shapes (2,6) and (2,1) not aligned: 6 (dim 1) != 2 (dim 0)
,為什么? 將 (2,1) 與 (2,6) 矩陣相乘不是很正常嗎?
因為我有一個hidden layer with 2 nodes
一個帶有6 nodes
的輸出層
在數學上這是不可能的,因為您將 (2, 6) 矩陣乘以 (2, 1)。 您需要做的就是轉置 W2。
PS:請注意,在線性代數中 np.dot(W2.T, A1) 與 np.dot(A1.T, W2) 不同
import numpy as np
A1 = np.asarray([[0.42940542], [0.55013895]])
W2 = np.asarray([[
-0.4734037, -0.39642393, -0.05440914, -0.24011293, -0.03670913, -0.37523234
], [-0.45501004, 0.23881832, 0.21831658, 0.32237388, 0.25674681, 0.27956714]])
print(W2.shape, A1.shape) # (2, 6), (2, 1)
Z2 = W2.T @ A1
print(Z2)
結果將是: [[-0.45360086] [-0.03884332] [ 0.09674087] [ 0.07424463] [ 0.12548332] [-0.00732603]]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.