AI ML - LAB - Manual - 2020 - 211213 - 100634
AI ML - LAB - Manual - 2020 - 211213 - 100634
AI ML - LAB - Manual - 2020 - 211213 - 100634
2020-21
FIFTH SEMESTER
ARTIFICIAL INTELLIGENCE
& MACHINE LEARNING LABORATORY WITH
APPLICATIONS MANUAL
Vision
focus on industry needs and research, for the students to be successful global
Mission
Collaborative learning.
* To collaborate with industry partners and professional societies and make the
activities.
* To inculcate human values and ethics to groom the students to be responsible citizens.
DAYANANDA SAGAR COLLEGE OF ENGINEERING
Do’s
Students shall
Come prepared for the program to be developed in the laboratory.
Report any broken plugs or exposed electrical wires to your faculty/laboratory technician
immediately.
Turn off the machine once you have finished using it.
Maintain silence while working in the lab.
Keep the Computer lab premises clean and tidy.
Place backpacks under the table or computer counters.
Treat fellow users of the laboratory, and all equipment within the laboratory, with the appropriate
level of care and respect.
Don’ts
Analyze and make use of logic to infer unknown facts using Pyke Logic Programming
CO1
in Python.
Analyze and Apply Simple Linear Regression and Multiple Linear Regression using
CO2
Python.
Analyze and Apply/Implement Classification/Supervised Learning Algorithms on
CO3
Different Datasets.
Analyze and Apply Clustering Algorithms on Different Datasets.
CO4
CO5 Analyze and Apply Neural Networks to Real Life Problems.
Analyze and Apply Neural Networks to Real Life Problems.
CO6
Apply:
2. a) Simple linear regression model for headBrain dataset and
predict brain weight based on head size using the least square
method.
Findout
(i) R^2 score for the predictedmodel
(ii) Display the all the data points along with the fitmodel
02 CO2
b) Simple linear regression model for housing_prices_SLR
dataset and predict house price based on the area of thehouse
using the libraryscikit_learn.
Find out
(i) AnalyzetheR^2scoreofpredictedtrainingandtestmodels
score.
(ii) Display the all the data points along with fitmodel
Apply:
a) Multiple linear regression model for student dataset and
predict writing skill of student based on the math skill and
3.
reading skill of the student using the Gradient descent
method.
Find out R^2 score for the predicted model 02 CO2
Apply:
SVM classifier on:
5. i) Iris Dataset, Draw Linearly separable decision boundary for
the generated dataset. 02 CO3
ii)Randomly generated dataset using package
library[MAKEMOON],Draw Non-linearly separable decision
boundary for the generated dataset.
a) Apply
Partitioning k-means clustering technique on ch1ex1 dataset
with different K (number of clusters) as input and record the
6.
output
b) Apply 02 CO4
Hierarchical Clustering Algorithm on seeds_less_rows
dataset for extracting cluster labels of different varieties of
seeds
7. Demonstrate
a) Usage of Sigmoid activation function in artificial neural 02 CO5
network
b) Identification of face using opencv library.
Text Books:
1. Stuart Russel, Peter Norvig: Artificial Intelligence A Modern Approach, 3rd Edition,
Pearson Education,2003.
2. “Data Mining Concepts and Techniques”, Jiawei Han, Micheline Kamber, Jian Pei,
Elsevier (MK) 3rd Edition,2012.
3. Deep Learning with Python: A Hands-on Introduction Nikhil Ketkar
4. https://towardsdatascience.com/notes-on-artificial-intelligence-ai-machine-learning-
ml-and-deep-learning-dl-for-56e51a2071c2.
Reference Books:
1. TomM.Mitchell,“MachineLearning”,McGraw-HillEducation(INDIANEDITION),
2013. (1.1,1.2,1.3,4.2,4.4,4.5,4.6,4.7).
2. An Introduction to Statistical Learning, with Applications in R (2013), by G.James,D.
Witten, T. Hastie, and R.Tibshirani.
3. Nils J. Nilsson: Principles of Artificial Intelligence, Elsevier, 1980.
Program 1:
For a given Standard Wumpus world scenario, encode the facts and rules and run queries to infer
about its neighboring locations are free of danger using the Pyke logic programming in python.
Program 2a:
Apply:
Simple linear regression model for head Brain dataset and predict brain weight based
on head size using the least square method.
Find out
i. R2 score for the predicted model.
ii. Display all the data points along with the fitting the data points to the model.
#importing libraries
import numpy as np
import pandas as pd
# Reading Data
data = pd.read_csv('headbrain.csv')
print(data.shape)
data.head()
(237, 4)
# Collecting X and Y
X = data['Head Size(cm^3)'].values
Y = data['Brain Weight(grams)'].values
# Calculating coefficient
# Mean X and Y
mean_x = np.mean(X)
mean_y = np.mean(Y)
print(mean_x)
print(mean_y)
n = len(X)
print(n)
3633.9915611814345
1282.873417721519
237
# Using the formula to calculate b1 and b0
numer = 0
denom = 0
for i in range(n):
b1 = numer / denom
# Printing coefficients
print("Coefficients")
print(b1, b0)
Coefficients
b1:0.26342933948939945 b0:325.57342104944223
y = b0 + b1 * x
# Ploting Line
plt.legend()
plt.show()
# Calculating R2 Score
ss_tot = 0
ss_res = 0
for i in range(n):
y_pred = b0 + b1 * X[i]
r2 = 1 - (ss_res/ss_tot)
print("R2 Score")
print(r2)
R2 Score
0.6393117199570003
Conclusion: The simple linear regression model gives average accuracy depending on
the R2 score value.
2b. Simple linear regression model for housing_ prices_ SLR dataset and predict
house price based on the area of the house using the library scikit_ learn.
Find out
i. Analyze the R2score of predicted training and test models score.
ii. Display all the data points along with the fitting the data points to the model.
import pandas as pd
importmatplotlib.pyplot as plt
%matplotlib inline
# Step2:load dataset
df=pd.read_csv("housing_prices_SLR.csv",delimiter=',')
df.head()
x=df[['AREA']].values#feature Matrix
y=df.PRICE.values#Target Matrix
x[:5] #slicing
y[:5]
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=100)
print(x_train.shape)
print(x_test.shape)
print(x_train.shape)
print(x_test.shape)
(40, 1)
(10, 1)
(40, 1)
(10, 1)
lr_model.fit(x_train,y_train)
print(lr_model.intercept_) # (PRICE=(-4481.80028058845)+8.65903854)*AREA
print(lr_model.coef_)#y=c+mx
b0:-3103.34066448488
b1:[7.75979089]
lr_model=Linear Regression(fit_intercept= False)
lr_model.fit(x_train,y_train)
print(lr_model.intercept_) # (PRICE=(-4481.80028058845)+8.65903854)*AREA
print(lr_model.coef_)#y=c+mx
b0:0.0
b1:6.03609138
y_train
lr_model.predict(x_train)
r2_score(y_train,lr_model.predict(x_train))
R^2_Train_Score:0.820250203127675
r2_score(y_test,lr_model.predict(x_test))
R^2_Test_Score:0.5059420550739799
R^2_Test_Score:0.5059420550739799
plt.scatter(x_train[:,0],y_train,c='red')
plt.scatter(x_test[:,0],y_test,c='blue')
plt.plot(x_train[:,0],lr_model.predict(x_train),c='y')
Conclusion: Comparing the training and testing R^2 score values, the accuracy of the
simple linear regression model with respect to this dataset is average.
Program 3
Apply:
a)Multiple linear regression model for student dataset and predict writing skill of
student based on the math skill and reading skill of the student using the Gradient
descent method. Find out R2 score for the predicted model.
#importing Libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
data = pd.read_csv('student.csv')
print(data.shape)
data.head()
(1000, 3)
math = data['Math'].values
read = data['Reading'].values
write = data['Writing'].values
# Initial Coefficients
B = np.array([0, 0, 0])
Y = np.array(write)
alpha = 0.0001
defcost_function(X, Y, B):
m = len(Y)
J = np.sum((X.dot(B) - Y) ** 2)/(2 * m)
return J
inital_cost = cost_function(X, Y, B)
print("Initial Cost")
print(inital_cost)
return B, cost_history
# 100000 Iterations
newB, cost_history = gradient_descent(X, Y, B, alpha, 100000)
# New Values of B
print("New Coefficients")
print(newB)
Initial Cost
2470.11
New Coefficients
[bo, b1,b2]:[-0.47889172 0.09137252 0.90144884]
Final Cost
10.475123473539167
Y_pred = X.dot(newB)
print("R2 Score")
print(r2_score(Y, Y_pred))
R2 Score
0.9097223273061553
Conclusion:
The accuracy of the multiple linear regression model is good depending on the R2score
value.
b.) Multiple linear regression model for housing_prices dataset and predict house price
based on the area, floor and room size of the house using the library scikit learn. Find
out the accuracy of the model using R2score statistics for the predicted model.
#importing libraries
import numpy as np
import pandas as pd
importmatplotlib.pyplot as plt
%matplotlib inline
#Loading dataset
df=pd.read_csv("housing_prices.csv")
df.head()
# Finding R2 score
print(mlr_model.score(x_train,y_train))
print(mlr_model.score(x_test,y_test))
R2_Train_Score:0.9220702400776505
R2_Test_Score:0.8090037959414931
Conclusion:The multiple linear regression model accuracy is good with respect to this
dataset by comparing R2 training and testing score values.
Program 4
Apply:
a) Decision tree on breast cancer dataset.
Find out
i) No of benign and malignant cases in the testing phase.
ii) Predict the accuracy of the both classifier.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# ### Step 2 : Load the csv/excel file into pandas dataframe and clean the data
df = pd.read_csv("../data/breast_cancer.csv")
df = df.iloc[:, :-1]
df.head()
# ### Step 3 : Create the Feature Matrix and Target Vector and check the first 5 rows
x = df.iloc[:, 2:].values
y = df.diagnosis.values
print(x[:2])
print(y[:5])
# ### Step 4 : Split the data into training set and test set
# ### Step 5 : Instantiate a decision tree model and train the model
dt_classifier = DecisionTreeClassifier()
dt_classifier.fit(x_train, y_train)
# ### Step 6 : Use the model to predict the class labels for new data
predictions = dt_classifier.predict(x_test)
prob_predictions = dt_classifier.predict_proba(x_test)
print(predictions)
print(prob_predictions)
# ### Step 7 : Calculate Accuracy score and confusion matrix for train and test data
Output:
Conclusion:
Comparing Training and testing accuracy scores the accuracy of Decision Tree model is
good. The Correctly classified tuples for training set is (286+169) and the misclassified
tuples are zero.The correctly classified for training set is (71+36)and misclassified tuples
are(7+0).
4b. Apply Naïve Bayesian classifier on breast cancer dataset.
Find out
i) No of benign and malignant cases in the testing phase.
ii) Predict the accuracy of the classifier
# coding: utf-8
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import sklearn as sk
# ### Step 2 : Load the csv/excel file into pandas dataframeand clean the data
df = pd.read_csv("breast_cancer.csv")
df = df.iloc[:, :-1]
df.shape()
df.head()
# ### Step 3 : Create the Feature Matrix and Target Vector and check the first 5 rows
x = df.iloc[:, 2:].values
y = df.diagnosis.values
print(x[:2])
print(y[:5])
# ### Step 4 : Split the data into training set and test set
x_train.shape #(455,30)
x_test.shape#(114, 30)
y_train.shape
y_test.shape
(y_train == 'M').sum()
(y_train=='B').sum()
# Baseline model, accuracy, confusion_matrix, classification_report
# ### Step 5 : Instantiate a Guassian Naive Bayes model and train the model
confusion_matrix(y_train,nb_model.predict(x_train))
print(classification_report(y_train,nb_model.predict(x_train)))
Conclusion: The naïve bayes model is good with respect to breast cancer dataset by
comparing the precision recall and F1 score values of training and testing dataset
(classification report)
Program 5:
Apply:
SVM classifier on:
a) Iris Dataset, Draw Linearly separable decision boundary for the generated dataset.
#Example of a Linear SVM Classifier (SVC) with hard margin decision boundaries
%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
margin = 1/w[1]
gutter_up = decision_boundary + margin
gutter_down = decision_boundary - margin
svs = svm_clf.support_vectors_
plt.scatter(svs[:, 0], svs[:, 1], s=180, facecolors='#FFAAAA')
plt.plot(x0, decision_boundary, "k-", linewidth=2)
plt.plot(x0, gutter_up, "k--", linewidth=2)
plt.plot(x0, gutter_down, "k--", linewidth=2)
#In [3]:
from sklearn.svm import SVC
from sklearn import datasets
iris = datasets.load_iris()
#print(iris)
X = iris["data"][:, (2, 3)] # petal length, petal width
#print(X)
y = iris["target"]
setosa_or_versicolor = (y == 0) | (y == 1)
X = X[setosa_or_versicolor]
y = y[setosa_or_versicolor]
plt.figure(figsize=(12,3.2))
Output:
Conclusion: For Iris dataset SVM model is applied to linearly separate petal length and
petal width with 2 support vectors.
b)Randomly generated dataset using package library[MAKEMOON],Draw Non-
linearly separable decision boundary for the generated dataset.
## Example of a Linear SVM Classifier (SVC) with hard margin decision boundaries
import numpy as np
%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
#create a pipeline to create features, scale data and fit the model
polynomial_svm_clf = Pipeline((
("poly_features", PolynomialFeatures(degree=3)),
("scalar", StandardScaler()),
("svm_clf", SVC(kernel="poly", degree=10, coef0=1, C=5)) ))
Apply:
import pandas as pd
df = pd.read_csv('ch1ex1.csv')
points = df.values
from sklearn.cluster import KMeans
model = KMeans(n_clusters=3)
model.fit(points)
labels = model.predict(points)
importmatplotlib.pyplot as plt
#This is great, but let's go one step further, and add the cluster centres (the
"centroids") to the scatter plot.
Step 3: Obtain the coordinates of the centroids using the .cluster_centers_ attribute of model.
Assign them to centroids.
centroids = model.cluster_centers_
Step 4: Assign column 0 of centroids to centroids_x, and column 1 of centroids to
centroids_y.
centroids_x = centroids[:,0]
centroids_y = centroids[:,1]
Step 5: In a single cell, create two scatter plots (this will show the two on top of one
another). Call `plt.show()` just once, at the end.
Firstly, the make the scatter plot you made above. Secondly, make a scatter plot of
`centroids_x` and `centroids_y`, using `'X'` (a cross) as a marker by specifying the
`marker` parameter. Set the size of the markers to be `200` using `s=200`.
Output:
The centroids are important because they are what enables KMeans to assign new,
previously unseen points to the existing clusters.
#we use the fcluster() function to extract the cluster labels for intermediate clustering, and
#compare the labels with the grain varieties using a cross-tabulation.
samples = seeds_df.values
Step 3: Run the hierarchical clustering of the grain samples
fromscipy.cluster.hierarchy import linkage, dendrogram
importmatplotlib.pyplot as plt
mergings = linkage(samples, method='complete')
dendrogram(mergings,labels=varieties,leaf_rotation=90,leaf_font_size=6)
plt.show()
In[15]: ct
Output:-
Demonstrate:
import numpy as np
from functools import reduce
def perceptron(weight, bias, x):
model = np.add(np.dot(x, weight), bias)
print('model: {}'.format(model))
logit = 1/(1+np.exp(-model))
print('Type: {}'.format(logit))
returnnp.round(logit)
def compute(logictype, weightdict, dataset):
weights = np.array([ weightdict[logictype][w] for w in weightdict[logictype].keys()])
output = np.array([ perceptron(weights, weightdict['bias'][logictype], val) for val in dataset])
print(logictype)
return logictype, output
def main():
logic = {
'logic_and' : {
'w0': -0.1,
'w1': 0.2,
'w2': 0.2
},
'logic_nand': {
'w0': 0.6,
'w1': -0.8,
'w2': -0.8
},
'bias': {
'logic_and': -0.2,
'logic_nand': 0.3,
}
}
dataset = np.array([
[1,0,0],
[1,0,1],
[1,1,0],
[1,1,1] ])
if __name__ == '__main__':
main()
output:
model: -0.30000000000000004
Type: 0.425557483188341
model: -0.1
Type: 0.47502081252106
model: -0.1
Type: 0.47502081252106
model: 0.10000000000000003
Type: 0.52497918747894
logic_and
model: 0.8999999999999999
Type: 0.7109495026250039
model: 0.09999999999999992
Type: 0.5249791874789399
model: 0.09999999999999992
Type: 0.5249791874789399
model: -0.7
Type: 0.3318122278318339
logic_nand
Logic Function: AND
X0 X1 X2 Y
1 0 0 0.0
1 0 1 0.0
1 1 0 0.0
1 1 1 1.0
Logic Function: NAND
X0 X1 X2 Y
1 0 0 1.0
1 0 1 1.0
1 1 0 1.0
1 1 1 0.0
Conclusion: Sigmoid or logistic function used to display the working of AND and NAND
logic functions.
7b)Identification of face using opencv library
#using opencv
#install -c menpoopencv
import numpy as np
import cv2
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
img = cv2.imread('people.jpg')
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Seven key steps in using Keras to create a neural network or deep learning model, step-by-
step including:
1) Importing necessary Libraries 2) How to load data. 3) How to define a neural network
in Keras. 4) How to compile a Keras model using the efficient numerical backend. 5)
How to train a model on data. 6) How to evaluate a model on data. 7) How to make
predictions with the model.
dataframe.shape
(767, 9)
features_train,features_test,target_train,target_test=train_test_split(X,y,
test_size=0.33,random_state=0)
Conclusion :Using Keras and Tensor flow framework loaded the Pima_indians_diabetes
dataset and designed a two-layer neural network with one hidden layer and one output
layer and generated predictions for 10 samples.
Program 9:
import numpy as np
from keras.datasets import mnist
from keras.utils import to_categorical
import matplotlib.pyplot as plt
%matplotlib inline
Using TensorFlow backend.
import keras
from keras.models import Sequential,Input,Model
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.layers.normalization import BatchNormalization
from keras.layers.advanced_activationsimport LeakyReLU
#from keras.datasets import mnist
(train_X,train_Y), (test_X,test_Y) = mnist.load_data()
print('Training data shape : ', train_X.shape, train_Y.shape)
train_X = train_X.astype('float32')
test_X = test_X.astype('float32')
train_X = train_X /255
test_X = test_X /255
((48000, 28, 28, 1), (12000, 28, 28, 1), (48000, 10), (12000, 10))
batch_size =64
epochs =3
num_classes =10
m_model = Sequential()
m_model.add(Conv2D(32, kernel_size=(3, 3),activation='linear',input_shape=(
28,28,1),padding='same'))
m_model.add(LeakyReLU(alpha=0.1))
m_model.add(MaxPooling2D((2, 2),padding='same'))
#fashion_model.add(Conv2D(64, (3, 3), activation='linear',padding='same'))
#fashion_model.add(LeakyReLU(alpha=0.1))
#fashion_model.add(MaxPooling2D(pool_size=(2, 2),padding='same'))
#fashion_model.add(Conv2D(128, (3, 3), activation='linear',padding='same'))
#fashion_model.add(LeakyReLU(alpha=0.1))
#fashion_model.add(MaxPooling2D(pool_size=(2, 2),padding='same'))
m_model.add(Flatten())
m_model.add(Dense(128, activation='linear'))
m_model.add(LeakyReLU(alpha=0.1))
m_model.add(Dense(num_classes, activation='softmax'))
m_model.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras
.optimizers.Adam(),metrics=['accuracy'])
m_model.summary()
Model: "sequential_3"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_3 (Conv2D) (None, 28, 28, 32) 320
_________________________________________________________________
leaky_re_lu_5 (LeakyReLU) (None, 28, 28, 32) 0
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 14, 14, 32) 0
_________________________________________________________________
flatten_3 (Flatten) (None, 6272) 0
_________________________________________________________________
dense_5 (Dense) (None, 128) 802944
_________________________________________________________________
leaky_re_lu_6 (LeakyReLU) (None, 128) 0
_________________________________________________________________
dense_6 (Dense) (None, 10) 1290
=================================================================
Total params: 804,554
Trainable params: 804,554
Non-trainable params: 0
_________________________________________________________________
m_train = m_model.fit(train_X, train_label, batch_size=batch_size,epochs=ep
ochs,verbose=1,validation_data=(valid_X, valid_label))
Train on 48000 samples, validate on 12000 samples
Epoch 1/3
48000/48000 [==============================] - 45s 928us/step - loss: 0.194
6 - accuracy: 0.9427 - val_loss: 0.0938 - val_accuracy: 0.9713
Epoch 2/3
48000/48000 [==============================] - 46s 948us/step - loss: 0.063
0 - accuracy: 0.9811 - val_loss: 0.0733 - val_accuracy: 0.9762
Epoch 3/3
48000/48000 [==============================] - 43s 897us/step - loss: 0.043
3 - accuracy: 0.9871 - val_loss: 0.0570 - val_accuracy: 0.9819
accuracy = m_train.history['accuracy']
val_accuracy = m_train.history['val_accuracy']
loss = m_train.history['loss']
val_loss = m_train.history['val_loss']
epochs =range(len(accuracy))
plt.plot(epochs, accuracy, '--', label='Training accuracy')
plt.plot(epochs, val_accuracy, 'b', label='Validation accuracy')
plt.title('Training and validation accuracy')
plt.legend()
plt.figure()
plt.plot(epochs, loss, '--', label='Training loss')
plt.plot(epochs, val_loss, 'b', label='Validation loss')
plt.title('Training and validation loss')
plt.legend()
plt.show()
epochs=1
# ADDING DROPOUT
m_model = Sequential()
m_model.add(Conv2D(32, kernel_size=(3, 3),activation='linear',padding='same
',input_shape=(28,28,1)))
m_model.add(LeakyReLU(alpha=0.1))
m_model.add(MaxPooling2D((2, 2),padding='same'))
m_model.add(Dropout(0.25))
#fashion_model.add(Conv2D(64, (3, 3), activation='linear',padding='same'))
#fashion_model.add(LeakyReLU(alpha=0.1))
#fashion_model.add(MaxPooling2D(pool_size=(2, 2),padding='same'))
#fashion_model.add(Dropout(0.25))
#fashion_model.add(Conv2D(128, (3, 3), activation='linear',padding='same'))
#fashion_model.add(LeakyReLU(alpha=0.1))
#fashion_model.add(MaxPooling2D(pool_size=(2, 2),padding='same'))
#fashion_model.add(Dropout(0.4))
m_model.add(Flatten())
m_model.add(Dense(128, activation='linear'))
m_model.add(LeakyReLU(alpha=0.1))
m_model.add(Dropout(0.3))
m_model.add(Dense(num_classes, activation='softmax'))
m_model.summary()
Model: "sequential_2"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_2 (Conv2D) (None, 28, 28, 32) 320
_________________________________________________________________
leaky_re_lu_3 (LeakyReLU) (None, 28, 28, 32) 0
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 14, 14, 32) 0
_________________________________________________________________
dropout_1 (Dropout) (None, 14, 14, 32) 0
_________________________________________________________________
flatten_2 (Flatten) (None, 6272) 0
_________________________________________________________________
dense_3 (Dense) (None, 128) 802944
_________________________________________________________________
leaky_re_lu_4 (LeakyReLU) (None, 128) 0
_________________________________________________________________
dropout_2 (Dropout) (None, 128) 0
_________________________________________________________________
dense_4 (Dense) (None, 10) 1290
=================================================================
Total params: 804,554
Trainable params: 804,554
Non-trainable params: 0
_________________________________________________________________
m_model.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras
.optimizers.Adam(),metrics=['accuracy'])
m_train_dropout = m_model.fit(train_X, train_label, batch_size=batch_size,e
pochs=epochs,verbose=1,validation_data=(valid_X, valid_label))
Train on 48000 samples, validate on 12000 samples
Epoch 1/1
48000/48000 [==============================] - 49s 1ms/step - loss: 0.2479
- accuracy: 0.9265 - val_loss: 0.1026 - val_accuracy: 0.9700
m_model.save("fashion_model_dropout.h5py")
test_eval = m_model.evaluate(test_X, test_Y_one_hot, verbose=1)
accuracy = m_train_dropout.history['accuracy']
val_accuracy = m_train_dropout.history['val_accuracy']
loss = m_train_dropout.history['loss']
val_loss = m_train_dropout.history['val_loss']
epochs =range(len(accuracy))
plt.plot(epochs, accuracy, 'bo', label='Training accuracy')
plt.plot(epochs, val_accuracy, 'b', label='Validation accuracy')
plt.title('Training and validation accuracy')
plt.legend()
plt.figure()
plt.plot(epochs, loss, 'bo', label='Training loss')
plt.plot(epochs, val_loss, 'b', label='Validation loss')
plt.title('Training and validation loss')
plt.legend()
plt.show()
predicted_classes = m_model.predict(test_X)
predicted_classes = np.argmax(np.round(predicted_classes),axis=1)
predicted_classes.shape, test_Y.shape
((10000,), (10000,))
correct = np.where(predicted_classes==test_Y)[0]
print ("Found %d correct labels"%len(correct))
for i, correct inenumerate(correct[:9]):
plt.subplot(3,3,i+1)
plt.imshow(test_X[correct].reshape(28,28), cmap='gray', interpolation='
none')
plt.title("Predicted {}, Class {}".format(predicted_classes[correct],
test_Y[correct]))
plt.tight_layout()
Found 9680 correct labels
incorrect = np.where(predicted_classes!=test_Y)[0]
print ("Found %d incorrect labels"%len(incorrect))
for i, incorrect in enumerate(incorrect[:9]):
plt.subplot(3,3,i+1)
plt.imshow(test_X[incorrect].reshape(28,28), cmap='gray', interpolation
='none')
plt.title("Predicted {}, Class {}".format(predicted_classes[incorrect],
test_Y[incorrect]))
plt.tight_layout()
Found 320 incorrect labels
Conclusion: Using Keras and tensor flow network loaded the mnist image dataset and
designed a two-layer neural network with one hidden layer and one output layer using
CNN with Leaky Relu activation function for the hidden layer.
Program 10:
model = Sequential()
model.add(Embedding(max_features, 32)) #max_feature=10,000 so, 320,000
model.add(SimpleRNN(32)) #(32+32+1)*32=2080
model.add(Dense(1, activation='sigmoid'))#(32+1)*1=33
model.summary()
Model: "sequential_2"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
embedding_2 (Embedding) (None, None, 32) 320000
_________________________________________________________________
simple_rnn_2 (SimpleRNN) (None, 32) 2080
_________________________________________________________________
dense_2 (Dense) (None, 1) 33
=================================================================
Total params: 322,113
Trainable params: 322,113
Non-trainable params: 0
_________________________________________________________________
model.compile(optimizer='rmsprop', loss='binary_crossentropy',metrics=['acc
'])
history = model.fit(input_train, y_train,epochs=10, batch_size=128, validat
ion_split=0.2)
Train on 20000 samples, validate on 5000 samples
Epoch 1/10
20000/20000 [==============================] - 33s 2ms/step - loss: 0.5955
- acc: 0.6679 - val_loss: 0.5106 - val_acc: 0.7566
Epoch 2/10
20000/20000 [==============================] - 36s 2ms/step - loss: 0.3544
- acc: 0.8530 - val_loss: 0.4272 - val_acc: 0.8158
Epoch 3/10
20000/20000 [==============================] - 37s 2ms/step - loss: 0.2823
- acc: 0.8870 - val_loss: 0.3698 - val_acc: 0.8652
Epoch 4/10
20000/20000 [==============================] - 41s 2ms/step - loss: 0.2192
- acc: 0.9174 - val_loss: 0.4816 - val_acc: 0.7870
Epoch 5/10
20000/20000 [==============================] - 36s 2ms/step - loss: 0.1675
- acc: 0.9376 - val_loss: 0.4021 - val_acc: 0.8440
Epoch 6/10
20000/20000 [==============================] - 32s 2ms/step - loss: 0.1261
- acc: 0.9570 - val_loss: 0.4502 - val_acc: 0.8312
Epoch 7/10
20000/20000 [==============================] - 32s 2ms/step - loss: 0.0758
- acc: 0.9740 - val_loss: 0.4815 - val_acc: 0.8328
Epoch 8/10
20000/20000 [==============================] - 35s 2ms/step - loss: 0.0552
- acc: 0.9829 - val_loss: 0.5122 - val_acc: 0.8474
Epoch 9/10
20000/20000 [==============================] - 33s 2ms/step - loss: 0.0313
- acc: 0.9908 - val_loss: 0.5852 - val_acc: 0.8282
Epoch 10/10
20000/20000 [==============================] - 32s 2ms/step - loss: 0.0239
- acc: 0.9933 - val_loss: 0.6137 - val_acc: 0.8376
predicted_classes = model.predict(input_test)
import numpy as np
predicted_classes = np.argmax(np.round(predicted_classes),axis=1)
predicted_classes.shape, y_test.shape
((25000,), (25000,))
correct = np.where(predicted_classes==y_test)[0]
print ("Found %d correct labels"%len(correct))
Found 12500 correct labels
incorrect = np.where(predicted_classes!=y_test)[0]
print ("Found %d incorrect labels"%len(incorrect))
Found 12500 incorrect labels
plt.figure()
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs =range(1, len(acc) +1)
plt.plot(epochs, loss, 'bo', label='Training loss')
plt.plot(epochs, val_loss, 'b', label='Validation loss')
plt.title('Training and validation loss')
plt.legend()
plt.show().
Conclusion: Using Keras and tensor flow network loaded the imdb text dataset and
designed a two-layer neural network with one hidden layer and one output layer using
simple RNN in the hidden layer.