How can Tensorflow be used with Estimator to transform the feature column?

TensorflowServer Side ProgrammingProgramming

Tensorflow can be used with Estimator to transform the feature column by first converting the first row of the dataset into a dictionary, and then uses one-hot encoding to transform this feature column, i.e. the ‘gender’ column.

Read More: What is TensorFlow and how Keras work with TensorFlow to create Neural Networks?

We will use the Keras Sequential API, which is helpful in building a sequential model that is used to work with a plain stack of layers, where every layer has exactly one input tensor and one output tensor.

A neural network that contains at least one layer is known as a convolutional layer. We can use the Convolutional Neural Network to build learning model. 

We are using the Google Colaboratory to run the below code. Google Colab or Colaboratory helps run Python code over the browser and requires zero configuration and free access to GPUs (Graphical Processing Units). Colaboratory has been built on top of Jupyter Notebook.

An Estimator is TensorFlow's high-level representation of a complete model. It is designed for easy scaling and asynchronous training. Estimators use feature columns to describe how the model would interpret the raw input features. An Estimator expects a vector of numeric inputs, and feature columns will help describe how the model should convert every feature in the dataset.

Selecting and using the right set of feature columns is essential to learning an effective model.


print("An example transformation produced by a feature column")
example = dict(dftrain.head(1))
class_fc = tf.feature_column.indicator_column(tf.feature_column.categorical_column_with_vocabulary_list('class', ('First', 'Second', 'Third')))
print('Feature value is: "{}"'.format(example['class'].iloc[0]))
print('One-hot encoded is: ', tf.keras.layers.DenseFeatures([class_fc])(example).numpy())

Code credit −


An example transformation produced by a feature column
Feature value is: "Third"
One-hot encoded is: [[0. 0. 1.]]


  • The transformation produced by a feature column can be seen.

  • The output of indicator_column on a single example can be viewed on the console.

Published on 12-Feb-2021 17:13:59