Réseau neuronal à 1 couche cachée

Category:


# -*- coding: utf-8 -*-
"""
Created on Thu Dec 13 14:50:40 2018

@author: K
"""

import numpy as np
import time
import scipy.io
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split

datas = scipy.io.loadmat('ex4data1.mat')
X = datas["X"]
y =  datas["y"]
im = X[2098].reshape(20, 20);
plt.imshow(im, cmap='gray')
#plt.imshow(X[0]).reshape([20, 20]);


#print(type(X[0]))
#realout = 
Y = np.zeros((5000,1))
for i in range(0, 5000):
    for j in range(1, 10):
        if np.mod(y[i],2) == 0:
            Y[i,0] = 1
        else:
                Y[i,0] = 0
                
# define the sigmoid function
def sigmoid(x, derivative=False):

    if (derivative == True):
        return x * (1 - x)
    else:
        return 1 / (1 + np.exp(-x))
    
K = np.hstack((Y, X))
np.random.shuffle(K)

#X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 42)

Xtrain = K[0:4000,1:401]
Ytrain = K[0:4000,:1]



Xtest = K[4001:4999,1:4001]

Ytest = K[4001:4999,:1]


alpha = 10
#nb_neurones = 10
nb_neurones = 25


#datas = scipy.io.loadmat('ex4data1.mat');
#X = datas["X"]; 
#y =  datas["y"];
#time.sleep(10)

'''
w = scipy.io.loadmat('ex4weights.mat');
Theta1 = w["Theta1"]; 
Theta2 =  w["Theta2"];
W2 =Theta1
W3 = Theta2



# inputs
X = np.array([  
    [0, 0, 1],
    [0, 1, 1],
    [1, 0, 0],
    [1, 1, 0],
    [1, 0, 1],
    [1, 1, 1],
])

y = np.array([[0, 1, 1, 1, 0, 1]]).T
'''
W2 = 2*np.random.random((nb_neurones,Xtrain.shape[1] + 1)) - 1
W3 = 2*np.random.random((Y.shape[1],nb_neurones + 1)) - 1

nb_iteration = 5


plt.style.use('dark_background')
plt.figure(figsize = (8,5))
N = nb_iteration
plt.axis([0, N, -1, 1])
plt.grid()
XX = np.array([])
YY = np.array([])
ZZ = np.array([])
plt.ion()
plt.plot(111)
plt.plot(XX,YY,c='r',marker='.',linewidth = 1,alpha=0.2)
err = 0


def PLOTX(a,b):  
    #global err
    plt.plot(a,b,c='y',marker='.',linewidth = 1,alpha=0.9)
    plt.show()

for i in range(nb_iteration):
    
    
    print("Itération i ==> ",i)
    X_tilde = np.hstack((np.ones((Xtrain.shape[0], 1)), Xtrain))
    A2 = sigmoid(np.dot(X_tilde,W2.T))
    A2_tilde = np.hstack((np.ones((A2.shape[0], 1)), A2))
    A3 = sigmoid(np.dot(A2_tilde,W3.T))
    
    erreur = A3 - Ytrain
    Delta3 = (A3 - Ytrain)*sigmoid(A3,True)
    
    #Delta2 = Delta3*sigmoid(A2,True) # ok
    Delta2 = np.dot(Delta3,W3[:, 1:])*sigmoid(A2,True) 
    
    #print("Delta2.shape => ",Delta2.shape)
    #print("Delta3.shape => ",Delta3.shape)
    
    W3_dp_all = Delta3[:, :, np.newaxis] * A2_tilde[:, np.newaxis, :]
    W3_dp = np.average(W3_dp_all, axis = 0)
    
    W2_dp_all = Delta2[:, :, np.newaxis] * X_tilde[:, np.newaxis, :]
    W2_dp = np.average(W2_dp_all, axis = 0)
    
    W2 = W2 - alpha*W2_dp
    W3 = W3 - alpha*W3_dp
    
    
    XX=np.append(XX,i)
    #print(YY)
    #a=sum(np.dot((A3 - Ytrain).T,A3 - Ytrain))
    a=np.average(np.dot(erreur.T,erreur))
    print("cout i ==> ",a/2000)
    YY=np.append(YY,a/2500)      
    PLOTX(XX,YY)
    plt.pause(.01)

retour = np.rint(((Ytrain.shape[0] - sum(abs(np.rint(A3-Ytrain))))/Ytrain.shape[0])*100)
#u = sum(abs(np.rint(A3-Ytrain)))
print("réussite After Training: ",np.float64(retour))



X_tilde = np.hstack((np.ones((Xtest.shape[0], 1)), Xtest))
A2 = sigmoid(np.dot(X_tilde,W2.T))
A2_tilde = np.hstack((np.ones((A2.shape[0], 1)), A2))
A3 = sigmoid(np.dot(A2_tilde,W3.T))

retour = np.rint(((Ytest.shape[0] - sum(abs(np.rint(A3-Ytest))))/Ytest.shape[0])*100).astype(str)
print("% réussite Sur Test : ", np.float64(retour))

Files: