# How can Tensorflow be used with Estimators to create feature columns and input functions?

TensorflowServer Side ProgrammingProgramming

Tensorflow can be used with Estimators to create feature columns and input functions by using one-hot encoding method. The ‘feature_column.indicator_column’ is used to return the output of this one-hot encoding technique.

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.

We will train a logistic regression model using the tf.estimator API. The model is used as a baseline for other algorithms. We use the titanic dataset with the goal of predicting passenger survival, given characteristics such as gender, age, class, etc.

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.

## Example

CATEGORICAL_COLUMNS = ['sex', 'n_siblings_spouses', 'parch', 'class', 'deck', 'embark_town', 'alone']
NUMERIC_COLUMNS = ['age', 'fare']
print("The method to use one hot encoding technique")
def one_hot_cat_column(feature_name, vocab):
return tf.feature_column.indicator_column(
tf.feature_column.categorical_column_with_vocabulary_list(feature_name, vocab))
feature_columns = []
print("One hot encode categorical features")
for feature_name in CATEGORICAL_COLUMNS:
vocabulary = dftrain[feature_name].unique()
feature_columns.append(one_hot_cat_column(feature_name, vocabulary))
for feature_name in NUMERIC_COLUMNS:
feature_columns.append(tf.feature_column.numeric_column(feature_name, dtype=tf.float32))

## Output

The method to use one hot encoding technique
One hot encode categorical features

## Explanation

• The fields in the CATEGORICAL_COLUMNS are transformed from categorical columns to one-hot-encoded columns.

• Before this, it is first converted into an indicator column.

• The categorical features are obtained.

Published on 12-Feb-2021 17:12:11