In [113]:
from __future__ import absolute_import, division, print_function

import os
import matplotlib.pyplot as plt

import tensorflow as tf
import tensorflow.contrib.eager as tfe

from sklearn.preprocessing import LabelEncoder

sess = tf.Session()

In [2]:
tf.enable_eager_execution()

print("TensorFlow version: {}".format(tf.VERSION))
print("Eager execution: {}".format(tf.executing_eagerly()))


TensorFlow version: 1.8.0
Eager execution: True

In [3]:
train_dataset_url = "file:/‪/C:/Users/André/Desktop/train.csv"

train_dataset_fp = tf.keras.utils.get_file(fname=os.path.basename(train_dataset_url),
                                           origin=train_dataset_url)

print("Local copy of the dataset file: {}".format(train_dataset_fp))


Downloading data from file:/‪/C:/Users/André/Desktop/train.csv
65536/61194 [================================] - 0s 0us/step
Local copy of the dataset file: C:\Users\André\.keras\datasets\train.csv

In [118]:
def parse_csv(line):
  example_defaults = [[0], [0], [0], [''], [''], [0.], [0], [0], [''], [0.], [''], ['']]  # sets field types
  parsed_line = tf.decode_csv(line, example_defaults)
  header = ['PassengerId','Survived','Pclass','Name','Sex','Age','SibSp','Parch','Ticket','Fare','Cabin','Embarked']
  # All but second fields are features, combine into single tensor
  label = parsed_line[2]
  le = LabelEncoder()
  le.fit(["male","female"])
  parsed_line[5]=tf.shape(le.transform(parsed_line[5].eval())) 
  le.fit(["Q", "C", "S"])
  parsed_line[12]=tf.shape(le.transform(parsed_line[12].eval()))
              
  del parsed_line[1:2,4,9,10]
  features = tf.reshape(parsed_line, shape=(10,1))
  #features = zip(header, parsed_line)
  # Second field is the label
  #label = features.pop('Survived')
  return features, label

In [122]:
train_dataset = tf.data.TextLineDataset(train_dataset_fp)
train_dataset = train_dataset.skip(1)             # skip the first header row
train_dataset = train_dataset.map(parse_csv)      # parse each row
train_dataset = train_dataset.shuffle(buffer_size=1000)  # randomize
train_dataset = train_dataset.batch(32)

# View a single example entry from a batch
features, label = iter(train_dataset).next()
print("example features:", features[0])
print("example label:", label[0])


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-122-65d6a9d9d017> in <module>()
      2 train_dataset = tf.data.TextLineDataset(train_dataset_fp)
      3 train_dataset = train_dataset.skip(1)             # skip the first header row
----> 4 train_dataset = train_dataset.map(parse_csv)      # parse each row
      5 train_dataset = train_dataset.shuffle(buffer_size=1000)  # randomize
      6 train_dataset = train_dataset.batch(32)

C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py in map(self, map_func, num_parallel_calls)
    849     """
    850     if num_parallel_calls is None:
--> 851       return MapDataset(self, map_func)
    852     else:
    853       return ParallelMapDataset(self, map_func, num_parallel_calls)

C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py in __init__(self, input_dataset, map_func)
   1837 
   1838     self._map_func = tf_map_func
-> 1839     self._map_func.add_to_graph(ops.get_default_graph())
   1840 
   1841   def _as_variant_tensor(self):

C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\function.py in add_to_graph(self, g)
    482   def add_to_graph(self, g):
    483     """Adds this function into the graph g."""
--> 484     self._create_definition_if_needed()
    485 
    486     # Adds this function into 'g'.

C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\function.py in _create_definition_if_needed(self)
    317     """Creates the function definition if it's not created yet."""
    318     with context.graph_mode():
--> 319       self._create_definition_if_needed_impl()
    320 
    321   def _create_definition_if_needed_impl(self):

C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\function.py in _create_definition_if_needed_impl(self)
    334       # Call func and gather the output tensors.
    335       with vs.variable_scope("", custom_getter=temp_graph.getvar):
--> 336         outputs = self._func(*inputs)
    337 
    338       # There is no way of distinguishing between a function not returning

C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py in tf_map_func(*args)
   1802         ret = map_func(*nested_args)
   1803       else:
-> 1804         ret = map_func(nested_args)
   1805 
   1806       # If `map_func` returns a list of tensors, `nest.flatten()` and

<ipython-input-118-82b4bfcb2fed> in parse_csv(line)
      7   le = LabelEncoder()
      8   le.fit(["male","female"])
----> 9   parsed_line[5]=tf.shape(le.transform(parsed_line[5].eval()))
     10   le.fit(["Q", "C", "S"])
     11   parsed_line[12]=tf.shape(le.transform(parsed_line[12].eval()))

C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py in eval(self, feed_dict, session)
    708 
    709     """
--> 710     return _eval_using_default_session(self, feed_dict, self.graph, session)
    711 
    712 

C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py in _eval_using_default_session(tensors, feed_dict, graph, session)
   5164     session = get_default_session()
   5165     if session is None:
-> 5166       raise ValueError("Cannot evaluate tensor using `eval()`: No default "
   5167                        "session is registered. Use `with "
   5168                        "sess.as_default()` or pass an explicit session to "

ValueError: Cannot evaluate tensor using `eval()`: No default session is registered. Use `with sess.as_default()` or pass an explicit session to `eval(session=sess)`

In [ ]:
model = tf.keras.Sequential([
  tf.keras.layers.Dense(10, activation="relu", input_shape=(4,)),  # input shape required
  tf.keras.layers.Dense(5, activation="relu"),
  tf.keras.layers.Dense(3)
])

In [ ]:
def loss(model, x, y):
  y_ = model(x)
  return tf.losses.sparse_softmax_cross_entropy(labels=y, logits=y_)


def grad(model, inputs, targets):
  with tf.GradientTape() as tape:
    loss_value = loss(model, inputs, targets)
  return tape.gradient(loss_value, model.variables)

In [ ]:
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)

In [ ]:
## Note: Rerunning this cell uses the same model variables

# keep results for plotting
train_loss_results = []
train_accuracy_results = []

num_epochs = 201

for epoch in range(num_epochs):
  epoch_loss_avg = tfe.metrics.Mean()
  epoch_accuracy = tfe.metrics.Accuracy()

  # Training loop - using batches of 32
  for x, y in train_dataset:
    # Optimize the model
    grads = grad(model, x, y)
    optimizer.apply_gradients(zip(grads, model.variables),
                              global_step=tf.train.get_or_create_global_step())

    # Track progress
    epoch_loss_avg(loss(model, x, y))  # add current batch loss
    # compare predicted label to actual label
    epoch_accuracy(tf.argmax(model(x), axis=1, output_type=tf.int32), y)

  # end epoch
  train_loss_results.append(epoch_loss_avg.result())
  train_accuracy_results.append(epoch_accuracy.result())
  
  if epoch % 50 == 0:
    print("Epoch {:03d}: Loss: {:.3f}, Accuracy: {:.3%}".format(epoch,
                                                                epoch_loss_avg.result(),
                                                                epoch_accuracy.result()))

In [ ]:
fig, axes = plt.subplots(2, sharex=True, figsize=(12, 8))
fig.suptitle('Training Metrics')

axes[0].set_ylabel("Loss", fontsize=14)
axes[0].plot(train_loss_results)

axes[1].set_ylabel("Accuracy", fontsize=14)
axes[1].set_xlabel("Epoch", fontsize=14)
axes[1].plot(train_accuracy_results)

plt.show()

In [ ]:
test_url = "http://download.tensorflow.org/data/iris_test.csv"

test_fp = tf.keras.utils.get_file(fname=os.path.basename(test_url),
                                  origin=test_url)

test_dataset = tf.data.TextLineDataset(test_fp)
test_dataset = test_dataset.skip(1)             # skip header row
test_dataset = test_dataset.map(parse_csv)      # parse each row with the funcition created earlier
test_dataset = test_dataset.shuffle(1000)       # randomize
test_dataset = test_dataset.batch(32)           # use the same batch size as the training set

In [ ]:
test_accuracy = tfe.metrics.Accuracy()

for (x, y) in test_dataset:
  prediction = tf.argmax(model(x), axis=1, output_type=tf.int32)
  test_accuracy(prediction, y)

print("Test set accuracy: {:.3%}".format(test_accuracy.result()))

In [ ]:
class_ids = ["Iris setosa", "Iris versicolor", "Iris virginica"]

predict_dataset = tf.convert_to_tensor([
    [5.1, 3.3, 1.7, 0.5,],
    [5.9, 3.0, 4.2, 1.5,],
    [6.9, 3.1, 5.4, 2.1]
])

predictions = model(predict_dataset)

for i, logits in enumerate(predictions):
  class_idx = tf.argmax(logits).numpy()
  name = class_ids[class_idx]
  print("Example {} prediction: {}".format(i, name))

In [ ]: