diff --git a/02-intro_rna/01-perceptron.py b/02-intro_rna/01-perceptron.py index e199f6c..e056ba0 100644 --- a/02-intro_rna/01-perceptron.py +++ b/02-intro_rna/01-perceptron.py @@ -42,7 +42,7 @@ t_debut = time.time() # Init des plots fig = plt.figure(figsize=(15, 5)) fig.suptitle("Classificateur par perceptron") -donnees_ax = fig.add_subplot(2,3,(1,4)) # Observations : x1 et cibles : y1 +donnees_ax = fig.add_subplot(2,4,(1,5)) # Observations : x1 et cibles : y1 # f_activ_ax = fig.add_subplot(2,3,2) # df_activ_ax = fig.add_subplot(2,3,5) @@ -113,23 +113,39 @@ def sigmoid(_x): def echelon(_x): # Heaviside return (_x >= 0).astype(_x.dtype) +def relu(_x): + return np.maximum(0, _x) + +def tanh(_x): + return np.tanh(_x) + +def derivation(f, _x, eps=0.000001): + return (f(_x + eps) - f(_x))/eps + def derivation(f, _x, eps=0.000001): return (f(_x + eps) - f(_x))/eps # Fonctions d'activation -f_activ_ax = fig.add_subplot(2,3,2) +f_activ_ax = fig.add_subplot(2,4,2) f_activ_ax.set_title("Fonctions d'activation") f_activ_x = np.linspace(-5, 5, 200) -f_activ_ax.set(xlim=(-5,5), ylim=(0, 1.25)) +f_activ_ax.set(xlim=(-5,5), ylim=(-1.25, 1.25)) f_activ_ax.plot(f_activ_x, sigmoid(f_activ_x), "g-", label="Sigmoïde") f_activ_ax.plot(f_activ_x, echelon(f_activ_x), "b-", label="Échelon") +f_activ_ax.plot(f_activ_x, tanh(f_activ_x), "r-", label="Tanh") +f_activ_ax.plot(f_activ_x, relu(f_activ_x), "m-", label="ReLU") +f_activ_ax.text(-4,1, "f (x)", va='center', ha='center') f_activ_ax.legend() # Dérivée -df_activ_ax = fig.add_subplot(2,3,5) +df_activ_ax = fig.add_subplot(2,4,6) +df_activ_ax.set(xlim=(-5,5), ylim=(0, 1.1)) df_activ_ax.plot(f_activ_x, derivation(sigmoid, f_activ_x), "g-", label="f'(Sigmoïde)") df_activ_ax.plot(f_activ_x, derivation(echelon, f_activ_x), "b-", label="f'(Échelon)") -df_activ_ax.legend() +df_activ_ax.plot(f_activ_x, derivation(tanh, f_activ_x), "r-", label="f'(Tanh)") +df_activ_ax.plot(f_activ_x, derivation(relu, f_activ_x), "m-", label="f'(ReLU)") +df_activ_ax.text(-4,1, "f '(x)", va='center', ha='center') +# df_activ_ax.legend() ############################################################################### # Perceptron multi-couches (pmc) @@ -142,22 +158,36 @@ x1_mg, x2_mg = np.meshgrid(np.linspace(-0.2, 1.2, 100), np.linspace(-0.2, 1.2, 1 z1_pmc_xor = pmc_xor(x1_mg, x2_mg, activation=echelon) z2_pmc_xor = pmc_xor(x1_mg, x2_mg, activation=sigmoid) +z3_pmc_xor = pmc_xor(x1_mg, x2_mg, activation=tanh) +z4_pmc_xor = pmc_xor(x1_mg, x2_mg, activation=relu) -pmc_xor_echelon_ax = fig.add_subplot(2,3,3) -pmc_xor_echelon_ax.set_title("Xor (perceptron multi-couches) - Activation par Échelon") +pmc_xor_echelon_ax = fig.add_subplot(2,4,3) +pmc_xor_echelon_ax.set_title("Xor PMC par Échelon") pmc_xor_echelon_ax.contourf(x1_mg, x2_mg, z1_pmc_xor) pmc_xor_echelon_ax.plot([0, 1], [0, 1], "rs", markersize=15) pmc_xor_echelon_ax.plot([0, 1], [1, 0], "g^", markersize=15) pmc_xor_echelon_ax.grid(True) -pmc_xor_sigmoid_ax = fig.add_subplot(2,3,6) -# pmc_xor_sigmoid_ax.set_title("Xor (perceptron multi-couches) - Activation par Sigmoïde)") +pmc_xor_sigmoid_ax = fig.add_subplot(2,4,4) +pmc_xor_sigmoid_ax.set_title("Xor PMC par Sigmoïde") pmc_xor_sigmoid_ax.contourf(x1_mg, x2_mg, z2_pmc_xor) pmc_xor_sigmoid_ax.plot([0, 1], [0, 1], "rs", markersize=15) pmc_xor_sigmoid_ax.plot([0, 1], [1, 0], "g^", markersize=15) pmc_xor_sigmoid_ax.grid(True) -pmc_xor_sigmoid_ax.set_xlabel("Xor (perceptron multi-couches) - Activation par Sigmoïde", fontsize=12) +pmc_xor_tanh_ax = fig.add_subplot(2,4,7) +pmc_xor_tanh_ax.contourf(x1_mg, x2_mg, z3_pmc_xor) +pmc_xor_tanh_ax.plot([0, 1], [0, 1], "rs", markersize=15) +pmc_xor_tanh_ax.plot([0, 1], [1, 0], "g^", markersize=15) +pmc_xor_tanh_ax.grid(True) +pmc_xor_tanh_ax.set_xlabel("Xor PMC par Tanh", fontsize=12) + +pmc_xor_relu_ax = fig.add_subplot(2,4,8) +pmc_xor_relu_ax.contourf(x1_mg, x2_mg, z4_pmc_xor) +pmc_xor_relu_ax.plot([0, 1], [0, 1], "rs", markersize=15) +pmc_xor_relu_ax.plot([0, 1], [1, 0], "g^", markersize=15) +pmc_xor_relu_ax.grid(True) +pmc_xor_relu_ax.set_xlabel("Xor PMC par ReLU", fontsize=12) plt.show() diff --git a/02-intro_rna/img/01-perceptron.png b/02-intro_rna/img/01-perceptron.png index e996022..c94707f 100644 Binary files a/02-intro_rna/img/01-perceptron.png and b/02-intro_rna/img/01-perceptron.png differ