Getting Your Hands Dirty With TensorFlow 2.0 and Keras API

Man with dirt on his hands

Getting Your Hands Dirty With TensorFlow 2.0 and Keras API

TensorFlow 2.0 comes with Keras packaged inside, so there is no need to import Keras as a separate module (although you can do this if you need). The TensorFlow 2.0 API is simplified and improved. This is good news for us machine learning developers.

This is how you import Keras from TensorFlow:

from tensorflow import feature_column
from tensorflow import keras
from tensorflow.keras import layers

I’m using the input pipeline to encode categorical columns. The Keras API works well with One of the main advantages of is the fact that it acts as a bridge between the data and model. There is no need to transform data by yourself — transformed data will automatically be applied during training.

Data is fetched from CSV file into Pandas dataframe:

column_names = ['report_id','report_params','day_part','exec_time']
raw_dataframe = pd.read_csv('report_exec_times.csv')
dataframe = raw_dataframe.copy()

Column values for report_params vary, and we need to normalize this column (make values to be on a similar scale):

eps=0.001 # 0 => 0.1¢
dataframe['report_params'] = np.log(dataframe.pop('report_params')+eps)

I’m using a utility method (this method is taken from TensorFlow tutorial) to create dataset from Pandas dataframe:

def df_to_dataset(dataframe, shuffle=True, batch_size=32): dataframe = dataframe.copy() labels = dataframe.pop('exec_time') ds =, labels)) if shuffle: ds = ds.shuffle(buffer_size=len(dataframe)) ds = ds.batch(batch_size) return ds

Next, we need to define data mapping for categorical columns encoding. I’m using the TensorFlow vocabulary list function, including mapping for all unique values (if there are many values, it would be better to use embedding API). Two columns are encoded: report_id and day_part:

feature_columns = []
report_id = feature_column.categorical_column_with_vocabulary_list('report_id', ['1', '2', '3', '4', '5'])
report_id_one_hot = feature_column.indicator_column(report_id)
day_part = feature_column.categorical_column_with_vocabulary_list('day_part', ['1', '2', '3'])
day_part_one_hot = feature_column.indicator_column(day_part)

Create Keras dense features layer out of array with TensorFlow encodings. We will use this layer during Keras model construction to define model training features:

feature_layer = tf.keras.layers.DenseFeatures(feature_columns)

We are done with features. Next, convert Pandas dataframe to with the help of the utility function:

batch_size = 32
train_ds = df_to_dataset(train, batch_size=batch_size)
val_ds = df_to_dataset(val, shuffle=False, batch_size=batch_size)
test_ds = df_to_dataset(test, shuffle=False, batch_size=batch_size)

Dense features layer is used when the Keras sequential model is defined (there is no need to pass an array of features later into a fit function):

def build_model(feature_layer): model = keras.Sequential([ feature_layer, layers.Dense(16, activation='relu'), layers.Dense(16, activation='relu'), layers.Dense(1) ]) optimizer = keras.optimizers.RMSprop(0.001) model.compile(loss='mse', optimizer=optimizer, metrics=['mae', 'mse']) return model

Training is executed through the function. We are using input pipeline to pass training and validation sets:

history =, validation_data=val_ds, epochs=EPOCHS, callbacks=[early_stop])

The awesome thing about it is that data encoding happens behind the scenes based on the rules defined for the feature layer.

How do we run the model.predict function with and feature layer? It’s simple.

Construct Pandas dataframe with input data:

headers = ['report_id', 'report_params', 'day_part']
dataframe_input = pd.DataFrame([[1, 15, 3]], columns=headers, dtype=float, index=['input'])

Convert report_params value to be on the same scale as it was for the training:

eps=0.001 # 0 => 0.1¢
dataframe_input['report_params'] = np.log(dataframe_input.pop('report_params')+eps)

Create input pipeline out of Pandas dataframe:

input_ds =
input_ds = input_ds.batch(1)

Run model.predict function:

res = model.predict(input_ds)


  • Source code with sample data is available on my GitHub repo


Further Reading

This UrIoTNews article is syndicated fromDzone