numpy.dot - 形状错误 - 神经网络

numpy.dot - Shapes error - Neural Network

我正在尝试将这些 A1W2 矩阵相乘 (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 nodes6 nodes

的输出层

从数学上讲这是不可能的,因为您将 (2, 6) 矩阵乘以 (2, 1)。您需要做的就是转置W2。

P.S: 请注意,在线性代数中 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]]