Python 矩阵乘法类型错误
我试图编写一个反向传播算法,但在尝试执行矩阵乘法时遇到了一个错误 我创建了下面的简单示例Python 矩阵乘法类型错误,python,numpy,matrix,Python,Numpy,Matrix,我试图编写一个反向传播算法,但在尝试执行矩阵乘法时遇到了一个错误 我创建了下面的简单示例 # necessary functions for this example def sigmoid(z): return 1.0/(1.0+np.exp(-z)) def prime(z): return sigmoid(z) * (1-sigmoid(z)) def cost_derivative(output_activations, y): return (output_
# necessary functions for this example
def sigmoid(z):
return 1.0/(1.0+np.exp(-z))
def prime(z):
return sigmoid(z) * (1-sigmoid(z))
def cost_derivative(output_activations, y):
return (output_activations-y)
# Mock weight and bias matrices
weights = [np.array([[ 1, 0, 2],
[2, -1, 0],
[4, -1, 0],
[1, 3, -2],
[0, 0, -1]]),
np.array([2, 0, -1, -1, 2])]
biases = [np.array([-1, 2, 0, 0, 4]), np.array([-2])]
# The mock training example
q = [(np.array([1, -2, 3]), np.array([0])),
(np.array([2, -3, 5]), np.array([1])),
(np.array([3, 6, -1]), np.array([1])),
(np.array([4, -1, -1]), np.array([0]))]
for x, y in q:
activation = x
activations = [x]
zs = []
for w, b in zip(weights, biases):
z = np.dot(w, activation) + b
zs.append(z)
activation = sigmoid(z)
activations.append(activation)
delta = cost_derivative(activations[-1], y) * prime(zs[-1])
print(np.dot(np.transpose(weights[-1])), delta)
我得到以下错误:
TypeError: Required argument 'b' (pos 2) not found
我已经打印了权重
转置的输出,它是一个5x2矩阵,增量
是一个2x1矩阵。这些产出是:
np.transpose(weights[-1]) = [[ 2 -3]
[ 0 2]
[-1 0]
[-1 1]
[ 2 -1]]
及
因此,乘法应该可以产生一个5x1矩阵最后一行的括号放错了位置。应该是
print(np.dot(np.transpose(weights[-1]), delta))
而不是
print(np.dot(np.transpose(weights[-1])), delta)
你最后一行的括号放错了地方。应该是
print(np.dot(np.transpose(weights[-1]), delta))
而不是
print(np.dot(np.transpose(weights[-1])), delta)
sigmoid
来自哪里?这是导入吗?对不起,忘了复制代码的那部分sigmoid
来自哪里?是导入的吗?对不起,忘了复制那部分代码