198 righe
No EOL
5,6 KiB
Python
198 righe
No EOL
5,6 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""
|
|
Created on Wed Dec 15 18:26:31 2021
|
|
|
|
@author: chouiya
|
|
"""
|
|
|
|
|
|
#********import***************
|
|
from sklearn.datasets import fetch_openml
|
|
from sklearn import datasets
|
|
import matplotlib.pyplot as plt
|
|
from sklearn.model_selection import train_test_split
|
|
import numpy as np
|
|
from sklearn.neural_network import MLPClassifier
|
|
from sklearn.metrics import precision_score
|
|
from sklearn.metrics import zero_one_loss
|
|
import time
|
|
#*******************MLP pour une seule couche de 50 neurons*****************
|
|
mnist = fetch_openml('mnist_784',as_frame=False)
|
|
|
|
data=mnist.data
|
|
target=mnist.target
|
|
xtrain, xtest, ytrain, ytest = train_test_split(data, target, train_size=0.7)
|
|
|
|
clf = MLPClassifier(hidden_layer_sizes=(50))
|
|
|
|
clf.fit(xtrain, ytrain)
|
|
prediction = clf.predict(xtest)
|
|
score = clf.score(xtest, ytest)
|
|
precision = precision_score(ytest, prediction, average ='micro')
|
|
loss1_0 = zero_one_loss(ytest, prediction)
|
|
|
|
# Print & Test :------
|
|
print("This MLP model, with one layer of 50, has a score of : ", score*100, "%.")
|
|
print(" 4 th image : Prediction ",prediction[3], "Vs Reel : ", ytest[3])
|
|
|
|
# Showing the 4th image:
|
|
images = xtest.reshape((-1, 28, 28))
|
|
plt.imshow(images[3],cmap=plt.cm.gray_r,interpolation="nearest")
|
|
plt.show()
|
|
|
|
# Metrics :
|
|
print ("This MLP model has a precision of :", precision*100, "%.")
|
|
print ("This MLP model has a zero-one_loss of :",loss1_0*100, "%.")
|
|
|
|
#*******************Variation de nombre de couche de 2 à 100*******
|
|
hidden_layer =(50,)*100
|
|
|
|
Scor = []
|
|
Pred= []
|
|
Loss= []
|
|
|
|
for i in range (100):
|
|
clf = MLPClassifier(hidden_layer_sizes = hidden_layer[0:i])
|
|
clf.fit(xtrain, ytrain)
|
|
prediction = clf.predict(xtest)
|
|
score = clf.score(xtest, ytest)
|
|
precision = precision_score(ytest, prediction, average='micro')
|
|
loss0_1 = zero_one_loss(ytest, prediction)
|
|
|
|
Scor.append(score)
|
|
Pred.append(precision)
|
|
Loss.append(loss0_1)
|
|
|
|
print("For ", i, "hidden layer (s), The score = ", score *100, "%", ", Precision = ", precision*100, "% .." )
|
|
|
|
|
|
fig, ax = plt.subplots(3, sharex=True, figsize=(10,10))
|
|
ax[0].plot(range(100), Scor)
|
|
ax[0].set_title('Number of hidden layers from 1 to 99')
|
|
ax[0].set_ylabel('Score')
|
|
ax[1].plot(range(100), Pred)
|
|
ax[1].set_ylabel('Precision')
|
|
ax[2].plot(range(100), Loss)
|
|
ax[2].set_ylabel('Zero-to-one Loss')
|
|
|
|
#**************les 5 modeles de classification*********
|
|
clf1 = MLPClassifier(hidden_layer_sizes=(300))
|
|
# 2 layers
|
|
clf2 = MLPClassifier(hidden_layer_sizes=(20, 50))
|
|
# 4 layers
|
|
clf4 = MLPClassifier(hidden_layer_sizes=(20,50, 100, 150))
|
|
# 6 layers
|
|
clf6 = MLPClassifier(hidden_layer_sizes=( 20, 50, 150, 100, 50, 10))
|
|
# 8 layers, increase neurals :
|
|
clf8 = MLPClassifier(hidden_layer_sizes=(20, 40, 60, 120, 150, 180, 200, 250))
|
|
|
|
ClassifierList = ("clf1", "clf2","clf4", "clf6", "clf8")
|
|
|
|
Score =[]
|
|
Precision = []
|
|
Loss = []
|
|
TimeTraining = []
|
|
TimePrediction = []
|
|
|
|
def clfs(clf, i):
|
|
|
|
#Training :
|
|
startTrain =time.time()
|
|
clf.fit(xtrain, ytrain)
|
|
endTrain = time.time()
|
|
|
|
#Prediction :
|
|
startpred= time.time()
|
|
predict = clf.predict(xtest)
|
|
endpred = time.time()
|
|
|
|
#Metrics :
|
|
score = clf.score(xtest,ytest)
|
|
precision = precision_score(ytest, predict, average='micro')
|
|
loss01 = zero_one_loss(ytest, predict)
|
|
timetrain = endTrain - startTrain
|
|
timePred = endpred - startpred
|
|
|
|
#Saving results
|
|
Score.append(score*100)
|
|
Precision.append(precision*100)
|
|
Loss.append(loss01)
|
|
TimePrediction.append(timePred)
|
|
TimeTraining.append(timetrain)
|
|
|
|
#Prints :
|
|
print("For the", i," model we have, score = ", score*100, "%, precision =",precision*100, "%." )
|
|
print(" Training's time = ", timetrain, " and prediction's time = ", timePred, "." )
|
|
|
|
|
|
#***********************plot*******
|
|
fig, ax = plt.subplots(5, sharex=True, figsize=(10,10))
|
|
ax[0].scatter(range(5), Score, c='orange')
|
|
ax[0].set_title('The five classifiers with 1,2,4,6,8 hidden layers')
|
|
ax[0].set_ylabel('Score (%)')
|
|
ax[1].scatter(range(5), Precision, c='red')
|
|
ax[1].set_ylabel('Precision (%)')
|
|
ax[2].scatter(range(5), Loss, c='blue')
|
|
ax[2].set_ylabel('Zero-to-one Loss')
|
|
ax[3].scatter(range(5), TimeTraining, c='pink')
|
|
ax[3].set_ylabel('Training Time (s)')
|
|
ax[4].scatter(range(5), TimePrediction, c='purple')
|
|
ax[4].set_ylabel('¨Prediction Time (s)')
|
|
|
|
plt.show()
|
|
|
|
#*****Etude de la convergence des algorithmes d'optimisation : adam, sgd, lbfgs***
|
|
|
|
tab1=['adam','sgd','lbfgs']
|
|
tab2=['relu','tanh','logistic','identity']
|
|
for i in tab1:# solver
|
|
for j in tab2:
|
|
#activation function
|
|
clf = MLPClassifier(hidden_layer_sizes =50,activation=j,solver=i,verbose=False)
|
|
clf.fit(xtrain, ytrain)
|
|
prediction = clf.predict(xtest)
|
|
score = clf.score(xtest, ytest)
|
|
precision = precision_score(ytest, prediction, average='micro')
|
|
loss0_1 = zero_one_loss(ytest, prediction)
|
|
print('the result of the solver',i,'and the activation function',j)
|
|
Scor.append(score)
|
|
Pred.append(precision)
|
|
Loss.append(loss0_1)
|
|
|
|
|
|
print('score :',Scor)
|
|
print('prediction',Pred)
|
|
print('loss',Loss)
|
|
|
|
#**********Variation de alpha**************
|
|
alphas = np.logspace(-5, 3, 5)
|
|
for i in alphas:
|
|
|
|
clf = MLPClassifier(hidden_layer_sizes =50,activation='relu',solver='adam',alpha=i,verbose=False)
|
|
clf.fit(xtrain, ytrain)
|
|
prediction = clf.predict(xtest)
|
|
score = clf.score(xtest, ytest)
|
|
precision = precision_score(ytest, prediction, average='micro')
|
|
loss0_1 = zero_one_loss(ytest, prediction)
|
|
print('for alpha equal to: ',i)
|
|
Scor.append(score)
|
|
Pred.append(precision)
|
|
Loss.append(loss0_1)
|
|
|
|
print('score :',Scor)
|
|
print('prediction',Pred)
|
|
print('loss',Loss)
|
|
#***************
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TP2_CNN.py
|
|
Affichage de TP2_CNN.py en cours... |