Tensorflow 2 for Deep Learning — Logistic Regression ( Softmax )

Ashwin Prasad
Analytics Vidhya
Published in
3 min readDec 22, 2020

Note : The Program files for tensorflow 2 can be found on - https://github.com/ashwinhprasad/Tensorflow-2.0

Logistic Regression is used for Classification tasks and This Blog will take you through the implementation of logistic regression using Tensorflow 2. This blog post won’t be covering about the theories regarding logistic regression and theory is a pre-requisite.

Let’s jump to the code part :

1. Importing The Dataset

The Dataset that is used in this example is iris dataset from the sklearn library.
we are importing the dataset and storing it in the form of a pandas dataframe

#importing the libraries
import numpy as np
import tensorflow as tf
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
#importing the dataset
from sklearn.datasets import load_iris
data = load_iris()
x = data['data']
y = data['target']
dataset = pd.DataFrame(data=np.concatenate((x,y.reshape(-1,1)),axis=1),columns=['sepal length','sepal width','petal length','petal width','target'])

2. Train Test Split

Splitting the dataset into training and testing set for future analysis of the model

#train, test split
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.1,shuffle=True)

3. Model Architecture and training using keras functional API

Keras Functional API is a better alternative to Sequential API. It can be used to create models with multiple inputs and multiple Inputs and complex operations.
For Logistic Regression, we can use either softmax or sigmoid activation function as the final layer. I have used softmax as the output can only be either one of the 3 types of plants.

Note : This is Not purely Logistic regression as we are using softmax activation in the final layer. Logistic Regression can be implemented by replacing softmax with sigmoid in the final layer. There won’t be much difference in the result

#model (Keras - Fuctional API)
i = tf.keras.layers.Input(shape=(4))
X = tf.keras.layers.Dense(3,activation=tf.keras.activations.softmax)(i)
model = tf.keras.models.Model(i,X)
#compile and fit the model
model.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=0.01),loss=tf.keras.losses.sparse_categorical_crossentropy,metrics=['accuracy'])
train = model.fit(x_train,y_train.reshape(-1),validation_data=(x_test,y_test.reshape(-1)),epochs=200)

4. Model Performance

The models performance is analyzed using plots. The accuracy of the Model is very high

#plotting loss over epochs
plt.figure(figsize=(10,8))
plt.plot(train.history['loss'],label='Training loss')
plt.plot(train.history['val_loss'],label='Validation loss')
plt.xlabel('epochs')
plt.ylabel('loss')
plt.legend()
#plotting accuracy over epochs
plt.figure(figsize=(10,8))
plt.plot(train.history['accuracy'],label='Training accuracy')
plt.plot(train.history['val_accuracy'],label='Validation accuracy')
plt.xlabel('Accuracy')
plt.ylabel('loss')
plt.legend()

Conclusion

From the above visualization. we can conclude that our model is efficient.
Logistic Regression is a powerful tool for we could use for classification and This is how Logistic Regression can be implemented with tensorflow 2.

--

--

Ashwin Prasad
Analytics Vidhya

I write about things that intrigue me on any field of Computer Science, with more weightage to Machine Learning and Systems Programming