【问题标题】:Keras Permute Layer vs Reshape LayerKeras 置换层与重塑层
【发布时间】:2020-06-08 20:48:32
【问题描述】:

下面的model1和model2都有输出形状(None, 64, 10)。 model1 和 model2 的行为是否完全相同?如果不是,他们的行为有何不同?如果是,当 Reshape 层可以完成 Permute 层所做的所有事情时,为什么我们还需要 Permute 层?

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

model2 = Sequential()
model2.add(Reshape((64, 10), input_shape=(10, 64)))

model2.add(重塑((64, 10), input_shape=(10, 64)))

【问题讨论】:

    标签: tensorflow keras keras-layer


    【解决方案1】:

    你可以自己测试一下……

    model1 = Sequential()
    model1.add(Permute((2, 1), input_shape=(10, 64)))
    
    model2 = Sequential()
    model2.add(Reshape((64, 10), input_shape=(10, 64)))
    
    X = np.random.uniform(0,1, (3,10,64))
    tf.reduce_all(model1(X) == model2(X)) # ===> False
    

    Permute 与 Reshape 不同。 Permute 只是简单地更改轴上的元素,而 Reshape 就像在展平元素后重新排序元素

    model3 = Sequential()
    model3.add(Flatten(input_shape=(10, 64)))
    model3.add(Reshape((64, 10)))
    
    tf.reduce_all(model2(X) == model3(X)) # ===> True
    

    【讨论】:

      猜你喜欢
      • 2018-09-12
      • 2019-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-22
      相关资源
      最近更新 更多