使用SageMaker时使用Keras进行早期停止和回调 [英] Early Stopping and Callbacks with Keras when using SageMaker
问题描述
我正在使用Sagemaker来训练keras模型.训练模型时,我需要实施早期停止方法.
I am using sagemaker to train a keras model. I need to implement early stoping approach when training the model.
有没有办法传递诸如EarlyStopping,Histories..etc之类的回调.
Is there a way to pass callbacks such as EarlyStopping, Histories..etc.
以传统方式,我们通常将此参数作为参数传递给keras的fit函数:
In traditional way, we used to pass this as a parameter to keras's fit function:
results = model.fit(train_x_trim, train_y_trim,
validation_data=(test_x, test_y),
epochs=FLAGS.epoch,
verbose=0,
callbacks=[tboard, checkpointer, early_stopping, history])
但是,如果使用SageMaker,则需要调用SageMaker的fit函数,该函数不支持回调.
However, if using SageMaker, we need to call SageMaker's fit function instead which doesn't support callbacks.
from sagemaker.tensorflow import TensorFlow
iris_estimator = TensorFlow(entry_point='training_code.py',
role=role, output_path=model_location,
code_location=custom_code_upload_location,
train_instance_count=1,
train_instance_type='ml.c4.xlarge',
training_steps=1000,
evaluation_steps=100)
任何想法如何在SageMaker中实现回调?
Any idea how to implement callbacks in SageMaker ?
推荐答案
对于抱歉的答复,我深表歉意.
I apologize for the late response.
看起来您在上面指定的Keras代码本质上是您的算法代码.这将在您的用户脚本中定义,在您提供的SageMaker Python SDK示例中为"training_code.py".
It looks like the Keras code you specified above is essentially your algorithm code. This would be defined in your user script, which would be "training_code.py" in the SageMaker Python SDK example you provided.
从TensorFlow 1.11开始,SageMaker预定义的TensorFlow容器支持脚本模式".您应该可以在用户脚本中指定Keras回调.
Starting with TensorFlow 1.11, the SageMaker predefined TensorFlow containers have support for "script mode". You should be able to specify your Keras callbacks within your user script.
这篇关于使用SageMaker时使用Keras进行早期停止和回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!