keras - 使用 SageMaker 时使用 Keras 提前停止和回调

标签 keras amazon-sagemaker

我正在使用 sagemaker 训练 keras 模型。我需要在训练模型时实现提前停止方法。

有没有办法传递诸如 EarlyStopping、Histories.. 等回调?

在传统方式中,我们习惯将其作为参数传递给 keras 的 fit 函数:

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 函数,该函数不支持回调。

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 中实现回调吗?

最佳答案

对于迟到的回复,我深表歉意。

看起来您上面指定的 Keras 代码本质上就是您的算法代码。这将在您的用户脚本中定义,在您提供的 SageMaker Python SDK 示例中为“training_code.py”。

从 TensorFlow 1.11 开始,SageMaker 预定义的 TensorFlow 容器支持“脚本模式”。您应该能够在用户脚本中指定 Keras 回调。

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

关于keras - 使用 SageMaker 时使用 Keras 提前停止和回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53486118/

相关文章:

python - Tensorflow 模型预测很慢

keras - Keras 中的 ResNet50v2

amazon-sagemaker - 创建训练作业后,如何在新数据上重新训练 sagemaker 模型

python - 在 AWS Sagemaker 中使用 Tensorflow Estimator 时如何在 S3 中保存 Tensorflow 模型(作为/output/model.tar.gz)

python - Keras - 数组形状与 model.predict() 不匹配

python - 如何在Keras中为 'Tensor'对象赋值?

python - Tensorflow:对前向和后向传递使用不同的表达式

python - AWS Sagemaker : Which function/code is required in entry_point file for a prediction when you upload your own, 本地训练的 SKlearn 模型 tarball?

csv - 如何处理 .csv 输入以在 Tensorflow Serving 批量转换中使用?