在tensorflow 2.0中编写自己的自定义训练循环时,如何执行早期停止



要在Tensorflow中执行早期停止,tf.keras有一个非常方便的方法,那就是调用tf.keras.callbacks,然后可以在model.fit((中使用它来执行它。当我们编写自定义训练循环时,我不明白如何使用tf.keras.allbacks来执行它,有人能提供一个关于如何执行它的基本教程吗?

https://www.tensorflow.org/guide/keras/writing_a_training_loop_from_scratch

https://machinelearningmastery.com/how-to-stop-training-deep-neural-networks-at-the-right-time-using-early-stopping/

您有两种方法来创建自定义训练循环。

一个是常见的2个嵌套循环。

或者你可以这样做。提供所有回调和其他功能

提示:代码BELLOW只是代码的一小部分,模型结构没有实现。你应该自己做

更多信息?检查此处

class CustomModel(keras.Model):
def train_step(self, data):
# Unpack the data. Its structure depends on your model and
# on what you pass to `fit()`.
print(data)
x, y = data
with tf.GradientTape() as tape:
y_pred = self(x, training=True)  # Forward pass
# Compute the loss value
# (the loss function is configured in `compile()`)
loss = self.compiled_loss(y, y_pred,
regularization_losses=self.losses)
# Compute gradients
trainable_vars = self.trainable_variables
gradients = tape.gradient(loss, trainable_vars)
# Update weights
self.optimizer.apply_gradients(zip(gradients, trainable_vars))
# Update metrics (includes the metric that tracks the loss)
self.compiled_metrics.update_state(y, y_pred)
# Return a dict mapping metric names to current value
return {m.name: m.result() for m in self.metrics}
# Construct and compile an instance of CustomModel
inputs = keras.Input(shape=(32,))
outputs = keras.layers.Dense(1)(inputs)
model = CustomModel(inputs, outputs)
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['...'])
earlystopping_cb = keras.callbacks.EarlyStopping(patience=10, restore_best_weights=True)
# Just use `fit` as usual
model.fit(train_ds, epochs=3, callbacks=[earlystopping_cb])

更多信息:https://keras.io/getting_started/intro_to_keras_for_engineers/#using-适合定制训练步骤

最新更新