使用 SageMaker 时使用 Keras 提前停止和回调 [英] Early Stopping and Callbacks with Keras when using SageMaker

查看:23
本文介绍了使用 SageMaker 时使用 Keras 提前停止和回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 sagemaker 来训练 keras 模型.我需要在训练模型时实施早期停止方法.

I am using sagemaker to train a keras model. I need to implement early stoping approach when training the model.

有没有办法传递回调,例如EarlyStopping、Histories..等.

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.

欲了解更多信息:https://github.com/aws/sagemaker-python-sdk/blob/master/src/sagemaker/tensorflow/README.rst#tensorflow-sagemaker-estimators-and-models

这篇关于使用 SageMaker 时使用 Keras 提前停止和回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆