Z docs:Jak permutować tranpozycję w tensorflow?
Transposes
a
. Permutes the dimensions according to perm.The returned tensor's dimension i will correspond to the input dimension
perm[i]
. Ifperm
is not given, it is set to (n-1...0), where n is the rank of the input tensor. Hence by default, this operation performs a regular matrix transpose on 2-D input Tensors.
Ale to wciąż niewiele wiadomo mi, jak mam być krojenie tensor wejściowego. Na przykład. od docs też:
tf.transpose(x, perm=[0, 2, 1]) ==> [[[1 4]
[2 5]
[3 6]]
[[7 10]
[8 11]
[9 12]]]
Dlaczego jest tak, że perm=[0,2,1]
produkuje tensora 1x3x2?
Po pewnym prób i błędów:
twothreefour = np.array([ [[1,2,3,4], [5,6,7,8], [9,10,11,12]] ,
[[13,14,15,16], [17,18,19,20], [21,22,23,24]] ])
twothreefour
[out]:
array([[[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]],
[[13, 14, 15, 16],
[17, 18, 19, 20],
[21, 22, 23, 24]]])
A gdybym transpozycji:
fourthreetwo = tf.transpose(twothreefour)
with tf.Session() as sess:
init = tf.initialize_all_variables()
sess.run(init)
print (fourthreetwo.eval())
dostaję 4x3x2 do 2x3x4 i to brzmi logicznie.
[out]:
[[[ 1 13]
[ 5 17]
[ 9 21]]
[[ 2 14]
[ 6 18]
[10 22]]
[[ 3 15]
[ 7 19]
[11 23]]
[[ 4 16]
[ 8 20]
[12 24]]]
Ale kiedy użyć parametru perm
wyjście, nie jestem pewien, co ja naprawdę się:
twofourthree = tf.transpose(twothreefour, perm=[0,2,1])
with tf.Session() as sess:
init = tf.initialize_all_variables()
sess.run(init)
print (threetwofour.eval())
[out]:
[[[ 1 5 9]
[ 2 6 10]
[ 3 7 11]
[ 4 8 12]]
[[13 17 21]
[14 18 22]
[15 19 23]
[16 20 24]]]
Dlaczego perm=[0,2,1]
zwraca matrycę 2x4x3 z 2x3x4?
Próbując go ponownie perm=[1,0,2]
:
threetwofour = tf.transpose(twothreefour, perm=[1,0,2])
with tf.Session() as sess:
init = tf.initialize_all_variables()
sess.run(init)
print (threetwofour.eval())
[out]:
[[[ 1 2 3 4]
[13 14 15 16]]
[[ 5 6 7 8]
[17 18 19 20]]
[[ 9 10 11 12]
[21 22 23 24]]]
Dlaczego perm=[1,0,2]
zamian 3x2x4 z 2x3x4?
Czy to oznacza, że parametr jest perm
biorąc moją np.shape
i transpozycji tensor opartą na elementach opiera się na mojej formie tablicy?
tj. :
_size = (2, 4, 3, 5)
randarray = np.random.randint(5, size=_size)
shape_idx = {i:_s for i, _s in enumerate(_size)}
randarray_t_func = tf.transpose(randarray, perm=[3,0,2,1])
with tf.Session() as sess:
init = tf.initialize_all_variables()
sess.run(init)
tranposed_array = randarray_t_func.eval()
print (tranposed_array.shape)
print (tuple(shape_idx[_s] for _s in [3,0,2,1]))
[z]:
(5, 2, 3, 4)
(5, 2, 3, 4)
Chyba tak, ale nie jestem pewien. Jeśli możesz połączyć się z konkretną linią kodu w repozytorium lub bardziej przejrzystym dokumencie, to bardzo ci pomoże! Z góry dziękuję! – alvas
Myślę, że przykład w dokumentach daje wystarczająco dobrą ilustrację tego, co się dzieje. Wymiar 0 jest macierzą wewnętrzną i są one niezmienione przez permutację, wymiar 1 to rzędy wewnętrznych macierzy, a wymiar 2 to kolumny i są one przełączane przez permutację. więc rząd 1 każdej macierzy wewnętrznej przechodzi do kolumny 1 tej samej macierzy wewnętrznej. Czy to ma sens? – maxymoo
To trochę zagmatwane do przeczytania, ale otrzymuję je po tym, jak przekonałem się do większej liczby przykładów podobnych do kodu, który napisałem. – alvas