2016-03-19 9 views
0

Ich habe eine Theano-Kovarianz-Matrix, und ich versuche, Element weise Quadrat davon zu berechnen. Ich habe folgenden Code für gleiche geschrieben:Elementweise Quadrat der Theano-Matrix

import theano 
    a, b = theano.tensor.matrices('a', 'b') 
    square = theano.function([a, b], a * b) 
    sq = square(cov, cov) 

wo cov Kovarianzmatrix berechnet:

y1_pre = T.dot(self.x, self.W_left) + self.b_left 
    y1 = activation(y1_pre, self.hidden_activation) 
    y2_pre = T.dot(self.x, self.W_right) + self.b_right 
    y2 = activation(y2_pre, self.hidden_activation) 
    y1_mean = T.mean(y1, axis=0) 
    y1_centered = y1 - y1_mean 
    y2_mean = T.mean(y2, axis=0) 
    y2_centered = y2 - y2_mean 
    cov = T.sum(y1_centered[:, :, None] * y2_centered[:, None, :], axis=0) 

Aber es wirft folgende Fehlermeldung:

TypeError: ('Bad input argument to theano function with name "cov.py:114" at index 0(0-based)', 'Expected an array-like object, but found a Variable: maybe you are trying to call a function on a (possibly shared) variable instead of a numeric array?') 

Ich weiß, es ist trivial zu tun, konnte aber immer noch keine mögliche Lösung finden. Bitte helfen Sie mir in dieser Hinsicht.

+0

Was ist 'cov'? – JeD

+0

theano Kovarianz Matrix – Shweta

+0

Haben Sie versucht mit 'sq = Quadrat (cov [:], cov [:])'? Ich assme theano Matrizen verhalten sich wie numpy Arrays? – JeD

Antwort

1

Ihre Eingabe in die von Ihnen kompilierte Theano-Funktion kann kein symbolischer Ausdruck sein, sondern muss ein NumPy-Array oder eine gemeinsam genutzte Variable sein. Zum Beispiel:

A = T.matrix('input matrix') 
B = T.matrix('dummy matrix') 
C = np.random.rand(5,5).astype(theano.config.floatX) 
squared = A**2 
get_squared = theano.function([A], squared) 

Wenn ich den folgenden Befehl ausführen:

get_squared(B) 

ich die folgende Fehlermeldung erhalten:

TypeError: ('Bad input argument to theano function with name ":1" at index 0(0-based)', 'Expected an array-like object, but found a Variable: maybe you are trying to call a function on a (possibly shared) variable instead of a numeric array?')

Allerdings, wenn ich laufen:

get_squared(C) 

Ich bekomme die quadratische Matrix zurück.

Ich bin über Ihre Code-Basis nicht sicher, wie sie strukturiert ist, aber eine sehr direkte (möglicherweise naiv, aber es wird funktionieren) Lösung ist ein symbolischer Ausdruck für Ihre squared Kovarianzmatrix zu erstellen und zurückzugeben, als Teil Ihrer Funktion. Zum Beispiel, wenn y1 und y2 Teil des Graphen sind cov zu berechnen, können Sie eine Theanos Funktion erstellen, die die Kovarianz zurück zum Quadrat:

cov = ... # (some expressions involving y1 and y2 as in your original post) 
get_cov_squared = theano.function([y1,y2], cov**2) 

Aber noch einmal, Ihre Eingabe in die Funktion muß tatsächlich Arrays oder gemeinsam genutzte Variablen sein , nicht symbolische Ausdrücke.

+0

Danke Indie für großartige Erklärung !!! Aber ich bin Neuling für Theano und es ist immer noch nicht für mich. Ich habe meinen Code aktualisiert, um mehr über die Codebasis zu erfahren – Shweta