首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在计算函数时为什么要添加num_epochs

在计算函数时为什么要添加num_epochs
EN

Stack Overflow用户
提问于 2017-08-23 16:18:01
回答 1查看 560关注 0票数 1

我是tensorflow的新手,它的第一个教程是下面的代码。我的问题是,为什么使用num_epochs属性创建train_input_fneval_input_fn变量。它们只用于评估,所以我认为没有必要运行同一个测试10000次。

谢谢。

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2018-02-18 09:05:28

num_epoch使用最大容量来迭代您的数据。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45834299

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档