Ich versuche Operation über Neuronales Netzwerk mit Theano-Bibliothek in Python zu implementieren. Hier ist mein Code:ValueError: Dimension Mismatch Fehler in 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)
ich nicht in der Lage bin ValueError: Dimension mismatch; shapes are (*, *), (*, 4), (4, 1), (*, *), (*, 4), (4, 1) Apply node that caused the error:
den Fehler zurückverfolgen. Selbst wenn ich die Dimension der Gewichtsvektoren w1
und w2
ändere, bleibt der Fehler gleich. Ich bin neu in Theano und weiß nicht viel über das Debuggen. Kann mir jemand helfen? Danke.
Dank fo Ihre Antwort. Ich habe ein "outlace" -Tutorial durchlaufen. Ich fand [dieses] (https://www.analyticsvidhya.com/blog/2016/04/neural-networks-python-theano/) eine Menge hilfreich für das Verständnis von theano. Ich weiß, dass "dim fishmatch" -Problem da ist, aber "welche Zeile im Code das Problem verursacht" wird hilfreich sein. – chandresh
Ich werde versuchen, morgen daran zu arbeiten, wenn ich Zeit habe und ich werde mit einer Antwort auf Sie zurückkommen, hoffentlich – Axel
Danke axel. Hier ist, was ich getan habe, um diesen Fehler loszuwerden. Ich habe 'z2 = T.dot (a1, w2.T)' in 'z2 = T.dot (w2, a1.T)' geändert. Es funktionierte. Ich weiß nicht, wie es das "Dimensions-Mismatch" -Problem unter Berücksichtigung der Matrix-Vektor-Multiplikation löst. – chandresh