![](/img/trans.png)
[英]Use tf.train.ExponentialMovingAverage() to Train the model
[英]How to use tf.train.ExponentialMovingAverage in keras model in tensorflow2.0
我正在構建一個深度學習模型來做一些分類,但我發現如果我使用隨機裁剪,驗證准確性會波動很大。 我想使用短窗口的訓練模型的運行平均值來幫助驗證准確性,但我對它的使用感到非常困惑。 我正在使用 keras 的 inceptionv3 模型。
我正在使用 keras 的 inceptionv3 模型,並希望使用短窗口的訓練模型的運行平均值來幫助驗證准確性。
ema = tf.train.ExponentialMovingAverage(0.99, step)
maintain_average = ema.apply()
model = tf.keras.applications.inception_v3.InceptionV3(include_top=True, weights=None, input_tensor=None, input_shape=None, pooling=None, classes=4)
def step_decay(epoch):
initial_lrate = 0.045
drop = 0.9
epochs_drop = 2.0
lrate = initial_lrate * math.pow(drop,
math.floor((epoch)/epochs_drop))
return lrate
model.compile(loss='categorical_crossentropy',
optimizer = tf.keras.optimizers.RMSprop(learning_rate=0.02, momentum=0.9, epsilon=0.1),
metrics=['accuracy'])
checkpoint_cb = tf.keras.callbacks.ModelCheckpoint("classification_model_tf2.0_test.h5",
save_best_only=True)
early_stopping_cb = tf.keras.callbacks.EarlyStopping(patience=10,
restore_best_weights=True)
lrate = tf.keras.callbacks.LearningRateScheduler(step_decay)
history = model.fit_generator(
data_generator(train_dataset),
steps_per_epoch=train_steps_per_epoch,
epochs=epochs,
verbose=1,
callbacks=[lrate, checkpoint_cb, early_stopping_cb],
validation_data=data_generator(validation_dataset),
validation_steps=vali_steps_per_epoch,
workers = 0 # runs generator on the main thread
)
我認為,您可以使用 tfa.optimizers.MovingAverage 代替 tf.train.ExponentialMovingAverage https://www.tensorflow.org/addons/api_docs/python/tfa/optimizers/MovingAverage
import tensorflow as tf
import tensorflow_addons as tfa
opt = tf.keras.optimizers.RMSprop(learning_rate=0.02, momentum=0.9, epsilon=0.1)
opt = tfa.optimizers.MovingAverage(opt)
model.compile(
loss='categorical_crossentropy',
optimizer=opt,
metrics=['accuracy'])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.