【问题标题】:ValueError: Dimension mismatch error in TheanoValueError:Theano 中的尺寸不匹配错误
【发布时间】:2017-09-12 20:19:36
【问题描述】:

我正在尝试使用 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:。即使我改变了权重向量w1w2 的维度,错误仍然是一样的。我是 Theano 的新手,对调试它不太了解。 有人可以帮我吗? 谢谢。

【问题讨论】:

    标签: python neural-network theano valueerror


    【解决方案1】:

    您的输入尺寸不匹配,您可以在错误消息中看到:

    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 ]])]
    

    你可以在异或神经网络上获得灵感,这个例子已经处理过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()
    

    这样我们得到形状为 (4,3) 的 a1(与第一层输入相同)和 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
    

    【讨论】:

    • 感谢您的回复。我已经完成了outlace 教程。我发现this one 对 theano 的理解很有帮助。我知道dim mismatch 有问题,但which line in the code is causing the problem 会有所帮助。
    • 明天有时间我会努力解决这个问题,希望能尽快回复您
    • 谢谢阿克塞尔。这是我为摆脱该错误所做的。我将z2 = T.dot(a1,w2.T) 更改为z2 = T.dot(w2, a1.T)。有效。考虑到矩阵向量乘法,我不知道它是如何解决dimension mismatch 问题的。
    • 不客气,我在权重方面犯了一个小错误,所以我再次编辑。我认为这实际上相当于您的更正。
    猜你喜欢
    • 1970-01-01
    • 2018-01-29
    • 1970-01-01
    • 2015-06-24
    • 2020-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多