【问题标题】:what is the difference between num_epochs and steps?num_epochs 和 steps 有什么区别?
【发布时间】:2017-09-13 05:17:08
【问题描述】:

在tensorflow中的入门代码:

import tensorflow as tf
import numpy as np

features = [tf.contrib.layers.real_valued_column("x", dimension=1)]
estimator = tf.contrib.learn.LinearRegressor(feature_columns=features)
x = np.array([1., 2., 3., 4.])
y = np.array([0., -1., -2., -3.])
input_fn = tf.contrib.learn.io.numpy_input_fn({"x":x}, y, batch_size=4, num_epochs=1000)
estimator.fit(input_fn=input_fn, steps=1000)
estimator.evaluate(input_fn=input_fn)

我知道batch_size是什么意思,但是在只有4个训练样例的情况下,num_epochs和steps分别是什么意思?

【问题讨论】:

标签: python machine-learning tensorflow


【解决方案1】:

num_epochs表示input_fn会返回多少次整批

而steps 表示函数应该运行多少次。

对于对象estimator这里的方法,它将停止运行超过“步骤”次或input_fn停止提供数据,根据Tensorflow API:

对于每一步,调用 input_fn,它会返回一批数据。评估直到: - 处理批次,或 - input_fn 引发输入结束异常(OutOfRangeError 或 StopIteration)。

【讨论】:

    【解决方案2】:

    一个纪元意味着使用你拥有的全部数据。

    一个步骤意味着使用单个批次数据。

    所以,n_steps = Number of data in single epoch // batch_size。

    根据https://www.tensorflow.org/api_docs/python/tf/contrib/learn/Trainable,

    • 步数:训练模型的步数。如果没有,永远训练。 “步骤”是渐进式的。如果您调用两次 fit(steps=10),则训练总共进行 20 步。如果您不想有增量行为,请改为设置 max_steps。如果设置,max_steps 必须为 None。

    • batch_size:用于输入的小批量大小,默认为 x 的第一维。如果提供了 input_fn,则必须为 None。

    【讨论】:

    • 有道理,但是这里的 num_epochs 呢?总共只有 4 个例子。
    • 我为 num_epochs 和 steps 尝试了不同的值,我发现 step 不能大于 num_epochs。也许 num_epochs 是训练时间的上限,而 steps 是实际的训练时间。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2010-10-02
    • 2011-12-12
    • 2010-09-16
    • 2012-03-14
    • 2012-02-06
    • 2011-02-25
    • 2011-11-22
    • 2015-03-26
    相关资源
    最近更新 更多