I'm new to Tensorflow and trying to run the example codes, and I just can't understand what happening in this program :
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)
Can any one explain to what happens starting from the input_fn
line.
Is the batch_size
the size of the input data ? and why do I need the num_epochs
since I tell the estimator the it needs 1000 steps ?
thanks in advance !
Welcome to TensorFlow. The line below:
input_fn = tf.contrib.learn.io.numpy_input_fn({"x":x}, y, batch_size=4,
num_epochs=1000)
generates a function input_fn
which is later passed to the method .fit
for your estimator object which was generated with the Linear Regressor Estimator. The input_fn
will provide batch_size=4
features and targets up to 1000 times (num_epochs=1000
). batch_size
refers to the mini-batch size. On Epoch is a complete run through your training examples. In this case, there are only 4 examples in the training data provided by this input_fn
.
This is a guffy example since Stochastic Gradient Decent is unnecessary to solve this linear regression problem, but is shows you the mechanisms that are necessary for solving tougher problems.