In [32]:
from NNPy import NetworkModule
Nous allons nous intéresser au développement d'un classifieur sur les données MNSIT (nous ferons des visages par la suite)
Le chargement des données MNIST s'effectue facilement grâce à la commande suivante:
In [25]:
from sklearn.datasets import fetch_mldata
mnist=fetch_mldata('MNIST original')
mnist
Out[25]:
Visualisation des données
In [26]:
import matplotlib.pyplot as plt
import numpy as np
plt.rc("image",cmap="binary")
plt.subplot(10,10,1)
for i in range(100):
plt.subplot(10,10,i)
idx=np.random.randint(len(mnist.data))
plt.imshow(mnist.data[idx].reshape(28,28))
Afin de réduire la taille des expérimentations, nous allons nous restreindre aux chiffres dont les étiquettes sont 6 et 8
In [27]:
import numpy as np
#Creation des vecteurs d'entrée
mnist_6=mnist.data[mnist.target==6]
nb_6=len(mnist_6)
mnist_8=mnist.data[mnist.target==8]
nb_8=len(mnist_8)
mnist_6_8=np.vstack((mnist_6,mnist_8))
print "%d 6s and %d 8s" % (nb_6,nb_8)
#Creation des vecteurs de sortie
target_6_8=np.array([[1]])
for i in range(nb_6-1):
target_6_8=np.vstack((target_6_8,[1]))
for i in range(nb_8):
target_6_8=np.vstack((target_6_8,[-1]))
print "%d/%d vecteurs d'apprentissage" % (len(target_6_8),len(mnist_6_8))
Sous-échantillonage des données
In [28]:
# Sous-Echantillonage des deux matrices (pour test)
randomvec=np.random.rand(len(target_6_8))
randomvec=randomvec>0.8
target_6_8=target_6_8[randomvec]
mnist_6_8=mnist_6_8[randomvec]
print "%d/%d vecteurs d'apprentissage apres echantillonage" % (len(target_6_8),len(mnist_6_8))
Création d'un ensemble de Train et de Test
In [29]:
#Creatin de train et test
randomvec=np.random.rand(len(target_6_8))
randomvec=randomvec>0.5
train_data=mnist_6_8[randomvec]
train_label=target_6_8[randomvec]
test_data=mnist_6_8[np.logical_not(randomvec)]
test_label=target_6_8[np.logical_not(randomvec)]
print "%d training examples and %d testing examples " % (len(train_data),len(test_data))
plt.imshow(train_data[np.random.randint(len(train_data))].reshape(28,28))
Out[29]:
Commencez par tester plusieurs classifieurs (architecture) sur ce problème.
In [30]:
perceptron = NetworkModule([LinearModule(28*28,1)],HingeLoss())
NBITER = 10
print "=======TRAIN ERROR======="
for i in xrange(0,NBITER):
perceptron.stochasticIter(train_data, train_label, verbose=False)
predicted = perceptron.forwardAll(train_data)
ok=0
ko=0
for pred,exp in zip(predicted,train_label):
if pred[0]*exp[0] > 0:
ok+=1
else:
ko+=1
print "Iteration n°%d: %d correct (%f%%), %d incorrect (%f%%) " % (i,ok,ok/(ok+ko+0.0)*100,ko,ko/(ok+ko+0.0)*100)
print "=======TEST ERROR======="
predicted = perceptron.forwardAll(test_data)
ok=0
ko=0
for pred,exp in zip(predicted,test_label):
if pred[0]*exp[0] > 0:
ok+=1
else:
ko+=1
print "%d correct (%f%%), %d incorrect (%f%%) " % (ok,ok/(ok+ko+0.0)*100,ko,ko/(ok+ko+0.0)*100)
In [22]:
mod2 = LinearModule(50,1)
mod1 = LinearModule(28*28,50)
MLP = NetworkModule([mod1,TanhModule(20,20),mod2],SquareLoss())
#print mod1.parameters
NBITER = 50
print "=======TRAIN ERROR======="
for i in xrange(0,NBITER):
MLP.stochasticIter(train_data, train_label,gradient_step=0.0001, verbose=False)
predicted = MLP.forwardAll(train_data)
ok=0
ko=0
for pred,exp in zip(predicted,train_label):
if pred[0]*exp[0] > 0:
ok+=1
else:
ko+=1
print "Iteration n°%d: %d correct (%f%%), %d incorrect (%f%%) " % (i,ok,ok/(ok+ko+0.0)*100,ko,ko/(ok+ko+0.0)*100)
#print mod1.parameters
print "=======TEST ERROR======="
predicted = MLP.forwardAll(test_data)
ok=0
ko=0
for pred,exp in zip(predicted,test_label):
if pred[0]*exp[0] > 0:
ok+=1
else:
ko+=1
print "%d correct (%f%%), %d incorrect (%f%%) " % (ok,ok/(ok+ko+0.0)*100,ko,ko/(ok+ko+0.0)*100)
In [10]:
mod2 = LinearModule(50,1)
mod1 = LinearModule(28*28,50)
MLP = NetworkModule([mod1,TanhModule(20,20),mod2],SquareLoss())
#print mod1.parameters
NBITER = 50
print "=======TRAIN ERROR======="
for i in xrange(0,NBITER):
MLP.batchIter(train_data, train_label,gradient_step=0.000001, verbose=False)
predicted = MLP.forwardAll(train_data)
ok=0
ko=0
for pred,exp in zip(predicted,train_label):
if pred[0]*exp[0] > 0:
ok+=1
else:
ko+=1
print "Iteration n°%d: %d correct (%f%%), %d incorrect (%f%%) " % (i,ok,ok/(ok+ko+0.0)*100,ko,ko/(ok+ko+0.0)*100)
#print mod1.parameters
print "=======TEST ERROR======="
predicted = MLP.forwardAll(test_data)
ok=0
ko=0
for pred,exp in zip(predicted,test_label):
if pred[0]*exp[0] > 0:
ok+=1
else:
ko+=1
print "%d correct (%f%%), %d incorrect (%f%%) " % (ok,ok/(ok+ko+0.0)*100,ko,ko/(ok+ko+0.0)*100)
In [21]:
mod2 = LinearModule(50,1)
mod1 = LinearModule(28*28,50)
MLP = NetworkModule([mod1,TanhModule(20,20),mod2],SquareLoss())
#print mod1.parameters
NBITER = 50
print "=======TRAIN ERROR======="
for i in xrange(0,NBITER):
MLP.miniBatchIter(train_data, train_label,batch_size=20,gradient_step=0.000001, verbose=False)
predicted = MLP.forwardAll(train_data)
ok=0
ko=0
for pred,exp in zip(predicted,train_label):
if pred[0]*exp[0] > 0:
ok+=1
else:
ko+=1
print "Iteration n°%d: %d correct (%f%%), %d incorrect (%f%%) " % (i,ok,ok/(ok+ko+0.0)*100,ko,ko/(ok+ko+0.0)*100)
#print mod1.parameters
print "=======TEST ERROR======="
predicted = MLP.forwardAll(test_data)
ok=0
ko=0
for pred,exp in zip(predicted,test_label):
if pred[0]*exp[0] > 0:
ok+=1
else:
ko+=1
print "%d correct (%f%%), %d incorrect (%f%%) " % (ok,ok/(ok+ko+0.0)*100,ko,ko/(ok+ko+0.0)*100)
Implémentez des auto-encodeurs et visualisez les patches appris
In [25]:
mod2 = LinearModule(100,28*28)
mod1 = LinearModule(28*28,100)
AE = NetworkModule([mod1,mod2],SquareLoss())
sq = SquareLoss()
#print sq.getLossValue(train_data[0],train_data[0])
#print mod1.parameters
NBITER = 200
print "=======TRAIN ERROR======="
for i in xrange(0,NBITER):
loss = AE.miniBatchIter(train_data, train_data,batch_size=150,gradient_step=0.000000000001, verbose=False)
predicted = AE.forwardAll(train_data)
err=0
for pred,exp in zip(predicted,train_data):
err += np.sum(np.power(pred-exp,2))
#print loss
print "Iteration n°%d - MSE: %f" % (i,err/len(predicted))
In [27]:
mod2 = LinearModule(100,28*28)
mod1 = LinearModule(28*28,100)
AE = NetworkModule([mod1,TanhModule(1,1),mod2],SquareLoss())
sq = SquareLoss()
#print sq.getLossValue(train_data[0],train_data[0])
#print mod1.parameters
NBITER = 50
print "=======TRAIN ERROR======="
for i in xrange(0,NBITER):
loss = AE.miniBatchIter(train_data, train_data,batch_size=150,gradient_step=0.000001, verbose=False)
predicted = AE.forwardAll(train_data)
err=0
for pred,exp in zip(predicted,train_data):
err += np.sum(np.power(pred-exp,2))
#print loss
print "Iteration n°%d - MSE: %f" % (i,err/len(predicted))
In [32]:
init = mod1.forward(train_data[10])
b = TanhModule(1,1)
comp = b.forward(init)
patch = np.zeros(100)
patch[10] = 1
#plt.imshow(train_data[0].reshape(28,28))
plt.imshow(recomp.reshape(28,28))
plt.show()
In [9]:
input = np.array([[1, 2, 3],[1,2,3]])
np.sum(input,axis=0)
Out[9]: