2016-07-22 48 views
7

docs如何在tensorflow中置换转位?

Transposes a . Permutes the dimensions according to perm.

The returned tensor's dimension i will correspond to the input dimension perm[i] . If perm 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.

但它仍然是一个有点不清楚我,我应该如何切分的输入张量。例如。从文档太:

tf.transpose(x, perm=[0, 2, 1]) ==> [[[1 4] 
             [2 5] 
             [3 6]] 

            [[7 10] 
             [8 11] 
             [9 12]]] 

为什么是它perm=[0,2,1]产生1x3x2张量?

一些试验和错误后:

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 

[出]:

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

如果我转它:

fourthreetwo = tf.transpose(twothreefour) 
with tf.Session() as sess: 
    init = tf.initialize_all_variables() 
    sess.run(init) 
    print (fourthreetwo.eval()) 

我得到一个4x3x2的2x3x4和这听起来很合理。

[出]:

[[[ 1 13] 
    [ 5 17] 
    [ 9 21]] 

[[ 2 14] 
    [ 6 18] 
    [10 22]] 

[[ 3 15] 
    [ 7 19] 
    [11 23]] 

[[ 4 16] 
    [ 8 20] 
    [12 24]]] 

但是,当我使用perm参数输出,我不知道什么我真的越来越:

twofourthree = tf.transpose(twothreefour, perm=[0,2,1]) 
with tf.Session() as sess: 
    init = tf.initialize_all_variables() 
    sess.run(init) 
    print (threetwofour.eval()) 

[出]:

[[[ 1 5 9] 
    [ 2 6 10] 
    [ 3 7 11] 
    [ 4 8 12]] 

[[13 17 21] 
    [14 18 22] 
    [15 19 23] 
    [16 20 24]]] 

为什么perm=[0,2,1]从2x3x4?返回2x4x3矩阵?

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

[出]:

[[[ 1 2 3 4] 
    [13 14 15 16]] 

[[ 5 6 7 8] 
    [17 18 19 20]] 

[[ 9 10 11 12] 
    [21 22 23 24]]] 

为什么perm=[1,0,2]回报从2x3x4一个3x2x4?

这是否意味着perm参数考虑我np.shape,并根据我的数组形状调换基于元素的张量?

I.e. :

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

[出]:

(5, 2, 3, 4) 
(5, 2, 3, 4) 

回答

13

我觉得perm被置换的尺寸。例如perm=[0,2,1]dim_0 -> dim_0, dim_1 -> dim_2, dim_2 -> dim_1的简称。所以对于二维张量,perm=[1,0]只是矩阵转置。这回答了你的问题了吗?

+0

我想是这样,但我不知道。如果你可以链接到repo中的特定代码行或更清晰的文档,这将会有很大的帮助!提前致谢! – alvas

+0

我认为文档中的例子给出了一个足够好的例子来说明发生了什么。维0是内矩阵,并且它们通过置换不变,维1是内矩阵的行,维2是列,并且它们通过置换来切换。因此每个内部矩阵的第1行进入相同内部矩阵的第1列。这是否有意义? – maxymoo

+0

阅读起来有点令人困惑,但在尝试用更多类似于我发布的代码的例子说服自己之后,我得到了它。 – alvas

1
A=[2,3,4] matrix, using perm(1,0,2) will get B=[3,2,4]. 

说明:

Index=(0,1,2) 
A =[2,3,4] 
Perm =(1,0,2) 
B =(3,2,4) --> Perm 1 from Index 1 (3), Perm 0 from Index 0 (2), Perm 2 from Index 2 (4) --> so get (3,2,4) 
+5

请添加一些解释。 –

+0

使用上面的例子(我在这里再次粘贴) A = [2,3,4]矩阵,使用perm(1,0,2)将得到B = [3,2,4]。 2是索引0; 3是索引1; 4是索引2。 –