为什么没有DNN二元分类器的评估图tf.估计器张量板模型?

2024-03-28 20:42:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在googleai平台上使用带有TensorFlow 1.13的tf.estimatorAPI来构建DNN二进制分类器。由于某些原因,我没有得到eval图,但是我得到了training图。在

这里有两种不同的训练方法。第一种是普通python方法,第二种是在本地模式下使用gcpai平台。在

请注意,在这两种方法中,求值只是一个点,表示最终结果。我期待着一个类似于训练的曲线。在

最后,我展示了性能度量的相关模型代码。在

普通python笔记本方法:

enter image description here

%%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平台方法:

enter image description here

^{pr2}$

性能指标代码

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 = tf.estimator.DNNClassifier(
                        model_dir = model_dir,
                        feature_columns = final_columns,
                        hidden_units=hidden_units,
                        n_classes=2)

model_trained/eval dir.文件的屏幕截图。

此目录中只有这一个文件。 它被命名为model_trained/eval/events.out.tfevents事件.1561296248。我的主机名.local看起来像

enter image description here


Tags: the方法labelsmodeltfdirevalpwd
2条回答

estimator.train_and_evaluate()中指定train_speceval_speceval_spec通常有不同的输入函数(例如,开发评估数据集,非无序)

每隔N步,来自train进程的一个检查点被保存,eval进程加载这些相同的权重并根据eval_spec运行。这些评估总结记录在检查点的步骤号下,因此您可以比较列车和测试性能。在

在您的例子中,evaluation只在图上为每个要求值的调用生成一个点。此点包含整个求值调用的平均值。 看看this类似的问题:

我将使用throttle_secs小值(默认值为600)和tf.estimator.RunConfig中的save_checkpoints_steps修改为一个小值:

tf.estimator.RunConfig(save_checkpoints_steps=SOME_SMALL_VALUE_TO_VERIFY)

enter image description here

随着评论和建议以及参数的调整,以下是对我有用的结果。在

启动张力板、训练模型等的代码。使用-表示笔记本单元


%%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}

^{pr2}$
%%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)

结果图

enter image description hereenter image description here

相关问题 更多 >