2017-04-17 210 views
0

我試圖通過神經網絡使用python中的Theano庫實現AND操作。這裏是我的代碼:ValueError:Theano中的尺寸不匹配錯誤

import theano 
import theano.tensor as T 
import numpy as np 
import matplotlib.pyplot as plt 

#Define variables: 
x = T.matrix('x') 
w1 = theano.shared(np.random.uniform(0,1,(3,3))) 
w2 = theano.shared(np.random.uniform(0,1,(1,3))) 

learning_rate = 0.01 

#Define mathematical expression:c for forward pass 
z1 = T.dot(x,w1) 
a1 = 1/(1+T.exp(-z1)) 
z2 = T.dot(a1,w2.T) 
a2 = 1/(1 + T.exp(-z2)) 
#Let’s determine the cost as follows: 
a_hat = T.vector('a_hat') #Actual output 
cost = -(a_hat*T.log(a2) + (1-a_hat)*T.log(1-a2)).sum() 
dw2,dw1 = T.grad(cost,[w2,w1]) 

train = theano.function(
inputs = [x,a_hat], 
outputs = [a2,cost], 
updates = [ 
    [w1, w1-learning_rate*dw1], 
    [w2, w2-learning_rate*dw2] 
] 
) 

#Define inputs and weights 
inputs = np.array([ 
[0, 0], 
[0, 1], 
[1, 0], 
[1, 1] 
]) 

inputs = np.append(np.ones((inputs.shape[0],1)), inputs, axis=1) 

outputs = np.array([0,0,0,1]).T 

#Iterate through all inputs and find outputs: 
cost = [] 
for iteration in range(30000): 
    pred, cost_iter = train(inputs, outputs) 
    cost.append(cost_iter) 

我無法追溯錯誤ValueError: Dimension mismatch; shapes are (*, *), (*, 4), (4, 1), (*, *), (*, 4), (4, 1) Apply node that caused the error:。即使我更改權重向量w1w2的維度,錯誤仍然保持不變。我是Theano的新手,對調試知之甚少。 有人可以幫我嗎? 謝謝。

回答

0

你有一個輸入的尺寸不匹配,你可以在錯誤信息中看到:

ValueError: Input dimension mis-match. (input[1].shape[1] = 4, input[2].shape[1] = 1) 

更精確的位置:

Inputs values: [array([[-1.]]), array([[ 0., 0., 0., 1.]]), array([[-1.13961476], 
     [-1.28500784], 
     [-1.3082276 ], 
     [-1.4312266 ]]), array([[-1.]]), array([[ 1., 1., 1., 0.]]), array([[ 1.13961476], 
     [ 1.28500784], 
     [ 1.3082276 ], 
     [ 1.4312266 ]])] 

您可以將XOR神經網絡中獲取靈感,這個例子有經過處理here,並通過本教程將幫助你瞭解theano。

我也有艱難的時候,當我第一次嘗試使用theano。很少的例子和教程。也許你也可以檢查Lasagne,它是一個基於Theano的圖書館,但是我覺得它更容易攜帶。

我希望這會幫助你。

[編輯]

使用下面的選項,以幫助您尋找到錯誤來自

theano.config.exception_verbosity='high' 
theano.config.optimizer='None' 

在你的情況,我們可以發現在輸出這個有趣的臺詞:

Backtrace when the node is created(use Theano flag traceback.limit=N to make it longer): 
    File "SO.py", line 30, in <module> 
    cost = -(a_hat*T.log(a2) + (1-a_hat)*T.log(1-a2)).T.sum() 

因此,這裏有我們想要的權重:

w1 = theano.shared(np.random.uniform(0,1,(3,3))) 
w2 = theano.shared(np.random.uniform(0,1,(3,1))) 

有了這個成本函數:

cost = -(a_hat*T.log(a2.T) + (1-a_hat)*T.log(1-a2.T)).sum() 

這樣,我們得到A1形狀(4,3)的(與第一層輸入)和A2(4,1)爲預期的輸出。

X * W1 =(4,3)*(3,3)=(4,3)= a1.shape

A1 * W2 =(4,3)*(3,1)=( 4,1)= a2.shape

您還必須添加此行:

from random import random 

它會給你,用30000次迭代:

The outputs of the NN are: 
The output for x1=0 | x2=0 is 0.0001 
The output for x1=0 | x2=1 is 0.0029 
The output for x1=1 | x2=0 is 0.0031 
The output for x1=1 | x2=1 is 0.9932 
+0

感謝FO你的迴應。我已經通過'outlace'教程。我發現[這一個](https://www.analyticsvidhya.com/blog/2016/04/neural-networks-python-theano/)對theano的理解很有幫助。我知道'dim dimmatch'問題在那裏,但'代碼中的哪一行導致問題'會有幫助。 – chandresh

+0

明天我會努力工作,當我有時間時,我會盡快給您答覆,希望 – Axel

+0

謝謝阿克塞爾。這是我做了什麼來擺脫那個錯誤。我將'z2 = T.dot(a1,w2.T)'改爲'z2 = T.dot(w2,a1.T)'。有效。我不知道如何考慮矩陣向量乘法來解決'尺寸不匹配'問題。 – chandresh