2017-04-17 209 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