开始使用 TensorFlow

Getting started with Tensorflow

我是 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 步?

提前致谢!

欢迎使用 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 是一个完整的 运行 通过你的训练例子。在这种情况下,这个input_fn.
提供的训练数据中只有4个例子 这是一个愚蠢的例子,因为随机梯度下降对于解决这个线性回归问题是不必要的,但它向您展示了解决更棘手问题所必需的机制。