Tensorflow Text Classification

 In this tutorial, you are going to learn


1. How to import the TensorFlow libraries?

2. How to Import the dataset?

3. How to explore the dataset?

4. How to implement a feed-forward neural network TensorFlow model?

5. How to compile a TensorFlow model?

6. How to train a compiled neural network TensorFlow model?


1. Import the Libraries

The first step is to import all the necessary libraries.
import pathlib
import numpy as np
import pandas as pd
import tensorflow as tf
import keras

2. Import the Dataset

We are importing the Breast cancer dataset. We are getting the data in .data format, so we need to convert the data into a pandas data frame.

path = keras.utils.get_file("breast-cancer-wisconsin.data", "https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.data")

Once we have the Pandas DataFrame, we can use inbuilt methods such as                                              

Dropna( ) : To drop the rows with null values.                        

Drop( ) : To drop a specific column in a DataFrame.

column_names = ['id','Clump_Thickness','Cell_Size','Cell_Shape','Marginal_Adhesion',

dataset = pd.read_csv(path, names=column_names,
                      na_values = "?", comment='\t',
                      sep=",", skipinitialspace=True)


3. Explore the Dataset


We can see that we have a total of 10 columns in our dataset. In which 9 columns are of integer type and 1 column is of float type.

The Class column is the value we are going to predict here.

dataset['Class']=dataset['Class'].apply(lambda x: 0 if x==2 else 1)

Class column contains value of [2,4]. 

2 : Cancer is benign.                                                                                                                             

4 : Cancer is malignant.

We need to change the values from [2,4] to [0,1].

y is our target column and X is our training set.

4. Model Implementation


Once we have our data processed to be feed into the model and we implement the model. We are building a feed-forward neural network in which layers will learn as we train the model.

The first layer has 50 hidden units and “relu” activation function. The parameter “input_shape” specifies the number of columns in the training dataset.

      tf.keras.layers.Dense(50, activation="relu", input_shape=[len(X.keys())]),
      tf.keras.layers.Dense(2, activation="softmax")

4. Model Compilation


For compiling a model in TensorFlow we need three parameters.

optimizer” : To update the model while training.

loss” : To tell the accuracy of the model while it is being trained.

metrics” : To monitor the steps while the model is being trained.


4. Model Training


We will use model.fit( ) method to train the model. The model learns to predict the target value based on the training data.




1. info( ) : To explore the dataset.

2.  tf.keras.Sequential( ) : To implement a neural network model.

3. compile( ) : To compile a model.

4. fit( ) : To train a model.

 You can find the Github link here.