How can Tensorflow be used with Estimators to define a function that shuffles data?

A function that shuffles data can be defined, with the help of estimators. A dictionary is created that stores the data. This is done using the ‘from_tensor_slices’ method.

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.

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.

Selecting and using the right set of feature columns is essential to learning an effective model. A feature column can be one of the raw inputs in the original features dict, or a new column created using transformations that are defined on one or multiple base columns.

The linear estimator uses as well as numeric and categorical features. Feature columns work with all the TensorFlow estimators. Their goal is to define the features used for modeling. They also have feature engineering capabilities like one-hot-encoding, normalization, and bucketization.


print("A function is defined to shuffle the data")
def make_input_fn(data_df, label_df, num_epochs=10, shuffle=True, batch_size=32):
def input_function():
ds =, label_df))
if shuffle:
  ds = ds.shuffle(1000)
  ds = ds.batch(batch_size).repeat(num_epochs)
 return ds
return input_function
print("The function is called")
train_input_fn = make_input_fn(dftrain, y_train)
eval_input_fn = make_input_fn(dfeval, y_eval, num_epochs=1, shuffle=False)

Code credit −


A function is defined to shuffle the data
The function is called


  • The input_function tells how data is converted to a

  • It feeds the input pipeline in a streaming method.

  • The takes multiple sources like a dataframe, a csv-formatted file, and so on.

Updated on: 25-Feb-2021


Kickstart Your Career

Get certified by completing the course

Get Started