I use tf.estimator.train_and_evaluate
训练和评估我的模型。这是我的代码:
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)
训练步长没问题,loss变得很小(0.001左右),但是评估结果错误(以下是评估日志):
...
INFO:tensorflow:Saving dict for global step 625: accuracy = 0.5, global_step = 625, loss = 1330830600000.0
...
任务很简单,就是二分类。我不认为这是过度拟合。我的评估代码有问题吗?