2016-07-28 122 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)