2017-02-22 1 views
3

Ich habe schwer zu verstehen, was und wie funktioniert dimshuffle() in Theano implementiert? Ich habe die folgenden Beispiele in der offiziellen Dokumentation erhalten, konnte aber deren Bedeutung nicht verstehen.Wie funktioniert die Funktion Dimshuffle in Theano

Kann jemand erklären, was jedes Beispiel im Folgenden bedeutet?

(‘x’) -> make a 0d (scalar) into a 1d vector 
(0, 1) -> identity for 2d vectors 
(1, 0) -> inverts the first and second dimensions 
(‘x’, 0) -> make a row out of a 1d vector (N to 1xN) 
(0, ‘x’) -> make a column out of a 1d vector (N to Nx1) 
(2, 0, 1) -> AxBxC to CxAxB 
(0, ‘x’, 1) -> AxB to Ax1xB 
(1, ‘x’, 0) -> AxB to Bx1xA 
(1,) -> This remove dimensions 0. It must be a broadcastable dimension (1xA to A) 

Bitte beachten Sie, ich weiß, über broadcasting concept in numpy Python.

Antwort

7

Ohne 'x', dimshuffle ist die gleiche wie transpose

Für Erklärungszwecke, lassen Sie uns gefälschte numpy hat eine dimshuffle Funktion

x = np.arange(60).reshape((3,4,5)) 
x.dimshuffle(0, 1, 2).shape # gives (3, 4, 5) 
x.dimshuffle(2, 1, 0).shape # gives (5, 4, 3) 

Da haben wir:

shp = (3,4,5) 
(shp[2], shp[1], shp[0]) == (5, 4, 3) 

Die Argumente 2, 1, 0 dimshuffle bedeutet nur die Permutation zum Formtupel.

Wann immer es 'x' vorhanden ist, es 1 große Dimension in der Anordnung ergänzt:

x = np.arange(60).reshape((3,4,5)) 
x.dimshuffle(2, 1, 0, 'x').shape # (5, 4, 3, 1) 
x.dimshuffle(2, 1, 'x', 0).shape # (5, 4, 1, 3) 
x.dimshuffle(2, 'x', 1, 0).shape # (5, 1, 4, 3) 

Immer dann, wenn die Permutation einen Index (oder mehrere) fehlt, werden diese Indizes von Form Tupels entfernt, vorausgesetzt, dass sie 1 (die broadcast ist)

x = np.arange(1337).reshape(2,1337,1) 
y = x.dimshuffle(1,0) # this works since shape[2] is 1 
y.shape # (1337, 2) 
z = y.dimshuffle(1) # ERROR 

Hinweis Theanos hat keine Möglichkeit, die Form von symbolischen Tensor, um zu bestimmen, so dimshuffle mit dimensional Entfernung broadcastable Attribut beziehen. (Dies unterscheidet sich von tensorflow wie Sie Form zur Compile-Zeit angeben)

>>> x = T.vector() 
>>> x.broadcastable 
(False,) 
>>> y = x.dimshuffle('x', 0, 'x') 
>>> y.broadcastable # the new dims are broadcastable because we added via 'x' 
(True, False, True) 

Mit dimshuffle können Sie mehrere Anrufe transpose und expand_dims (Anmerkung Theano hat keine expand_dims)

speichern
Verwandte Themen