矩阵乘法TypeError

时间:2016-07-28 05:31:33

标签: 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_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

我打印了weights转置的输出,这是5x2矩阵,delta是2x1。输出是:

np.transpose(weights[-1]) = [[ 2 -3]
                             [ 0  2]
                             [-1  0]
                             [-1  1]
                             [ 2 -1]]

delta = [-0.14342712 -0.03761959]

因此乘法应该起作用并产生一个5x1矩阵

1 个答案:

答案 0 :(得分:2)

你的最后一行有一个错位的括号。它应该是

print(np.dot(np.transpose(weights[-1])), delta)

而不是

{{1}}