In [91]:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import itertools


import numpy as np
import pandas as pd
import tensorflow as tf

In [92]:
tf.logging.set_verbosity(tf.logging.INFO)

In [93]:
COLUMNS = ["crim", "zn", "indus", "nox", "rm", "age",
           "dis", "tax", "ptratio", "medv"]
FEATURES = ["crim", "zn", "indus", "nox", "rm",
            "age", "dis", "tax", "ptratio"]
LABEL = "medv"

training_set = pd.read_csv("boston_train.csv", skipinitialspace=True,
                           skiprows=1, names=COLUMNS)
test_set = pd.read_csv("boston_test.csv", skipinitialspace=True,
                       skiprows=1, names=COLUMNS)
prediction_set = pd.read_csv("boston_predict.csv", skipinitialspace=True,
                             skiprows=1, names=COLUMNS)

In [98]:
feature_cols = [tf.feature_column.numeric_column(k) for k in FEATURES]

In [100]:
regressor = tf.estimator.DNNRegressor(feature_columns=feature_cols,
                                      hidden_units=[10, 10],
                                      model_dir="/tmp/boston_model")


INFO:tensorflow:Using default config.
INFO:tensorflow:Using config: {'_save_checkpoints_secs': 600, '_session_config': None, '_keep_checkpoint_max': 5, '_tf_random_seed': 1, '_keep_checkpoint_every_n_hours': 10000, '_log_step_count_steps': 100, '_save_checkpoints_steps': None, '_model_dir': '/tmp/boston_model', '_save_summary_steps': 100}

In [101]:
def get_input_fn(data_set, num_epochs=None, shuffle=True):
  return tf.estimator.inputs.pandas_input_fn(
      x=pd.DataFrame({k: data_set[k].values for k in FEATURES}),
      y = pd.Series(data_set[LABEL].values),
      num_epochs=num_epochs,
      shuffle=shuffle)

In [102]:
regressor.train(input_fn=get_input_fn(training_set), steps=5000)


INFO:tensorflow:Create CheckpointSaverHook.
INFO:tensorflow:Saving checkpoints for 1 into /tmp/boston_model/model.ckpt.
INFO:tensorflow:loss = 49528.9, step = 1
INFO:tensorflow:global_step/sec: 289.007
INFO:tensorflow:loss = 12053.4, step = 101 (0.347 sec)
INFO:tensorflow:global_step/sec: 322.83
INFO:tensorflow:loss = 12480.3, step = 201 (0.311 sec)
INFO:tensorflow:global_step/sec: 311.359
INFO:tensorflow:loss = 7034.04, step = 301 (0.322 sec)
INFO:tensorflow:global_step/sec: 313.204
INFO:tensorflow:loss = 7934.45, step = 401 (0.319 sec)
INFO:tensorflow:global_step/sec: 314.896
INFO:tensorflow:loss = 9478.76, step = 501 (0.315 sec)
INFO:tensorflow:global_step/sec: 319.332
INFO:tensorflow:loss = 12183.3, step = 601 (0.315 sec)
INFO:tensorflow:global_step/sec: 299.315
INFO:tensorflow:loss = 5746.13, step = 701 (0.335 sec)
INFO:tensorflow:global_step/sec: 258.435
INFO:tensorflow:loss = 8567.06, step = 801 (0.388 sec)
INFO:tensorflow:global_step/sec: 188.368
INFO:tensorflow:loss = 9319.67, step = 901 (0.532 sec)
INFO:tensorflow:global_step/sec: 295.455
INFO:tensorflow:loss = 7470.12, step = 1001 (0.336 sec)
INFO:tensorflow:global_step/sec: 325.715
INFO:tensorflow:loss = 6023.4, step = 1101 (0.305 sec)
INFO:tensorflow:global_step/sec: 324.3
INFO:tensorflow:loss = 4616.71, step = 1201 (0.311 sec)
INFO:tensorflow:global_step/sec: 320.935
INFO:tensorflow:loss = 5843.02, step = 1301 (0.312 sec)
INFO:tensorflow:global_step/sec: 308.673
INFO:tensorflow:loss = 3202.27, step = 1401 (0.324 sec)
INFO:tensorflow:global_step/sec: 306.351
INFO:tensorflow:loss = 4429.14, step = 1501 (0.325 sec)
INFO:tensorflow:global_step/sec: 282.614
INFO:tensorflow:loss = 4777.83, step = 1601 (0.357 sec)
INFO:tensorflow:global_step/sec: 217.562
INFO:tensorflow:loss = 3422.54, step = 1701 (0.458 sec)
INFO:tensorflow:global_step/sec: 319.675
INFO:tensorflow:loss = 4269.16, step = 1801 (0.313 sec)
INFO:tensorflow:global_step/sec: 322.19
INFO:tensorflow:loss = 3759.13, step = 1901 (0.310 sec)
INFO:tensorflow:global_step/sec: 324.385
INFO:tensorflow:loss = 3474.11, step = 2001 (0.306 sec)
INFO:tensorflow:global_step/sec: 327.349
INFO:tensorflow:loss = 4642.67, step = 2101 (0.306 sec)
INFO:tensorflow:global_step/sec: 227.212
INFO:tensorflow:loss = 2493.2, step = 2201 (0.444 sec)
INFO:tensorflow:global_step/sec: 310.552
INFO:tensorflow:loss = 2710.02, step = 2301 (0.318 sec)
INFO:tensorflow:global_step/sec: 319.138
INFO:tensorflow:loss = 4863.63, step = 2401 (0.313 sec)
INFO:tensorflow:global_step/sec: 325.259
INFO:tensorflow:loss = 4994.38, step = 2501 (0.308 sec)
INFO:tensorflow:global_step/sec: 322.637
INFO:tensorflow:loss = 1879.84, step = 2601 (0.309 sec)
INFO:tensorflow:global_step/sec: 328.079
INFO:tensorflow:loss = 3959.43, step = 2701 (0.305 sec)
INFO:tensorflow:global_step/sec: 325.559
INFO:tensorflow:loss = 5306.36, step = 2801 (0.309 sec)
INFO:tensorflow:global_step/sec: 308.293
INFO:tensorflow:loss = 3314.23, step = 2901 (0.325 sec)
INFO:tensorflow:global_step/sec: 271.218
INFO:tensorflow:loss = 2857.12, step = 3001 (0.368 sec)
INFO:tensorflow:global_step/sec: 227.951
INFO:tensorflow:loss = 4615.06, step = 3101 (0.441 sec)
INFO:tensorflow:global_step/sec: 299.159
INFO:tensorflow:loss = 3202.69, step = 3201 (0.330 sec)
INFO:tensorflow:global_step/sec: 321.576
INFO:tensorflow:loss = 3426.37, step = 3301 (0.313 sec)
INFO:tensorflow:global_step/sec: 322.83
INFO:tensorflow:loss = 1981.9, step = 3401 (0.310 sec)
INFO:tensorflow:global_step/sec: 213.875
INFO:tensorflow:loss = 2351.8, step = 3501 (0.472 sec)
INFO:tensorflow:global_step/sec: 294.376
INFO:tensorflow:loss = 2936.26, step = 3601 (0.333 sec)
INFO:tensorflow:global_step/sec: 319.449
INFO:tensorflow:loss = 5116.86, step = 3701 (0.313 sec)
INFO:tensorflow:global_step/sec: 323.556
INFO:tensorflow:loss = 3181.66, step = 3801 (0.309 sec)
INFO:tensorflow:global_step/sec: 296.689
INFO:tensorflow:loss = 3830.36, step = 3901 (0.339 sec)
INFO:tensorflow:global_step/sec: 314.811
INFO:tensorflow:loss = 2939.96, step = 4001 (0.318 sec)
INFO:tensorflow:global_step/sec: 322.22
INFO:tensorflow:loss = 2754.08, step = 4101 (0.308 sec)
INFO:tensorflow:global_step/sec: 318.376
INFO:tensorflow:loss = 3304.89, step = 4201 (0.316 sec)
INFO:tensorflow:global_step/sec: 238.7
INFO:tensorflow:loss = 3187.45, step = 4301 (0.422 sec)
INFO:tensorflow:global_step/sec: 222.949
INFO:tensorflow:loss = 2316.51, step = 4401 (0.443 sec)
INFO:tensorflow:global_step/sec: 304.697
INFO:tensorflow:loss = 2896.22, step = 4501 (0.329 sec)
INFO:tensorflow:global_step/sec: 323.762
INFO:tensorflow:loss = 3316.62, step = 4601 (0.311 sec)
INFO:tensorflow:global_step/sec: 323.942
INFO:tensorflow:loss = 2173.54, step = 4701 (0.307 sec)
INFO:tensorflow:global_step/sec: 320.776
INFO:tensorflow:loss = 5135.77, step = 4801 (0.312 sec)
INFO:tensorflow:global_step/sec: 327.766
INFO:tensorflow:loss = 3272.52, step = 4901 (0.305 sec)
INFO:tensorflow:Saving checkpoints for 5000 into /tmp/boston_model/model.ckpt.
INFO:tensorflow:Loss for final step: 4492.03.
Out[102]:
<tensorflow.python.estimator.canned.dnn.DNNRegressor at 0x123f99590>

In [103]:
ev = regressor.evaluate(
    input_fn=get_input_fn(test_set, num_epochs=1, shuffle=False))


INFO:tensorflow:Starting evaluation at 2018-01-17-14:22:07
INFO:tensorflow:Restoring parameters from /tmp/boston_model/model.ckpt-5000
INFO:tensorflow:Finished evaluation at 2018-01-17-14:22:07
INFO:tensorflow:Saving dict for global step 5000: average_loss = 15.4869, global_step = 5000, loss = 1548.69

In [104]:
loss_score = ev["loss"]
print("Loss: {0:f}".format(loss_score))


Loss: 1548.693970

In [105]:
y = regressor.predict(
    input_fn=get_input_fn(prediction_set, num_epochs=1, shuffle=False))
# .predict() returns an iterator of dicts; convert to a list and print
# predictions
predictions = list(p["predictions"] for p in itertools.islice(y, 6))
print("Predictions: {}".format(str(predictions)))


INFO:tensorflow:Restoring parameters from /tmp/boston_model/model.ckpt-5000
Predictions: [array([ 35.31574631], dtype=float32), array([ 18.49681473], dtype=float32), array([ 24.10115433], dtype=float32), array([ 35.99119568], dtype=float32), array([ 15.93786335], dtype=float32), array([ 20.11325073], dtype=float32)]

In [109]:
word_embeddings = tf.get_variable("word_embeddings",
    [5, 3])


---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-109-e94605171995> in <module>()
      1 word_embeddings = tf.get_variable("word_embeddings",
----> 2     [5, 3])

/Users/shouzeluo/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.pyc in get_variable(name, shape, dtype, initializer, regularizer, trainable, collections, caching_device, partitioner, validate_shape, use_resource, custom_getter)
   1063       collections=collections, caching_device=caching_device,
   1064       partitioner=partitioner, validate_shape=validate_shape,
-> 1065       use_resource=use_resource, custom_getter=custom_getter)
   1066 get_variable_or_local_docstring = (
   1067     """%s

/Users/shouzeluo/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.pyc in get_variable(self, var_store, name, shape, dtype, initializer, regularizer, reuse, trainable, collections, caching_device, partitioner, validate_shape, use_resource, custom_getter)
    960           collections=collections, caching_device=caching_device,
    961           partitioner=partitioner, validate_shape=validate_shape,
--> 962           use_resource=use_resource, custom_getter=custom_getter)
    963 
    964   def _get_partitioned_variable(self,

/Users/shouzeluo/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.pyc in get_variable(self, name, shape, dtype, initializer, regularizer, reuse, trainable, collections, caching_device, partitioner, validate_shape, use_resource, custom_getter)
    365           reuse=reuse, trainable=trainable, collections=collections,
    366           caching_device=caching_device, partitioner=partitioner,
--> 367           validate_shape=validate_shape, use_resource=use_resource)
    368 
    369   def _get_partitioned_variable(

/Users/shouzeluo/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.pyc in _true_getter(name, shape, dtype, initializer, regularizer, reuse, trainable, collections, caching_device, partitioner, validate_shape, use_resource)
    350           trainable=trainable, collections=collections,
    351           caching_device=caching_device, validate_shape=validate_shape,
--> 352           use_resource=use_resource)
    353 
    354     if custom_getter is not None:

/Users/shouzeluo/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.pyc in _get_single_variable(self, name, shape, dtype, initializer, regularizer, partition_info, reuse, trainable, collections, caching_device, validate_shape, use_resource)
    723           caching_device=caching_device,
    724           dtype=variable_dtype,
--> 725           validate_shape=validate_shape)
    726     self._vars[name] = v
    727     logging.vlog(1, "Created variable %s with shape %s and init %s", v.name,

/Users/shouzeluo/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/tensorflow/python/ops/variables.pyc in __init__(self, initial_value, trainable, collections, validate_shape, caching_device, name, variable_def, dtype, expected_shape, import_scope)
    197           name=name,
    198           dtype=dtype,
--> 199           expected_shape=expected_shape)
    200 
    201   def __repr__(self):

/Users/shouzeluo/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/tensorflow/python/ops/variables.pyc in _init_from_args(self, initial_value, trainable, collections, validate_shape, caching_device, name, dtype, expected_shape)
    275             with ops.name_scope("Initializer"),  ops.device(None):
    276               self._initial_value = ops.convert_to_tensor(
--> 277                   initial_value(), name="initial_value", dtype=dtype)
    278               shape = (self._initial_value.get_shape()
    279                        if validate_shape else tensor_shape.unknown_shape())

/Users/shouzeluo/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.pyc in <lambda>()
    699           initializer = initializer(dtype=dtype)
    700         init_val = lambda: initializer(  # pylint: disable=g-long-lambda
--> 701             shape.as_list(), dtype=dtype, partition_info=partition_info)
    702         variable_dtype = dtype.base_dtype
    703 

/Users/shouzeluo/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/tensorflow/python/ops/init_ops.pyc in __call__(self, shape, dtype, partition_info)
    439       limit = math.sqrt(3.0 * scale)
    440       return random_ops.random_uniform(shape, -limit, limit,
--> 441                                        dtype, seed=self.seed)
    442 
    443   def get_config(self):

/Users/shouzeluo/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/tensorflow/python/ops/random_ops.pyc in random_uniform(shape, minval, maxval, dtype, seed, name)
    229     maxval = 1
    230   with ops.name_scope(name, "random_uniform", [shape, minval, maxval]) as name:
--> 231     shape = _ShapeTensor(shape)
    232     minval = ops.convert_to_tensor(minval, dtype=dtype, name="min")
    233     maxval = ops.convert_to_tensor(maxval, dtype=dtype, name="max")

/Users/shouzeluo/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/tensorflow/python/ops/random_ops.pyc in _ShapeTensor(shape)
     40   else:
     41     dtype = None
---> 42   return ops.convert_to_tensor(shape, dtype=dtype, name="shape")
     43 
     44 

/Users/shouzeluo/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in convert_to_tensor(value, dtype, name, preferred_dtype)
    609       name=name,
    610       preferred_dtype=preferred_dtype,
--> 611       as_ref=False)
    612 
    613 

/Users/shouzeluo/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype)
    674 
    675         if ret is None:
--> 676           ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
    677 
    678         if ret is NotImplemented:

/Users/shouzeluo/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/tensorflow/python/framework/constant_op.pyc in _constant_tensor_conversion_function(v, dtype, name, as_ref)
    119                                          as_ref=False):
    120   _ = as_ref
--> 121   return constant(v, dtype=dtype, name=name)
    122 
    123 

/Users/shouzeluo/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/tensorflow/python/framework/constant_op.pyc in constant(value, dtype, shape, name, verify_shape)
    104   const_tensor = g.create_op(
    105       "Const", [], [dtype_value.type],
--> 106       attrs={"value": tensor_value, "dtype": dtype_value}, name=name).outputs[0]
    107   return const_tensor
    108 

/Users/shouzeluo/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in create_op(self, op_type, inputs, dtypes, input_types, name, attrs, op_def, compute_shapes, compute_device)
   2580 
   2581     """
-> 2582     self._check_not_finalized()
   2583     for idx, a in enumerate(inputs):
   2584       if not isinstance(a, Tensor):

/Users/shouzeluo/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in _check_not_finalized(self)
   2288     """
   2289     if self._finalized:
-> 2290       raise RuntimeError("Graph is finalized and cannot be modified.")
   2291 
   2292   def _add_op(self, op):

RuntimeError: Graph is finalized and cannot be modified.

In [110]:
embedded_word_ids = tf.nn.embedding_lookup(word_embeddings, word_ids)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-110-88f5fdcc8b54> in <module>()
----> 1 embedded_word_ids = tf.nn.embedding_lookup(word_embeddings, word_ids)

NameError: name 'word_embeddings' is not defined

In [ ]: