问:Tensorflow估计器没有正确地重新初始化

2024-09-28 05:21:23 发布

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

我用一个定制的估计器构建并训练了一个分类器。因为我以后需要在一个单独的数据集上使用它,所以我把我的模型放在一个单独的文件中。培训和评估我的数据集如下(输入\u fn缩短为可读性)

# Instantiate the estimator
birdsclassifier = tf.estimator.Estimator(
    model_fn=cnn_model, model_dir=model_path)
tensors_to_log = {"probabilities": "softmax_tensor"}
logging_hook = tf.train.LoggingTensorHook(
          tensors=tensors_to_log, every_n_iter=50)

# Train the model   
def input_fn():
    #Training dataset preparation happens here
    return {'x': features}, labels
birdsclassifier.train(
    input_fn=input_fn,
    steps=num_steps,
    hooks=[logging_hook])
# Evaluate the model and print results
print('begin eval')
def eval_input_fn():
    #Validation dataset preparation happens here
    return {'x': featuresTest}, labelsT

eval_results = birdsclassifier.evaluate(input_fn=eval_input_fn)
print(eval_results)

并给出以下输出

 {'accuracy': 0.5952381, 'loss': 1.4494723, 'global_step': 2000}

但是,仅使用评估代码运行脚本(对洗牌问题进行了双重检查,并且两个验证数据集是相同的)会产生

{'accuracy': 0.083333336, 'loss': 4.551247, 'global_step': 2000}

由于全局步骤是相同的,因此图形似乎至少部分地正确地从检查点重建了-但是看起来好像没有正确地加载变量。另外,重复运行第二个脚本会得到稍微不同的输出。 我还注意到,用少量的步骤重新运行训练脚本会不断增加全局\u步数,但也会导致精度下降,这表明训练是从随机重新开始的

This threadthis表明我的方法应该可以很好地工作。你知道吗

注意:如果代码片段不足,代码的剩余部分在这里:https://github.com/pbkowalski/cnn_classification_birds


Tags: theto数据脚本inputmodeltfeval

热门问题