为什么张量板中没有此 DNN 二元分类器 tf.estimator 模型的评估图?
Why no eval plot for this DNN binary classifier tf.estimator model in tensorboard?
我在 Google AI 平台上使用 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)ai平台方法:
%%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 看起来像
在 estimator.train_and_evaluate()
中指定 train_spec
和 eval_spec
。 eval_spec
通常有不同的输入函数(例如开发评估数据集,non-shuffled)
每 N 步,train 过程中的一个检查点被保存,eval 过程加载这些相同的权重并根据 eval_spec
运行。这些评估摘要记录在检查点的步数下,因此您可以比较训练与测试的性能。
在您的情况下,对于每次评估调用,评估只会在图表上生成一个点。该点包含整个评估调用的平均值。
看看 this 类似的问题:
我会将 tf.estimator.EvalSpec
修改为 throttle_secs
较小的值(默认为 600),并将 tf.estimator.RunConfig
中的 save_checkpoints_steps
修改为较小的值:
tf.estimator.RunConfig(save_checkpoints_steps=SOME_SMALL_VALUE_TO_VERIFY)
通过评论和建议以及调整参数,这是对我有用的结果。
启动tensorboard、训练模型等的代码。使用------表示笔记本单元
%%bash
# clean model output dirs
# This is so that the trained model is deleted
output_dir=${PWD}/${TRAINING_DIR}
echo ${output_dir}
rm -rf ${output_dir}
# start tensorboard
def tb(logdir="logs", port=6006, open_tab=True, sleep=2):
import subprocess
proc = subprocess.Popen(
"exec " + "tensorboard --logdir={0} --port={1}".format(logdir, port), shell=True)
if open_tab:
import time
time.sleep(sleep)
import webbrowser
webbrowser.open("http://127.0.0.1:{}/".format(port))
return proc
cwd = os.getcwd()
output_dir=cwd + '/' + TRAINING_DIR
print(output_dir)
server1 = tb(logdir=output_dir)
%%bash
# The model run config is hard coded to checkpoint every 500 steps
#
#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_batch_size=10 \
--eval_batch_size=100 \
--eval_steps=1000 \
--min_eval_frequency=15 \
--train_steps=20000 --job-dir=./tmp
相关型号代码
# This hard codes the checkpoints to be
# every 500 training steps?
estimator = tf.estimator.DNNClassifier(
model_dir = model_dir,
feature_columns = final_columns,
hidden_units=hidden_units,
config=tf.estimator.RunConfig(save_checkpoints_steps=500),
n_classes=2)
# trainspec to tell the estimator how to get training data
train_spec = tf.estimator.TrainSpec(
input_fn = read_dataset(
filename = args['train_data_paths'],
mode = tf.estimator.ModeKeys.TRAIN, # make sure you use the dataset api
batch_size = args['train_batch_size']),
max_steps = args['train_steps']) # max_steps allows a resume
exporter = tf.estimator.LatestExporter(name = 'exporter',
serving_input_receiver_fn = serving_input_fn)
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=args['eval_steps'],
throttle_secs = args['min_eval_frequency'],
exporters = exporter)
tf.estimator.train_and_evaluate(estimator, train_spec, eval_spec)
结果图
我在 Google AI 平台上使用 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)ai平台方法:
%%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 看起来像
在 estimator.train_and_evaluate()
中指定 train_spec
和 eval_spec
。 eval_spec
通常有不同的输入函数(例如开发评估数据集,non-shuffled)
每 N 步,train 过程中的一个检查点被保存,eval 过程加载这些相同的权重并根据 eval_spec
运行。这些评估摘要记录在检查点的步数下,因此您可以比较训练与测试的性能。
在您的情况下,对于每次评估调用,评估只会在图表上生成一个点。该点包含整个评估调用的平均值。 看看 this 类似的问题:
我会将 tf.estimator.EvalSpec
修改为 throttle_secs
较小的值(默认为 600),并将 tf.estimator.RunConfig
中的 save_checkpoints_steps
修改为较小的值:
tf.estimator.RunConfig(save_checkpoints_steps=SOME_SMALL_VALUE_TO_VERIFY)
通过评论和建议以及调整参数,这是对我有用的结果。
启动tensorboard、训练模型等的代码。使用------表示笔记本单元
%%bash
# clean model output dirs
# This is so that the trained model is deleted
output_dir=${PWD}/${TRAINING_DIR}
echo ${output_dir}
rm -rf ${output_dir}
# start tensorboard
def tb(logdir="logs", port=6006, open_tab=True, sleep=2):
import subprocess
proc = subprocess.Popen(
"exec " + "tensorboard --logdir={0} --port={1}".format(logdir, port), shell=True)
if open_tab:
import time
time.sleep(sleep)
import webbrowser
webbrowser.open("http://127.0.0.1:{}/".format(port))
return proc
cwd = os.getcwd()
output_dir=cwd + '/' + TRAINING_DIR
print(output_dir)
server1 = tb(logdir=output_dir)
%%bash
# The model run config is hard coded to checkpoint every 500 steps
#
#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_batch_size=10 \
--eval_batch_size=100 \
--eval_steps=1000 \
--min_eval_frequency=15 \
--train_steps=20000 --job-dir=./tmp
相关型号代码
# This hard codes the checkpoints to be
# every 500 training steps?
estimator = tf.estimator.DNNClassifier(
model_dir = model_dir,
feature_columns = final_columns,
hidden_units=hidden_units,
config=tf.estimator.RunConfig(save_checkpoints_steps=500),
n_classes=2)
# trainspec to tell the estimator how to get training data
train_spec = tf.estimator.TrainSpec(
input_fn = read_dataset(
filename = args['train_data_paths'],
mode = tf.estimator.ModeKeys.TRAIN, # make sure you use the dataset api
batch_size = args['train_batch_size']),
max_steps = args['train_steps']) # max_steps allows a resume
exporter = tf.estimator.LatestExporter(name = 'exporter',
serving_input_receiver_fn = serving_input_fn)
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=args['eval_steps'],
throttle_secs = args['min_eval_frequency'],
exporters = exporter)
tf.estimator.train_and_evaluate(estimator, train_spec, eval_spec)
结果图