Tensorflow入门

时间:2017-03-15 09:31:50

标签: tensorflow tflearn

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

提前谢谢!

1 个答案:

答案 0 :(得分: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指的是小批量大小。关于Epoch是完整的训练示例。在这种情况下,此input_fn提供的培训数据中只有4个示例 这是一个很好的例子,因为随机梯度下降不是解决这个线性回归问题所必需的,但它向你展示了解决更棘手问题所必需的机制。