问题
I use tf.estimator.train_and_evaluate
to train and evaluate my model. This is my code:
import tensorflow as tf
import numpy as np
from tensorflow.contrib.slim.nets import resnet_v2
import tensorflow.contrib.slim as slim
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data(path='mnist.npz')
x_train = np.expand_dims(x_train, 3).astype(np.float32)[:5000]
y_train = y_train.astype(np.int32)[:5000]
x_test = np.expand_dims(x_test, 3).astype(np.float32)[:1000]
y_test = y_test.astype(np.int32)[:1000]
tf.logging.set_verbosity(tf.logging.INFO)
cls_num = 10
def model_fn(features, labels, mode):
is_training = False
if mode == tf.estimator.ModeKeys.TRAIN:
is_training = True
with slim.arg_scope(resnet_v2.resnet_arg_scope()):
logits, endpoints = resnet_v2.resnet_v2_50(features,
num_classes=cls_num,
is_training=is_training,
reuse=None)
logits = tf.squeeze(logits, [1, 2])
preds = tf.argmax(logits, 1)
loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits)
accuracy = tf.metrics.accuracy(labels=labels, predictions=preds)
metrics = {'accuracy': accuracy}
if mode == tf.estimator.ModeKeys.EVAL:
return tf.estimator.EstimatorSpec(mode, loss=loss, eval_metric_ops=metrics)
optimizer = tf.train.AdagradOptimizer(learning_rate=0.1)
train_op = optimizer.minimize(loss, global_step=tf.train.get_global_step())
return tf.estimator.EstimatorSpec(mode, loss=loss, train_op=train_op)
def process_fn(feature, label):
feature = tf.expand_dims(feature, 3)
return feature, label
def train_input_fn():
dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train))
#dataset.map(process_fn)
dataset = dataset.repeat(1).batch(8)
return dataset
def eval_input_fn():
dataset = tf.data.Dataset.from_tensor_slices((x_test, y_test))
dataset = dataset.repeat(1).batch(8)
return dataset
estimator = tf.estimator.Estimator(model_fn=model_fn, model_dir='logs')
train_spec = tf.estimator.TrainSpec(input_fn=train_input_fn)
eval_specs = tf.estimator.EvalSpec(input_fn=eval_input_fn)
for _ in xrange(10):
tf.estimator.train_and_evaluate(estimator, train_spec, eval_specs)
The training step is ok and the loss became very small (about 0.001), but the evaluation result is wrong (the following is the evaluaiton log):
...
INFO:tensorflow:Saving dict for global step 625: accuracy = 0.5, global_step = 625, loss = 1330830600000.0
...
The task is very simple, just a binaray classfication. I don not think it is overfitting. Is there something wrong for my evaluation code?
来源:https://stackoverflow.com/questions/51798423/tf-estimator-train-and-evaluate-got-wrong-evaluation-accuray-and-loss