为什么在评估函数时添加num_epochs

时间:2017-08-23 08:18:01

标签: tensorflow

我是tensorflow的新手,它的第一个教程是下面的代码。我的问题是为什么使用num_epochs属性创建train_input_fneval_input_fn变量。它们仅用于评估,因此我不认为需要运行相同的测试10000次。

感谢。

import tensorflow as tf
# NumPy is often used to load, manipulate and preprocess data.
import numpy as np

import logging
logging.getLogger("tensorflow").setLevel(logging.WARNING)


# Declare list of features. We only have one numeric feature. There are many
# other types of columns that are more complicated and useful.
feature_columns = [tf.feature_column.numeric_column("x", shape=[1])]

# An estimator is the front end to invoke training (fitting) and evaluation
# (inference). There are many predefined types like linear regression,
# linear classification, and many neural network classifiers and regressors.
# The following code provides an estimator that does linear regression.
estimator = tf.estimator.LinearRegressor(feature_columns=feature_columns)

# TensorFlow provides many helper methods to read and set up data sets.
# Here we use two data sets: one for training and one for evaluation
# We have to tell the function how many batches
# of data (num_epochs) we want and how big each batch should be.
x_train = np.array([1., 2., 3., 4.])
y_train = np.array([0., -1., -2., -3.])
x_eval = np.array([2., 5., 8., 1.])b
y_eval = np.array([-1.01, -4.1, -7, 0.])
input_fn = tf.estimator.inputs.numpy_input_fn(
    {"x": x_train}, y_train, batch_size=4, num_epochs=None, shuffle=True)
# WHY THE EPOCH?
train_input_fn = tf.estimator.inputs.numpy_input_fn(
    {"x": x_train}, y_train, batch_size=4, num_epochs=10000, shuffle=False)
# WHY THE EPOCH?
eval_input_fn = tf.estimator.inputs.numpy_input_fn(
    {"x": x_eval}, y_eval, batch_size=4, num_epochs=10000, shuffle=False)

# We can invoke 1000 training steps by invoking the  method and passing the
# training data set.
estimator.train(input_fn=input_fn, steps=1000)

# Here we evaluate how well our model did.
train_metrics = estimator.evaluate(input_fn=train_input_fn, steps=1)
eval_metrics = estimator.evaluate(input_fn=eval_input_fn, steps=1)
print('train eval time', t2 - t1)
print('test eval time', t3 - t2)
print("train metrics: %r"% train_metrics)
print("eval metrics: %r"% eval_metrics)

1 个答案:

答案 0 :(得分:0)

num_epoch用于迭代数据的最大容量。

在您的情况下,您在训练数据中只有4个示例,并且您的批量大小为4,因此您只能在每个时期执行一个步骤。一步后,如果tf.errors.OutOfRangeError,它将引发num_epoch = 1,因为您的数据只能循环一次。您在代码中执行了1000步,因为您设置了num_epoch=None,这意味着它允许无限次迭代。