"ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0 ... (size 2 is different from 1)"

"ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0 ... (size 2 is different from 1)"

我才刚刚开始学习Python/NumPy。我想编写一个函数,它将应用具有 2 个输入和 1 个输出以及给定权重矩阵的操作,即两个形状为 (2,1) 的 NumPy 数组并且应该 return 一个形状为 (1,1) 的 NumPy 数组使用 tanh。这是我想出的:

import numpy as np
def test_neural(inputs,weights):
    result=np.matmul(inputs,weights)
    print(result)
    z = np.tanh(result)
    return (z)
x = np.array([[1],[1]])
y = np.array([[1],[1]])

z=test_neural(x,y)
print("final result:",z)

但我收到以下 matmul 错误:

ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 2 is different from 1)

有人可以告诉我我缺少什么吗?

问题是矩阵乘法的维数。

您可以像这样将具有共享维度的矩阵相乘(阅读更多 here):

(M , N) * (N , K) => Result dimensions is (M, K)

你试试乘:

(2 , 1) * (2, 1)

但是尺寸不合法。

所以你必须在乘法之前转置 inputs(只需在矩阵上应用 .T),这样你就可以得到有效的乘法维度:

(1, 2) * (2, 1) => Result dimension is (1, 1)

代码:

import numpy as np
def test_neural(inputs,weights):
    result=np.matmul(inputs.T, weights)
    print(result)
    z = np.tanh(result)
    return (z)
x = np.array([[1],[1]])
y = np.array([[1],[1]])

z=test_neural(x,y)

# final result: [[0.96402758]]
print("final result:",z)