keras

Train a Simple Deep Learning Network using Keras and Iris DataSet

Keras Deep learning framwork is  very popular among researchers and developers which is built at top of Tensorflow. Furthermore, Keras is very handy and easy to use, creating layers and connections is matter of few lines of code.  In this post, I am going to explain, how you can train a simple Deep learning network using Keras for Iris dataset.

In Irish dataset there are four input features sepal length, sepal width, petal length and petal width and one output feature having three classes Iris Setosa, Iris Versicolour and Iris Virginica. The dataset has 150 instances.

Since classes are string form they need to be converted and to do that I have used scikit-learn’s One-hot encoder class. Thereafter, a deep learning model is created, in which has four inputs and the first layer has 512 units having relu activation function. The second and the last layer is output layer having softmax activation function. Loss function is categorical cross-entropy and I have used Stochastic Gradient Decent (SGD) as optimizer.

Finally, model has been trained using fit() function.

from keras import models
from keras import layers
import pandas as pd
from sklearn.model_selection import train_test_split
from numpy import array
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import OneHotEncoder

ds="/content/drive/MyDrive/datasets/iris.csv"
dataset = pd.read_csv(ds)
X=dataset.iloc[: , 0:4]
y=dataset.iloc[ :,-1]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.05)
y_train = array(y_train)
y_test=array(y_test)
label_encoder = LabelEncoder()
y_train = label_encoder.fit_transform(y_train)
y_test = label_encoder.fit_transform(y_test)
onehot_encoder = OneHotEncoder(sparse=False)
y_train = y_train.reshape(len(y_train), 1)
y_train = onehot_encoder.fit_transform(y_train)
onehot_encoder = OneHotEncoder(sparse=False)
y_test = y_test.reshape(len(y_test), 1)
y_test = onehot_encoder.fit_transform(y_test)

network=models.Sequential()
network.add(layers.Dense(512,activation='relu',input_shape=(4,)))
network.add(layers.Dense(3,activation='softmax'))
network.summary()
network.compile(loss='categorical_crossentropy',optimizer='SGD',metrics=['acc'])
history=network.fit(X_train,y_train,epochs=15,batch_size=5)

See on Google Colab

Leave a Comment

Your email address will not be published. Required fields are marked *

©Postnetwork-All rights reserved.