【发布时间】:2019-11-03 19:41:06
【问题描述】:
我在 Google AI Platform 上使用 tf.estimator API 和 TensorFlow 1.13 来构建 DNN 二进制分类器。出于某种原因,我没有得到eval 图表,但我得到了training 图表。
这里有两种不同的训练方法。第一种是普通的python方法,第二种是在本地模式下使用GCP AI Platform。
请注意,在任何一种方法中,评估都只是最终结果的一个点。我期待一个类似于训练曲线的情节。
最后,我展示了性能指标的相关模型代码。
普通的python笔记本方法:
%%bash
#echo ${PYTHONPATH}:${PWD}/${MODEL_NAME}
export PYTHONPATH=${PYTHONPATH}:${PWD}/${MODEL_NAME}
python -m trainer.task \
--train_data_paths="${PWD}/samples/train_sounds*" \
--eval_data_paths=${PWD}/samples/valid_sounds.csv \
--output_dir=${PWD}/${TRAINING_DIR} \
--hidden_units="175" \
--train_steps=5000 --job-dir=./tmp
本地 gcloud (GCP) 人工智能平台方法:
%%bash
OUTPUT_DIR=${PWD}/${TRAINING_DIR}
echo "OUTPUT_DIR=${OUTPUT_DIR}"
echo "train_data_paths=${PWD}/${TRAINING_DATA_DIR}/train_sounds*"
gcloud ai-platform local train \
--module-name=trainer.task \
--package-path=${PWD}/${MODEL_NAME}/trainer \
-- \
--train_data_paths="${PWD}/${TRAINING_DATA_DIR}/train_sounds*" \
--eval_data_paths=${PWD}/${TRAINING_DATA_DIR}/valid_sounds.csv \
--hidden_units="175" \
--train_steps=5000 \
--output_dir=${OUTPUT_DIR}
性能指标代码
estimator = tf.contrib.estimator.add_metrics(estimator, my_auc)
还有
# This is from the tensorflow website for adding metrics for a DNNClassifier
# https://www.tensorflow.org/api_docs/python/tf/metrics/auc
def my_auc(features, labels, predictions):
return {
#'auc': tf.metrics.auc( labels, predictions['logistic'], weights=features['weight'])
#'auc': tf.metrics.auc( labels, predictions['logistic'], weights=features[LABEL])
# 'auc': tf.metrics.auc( labels, predictions['logistic'])
'auc': tf.metrics.auc( labels, predictions['class_ids']),
'accuracy': tf.metrics.accuracy( labels, predictions['class_ids'])
}
训练和评估时使用的方法
eval_spec = tf.estimator.EvalSpec(
input_fn = read_dataset(
filename = args['eval_data_paths'],
mode = tf.estimator.ModeKeys.EVAL,
batch_size = args['eval_batch_size']),
steps=100,
throttle_secs=10,
exporters = exporter)
# addition of throttle_secs=10 above and this
# below as a result of one of the suggested answers.
# The result is that these mods do no print the final
# evaluation graph much less the intermediate results
tf.estimator.RunConfig(save_checkpoints_steps=10)
tf.estimator.train_and_evaluate(estimator, train_spec, eval_spec)
使用 tf.estimator 的 DNN 二元分类器
estimator = tf.estimator.DNNClassifier(
model_dir = model_dir,
feature_columns = final_columns,
hidden_units=hidden_units,
n_classes=2)
model_trained/eval 目录中文件的屏幕截图。
只有这个文件在这个目录中。 它被命名为model_trained/eval/events.out.tfevents.1561296248.myhostname.local,看起来像
【问题讨论】:
-
你是如何训练估计器的?是
tf.estimator.train_and_evaluate吗? -
是的,我用我的用法更新了这个问题。
-
我根据您的 cmets 和建议以及只是使用参数在下面添加了一个答案。结果在我看来并没有过度拟合。评估损失仍呈下降趋势,尚未开始上升。这让我想起了一个问题。在 keras 中有一种基于 eval 上升的提前停止的方法。估计器有这样的方法吗?或者,您是否只是检查图表并根据评估损失的上升修改训练步骤?
标签: python tensorflow google-cloud-platform google-cloud-ml gcp-ai-platform-notebook