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


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.

Example

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 −https://www.tensorflow.org/tutorials/estimator/boosted_trees

Output

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

Explanation

  • 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.

Updated on: 25-Feb-2021

98 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements