TP4-M1ProML-Correction

Posted on Tue 30 April 2019 in posts

TP : regression et autoencoder

Le but de ce TP est de voir les deux modèles suivants

  • la régression par réseau de neurone
  • l'autoencoder

Régression

On a vu comment classifier des données par l'utilisation des réseaux de neurones. Ici on va voir rapidement comment il est possible de régresser des fonctions.

Soit la fonction suivante

In [21]:
import numpy as np
import matplotlib.pyplot as plt

X = np.random.random(5000)*20-10
y = (0.5+5*X+0.3*X**2-0.1*X**3)
plt.plot(X,y,'.')
plt.show()
In [22]:
from sklearn.neural_network import MLPRegressor

mlp1 = MLPRegressor(hidden_layer_sizes=(200,),batch_size=32,alpha=0,learning_rate_init=0.01,max_iter=1,warm_start=True, activation='relu',n_iter_no_change=200)

TMAX = 100
loss = np.zeros(TMAX)
for t in range(TMAX):
    mlp1.fit(X.reshape(5000,1),y.reshape(5000,))
    loss[t] = mlp1.loss_
/home/aurele/anaconda3/lib/python3.6/site-packages/sklearn/neural_network/multilayer_perceptron.py:562: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (1) reached and the optimization hasn't converged yet.
  % self.max_iter, ConvergenceWarning)
In [23]:
plt.loglog(loss)
Out[23]:
[<matplotlib.lines.Line2D at 0x7f21a5b7d208>]
In [24]:
xx = np.linspace(-10,10,100)
yp = mlp1.predict(xx.reshape(100,1))
yt = (0.5+5*xx+0.3*xx**2-0.1*xx**3)
In [25]:
plt.plot(xx,yp.reshape(100,1),'.')
plt.plot(xx,yt.reshape(100,1),'.')
plt.show()
In [26]:
# Let's put some noise

Ns = 10000
X = np.random.random(Ns)*20-10
y = (0.5+5*X+0.3*X**2-0.1*X**3) + 5*np.ones(X.shape)*np.random.normal(size=X.shape)
plt.plot(X,y,'.')
plt.show()
In [27]:
mlp1 = MLPRegressor(hidden_layer_sizes=(100,),batch_size=32,alpha=0,learning_rate_init=0.01,max_iter=1,warm_start=True, activation='relu',n_iter_no_change=200)

TMAX = 100
loss = np.zeros(TMAX)
for t in range(TMAX):
    mlp1.fit(X.reshape(Ns,1),y.reshape(Ns,))
    loss[t] = mlp1.loss_
/home/aurele/anaconda3/lib/python3.6/site-packages/sklearn/neural_network/multilayer_perceptron.py:562: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (1) reached and the optimization hasn't converged yet.
  % self.max_iter, ConvergenceWarning)
In [32]:
plt.plot(loss)
Out[32]:
[<matplotlib.lines.Line2D at 0x7f21a1c42e10>]
In [33]:
xx = np.linspace(-10,10,100)
yp = mlp1.predict(xx.reshape(100,1))
yt = (0.5+5*xx+0.3*xx**2-0.1*xx**3)
In [34]:
plt.plot(xx,yp.reshape(100,1),'.')
plt.plot(xx,yt.reshape(100,1),'.')
plt.show()

AutoEncoder

pour faire un autoencoder, il suffit d'utiliser soit la classe MLPClassifier (pour des variables binaires) soit la clase MLPRegressor pour des variables continues. On commencera par regarder le cas d'un autoencoder simple (binaire) sur MNIST. On verra ensuite comment on peut faire un "denoising autoencoder" afin de débruiter automatiquement une image contenant du bruit gaussien.

VOtre travail:

Première partie

  • charger la bibliothèque MNIST
  • Utiliser la classe MLPClassifier pour faire un autoencoder. Question : comment faut-il appeler la fonction 'fit' ?
  • On plottera la loss de l'autoencoder en fonction du nombre d'itération.
  • Afin de voir ce qui se passe, on affichera à chaque itération une image qui a été encodée puis décodée
  • Finalement, vous ferez des tests en variant le nombre de variables cachées afin de trouver quel est le nombre optimal.
In [105]:
import pickle
import gzip
import numpy as np
import matplotlib.pyplot as plt
import numpy as np

f = gzip.open('../mnist.pkl.gz', 'rb')
u = pickle._Unpickler(f)
u.encoding = 'latin1'
p = u.load()
train_set, valid_set, test_set = p
In [106]:
from sklearn.neural_network import MLPRegressor
AE1 = MLPRegressor(solver='adam', hidden_layer_sizes=(100,),batch_size=32,alpha=0,max_iter=1,warm_start=True, activation='relu',n_iter_no_change=200)
# AE1 = MLPRegressor(solver='sgd',learning_rate_init=0.1,hidden_layer_sizes=(200,),batch_size=32,alpha=0,max_iter=1,warm_start=True, activation='relu',n_iter_no_change=200)

TMAX = 10
loss = np.zeros(TMAX)

for t in range(TMAX):    
    AE1.fit(train_set[0],train_set[0])
    print(t,AE1.loss_)
    loss[t] = AE1.loss_
    
/home/aurele/anaconda3/lib/python3.6/site-packages/sklearn/neural_network/multilayer_perceptron.py:562: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (1) reached and the optimization hasn't converged yet.
  % self.max_iter, ConvergenceWarning)
0 0.008384972839586158
1 0.004283709591395255
2 0.004045343059907821
3 0.00394018232946084
4 0.003894610642787574
5 0.0038500512701700354
6 0.003838698611416833
7 0.003831290149504087
8 0.0038092302060829153
9 0.003780223676978594
In [107]:
f, ax = plt.subplots(2,2)

print("Resultat sur le train")
ax[0,0].imshow(train_set[0][0,:].reshape(28,28))
ax[0,1].imshow(AE1.predict(train_set[0][:1,:]).reshape(28,28))

print("Resultat sur le test")
ax[1,0].imshow(test_set[0][0,:].reshape(28,28))
ax[1,1].imshow(AE1.predict(test_set[0][:1,:]).reshape(28,28))
Resultat sur le train
Resultat sur le test
Out[107]:
<matplotlib.image.AxesImage at 0x7f21a6dda860>
In [108]:
# Discretisation

x_train_dis = (train_set[0] > 0.3)*1
x_test_dis = (test_set[0] > 0.3)*1
In [109]:
from sklearn.neural_network import MLPClassifier
AE1_Dis = MLPClassifier(solver='adam', hidden_layer_sizes=(100,),batch_size=32,alpha=0,max_iter=1,warm_start=True, activation='relu',n_iter_no_change=200)
# AE1 = MLPRegressor(solver='sgd',learning_rate_init=0.1,hidden_layer_sizes=(200,),batch_size=32,alpha=0,max_iter=1,warm_start=True, activation='relu',n_iter_no_change=200)

TMAX = 10
loss = np.zeros(TMAX)

for t in range(TMAX):    
    AE1_Dis.fit(x_train_dis,x_train_dis)
    print(t,AE1_Dis.loss_)
    loss[t] = AE1_Dis.loss_
/home/aurele/anaconda3/lib/python3.6/site-packages/sklearn/neural_network/multilayer_perceptron.py:562: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (1) reached and the optimization hasn't converged yet.
  % self.max_iter, ConvergenceWarning)
0 84.63777471079828
1 32.15633458908422
2 21.3333623885503
3 16.743697158563172
4 14.306697280630768
5 12.791990038778838
6 11.743451011230118
7 11.024613345144864
8 10.481523349761602
9 10.043372514803778
In [110]:
f, ax = plt.subplots(2,2)

print("Resultat sur le train")
ax[0,0].imshow(x_train_dis[0,:].reshape(28,28))
ax[0,1].imshow(AE1_Dis.predict(x_train_dis[:1,:]).reshape(28,28))

print("Resultat sur le test")
ax[1,0].imshow(x_test_dis[0,:].reshape(28,28))
ax[1,1].imshow(AE1_Dis.predict(x_test_dis[:1,:]).reshape(28,28))
Resultat sur le train
Resultat sur le test
Out[110]:
<matplotlib.image.AxesImage at 0x7f21a5b3f048>
In [120]:
# Test sur la première époque, comment évolue la reconstruction

from sklearn.neural_network import MLPClassifier
AE2_Dis = MLPClassifier(solver='adam', hidden_layer_sizes=(100,),batch_size=50,alpha=0,max_iter=1,warm_start=True, activation='relu',n_iter_no_change=200)

idx_plt_x = 0
idx_plt_y = 0

idx = np.random.choice(train_set[0].shape[0])
plt.imshow(x_train_dis[idx,:].reshape(28,28))
plt.show()

f, ax = plt.subplots(4,5,figsize=(10,5))

s_mb = 50
n_mb = int(50000/s_mb)
for mb in range(n_mb):
    AE2_Dis.fit(x_train_dis[mb*s_mb:(mb+1)*s_mb,:],x_train_dis[mb*s_mb:(mb+1)*s_mb,:])
    #print(AE2_Dis.loss_)
    if(mb<200)&(mb%10==0):
        pr = AE2_Dis.predict(x_train_dis[idx:(idx+1),:])
        ax[idx_plt_y,idx_plt_x].imshow(pr.reshape(28,28))
        idx_plt_x += 1
        if(idx_plt_x==5):
            idx_plt_y += 1
            idx_plt_x = 0
plt.show()
/home/aurele/anaconda3/lib/python3.6/site-packages/sklearn/neural_network/multilayer_perceptron.py:562: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (1) reached and the optimization hasn't converged yet.
  % self.max_iter, ConvergenceWarning)
In [121]:
plt.plot(loss)
Out[121]:
[<matplotlib.lines.Line2D at 0x7f21a7f7e630>]

 Test du nombre de variables cachées

(attention c'est long)

In [87]:
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import mean_squared_error, log_loss

hidd_conf = [10,20,50,100,200]


loss = np.zeros((len(hidd_conf),TMAX))
loss_t = np.zeros((len(hidd_conf),TMAX))
idx_xp = 0

for n_hidd in hidd_conf:
    AE1_Dis = MLPClassifier(solver='adam', hidden_layer_sizes=(n_hidd,),batch_size=32,alpha=0,max_iter=1,warm_start=True, activation='relu',n_iter_no_change=200)
    # AE1 = MLPRegressor(solver='sgd',learning_rate_init=0.1,hidden_layer_sizes=(200,),batch_size=32,alpha=0,max_iter=1,warm_start=True, activation='relu',n_iter_no_change=200)

    TMAX = 10
        
    for t in range(TMAX):        
        AE1_Dis.fit(x_train_dis,x_train_dis)
        print(t,AE1_Dis.loss_)
        loss[idx_xp,t] = mean_squared_error(x_train_dis,AE1_Dis.predict(x_train_dis))
        loss_t[idx_xp,t] = mean_squared_error(x_test_dis,AE1_Dis.predict(x_test_dis))
    
    idx_xp += 1
/home/aurele/anaconda3/lib/python3.6/site-packages/sklearn/neural_network/multilayer_perceptron.py:562: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (1) reached and the optimization hasn't converged yet.
  % self.max_iter, ConvergenceWarning)
0 167.17296644662633
1 125.65480995052542
2 121.85632154235189
3 119.70238405663507
4 118.35451535958082
5 117.49462215897681
6 116.90012013166167
7 116.43114125846648
8 116.04863893794887
9 115.73187268080302
/home/aurele/anaconda3/lib/python3.6/site-packages/sklearn/neural_network/multilayer_perceptron.py:562: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (1) reached and the optimization hasn't converged yet.
  % self.max_iter, ConvergenceWarning)
0 138.31693398247583
1 89.6289645253044
2 82.52843946548558
3 78.53801057044045
4 76.8781446218425
5 76.21400098226826
6 75.89477026121617
7 75.67383187811336
8 75.50755737011683
9 75.38106882940612
/home/aurele/anaconda3/lib/python3.6/site-packages/sklearn/neural_network/multilayer_perceptron.py:562: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (1) reached and the optimization hasn't converged yet.
  % self.max_iter, ConvergenceWarning)
0 106.69850200815208
1 50.44685671813037
2 37.92566266360828
3 33.26769141749179
4 31.31131933376901
5 30.338739788278016
6 29.758422099135476
7 29.351333807969134
8 29.07965194504439
9 28.85847093370281
/home/aurele/anaconda3/lib/python3.6/site-packages/sklearn/neural_network/multilayer_perceptron.py:562: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (1) reached and the optimization hasn't converged yet.
  % self.max_iter, ConvergenceWarning)
0 85.14482887521156
1 32.12592169801699
2 21.36640301949342
3 16.760164660922975
4 14.232380898326394
5 12.701033047913523
6 11.671785154629967
7 10.952241854687994
8 10.415023707748896
9 9.99766763143235
/home/aurele/anaconda3/lib/python3.6/site-packages/sklearn/neural_network/multilayer_perceptron.py:562: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (1) reached and the optimization hasn't converged yet.
  % self.max_iter, ConvergenceWarning)
0 65.01770824138765
1 19.537414754189992
2 11.21633663520797
3 7.509982913987686
4 5.417767918465355
5 4.140770093979877
6 3.2832841495500755
7 2.696479010462209
8 2.2631355899577303
9 1.9403936544855322
In [92]:
f, ax = plt.subplots(1,5,figsize=(15,5))
for i in range(5):
    ax[i].plot(loss[i,:])
    ax[i].plot(loss_t[i,:])
plt.show()
for i in range(5):
    plt.plot(loss_t[i,:])

Seconde Partie

Ici, on va chercher à construire un autoencoder permettant de débruiter les images du jeu de données appris. On gardera donc MNIST, mais on appliquera la transformation suivante permettant de rajouter un bruit gaussien.

In [97]:
noise_factor = 0.3
x_train_noisy = x_train_dis + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=train_set[0].shape) 
x_test_noisy = x_test_dis + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=test_set[0].shape) 

x_train_noisy = np.clip(x_train_noisy, 0., 1.)
x_test_noisy = np.clip(x_test_noisy, 0., 1.)
In [98]:
plt.imshow(x_train_noisy[0,:].reshape(28,28))
Out[98]:
<matplotlib.image.AxesImage at 0x7f21a80ab048>

En utilisant le jeu de données bruités, essayer de trouver une façon d'utiliser l'autoencoder pour automatique débruiter les images. Vous vérifierez votre procédure

In [102]:
from sklearn.neural_network import MLPRegressor
AE1 = MLPClassifier(solver='adam', hidden_layer_sizes=(200,),batch_size=32,alpha=0,max_iter=1,warm_start=True, activation='relu',n_iter_no_change=200)
# AE1 = MLPRegressor(solver='sgd',learning_rate_init=0.1,hidden_layer_sizes=(200,),batch_size=32,alpha=0,max_iter=1,warm_start=True, activation='relu',n_iter_no_change=200)

TMAX = 15
loss = np.zeros(TMAX)
loss_t = np.zeros(TMAX)

for t in range(TMAX):
    AE1.fit(x_train_noisy,x_train_dis)
    print(t,AE1.loss_)
    loss[t] = mean_squared_error(x_train_dis,AE1.predict(x_train_noisy))
    loss_t[t] = mean_squared_error(x_test_dis,AE1.predict(x_test_noisy))
/home/aurele/anaconda3/lib/python3.6/site-packages/sklearn/neural_network/multilayer_perceptron.py:562: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (1) reached and the optimization hasn't converged yet.
  % self.max_iter, ConvergenceWarning)
0 80.57918868660423
1 37.666861052248244
2 30.13199765596605
3 26.295341670113984
4 23.907143139141983
5 22.255709101189538
6 21.05399130080169
7 20.18242895053196
8 19.54344210143221
9 19.05297678320036
10 18.6785418641579
11 18.41438238538008
12 18.19410273228226
13 17.996139237060753
14 17.81574069816445
In [103]:
f, ax = plt.subplots(2,2)

print("Resultat sur le train")
ax[0,0].imshow(x_train_noisy[0,:].reshape(28,28))
ax[0,1].imshow(AE1.predict(x_train_noisy[:1,:]).reshape(28,28))

print("Resultat sur le test")
ax[1,0].imshow(x_test_noisy[0,:].reshape(28,28))
ax[1,1].imshow(AE1.predict(x_test_noisy[:1,:]).reshape(28,28))
Resultat sur le train
Resultat sur le test
Out[103]:
<matplotlib.image.AxesImage at 0x7f21a5b4d860>
In [104]:
plt.plot(loss)
plt.plot(loss_t)
Out[104]:
[<matplotlib.lines.Line2D at 0x7f21a1b48438>]
In [ ]: