【问题标题】:Selecting num_epochs value for the tutorial example in Tensorflow为 Tensorflow 中的教程示例选择 num_epochs 值
【发布时间】:2017-10-07 01:35:02
【问题描述】:

https://www.tensorflow.org/get_started/get_started 上的 tensorflow 教程有一个估算器示例,他们正在创建线性回归模型,如下所示:

import tensorflow as tf
import numpy as np 

feature_columns = [tf.feature_column.numeric_column("x", shape=[1])]

estimator = tf.estimator.LinearRegressor(feature_columns=feature_columns)

x_train = np.array([1., 2., 3., 4.])
y_train = np.array([0, -1., -2., -3.])
x_eval = np.array([2., 5., 8., 1.])
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)
train_input_fn = tf.estimator.inputs.numpy_input_fn({"x":x_train}, y_train, batch_size = 4, num_epochs=1000, shuffle=False)
eval_input_fn = tf.estimator.inputs.numpy_input_fn({"x":x_eval}, y_eval, batch_size = 4, num_epochs=1000, shuffle = False)

estimator.train(input_fn=input_fn, steps=1000)

train_metrics = estimator.evaluate(input_fn = train_input_fn)
eval_metrics = estimator.evaluate(input_fn = eval_input_fn)

print("train metrics: %r"% train_metrics)
print("eval metrics: %r"% eval_metrics)

我的问题是关于 'train_input_fn' 和 'eval_input_fn' 为什么我们需要选择 'num_epochs=1000'?

这些是具有不同 'num_epochs' 值的输出:

num_epochs=1000

train metrics: {'global_step': 1000, 'loss': 4.3708383e-08, 'average_loss': 1.0927096e-08}
eval metrics: {'global_step': 1000, 'loss': 0.010135064, 'average_loss': 0.002533766}

num_epochs=1

train metrics: {'global_step': 1000, 'loss': 9.6500253e-07, 'average_loss': 2.4125063e-07}
eval metrics: {'global_step': 1000, 'loss': 0.010293347, 'average_loss': 0.0025733367}

当 num_epochs=1 时,我期望 'loss' 和 'average_loss' 的值相同。有人可以帮我理解这一点吗?

谢谢。

【问题讨论】:

    标签: machine-learning tensorflow linear-regression


    【解决方案1】:

    你的直觉是正确的:

    0.002533766 == 0.010293347 / 4
    

    0.002533766 == 0.010293347 / x_eval.shape[0]
    

    average_loss == epoch_loss / x_eval.shape[0]
    

    但是,我也很困惑为什么选择多个时期进行评估是有意义的,尤其是。如果x_eval.shape[0] == batch_size。也许他们只是想证明函数tf.estimator.inputs.numpy_input_fn 接受num_epochs 作为参数。

    【讨论】:

      猜你喜欢
      • 2020-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-18
      • 2012-02-01
      • 2017-01-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多