How can Tensorflow be used with Estimators to train the model for titanic dataset?


The titanic dataset can be trained by creating a model using the ‘LinearClassifier’, and training it using the ‘train’ method. The train method is present in the ‘estimator’ class of tensorflow library.

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

print("The base features are added")
print("The model is trained")
linear_est = tf.estimator.LinearClassifier(feature_columns=feature_columns)
linear_est.train(train_input_fn)
result = linear_est.evaluate(eval_input_fn)
clear_output()
print(result)

Code credit −https://www.tensorflow.org/tutorials/estimator/linear

Output

The base features are added
The model is trained
{'accuracy': 0.7613636, 'accuracy_baseline': 0.625, 'auc': 0.809244, 'auc_precision_recall': 0.75609726, 'average_loss': 0.5452906, 'label/mean': 0.375, 'loss': 0.5347039, 'precision': 0.75, 'prediction/mean': 0.27201703, 'recall': 0.54545456, 'global_step': 200}

Explanation

  • Once the base features are all added to the model, the mode is trained.
  • This is done using a single command with the help of tf.estimator API.

Updated on: 25-Feb-2021

87 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements