ai-platform:当 运行 TensorFlow 2.1 训练作业使用 Estimator 时,输出中没有 eval 文件夹或导出文件夹
ai-platform: No eval folder or export folder in outputs when running TensorFlow 2.1 training job using Estimators
问题
我的代码在本地运行,但在升级到 TensorFlow 2.1 后提交在线训练作业时,我无法从我的 TensorFlow 估算器中获取任何评估数据或导出。这是我的大部分代码:
def build_estimator(model_dir, config):
return tf.estimator.LinearClassifier(
feature_columns=feature_columns,
n_classes=2,
optimizer=tf.keras.optimizers.Ftrl(
learning_rate=args.learning_rate,
l1_regularization_strength=args.l1_strength
),
model_dir=model_dir,
config=config
)
run_config = tf.estimator.RunConfig(save_checkpoints_steps=100,
save_summary_steps=100)
...
estimator = build_estimator(model_dir=args.job_dir, config=run_config)
...
def serving_input_fn():
inputs = {
'feature1': tf.compat.v1.placeholder(shape=None, dtype=tf.string),
'feature2': tf.compat.v1.placeholder(shape=None, dtype=tf.string),
'feature3': tf.compat.v1.placeholder(shape=None, dtype=tf.string),
...
}
split_features = {}
for feature in inputs:
split_features[feature] = tf.strings.split(inputs[feature], "||").to_sparse()
return tf.estimator.export.ServingInputReceiver(features=split_features, receiver_tensors=inputs)
exporter_cls = tf.estimator.LatestExporter('predict', serving_input_fn)
eval_spec = tf.estimator.EvalSpec(
input_fn=lambda: input_eval_fn(args.test_dir),
exporters=[exporter_cls],
start_delay_secs=10,
throttle_secs=0)
tf.estimator.train_and_evaluate(estimator, train_spec, eval_spec)
如果我 运行 使用本地 gcloud 命令它工作正常,我得到我的 /eval
和 /export
文件夹:
gcloud ai-platform local train \
--package-path trainer \
--module-name trainer.task \
-- \
--train-dir $TRAIN_DATA \
--test-dir $TEST_DATA \
--training-steps $TRAINING_STEPS \
--job-dir $OUTPUT
但是当我尝试在云中 运行 它时,我没有得到我的 /eval
/export
文件夹。这仅在升级到 2.1 时才开始发生。以前在 1.14 中一切正常。
gcloud ai-platform jobs submit training $JOB_NAME \
--job-dir $OUTPUT_PATH \
--staging-bucket gs://$STAGING_BUCKET_NAME \
--runtime-version 2.1 \
--python-version 3.7 \
--package-path trainer/ \
--module-name trainer.task \
--region $REGION \
--config config.yaml \
-- \
--train-dir $TRAIN_DATA \
--test-dir $TEST_DATA \
我试过的
我没有使用 EvalSpec
导出我的模型,而是尝试使用 tf.estimator.export_saved_model
。虽然这在本地和在线都有效,但如果可能的话,我想继续使用 EvalSpec
和 train_and_evaluate
,因为我可以传入不同的导出方法,如 BestExporter
、LastExporter
等
我的主要问题是...
我是否在 TensorFlow 2.1 中错误地导出了我的模型,或者这是新版本平台上发生的错误?
找到答案...
基于有关 TF_CONFIG
环境变量的文档...
master is a deprecated task type in TensorFlow. master represented a task that performed a similar role as chief but also acted as an evaluator in some configurations. TensorFlow 2 does not support TF_CONFIG environment variables that contain a master task.
所以之前我们使用的是TF 1.X,它使用了master worker。但是,master 在训练 TF 2.X 作业时已被弃用。现在默认是 chief,但默认情况下 chief 不充当评估者。为了获得评估数据,我们需要更新配置 yaml 以显式分配评估器。
https://cloud.google.com/ai-platform/training/docs/distributed-training-details#tf-config-format
我们用 evaluatorType
和 evaluatorCount
更新了 config.yaml
trainingInput:
scaleTier: CUSTOM
masterType: standard_gpu
workerType: standard_gpu
workerCount: 1
evaluatorType: standard_gpu
evaluatorCount: 1
成功了!!!
问题
我的代码在本地运行,但在升级到 TensorFlow 2.1 后提交在线训练作业时,我无法从我的 TensorFlow 估算器中获取任何评估数据或导出。这是我的大部分代码:
def build_estimator(model_dir, config):
return tf.estimator.LinearClassifier(
feature_columns=feature_columns,
n_classes=2,
optimizer=tf.keras.optimizers.Ftrl(
learning_rate=args.learning_rate,
l1_regularization_strength=args.l1_strength
),
model_dir=model_dir,
config=config
)
run_config = tf.estimator.RunConfig(save_checkpoints_steps=100,
save_summary_steps=100)
...
estimator = build_estimator(model_dir=args.job_dir, config=run_config)
...
def serving_input_fn():
inputs = {
'feature1': tf.compat.v1.placeholder(shape=None, dtype=tf.string),
'feature2': tf.compat.v1.placeholder(shape=None, dtype=tf.string),
'feature3': tf.compat.v1.placeholder(shape=None, dtype=tf.string),
...
}
split_features = {}
for feature in inputs:
split_features[feature] = tf.strings.split(inputs[feature], "||").to_sparse()
return tf.estimator.export.ServingInputReceiver(features=split_features, receiver_tensors=inputs)
exporter_cls = tf.estimator.LatestExporter('predict', serving_input_fn)
eval_spec = tf.estimator.EvalSpec(
input_fn=lambda: input_eval_fn(args.test_dir),
exporters=[exporter_cls],
start_delay_secs=10,
throttle_secs=0)
tf.estimator.train_and_evaluate(estimator, train_spec, eval_spec)
如果我 运行 使用本地 gcloud 命令它工作正常,我得到我的 /eval
和 /export
文件夹:
gcloud ai-platform local train \
--package-path trainer \
--module-name trainer.task \
-- \
--train-dir $TRAIN_DATA \
--test-dir $TEST_DATA \
--training-steps $TRAINING_STEPS \
--job-dir $OUTPUT
但是当我尝试在云中 运行 它时,我没有得到我的 /eval
/export
文件夹。这仅在升级到 2.1 时才开始发生。以前在 1.14 中一切正常。
gcloud ai-platform jobs submit training $JOB_NAME \
--job-dir $OUTPUT_PATH \
--staging-bucket gs://$STAGING_BUCKET_NAME \
--runtime-version 2.1 \
--python-version 3.7 \
--package-path trainer/ \
--module-name trainer.task \
--region $REGION \
--config config.yaml \
-- \
--train-dir $TRAIN_DATA \
--test-dir $TEST_DATA \
我试过的
我没有使用 EvalSpec
导出我的模型,而是尝试使用 tf.estimator.export_saved_model
。虽然这在本地和在线都有效,但如果可能的话,我想继续使用 EvalSpec
和 train_and_evaluate
,因为我可以传入不同的导出方法,如 BestExporter
、LastExporter
等
我的主要问题是...
我是否在 TensorFlow 2.1 中错误地导出了我的模型,或者这是新版本平台上发生的错误?
找到答案...
基于有关 TF_CONFIG
环境变量的文档...
master is a deprecated task type in TensorFlow. master represented a task that performed a similar role as chief but also acted as an evaluator in some configurations. TensorFlow 2 does not support TF_CONFIG environment variables that contain a master task.
所以之前我们使用的是TF 1.X,它使用了master worker。但是,master 在训练 TF 2.X 作业时已被弃用。现在默认是 chief,但默认情况下 chief 不充当评估者。为了获得评估数据,我们需要更新配置 yaml 以显式分配评估器。
https://cloud.google.com/ai-platform/training/docs/distributed-training-details#tf-config-format
我们用 evaluatorType
和 evaluatorCount
config.yaml
trainingInput:
scaleTier: CUSTOM
masterType: standard_gpu
workerType: standard_gpu
workerCount: 1
evaluatorType: standard_gpu
evaluatorCount: 1
成功了!!!