【问题标题】:Why add num_epochs when in evaluating function为什么在评估函数时添加 num_epochs
【发布时间】:2017-08-23 08:18:01
【问题描述】:

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

【问题讨论】:

  • 对!评估只需要num_epochs=1

标签: tensorflow


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2012-09-23
    • 2018-09-20
    • 1970-01-01
    • 1970-01-01
    • 2010-12-11
    • 2014-05-19
    • 2019-05-24
    • 1970-01-01
    相关资源
    最近更新 更多