In [1]:
"""Examples of DNN regressor for Housing dataset"""

from __future__  import  absolute_import 
from __future__ import  division
from __future__ import print_function

from sklearn import model_selection
from sklearn import metrics
from sklearn import preprocessing
import tensorflow as tf
import numpy as np

In [3]:
# load dataset
boston = tf.contrib.learn.datasets.load_dataset('boston')
x, y = boston.data, boston.target
# print(np.shape(x),np.shape(y))

# Split dataset into train/test
x_train, x_test, y_train, y_test = model_selection.train_test_split(x, y, test_size=0.2, random_state=42)

# scale data(training set) to 0 mean and unit standard deviation
scaler = preprocessing.StandardScaler()
x_train = scaler.fit_transform(x_train)

# build 2 layers fully connected DNN with 10, 10 units respectively
feature_columns = tf.contrib.learn.infer_real_valued_columns_from_input(x_train)
regressor = tf.contrib.learn.DNNRegressor(feature_columns=feature_columns, hidden_units=[10,10])

print(x_train)
print(feature_columns)

# fit
regressor.fit(x_train, y_train, steps=5000, batch_size=1)

x_transformed = scaler.transform(x_test)

y_predicted = list(regressor.predict(x_transformed, as_iterable=True))
score = metrics.mean_squared_error(y_predicted, y_test)

print('MSE: {0:f}'.format(score))


WARNING:tensorflow:float64 is not supported by many models, consider casting to float32.
WARNING:tensorflow:Using temporary folder as model directory: /tmp/tmpcms8qa0p
INFO:tensorflow:Using default config.
INFO:tensorflow:Using config: {'_tf_random_seed': None, '_save_checkpoints_secs': 600, '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x7f43a81696d8>, '_is_chief': True, '_tf_config': gpu_options {
  per_process_gpu_memory_fraction: 1.0
}
, '_keep_checkpoint_max': 5, '_num_ps_replicas': 0, '_task_type': None, '_environment': 'local', '_save_summary_steps': 100, '_master': '', '_task_id': 0, '_save_checkpoints_steps': None, '_keep_checkpoint_every_n_hours': 10000, '_evaluation_master': ''}
[[ 1.29133866 -0.50032012  1.03323679 ...,  0.84534281 -0.07433689
   1.75350503]
 [-0.3338103  -0.50032012 -0.41315956 ...,  1.20474139  0.4301838
  -0.5614742 ]
 [-0.40072291  1.01327135 -0.71521823 ..., -0.63717631  0.06529747
  -0.65159505]
 ..., 
 [-0.40294118  2.95931752 -1.30336132 ..., -0.59225149  0.37901005
  -0.91069248]
 [ 0.85524904 -0.50032012  1.03323679 ...,  0.84534281 -2.69458597
   1.52257036]
 [-0.37881118 -0.50032012 -0.35216694 ...,  1.15981657 -3.12158061
  -0.25731635]]
[_RealValuedColumn(column_name='', dimension=13, default_value=None, dtype=tf.float64, normalizer=None)]
WARNING:tensorflow:From <ipython-input-3-15f8d323ad68>:21: calling BaseEstimator.fit (from tensorflow.contrib.learn.python.learn.estimators.estimator) with x is deprecated and will be removed after 2016-12-01.
Instructions for updating:
Estimator is decoupled from Scikit Learn interface by moving into
separate class SKCompat. Arguments x, y and batch_size are only
available in the SKCompat class, Estimator will only accept input_fn.
Example conversion:
  est = Estimator(...) -> est = SKCompat(Estimator(...))
WARNING:tensorflow:From <ipython-input-3-15f8d323ad68>:21: calling BaseEstimator.fit (from tensorflow.contrib.learn.python.learn.estimators.estimator) with y is deprecated and will be removed after 2016-12-01.
Instructions for updating:
Estimator is decoupled from Scikit Learn interface by moving into
separate class SKCompat. Arguments x, y and batch_size are only
available in the SKCompat class, Estimator will only accept input_fn.
Example conversion:
  est = Estimator(...) -> est = SKCompat(Estimator(...))
WARNING:tensorflow:From <ipython-input-3-15f8d323ad68>:21: calling BaseEstimator.fit (from tensorflow.contrib.learn.python.learn.estimators.estimator) with batch_size is deprecated and will be removed after 2016-12-01.
Instructions for updating:
Estimator is decoupled from Scikit Learn interface by moving into
separate class SKCompat. Arguments x, y and batch_size are only
available in the SKCompat class, Estimator will only accept input_fn.
Example conversion:
  est = Estimator(...) -> est = SKCompat(Estimator(...))
WARNING:tensorflow:float64 is not supported by many models, consider casting to float32.
WARNING:tensorflow:float64 is not supported by many models, consider casting to float32.
WARNING:tensorflow:From /usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/estimators/head.py:1362: scalar_summary (from tensorflow.python.ops.logging_ops) is deprecated and will be removed after 2016-11-30.
Instructions for updating:
Please switch to tf.summary.scalar. Note that tf.summary.scalar uses the node name instead of the tag. This means that TensorFlow will automatically de-duplicate summary names based on the scope they are created in. Also, passing a tensor or list of tags to a scalar summary op is no longer supported.
INFO:tensorflow:Create CheckpointSaverHook.
/usr/local/lib/python3.5/dist-packages/tensorflow/python/util/deprecation.py:247: FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.
  equality = a == b
INFO:tensorflow:Saving checkpoints for 1 into /tmp/tmpcms8qa0p/model.ckpt.
INFO:tensorflow:step = 1, loss = 424.584
INFO:tensorflow:global_step/sec: 564.071
INFO:tensorflow:step = 101, loss = 123.433
INFO:tensorflow:global_step/sec: 510.264
INFO:tensorflow:step = 201, loss = 57.0556
INFO:tensorflow:global_step/sec: 416.586
INFO:tensorflow:step = 301, loss = 57.7483
INFO:tensorflow:global_step/sec: 514.869
INFO:tensorflow:step = 401, loss = 31.7506
INFO:tensorflow:global_step/sec: 382.412
INFO:tensorflow:step = 501, loss = 0.0599292
INFO:tensorflow:global_step/sec: 521.978
INFO:tensorflow:step = 601, loss = 34.0339
INFO:tensorflow:global_step/sec: 180.182
INFO:tensorflow:step = 701, loss = 24.3098
INFO:tensorflow:global_step/sec: 218.817
INFO:tensorflow:step = 801, loss = 38.5036
INFO:tensorflow:global_step/sec: 414.224
INFO:tensorflow:step = 901, loss = 11.2068
INFO:tensorflow:global_step/sec: 433.899
INFO:tensorflow:step = 1001, loss = 0.0983214
INFO:tensorflow:global_step/sec: 189.753
INFO:tensorflow:step = 1101, loss = 25.937
INFO:tensorflow:global_step/sec: 560.387
INFO:tensorflow:step = 1201, loss = 0.926016
INFO:tensorflow:global_step/sec: 224.903
INFO:tensorflow:step = 1301, loss = 12.8843
INFO:tensorflow:global_step/sec: 271.272
INFO:tensorflow:step = 1401, loss = 30.6301
INFO:tensorflow:global_step/sec: 411.478
INFO:tensorflow:step = 1501, loss = 3.54569
INFO:tensorflow:global_step/sec: 589.747
INFO:tensorflow:step = 1601, loss = 7.35706
INFO:tensorflow:global_step/sec: 369.297
INFO:tensorflow:step = 1701, loss = 17.9454
INFO:tensorflow:global_step/sec: 378.276
INFO:tensorflow:step = 1801, loss = 2.9191
INFO:tensorflow:global_step/sec: 470.473
INFO:tensorflow:step = 1901, loss = 0.307682
INFO:tensorflow:global_step/sec: 242.918
INFO:tensorflow:step = 2001, loss = 0.481832
INFO:tensorflow:global_step/sec: 432.454
INFO:tensorflow:step = 2101, loss = 2.56622
INFO:tensorflow:global_step/sec: 479.813
INFO:tensorflow:step = 2201, loss = 2.8486
INFO:tensorflow:global_step/sec: 511.219
INFO:tensorflow:step = 2301, loss = 49.0222
INFO:tensorflow:global_step/sec: 515.14
INFO:tensorflow:step = 2401, loss = 12.0469
INFO:tensorflow:global_step/sec: 630.38
INFO:tensorflow:step = 2501, loss = 445.309
INFO:tensorflow:global_step/sec: 432.065
INFO:tensorflow:step = 2601, loss = 20.2818
INFO:tensorflow:global_step/sec: 569.918
INFO:tensorflow:step = 2701, loss = 11.6389
INFO:tensorflow:global_step/sec: 282.303
INFO:tensorflow:step = 2801, loss = 2.07122
INFO:tensorflow:global_step/sec: 625.597
INFO:tensorflow:step = 2901, loss = 0.063539
INFO:tensorflow:global_step/sec: 288.569
INFO:tensorflow:step = 3001, loss = 16.6226
INFO:tensorflow:global_step/sec: 649.398
INFO:tensorflow:step = 3101, loss = 35.1261
INFO:tensorflow:global_step/sec: 287.565
INFO:tensorflow:step = 3201, loss = 4.09131
INFO:tensorflow:global_step/sec: 465.064
INFO:tensorflow:step = 3301, loss = 5.5973
INFO:tensorflow:global_step/sec: 258.796
INFO:tensorflow:step = 3401, loss = 4.88238
INFO:tensorflow:global_step/sec: 465.764
INFO:tensorflow:step = 3501, loss = 16.8097
INFO:tensorflow:global_step/sec: 449.76
INFO:tensorflow:step = 3601, loss = 23.5019
INFO:tensorflow:global_step/sec: 537.261
INFO:tensorflow:step = 3701, loss = 1.84739
INFO:tensorflow:global_step/sec: 632.044
INFO:tensorflow:step = 3801, loss = 6.93573
INFO:tensorflow:global_step/sec: 633.091
INFO:tensorflow:step = 3901, loss = 2.90835
INFO:tensorflow:global_step/sec: 513.258
INFO:tensorflow:step = 4001, loss = 3.66147
INFO:tensorflow:global_step/sec: 566.346
INFO:tensorflow:step = 4101, loss = 1.21377
INFO:tensorflow:global_step/sec: 538.643
INFO:tensorflow:step = 4201, loss = 0.608371
INFO:tensorflow:global_step/sec: 548.352
INFO:tensorflow:step = 4301, loss = 31.4658
INFO:tensorflow:global_step/sec: 479.348
INFO:tensorflow:step = 4401, loss = 1.22985
INFO:tensorflow:global_step/sec: 494.129
INFO:tensorflow:step = 4501, loss = 0.0811238
INFO:tensorflow:global_step/sec: 429.167
INFO:tensorflow:step = 4601, loss = 1.82114
INFO:tensorflow:global_step/sec: 521.881
INFO:tensorflow:step = 4701, loss = 30.4296
INFO:tensorflow:global_step/sec: 634.847
INFO:tensorflow:step = 4801, loss = 5.30666
INFO:tensorflow:global_step/sec: 562.818
INFO:tensorflow:step = 4901, loss = 0.105856
INFO:tensorflow:Saving checkpoints for 5000 into /tmp/tmpcms8qa0p/model.ckpt.
INFO:tensorflow:Loss for final step: 30.834.
WARNING:tensorflow:From /usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/estimators/dnn.py:692: calling BaseEstimator.predict (from tensorflow.contrib.learn.python.learn.estimators.estimator) with x is deprecated and will be removed after 2016-12-01.
Instructions for updating:
Estimator is decoupled from Scikit Learn interface by moving into
separate class SKCompat. Arguments x, y and batch_size are only
available in the SKCompat class, Estimator will only accept input_fn.
Example conversion:
  est = Estimator(...) -> est = SKCompat(Estimator(...))
WARNING:tensorflow:float64 is not supported by many models, consider casting to float32.
MSE: 14.746032

In [ ]:


In [ ]: