2016-07-28 98 views
0

我试图写一个反向传播算法,并且在尝试执行矩阵乘法时遇到错误。矩阵乘法类型错误

我创建了下面的简单示例与

# 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这是一个5×2矩阵和delta是一个2×1。输出为:

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

delta = [-0.14342712 -0.03761959] 

所以乘法应该工作,并产生一个5X1矩阵

+0

哪里'sigmoid'从何而来?这是重要的吗? – mitoRibo

+0

对不起,忘了复制那部分代码 – Lukasz

回答

2

有你的最后一行放错地方的括号。它应该是

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

,而不是

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