I am using Tensorflow 1.2, here's the code:
import tensorflow as tf
import tensorflow.contrib.layers as layers
import numpy as np
import tensorflow.contrib.learn as tflearn
tf.logging.set_verbosity(tf.logging.INFO)
# Naturally this is a very simple straight line
# of y = -x + 10
train_x = np.asarray([0., 1., 2., 3., 4., 5.])
train_y = np.asarray([10., 9., 8., 7., 6., 5.])
test_x = np.asarray([10., 11., 12.])
test_y = np.asarray([0., -1., -2.])
input_fn_train = tflearn.io.numpy_input_fn({"x": train_x}, train_y, num_epochs=1000)
input_fn_test = tflearn.io.numpy_input_fn({"x": test_x}, test_y, num_epochs=1000)
validation_monitor = tflearn.monitors.ValidationMonitor(
input_fn=input_fn_test,
every_n_steps=10)
fts = [layers.real_valued_column('x')]
estimator = tflearn.LinearRegressor(feature_columns=fts)
estimator.fit(input_fn=input_fn_train,
steps=1000,
monitors=[validation_monitor])
print(estimator.evaluate(input_fn=input_fn_test))
It runs as expected. What's happening is that the training stops at step 47 with a very high loss value:
INFO:tensorflow:Starting evaluation at 2017-06-18-20:52:10
INFO:tensorflow:Finished evaluation at 2017-06-18-20:52:10
INFO:tensorflow:Saving dict for global step 1: global_step = 1, loss = 12.5318
INFO:tensorflow:Validation (step 10): global_step = 1, loss = 12.5318
INFO:tensorflow:Saving checkpoints for 47 into
INFO:tensorflow:Loss for final step: 19.3527.
INFO:tensorflow:Starting evaluation at 2017-06-18-20:52:11
INFO:tensorflow:Restoring parameters from
INFO:tensorflow:Finished evaluation at 2017-06-18-20:52:11
INFO:tensorflow:Saving dict for global step 47: global_step = 47, loss = 271.831
{'global_step': 47, 'loss': 271.83133}
Few things I completely don't understand (admittedly I'm a complete noob in TF):
- Why the loss on step 10 is smaller than loss on step 47?
- Why TF decides to stop the training anyway after?
- Why "INFO:tensorflow:Loss for final step: 19.3527." and the loss at step 47 do not match each other?
I have imlemented this very algorithm using vanilla TensorFlow and it works as expected, but I really can't get the grasp of what LinearRegressor wants from me here.