【问题标题】:swap tensor axis in keras在keras中交换张量轴
【发布时间】:2017-01-26 16:17:19
【问题描述】:

我想将图像批次的张量轴从 (batch_size, row, col, ch) 交换为 (batch_size, ch, row, col)。

在 numpy 中,这可以用

来完成
X_batch = np.moveaxis( X_batch, 3, 1)

我将如何在 Keras 中做到这一点?

【问题讨论】:

    标签: tensorflow keras


    【解决方案1】:

    您可以使用与np.transpose() 完全相同的K.permute_dimensions()

    例子:

    import numpy as np 
    from keras import backend as K 
    
    A = np.random.random((1000,32,64,3))
    # B = np.moveaxis( A, 3, 1)
    C = np.transpose( A, (0,3,1,2))
    
    print A.shape
    print C.shape
    
    A_t = K.variable(A)
    C_t = K.permute_dimensions(A_t, (0,3,1,2))
    
    print K.eval(A_t).shape
    print K.eval(C_t).shape
    

    【讨论】:

      【解决方案2】:

      使用keras.layers.Permute(dims)dimsdoes not include the samples dimension

      model.add(Permute((2, 1), input_shape=(10, 64)))
      

      【讨论】:

        猜你喜欢
        • 2016-11-07
        • 1970-01-01
        • 2017-12-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-29
        • 2018-12-02
        相关资源
        最近更新 更多