【问题标题】:How to use LayerNormalization layer in a Keras sequential Model?如何在 Keras 顺序模型中使用 LayerNormalization 层?
【发布时间】:2021-07-31 04:51:12
【问题描述】:

我刚刚进入 Keras 和 Tensor 流。 我在顺序模型中添加输入规范化层时遇到了很多问题。 现在我的模型是;

 model = tf.keras.models.Sequential()
 model.add(keras.layers.Dense(256, input_shape=(13, ), activation='relu'))
 model.add(tf.keras.layers.LayerNormalization(axis=-1 , center=True , scale=True))
 model.add(keras.layers.Dense(128, activation='relu'))
 model.add(keras.layers.Dense(64, activation='relu'))
 model.add(keras.layers.Dense(64, activation='relu'))
 model.add(keras.layers.Dense(1))
 model.summary()

我的疑问是我是否应该首先执行一个适应功能以及如何在顺序模型中使用它。 谢谢大家!!

【问题讨论】:

    标签: tensorflow keras normalization sequential deeplearning4j


    【解决方案1】:

    我也在努力解决这个问题。根据this example,不需要适应。

    model = tf.keras.models.Sequential([
      # Reshape into "channels last" setup.
      tf.keras.layers.Reshape((28,28,1), input_shape=(28,28)),
      tf.keras.layers.Conv2D(filters=10, kernel_size=(3,3),data_format="channels_last"),
      # LayerNorm Layer
      tf.keras.layers.LayerNormalization(axis=3 , center=True , scale=True),
      tf.keras.layers.Flatten(),
      tf.keras.layers.Dense(128, activation='relu'),
      tf.keras.layers.Dropout(0.2),
      tf.keras.layers.Dense(10, activation='softmax')
    ])
    
    model.compile(optimizer='adam',
                  loss='sparse_categorical_crossentropy',
                  metrics=['accuracy'])
    model.fit(x_test, y_test)
    

    另外,请确保您需要 LayerNormalization。如果我理解正确,这会自行规范每个输入。批量标准化可能更合适。 See this for more info.

    【讨论】:

    • 这个想法是对输入进行规范化,最后我可以在模型的上一步中这样做; norm = tf.keras.layers.experimental.preprocessing.Normalization(axis=-1, dtype=None, mean=None, variance=None) norm.adapt(x_train) x_train = norm(x_train)。非常感谢您的帮助!
    • 所以您必须在模型层之前直接在实际数据上执行此操作?我问,因为如果模型要导出到其他不容易进行标准化的环境,那么在模型中加入标准化可能会更好。
    猜你喜欢
    • 2017-09-30
    • 1970-01-01
    • 2019-05-27
    • 2019-05-11
    • 2018-03-05
    • 2019-11-08
    • 2017-06-13
    • 1970-01-01
    • 2021-08-20
    相关资源
    最近更新 更多