[英]tf.estimator - how to print accuracy on test set after every epoch?
I'd like to be able to print the accuracy of this neural network model on the test MNIST dataset with varying number of epochs - I'm using the for loop at the end and testing 1 vs. 2 epochs, but for some reason I get the same accuracy for both. 我希望能够在具有不同时期数的测试MNIST数据集上打印此神经网络模型的准确性-我在末尾使用了for循环并测试了1对2时期,但是由于某些原因我两者的精度相同。 Is it somehow not actually training a new model with 2 epochs in the second iteration of the for loop?
在某种程度上,实际上不是在for循环的第二次迭代中训练带有2个历元的新模型吗?
Any thoughts are much appreciated! 任何想法都非常感谢!
from __future__ import print_function
# Import MNIST data
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot=False)
import tensorflow as tf
# Parameters
learning_rate = 0.1
num_steps = 1000
batch_size = 128
display_step = 100
# Network Parameters
n_hidden_1 = 256 # 1st layer number of neurons
n_hidden_2 = 256 # 2nd layer number of neurons
num_input = 784 # MNIST data input (img shape: 28*28)
num_classes = 10 # MNIST total classes (0-9 digits)
# Define the neural network
def neural_net(x_dict):
# TF Estimator input is a dict, in case of multiple inputs
x = x_dict['images']
# Hidden fully connected layer with 256 neurons
layer_1 = tf.layers.dense(x, n_hidden_1)
# Hidden fully connected layer with 256 neurons
layer_2 = tf.layers.dense(layer_1, n_hidden_2)
# Output fully connected layer with a neuron for each class
out_layer = tf.layers.dense(layer_2, num_classes)
return out_layer
# Define the model function (following TF Estimator Template)
def model_fn(features, labels, mode):
# Build the neural network
logits = neural_net(features)
# Predictions
pred_classes = tf.argmax(logits, axis=1)
pred_probas = tf.nn.softmax(logits)
# If prediction mode, early return
if mode == tf.estimator.ModeKeys.PREDICT:
return tf.estimator.EstimatorSpec(mode, predictions=pred_classes)
##squared loss
loss_op=tf.reduce_sum(tf.pow(tf.subtract(pred_probas,tf.one_hot(labels,10)), 2))/batch_size
##cross-entropy loss (exclusive labels)
#loss_op=tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=pred_probas, labels=labels))
# Evaluate the accuracy of the model
acc_op = tf.metrics.accuracy(labels=labels, predictions=pred_classes)
# TF Estimators requires to return a EstimatorSpec, that specify
# the different ops for training, evaluating, ...
estim_specs = tf.estimator.EstimatorSpec(
mode=mode,
predictions=pred_classes,
loss=loss_op,
train_op=train_op,
eval_metric_ops={'accuracy': acc_op})
return estim_specs
# Build the Estimator
model = tf.estimator.Estimator(model_fn)
f=open("nn_errors_sqloss.txt","w")
for i in [1,2]:
# Define the input function for training
input_fn = tf.estimator.inputs.numpy_input_fn(
x={'images': mnist.train.images}, y=mnist.train.labels,
batch_size=batch_size, num_epochs=i, shuffle=True)
# Train the Model
model.train(input_fn, steps=num_steps)
# Evaluate the Model
# Define the input function for evaluating
input_fn_test = tf.estimator.inputs.numpy_input_fn(
x={'images': mnist.test.images}, y=mnist.test.labels,
batch_size=batch_size, shuffle=False)
# Use the Estimator 'evaluate' method
e = model.evaluate(input_fn_test)
f.write("%f\n" % e['accuracy'])
f.close()
You can use train_and_evaluate
. 您可以使用
train_and_evaluate
。 First, you need to return different EstimatorSpec
for train mode and for eval mode. 首先,您需要针对火车模式和评估模式返回不同的
EstimatorSpec
。
tf.estimator.EstimatorSpec(mode=mode, loss=loss, eval_metric_ops=eval_metric_ops)
Also you need to add RunConfig
with save_checkpoints_steps
, which controls how often evaluation should be done 你也需要添加
RunConfig
与save_checkpoints_steps
,控制多久评价应该做的
run_config = tf.estimator.RunConfig(save_checkpoints_steps=1000)
train_spec = tf.estimator.TrainSpec(input_fn, max_steps)
eval_spec = tf.estimator.EvalSpec(input_fn)
tf.estimator.train_and_evaluate(model, train_spec, eval_spec)
https://www.tensorflow.org/api_docs/python/tf/estimator/train_and_evaluate https://www.tensorflow.org/api_docs/python/tf/estimator/train_and_evaluate
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.