0

我是 Tensorflow 的新手并尝试运行示例代码,但我无法理解该程序中发生了什么:

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

# Declare list of features. We only have one real-valued feature. There are many
# other types of columns that are more complicated and useful.
features = [tf.contrib.layers.real_valued_column("x", dimension=1)]

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

# TensorFlow provides many helper methods to read and set up data sets.
# Here we use `numpy_input_fn`. We have to tell the function how many batches
# of data (num_epochs) we want and how big each batch should be.
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)

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

# Here we evaluate how well our model did. In a real example, we would want
# to use a separate validation and testing data set to avoid overfitting.
estimator.evaluate(input_fn=input_fn)

任何人都可以解释从该input_fn行开始发生的事情。是batch_size输入数据的大小吗?num_epochs既然我告诉估算器它需要 1000 步,为什么我需要它?

提前致谢 !

4

1 回答 1

1

欢迎来到 TensorFlow。下面的行:
input_fn = tf.contrib.learn.io.numpy_input_fn({"x":x}, y, batch_size=4, num_epochs=1000)
生成一个函数,该函数input_fn稍后传递给.fit使用线性回归估计器生成的估计器对象的方法。将input_fn提供batch_size=4多达 1000 次的功能和目标 ( num_epochs=1000)。 batch_size指小批量大小。On Epoch 是对您的训练示例的完整运行。在这种情况下,this 提供的训练数据中只有 4 个示例input_fn
这是一个愚蠢的例子,因为随机梯度体面对于解决这个线性回归问题是不必要的,但它向您展示了解决更棘手问题所必需的机制。

于 2017-03-16T21:28:18.673 回答