繁体   English   中英

ValueError:Theano中的尺寸不匹配错误

[英]ValueError: Dimension mismatch error in Theano

我正在尝试使用python中的Theano库通过神经网络实现AND操作。 这是我的代码:

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: ValueError: Dimension mismatch; shapes are (*, *), (*, 4), (4, 1), (*, *), (*, 4), (4, 1) Apply node that caused the error: 即使更改权重向量w1w2的尺寸,误差仍然相同。 我是Theano的新手,对调试它并不了解。 有人可以帮我吗? 谢谢。

如错误消息中所示,您的输入尺寸不匹配:

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神经网络上获得启发,此示例已在此处进行了处理,通过本教程将有助于您大量了解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()

这样,我们得到形状为(4,3)的a1(与第一层输入相同),而预期的输出为a2(4,1)。

x * w1 =(4,3)*(3,3)=(4,3)= a1。形状

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM