<jupyter_start><jupyter_text>Load CSV data  Setup<jupyter_code>import pandas as pd
import numpy as np

# Make numpy values easier to read.
np.set_printoptions(precision=3, suppress=True)

import tensorflow as tf
from tensorflow.keras import layers
from tensorflow.keras.layers.experimental import preprocessing<jupyter_output><empty_output><jupyter_text>Mixed data typesThe "Titanic" dataset contains information about the passengers on the Titanic. The nominal task on this dataset is to predict who survived.<jupyter_code>titanic = pd.read_csv("https://storage.googleapis.com/tf-datasets/titanic/train.csv")
titanic.head()
titanic_features = titanic.copy()
titanic_labels = titanic_features.pop('survived')
titanic_features
titanic_labels<jupyter_output><empty_output><jupyter_text>Because of the different data types and ranges you can't simply stack the features into  NumPy array and pass it to a `keras.Sequential` model. Each column needs to be handled individually. As one option, you could preprocess your data offline (using any tool you like) to convert categorical columns to numeric columns, then pass the processed output to your TensorFlow model. The disadvantage to that approach is that if you save and export your model the preprocessing is not saved with it. The `experimental.preprocessing` layers avoid this problem because they're part of the model. In this example, you'll build a model that implements the preprocessing logic using [Keras functional API](https://www.tensorflow.org/guide/keras/functional.ipynb). You could also do it by [subclassing](https://www.tensorflow.org/guide/keras/custom_layers_and_models).The functional API operates on "symbolic" tensors. Normal "eager" tensors have a value. In contrast these "symbolic" tensors do not. Instead they keep track of which operations are run on them, and build representation of the calculation, that you can run later. Here's a quick example:<jupyter_code># Create a symbolic input
input = tf.keras.Input(shape=(), dtype=tf.float32)

# Do a calculation using is
result = 2*input + 1

# the result doesn't have a value
result
calc = tf.keras.Model(inputs=input, outputs=result)
print(calc(1).numpy())
print(calc(2).numpy())<jupyter_output>3.0
5.0<jupyter_text>To build the preprocessing model, start by building a set of symbolic `keras.Input` objects, matching the names and data-types of the CSV columns.<jupyter_code>inputs = {}

for name, column in titanic_features.items():
  dtype = column.dtype
  if dtype == object:
    dtype = tf.string
  else:
    dtype = tf.float32

  inputs[name] = tf.keras.Input(shape=(1,), name=name, dtype=dtype)

inputs<jupyter_output><empty_output><jupyter_text>The first step in your preprocessing logic is to concatenate the numeric inputs together, and run them through a normalization layer:<jupyter_code>numeric_inputs = {name:input for name,input in inputs.items()
                  if input.dtype==tf.float32}

x = layers.Concatenate()(list(numeric_inputs.values()))
norm = preprocessing.Normalization()
norm.adapt(np.array(titanic[numeric_inputs.keys()]))
all_numeric_inputs = norm(x)

all_numeric_inputs<jupyter_output><empty_output><jupyter_text>Collect all the symbolic preprocessing results, to concatenate them later.<jupyter_code>preprocessed_inputs = [all_numeric_inputs]<jupyter_output><empty_output><jupyter_text>For the string inputs use the `preprocessing.StringLookup` function to map from strings to integer indices in a vocabulary. Next, use `preprocessing.CategoryEncoding` to convert the indexes into `float32` data appropriate for the model. The default settings for the `preprocessing.CategoryEncoding` layer create a one-hot vector for each input. A `layers.Embedding` would also work. See the [preprocessing layers guide](https://www.tensorflow.org/guide/keras/preprocessing_layersquick_recipes) and [tutorial](../structured_data/preprocessing_layers.ipynb) for more on this topic.<jupyter_code>for name, input in inputs.items():
  if input.dtype == tf.float32:
    continue
  
  lookup = preprocessing.StringLookup(vocabulary=np.unique(titanic_features[name]))
  one_hot = preprocessing.CategoryEncoding(max_tokens=lookup.vocab_size())

  x = lookup(input)
  x = one_hot(x)
  preprocessed_inputs.append(x)<jupyter_output><empty_output><jupyter_text>With the collection of `inputs` and `processed_inputs`, you can concatenate all the preprocessed inputs together, and build a model that handles the preprocessing:<jupyter_code>preprocessed_inputs_cat = layers.Concatenate()(preprocessed_inputs)

titanic_preprocessing = tf.keras.Model(inputs, preprocessed_inputs_cat)

tf.keras.utils.plot_model(model = titanic_preprocessing , rankdir="LR", dpi=72, show_shapes=True)<jupyter_output>('Failed to import pydot. You must `pip install pydot` and install graphviz (https://graphviz.gitlab.io/download/), ', 'for `pydotprint` to work.')<jupyter_text>This `model` just contains the input preprocessing. You can run it to see what it does to your data. Keras models don't automatically convert Pandas `DataFrames` because it's not clear if it should be converted to one tensor or to a dictionary of tensors. So convert it to a dictionary of tensors:<jupyter_code>titanic_features_dict = {name: np.array(value) 
                         for name, value in titanic_features.items()}<jupyter_output><empty_output><jupyter_text>Slice out the first training example and pass it to this preprocessing model, you see the numeric features and string one-hots all concatenated together:<jupyter_code>features_dict = {name:values[:1] for name, values in titanic_features_dict.items()}
titanic_preprocessing(features_dict)<jupyter_output><empty_output><jupyter_text>Now build the model on top of this:<jupyter_code>def titanic_model(preprocessing_head, inputs):
  body = tf.keras.Sequential([
    layers.Dense(64),
    layers.Dense(1)
  ])

  preprocessed_inputs = preprocessing_head(inputs)
  result = body(preprocessed_inputs)
  model = tf.keras.Model(inputs, result)

  model.compile(loss=tf.losses.BinaryCrossentropy(from_logits=True),
                optimizer=tf.optimizers.Adam())
  return model

titanic_model = titanic_model(titanic_preprocessing, inputs)<jupyter_output><empty_output><jupyter_text>When you train the model, pass the dictionary of features as `x`, and the label as `y`.<jupyter_code>titanic_model.fit(x=titanic_features_dict, y=titanic_labels, epochs=10)<jupyter_output>Epoch 1/10
20/20 [==============================] - 0s 1ms/step - loss: 0.6473
Epoch 2/10
20/20 [==============================] - 0s 2ms/step - loss: 0.5534
Epoch 3/10
20/20 [==============================] - 0s 2ms/step - loss: 0.5011
Epoch 4/10
20/20 [==============================] - 0s 2ms/step - loss: 0.4720
Epoch 5/10
20/20 [==============================] - 0s 2ms/step - loss: 0.4513
Epoch 6/10
20/20 [==============================] - 0s 1ms/step - loss: 0.4405
Epoch 7/10
20/20 [==============================] - 0s 1ms/step - loss: 0.4325
Epoch 8/10
20/20 [==============================] - 0s 2ms/step - loss: 0.4281
Epoch 9/10
20/20 [==============================] - 0s 1ms/step - loss: 0.4241
Epoch 10/10
20/20 [==============================] - 0s 1ms/step - loss: 0.4225<jupyter_text>Since the preprocessing is part of the model, you can save the model and reload it somewhere else and get identical results:<jupyter_code>titanic_model.save('test')
reloaded = tf.keras.models.load_model('test')
features_dict = {name:values[:1] for name, values in titanic_features_dict.items()}

before = titanic_model(features_dict)
after = reloaded(features_dict)
assert (before-after)<1e-3
print(before)
print(after)<jupyter_output><empty_output><jupyter_text>Using tf.data In the previous section you relied on the model's built-in data shuffling and batching while training the model. If you need more control over the input data pipeline or need to use data that doesn't easily fit into memory: use `tf.data`. For more examples see the [tf.data guide](../../guide/data.ipynb).  On in memory dataAs a first example of applying `tf.data` to CSV data consider the following code to manually slice up the dictionary of features from the previous section. For each index, it takes that index for each feature:<jupyter_code>import itertools

def slices(features):
  for i in itertools.count():
    # For each feature take index `i`
    example = {name:values[i] for name, values in features.items()}
    yield example<jupyter_output><empty_output><jupyter_text>Run this and print the first example:<jupyter_code>for example in slices(titanic_features_dict):
  for name, value in example.items():
    print(f"{name:19s}: {value}")
  break<jupyter_output><empty_output><jupyter_text>The most basic `tf.data.Dataset` in memory data loader is the `Dataset.from_tensor_slices` constructor. This returns a `tf.data.Dataset` that implements a generalized version of the above `slices` function, in TensorFlow.<jupyter_code>features_ds = tf.data.Dataset.from_tensor_slices(titanic_features_dict)<jupyter_output><empty_output><jupyter_text>You can iterate over a `tf.data.Dataset` like any other python iterable:<jupyter_code>for example in features_ds:
  for name, value in example.items():
    print(f"{name:19s}: {value}")
  break<jupyter_output><empty_output><jupyter_text>The `from_tensor_slices` function can handle any structure of nested dictionaries or tuples. The following code makes a dataset of `(features_dict, labels)` pairs:<jupyter_code>titanic_ds = tf.data.Dataset.from_tensor_slices((titanic_features_dict, titanic_labels))<jupyter_output><empty_output><jupyter_text>To train a model using this `Dataset`, you'll need to at least `shuffle` and `batch` the data.<jupyter_code>titanic_batches = titanic_ds.shuffle(len(titanic_labels)).batch(32)<jupyter_output><empty_output><jupyter_text>Instead of passing `features` and `labels` to `Model.fit`, you pass the dataset:<jupyter_code>titanic_model.fit(titanic_batches, epochs=5)<jupyter_output><empty_output><jupyter_text>From a single fileSo far this tutorial has worked with in-memory data. `tf.data` is a highly scalable toolkit for building data pipelines, and provides a few functions for dealing loading CSV files.<jupyter_code>titanic_file_path = tf.keras.utils.get_file("train.csv", "https://storage.googleapis.com/tf-datasets/titanic/train.csv")<jupyter_output><empty_output><jupyter_text>Now read the CSV data from the file and create a `tf.data.Dataset`. (For the full documentation, see `tf.data.experimental.make_csv_dataset`)<jupyter_code>titanic_csv_ds = tf.data.experimental.make_csv_dataset(
    titanic_file_path,
    batch_size=5, # Artificially small to make examples easier to show.
    label_name='survived',
    num_epochs=1,
    ignore_errors=True,)<jupyter_output><empty_output><jupyter_text>This function includes many convenient features so the data is easy to work with. This includes:* Using the column headers as dictionary keys.* Automatically determining the type of each column.<jupyter_code>for batch, label in titanic_csv_ds.take(1):
  for key, value in batch.items():
    print(f"{key:20s}: {value}")
  print()
  print(f"{'label':20s}: {label}")<jupyter_output><empty_output><jupyter_text>Note: if you run the above cell twice it will produce different results. The default settings for `make_csv_dataset` include `shuffle_buffer_size=1000`, which is more than sufficient for this small dataset, but may not be for a real-world dataset. It can also decompress the data on the fly. Here's a gzipped CSV file containing the [metro interstate traffic dataset](https://archive.ics.uci.edu/ml/datasets/Metro+Interstate+Traffic+Volume)![A traffic jam.](https://github.com/tensorflow/docs/blob/master/site/en/tutorials/load_data/images/csv/traffic.jpg?raw=1)Image [from Wikimedia](https://commons.wikimedia.org/wiki/File:Trafficjam.jpg)<jupyter_code>traffic_volume_csv_gz = tf.keras.utils.get_file(
    'Metro_Interstate_Traffic_Volume.csv.gz', 
    "https://archive.ics.uci.edu/ml/machine-learning-databases/00492/Metro_Interstate_Traffic_Volume.csv.gz",
    cache_dir='.', cache_subdir='traffic')<jupyter_output><empty_output><jupyter_text>Set the `compression_type` argument to read directly from the compressed file:<jupyter_code>traffic_volume_csv_gz_ds = tf.data.experimental.make_csv_dataset(
    traffic_volume_csv_gz,
    batch_size=256,
    label_name='traffic_volume',
    num_epochs=1,
    compression_type="GZIP")

for batch, label in traffic_volume_csv_gz_ds.take(1):
  for key, value in batch.items():
    print(f"{key:20s}: {value[:5]}")
  print()
  print(f"{'label':20s}: {label[:5]}")<jupyter_output><empty_output><jupyter_text>Note: If you need to parse those date-time strings in the `tf.data` pipeline you can use `tfa.text.parse_time`.  Caching There is some overhead to parsing the csv data. For small models this can be the bottleneck in training.Depending on your use case it may be a good idea to use `Dataset.cache` or `data.experimental.snapshot` so that the csv data is only parsed on the first epoch. The main difference between the `cache` and `snapshot` methods is that `cache` files can only be used by the TensorFlow process that created them, but `snapshot` files can be read by other processes.For example, iterating over the `traffic_volume_csv_gz_ds` 20 times, takes ~15 seconds without caching, or ~2s with caching.<jupyter_code>%%time
for i, (batch, label) in enumerate(traffic_volume_csv_gz_ds.repeat(20)):
  if i % 40 == 0:
    print('.', end='')
print()<jupyter_output><empty_output><jupyter_text>Note: `Dataset.cache`  stores the data form the first epoch and replays it in order. So using `.cache` disables any shuffles earlier in the pipeline. Below the `.shuffle` is added back in after `.cache`.<jupyter_code>%%time
caching = traffic_volume_csv_gz_ds.cache().shuffle(1000)

for i, (batch, label) in enumerate(caching.shuffle(1000).repeat(20)):
  if i % 40 == 0:
    print('.', end='')
print()<jupyter_output><empty_output><jupyter_text>Note: `snapshot` files are meant for *temporary* storage of a dataset while in use. This is *not* a format for long term storage. The file format is considered an internal detail, and not guaranteed between TensorFlow versions.<jupyter_code>%%time
snapshot = tf.data.experimental.snapshot('titanic.tfsnap')
snapshotting = traffic_volume_csv_gz_ds.apply(snapshot).shuffle(1000)

for i, (batch, label) in enumerate(snapshotting.shuffle(1000).repeat(20)):
  if i % 40 == 0:
    print('.', end='')
print()<jupyter_output><empty_output><jupyter_text>If your data loading is slowed by loading csv files, and `cache` and `snapshot` are insufficient for your use case, consider re-encoding your data into a more streamlined format.  Multiple files All the examples so far in this section could easily be done without `tf.data`. One place where `tf.data` can really simplify things is when dealing with collections of files.For example, the [character font images](https://archive.ics.uci.edu/ml/datasets/Character+Font+Images) dataset is distributed as a collection of csv files, one per font.![Fonts](https://github.com/tensorflow/docs/blob/master/site/en/tutorials/load_data/images/csv/fonts.jpg?raw=1)Image by <NAME> from PixabayDownload the dataset, and have a look at the files inside:<jupyter_code>fonts_zip = tf.keras.utils.get_file(
    'fonts.zip',  "https://archive.ics.uci.edu/ml/machine-learning-databases/00417/fonts.zip",
    cache_dir='.', cache_subdir='fonts',
    extract=True)
import pathlib
font_csvs =  sorted(str(p) for p in pathlib.Path('fonts').glob("*.csv"))

font_csvs[:10]
len(font_csvs)<jupyter_output><empty_output><jupyter_text>When dealing with a bunch of files you can pass a glob-style `file_pattern` to the `experimental.make_csv_dataset` function. The order of the files is shuffled each iteration.Use the `num_parallel_reads` argument to set how many files are read in parallel and interleaved together.<jupyter_code>fonts_ds = tf.data.experimental.make_csv_dataset(
    file_pattern = "fonts/*.csv",
    batch_size=10, num_epochs=1,
    num_parallel_reads=20,
    shuffle_buffer_size=10000)<jupyter_output><empty_output><jupyter_text>These csv files have the images flattened out into a single row. The column names are formatted `r{row}c{column}`. Here's the first batch:<jupyter_code>for features in fonts_ds.take(1):
  for i, (name, value) in enumerate(features.items()):
    if i>15:
      break
    print(f"{name:20s}: {value}")
print('...')
print(f"[total: {len(features)} features]")<jupyter_output><empty_output><jupyter_text>Optional: Packing fieldsYou probably don't want to work with each pixel in separate columns like this. Before trying to use this dataset be sure to pack the pixels into an image-tensor. Here is code that parses the column names to build images for each example:<jupyter_code>import re

def make_images(features):
  image = [None]*400
  new_feats = {}

  for name, value in features.items():
    match = re.match('r(\d+)c(\d+)', name)
    if match:
      image[int(match.group(1))*20+int(match.group(2))] = value
    else:
      new_feats[name] = value

  image = tf.stack(image, axis=0)
  image = tf.reshape(image, [20, 20, -1])
  new_feats['image'] = image

  return new_feats<jupyter_output><empty_output><jupyter_text>Apply that function to each batch in the dataset:<jupyter_code>fonts_image_ds = fonts_ds.map(make_images)

for features in fonts_image_ds.take(1):
  break<jupyter_output><empty_output><jupyter_text>Plot the resulting images:<jupyter_code>from matplotlib import pyplot as plt

plt.figure(figsize=(6,6), dpi=120)

for n in range(9):
  plt.subplot(3,3,n+1)
  plt.imshow(features['image'][..., n])
  plt.title(chr(features['m_label'][n]))
  plt.axis('off')<jupyter_output><empty_output><jupyter_text>Lower level functions So far this tutorial has focused on the highest level utilities for reading csv data. There are other two APIs that may be helpful for advanced users if your use-case doesn't fit the basic patterns.* `tf.io.decode_csv` - a function for parsing lines of text into a list of CSV column tensors.* `tf.data.experimental.CsvDataset` - a lower level csv dataset constructor.This section recreates functionality provided by `make_csv_dataset`, to demonstrate how this lower level functionality can be used.  `tf.io.decode_csv`This function decodes a string, or list of strings into a list of columns.Unlike `make_csv_dataset` this function does not try to guess column data-types. You specify the column types by providing a list of `record_defaults` containing a value of the correct type, for each column.To read the Titanic data **as strings** using `decode_csv` you would say:<jupyter_code>text = pathlib.Path(titanic_file_path).read_text()
lines = text.split('\n')[1:-1]

all_strings = [str()]*10
all_strings
features = tf.io.decode_csv(lines, record_defaults=all_strings) 

for f in features:
  print(f"type: {f.dtype.name}, shape: {f.shape}")<jupyter_output><empty_output><jupyter_text>To parse them with their actual types, create a list of `record_defaults` of the corresponding types:<jupyter_code>print(lines[0])
titanic_types = [int(), str(), float(), int(), int(), float(), str(), str(), str(), str()]
titanic_types
features = tf.io.decode_csv(lines, record_defaults=titanic_types) 

for f in features:
  print(f"type: {f.dtype.name}, shape: {f.shape}")<jupyter_output><empty_output><jupyter_text>Note: it is more efficient to call `decode_csv` on large batches of lines than on individual lines of csv text.  `tf.data.experimental.CsvDataset`The `tf.data.experimental.CsvDataset` class provides a minimal CSV `Dataset` interface without the convenience features of the `make_csv_dataset` function: column header parsing, column type-inference, automatic shuffling, file interleaving.This constructor follows uses `record_defaults` the same way as `io.parse_csv`:<jupyter_code>simple_titanic = tf.data.experimental.CsvDataset(titanic_file_path, record_defaults=titanic_types, header=True)

for example in simple_titanic.take(1):
  print([e.numpy() for e in example])<jupyter_output><empty_output><jupyter_text>The above code is basically equivalent to:<jupyter_code>def decode_titanic_line(line):
  return tf.io.decode_csv(line, titanic_types)

manual_titanic = (
    # Load the lines of text
    tf.data.TextLineDataset(titanic_file_path)
    # Skip the header row.
    .skip(1)
    # Decode the line.
    .map(decode_titanic_line)
)

for example in manual_titanic.take(1):
  print([e.numpy() for e in example])<jupyter_output><empty_output><jupyter_text>Multiple filesTo parse the fonts dataset using `experimental.CsvDataset`, you first need to determine the column types for the `record_defaults`. Start by inspecting the first row of one file:<jupyter_code>font_line = pathlib.Path(font_csvs[0]).read_text().splitlines()[1]
print(font_line)<jupyter_output><empty_output><jupyter_text>Only the first two fields are strings, the rest are ints or floats, and you can get the total number of features by counting the commas:<jupyter_code>num_font_features = font_line.count(',')+1
font_column_types = [str(), str()] + [float()]*(num_font_features-2)<jupyter_output><empty_output><jupyter_text>The `CsvDatasaet` constructor can take a list of input files, but reads them sequentially. The first file in the list of CSVs is `AGENCY.csv`:<jupyter_code>font_csvs[0]<jupyter_output><empty_output><jupyter_text>So when you pass pass the list of files to `CsvDataaset` the records from `AGENCY.csv` are read first:<jupyter_code>simple_font_ds = tf.data.experimental.CsvDataset(
    font_csvs, 
    record_defaults=font_column_types, 
    header=True)
for row in simple_font_ds.take(10):
  print(row[0].numpy())<jupyter_output><empty_output><jupyter_text>To interleave multiple files, use `Dataset.interleave`.Here's an initial dataset that contains the csv file names:<jupyter_code>font_files = tf.data.Dataset.list_files("fonts/*.csv")<jupyter_output><empty_output><jupyter_text>This shuffles the file names each epoch:<jupyter_code>print('Epoch 1:')
for f in list(font_files)[:5]:
  print("    ", f.numpy())
print('    ...')
print()

print('Epoch 2:')
for f in list(font_files)[:5]:
  print("    ", f.numpy())
print('    ...')<jupyter_output><empty_output><jupyter_text>The `interleave` method takes a `map_func` that creates a child-`Dataset` for each element of the parent-`Dataset`. Here, you want to create a `CsvDataset` from each element of the dataset of files:<jupyter_code>def make_font_csv_ds(path):
  return tf.data.experimental.CsvDataset(
    path, 
    record_defaults=font_column_types, 
    header=True)<jupyter_output><empty_output><jupyter_text>The `Dataset` returned by interleave returns elements by cycling over a number of the child-`Dataset`s. Note, below, how the dataset cycles over `cycle_length)=3` three font files:<jupyter_code>font_rows = font_files.interleave(make_font_csv_ds,
                                  cycle_length=3)
fonts_dict = {'font_name':[], 'character':[]}

for row in font_rows.take(10):
  fonts_dict['font_name'].append(row[0].numpy().decode())
  fonts_dict['character'].append(chr(row[2].numpy()))

pd.DataFrame(fonts_dict)<jupyter_output><empty_output><jupyter_text>Performance Earlier, it was noted that `io.decode_csv` is more efficient when run on a batch of strings.It is possible to take advantage of this fact, when using large batch sizes, to improve CSV loading performance (but try [caching](caching) first). With the built-in loader 20, 2048-example batches take about 17s.<jupyter_code>BATCH_SIZE=2048
fonts_ds = tf.data.experimental.make_csv_dataset(
    file_pattern = "fonts/*.csv",
    batch_size=BATCH_SIZE, num_epochs=1,
    num_parallel_reads=100)
%%time
for i,batch in enumerate(fonts_ds.take(20)):
  print('.',end='')

print()<jupyter_output><empty_output><jupyter_text>Passing **batches of text lines** to`decode_csv` runs faster, in about 5s:<jupyter_code>fonts_files = tf.data.Dataset.list_files("fonts/*.csv")
fonts_lines = fonts_files.interleave(
    lambda fname:tf.data.TextLineDataset(fname).skip(1), 
    cycle_length=100).batch(BATCH_SIZE)

fonts_fast = fonts_lines.map(lambda x: tf.io.decode_csv(x, record_defaults=font_column_types))
%%time
for i,batch in enumerate(fonts_fast.take(20)):
  print('.',end='')

print()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11694343965800723, 'fraction_numerical': 0.013975665899375206, 'mean_word_length': 5.897930252339099, 'pattern_counts': {'":': 0, '<': 192, '<?xml version=': 0, '>': 192, 'https://': 13, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '48', 'dir': 'jupyter-structured-clean-dedup', 'id': '307506', 'license': 'MIT', 'n_tokens_mistral': 8083, 'n_tokens_neox': 7706, 'n_words': 2260, 'path': '1. Introduction to TensorFlow/AZ/ANN/Regression/08_titanic_03.ipynb', 'repo_name': 'AmirRazaMBA/TensorFlow-Certification', 'size': '24338'}