简体   繁体   中英

ValueError: Dimension mismatch error in Theano

I'm trying to implement AND operation via Neural network using Theano library in python. Here is my code:

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)

I'm not able to trace back the error 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: . Even if I change the dimension of the weight vectors w1 and w2 , error remains the same. I'm new to Theano and don't know much about debugging it. Can someone help me out? Thanks.

Your have an input dimension mismatch as you can see in the error message :

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

More precisely here :

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

You can get inspiration on the XOR neural network, this example has been treated here , and going through this tutorial will help you a lot understanding theano.

I also had tough times when I first tried using theano. Very few examples and tutorials. Maybe you can also check Lasagne , it is a library based on Theano, however I felt like it was easier to take in hand.

I hope this will help you.

[EDIT]

Use the following flags to help you find where the error comes from

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

In your case we can find this interesting lines in the output :

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

So, here are the weights we wanted :

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

With this cost function :

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

This way we get a1 of shape (4,3) (same as 1st layer input) and a2 (4,1) as the expected output.

x * w1 = (4,3) * (3,3) = (4,3) = a1.shape

a1 * w2 = (4,3) * (3,1) = (4,1) = a2.shape

You will also have to add this line :

from random import random

It will give you, with 30000 iterations :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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