Add float clip(float) to update.c

This commit is contained in:
augustin64 2023-03-02 10:35:25 +01:00
parent c45b21e322
commit 7f88acf17f
2 changed files with 94 additions and 86 deletions

View File

@ -11,6 +11,12 @@
*/ */
#define CLIP_VALUE 300 #define CLIP_VALUE 300
/*
* Réduit la valeur de a si abs(a) > CLIP_VALUE
* Renvoie la valeur modifiée càd `signe(a)*min(abs(a), CLIP_VALUE)`
*/
float clip(float a);
/* /*
* Met à jours les poids à partir de données obtenus après plusieurs backpropagations * Met à jours les poids à partir de données obtenus après plusieurs backpropagations
* Puis met à 0 tous les d_weights * Puis met à 0 tous les d_weights

View File

@ -3,34 +3,41 @@
#include "include/update.h" #include "include/update.h"
#include "include/struct.h" #include "include/struct.h"
float clip(float a) {
if (a > CLIP_VALUE) {
return CLIP_VALUE;
}
if (a < -CLIP_VALUE) {
return -CLIP_VALUE;
}
return a;
}
void update_weights(Network* network, Network* d_network) { void update_weights(Network* network, Network* d_network) {
int n = network->size; int n = network->size;
int input_depth, input_width, output_depth, output_width, k_size;
Kernel* k_i; for (int i=0; i < (n-1); i++) {
Kernel* dk_i; Kernel* k_i = network->kernel[i];
for (int i=0; i<(n-1); i++) { Kernel* dk_i = d_network->kernel[i];
k_i = network->kernel[i];
dk_i = d_network->kernel[i]; int input_depth = network->depth[i];
input_depth = network->depth[i]; int input_width = network->width[i];
input_width = network->width[i];
output_depth = network->depth[i+1]; int output_depth = network->depth[i+1];
output_width = network->width[i+1]; int output_width = network->width[i+1];
if (k_i->cnn) { // Convolution if (k_i->cnn) { // Convolution
Kernel_cnn* cnn = k_i->cnn; Kernel_cnn* cnn = k_i->cnn;
Kernel_cnn* d_cnn = dk_i->cnn; Kernel_cnn* d_cnn = dk_i->cnn;
k_size = cnn->k_size; int k_size = cnn->k_size;
for (int a=0; a<input_depth; a++) { for (int a=0; a < input_depth; a++) {
for (int b=0; b<output_depth; b++) { for (int b=0; b < output_depth; b++) {
for (int c=0; c<k_size; c++) { for (int c=0; c < k_size; c++) {
for (int d=0; d<k_size; d++) { for (int d=0; d < k_size; d++) {
cnn->weights[a][b][c][d] -= network->learning_rate * d_cnn->d_weights[a][b][c][d]; cnn->weights[a][b][c][d] -= network->learning_rate * d_cnn->d_weights[a][b][c][d];
d_cnn->d_weights[a][b][c][d] = 0; d_cnn->d_weights[a][b][c][d] = 0;
if (cnn->weights[a][b][c][d] > CLIP_VALUE) cnn->weights[a][b][c][d] = clip(cnn->weights[a][b][c][d]);
cnn->weights[a][b][c][d] = CLIP_VALUE;
else if (cnn->weights[a][b][c][d] < -CLIP_VALUE)
cnn->weights[a][b][c][d] = -CLIP_VALUE;
} }
} }
} }
@ -39,8 +46,9 @@ void update_weights(Network* network, Network* d_network) {
if (k_i->linearisation == 0) { // Vecteur -> Vecteur if (k_i->linearisation == 0) { // Vecteur -> Vecteur
Kernel_nn* nn = k_i->nn; Kernel_nn* nn = k_i->nn;
Kernel_nn* d_nn = dk_i->nn; Kernel_nn* d_nn = dk_i->nn;
for (int a=0; a<input_width; a++) {
for (int b=0; b<output_width; b++) { for (int a=0; a < input_width; a++) {
for (int b=0; b < output_width; b++) {
nn->weights[a][b] -= network->learning_rate * d_nn->d_weights[a][b]; nn->weights[a][b] -= network->learning_rate * d_nn->d_weights[a][b];
d_nn->d_weights[a][b] = 0; d_nn->d_weights[a][b] = 0;
} }
@ -48,91 +56,83 @@ void update_weights(Network* network, Network* d_network) {
} else { // Matrice -> vecteur } else { // Matrice -> vecteur
Kernel_nn* nn = k_i->nn; Kernel_nn* nn = k_i->nn;
Kernel_nn* d_nn = dk_i->nn; Kernel_nn* d_nn = dk_i->nn;
int size_input = input_width*input_width*input_depth; int size_input = input_width*input_width*input_depth;
for (int a=0; a<size_input; a++) {
for (int b=0; b<output_width; b++) { for (int a=0; a < size_input; a++) {
for (int b=0; b < output_width; b++) {
nn->weights[a][b] -= network->learning_rate * d_nn->d_weights[a][b]; nn->weights[a][b] -= network->learning_rate * d_nn->d_weights[a][b];
d_nn->d_weights[a][b] = 0; d_nn->d_weights[a][b] = 0;
if (nn->weights[a][b] > CLIP_VALUE) nn->weights[a][b] = clip(nn->weights[a][b]);
nn->weights[a][b] = CLIP_VALUE;
else if (nn->weights[a][b] < -CLIP_VALUE)
nn->weights[a][b] = -CLIP_VALUE;
} }
} }
} }
} else { // Pooling
(void)0; // Ne rien faire pour la couche pooling
} }
// Une couche de pooling ne nécessite pas de traitement
} }
} }
void update_bias(Network* network, Network* d_network) { void update_bias(Network* network, Network* d_network) {
int n = network->size; int n = network->size;
int output_width, output_depth;
Kernel* k_i; for (int i=0; i < (n-1); i++) {
Kernel* dk_i; Kernel* k_i = network->kernel[i];
for (int i=0; i<(n-1); i++) { Kernel* dk_i = d_network->kernel[i];
k_i = network->kernel[i]; int output_width = network->width[i+1];
dk_i = d_network->kernel[i]; int output_depth = network->depth[i+1];
output_width = network->width[i+1];
output_depth = network->depth[i+1];
if (k_i->cnn) { // Convolution if (k_i->cnn) { // Convolution
Kernel_cnn* cnn = k_i->cnn; Kernel_cnn* cnn = k_i->cnn;
Kernel_cnn* d_cnn = dk_i->cnn; Kernel_cnn* d_cnn = dk_i->cnn;
for (int a=0; a<output_depth; a++) {
for (int b=0; b<output_width; b++) { for (int a=0; a < output_depth; a++) {
for (int c=0; c<output_width; c++) { for (int b=0; b < output_width; b++) {
for (int c=0; c < output_width; c++) {
cnn->bias[a][b][c] -= network->learning_rate * d_cnn->d_bias[a][b][c]; cnn->bias[a][b][c] -= network->learning_rate * d_cnn->d_bias[a][b][c];
d_cnn->d_bias[a][b][c] = 0; d_cnn->d_bias[a][b][c] = 0;
if (cnn->bias[a][b][c] > CLIP_VALUE) cnn->bias[a][b][c] = clip(cnn->bias[a][b][c]);
cnn->bias[a][b][c] = CLIP_VALUE;
else if (cnn->bias[a][b][c] < -CLIP_VALUE)
cnn->bias[a][b][c] = -CLIP_VALUE;
} }
} }
} }
} else if (k_i->nn) { // Full connection } else if (k_i->nn) { // Full connection
Kernel_nn* nn = k_i->nn; Kernel_nn* nn = k_i->nn;
Kernel_nn* d_nn = dk_i->nn; Kernel_nn* d_nn = dk_i->nn;
for (int a=0; a<output_width; a++) {
for (int a=0; a < output_width; a++) {
nn->bias[a] -= network->learning_rate * d_nn->d_bias[a]; nn->bias[a] -= network->learning_rate * d_nn->d_bias[a];
d_nn->d_bias[a] = 0; d_nn->d_bias[a] = 0;
if (nn->bias[a] > CLIP_VALUE) nn->bias[a] = clip(nn->bias[a]);
nn->bias[a] = CLIP_VALUE;
else if (nn->bias[a] < -CLIP_VALUE)
nn->bias[a] = -CLIP_VALUE;
} }
} else { // Pooling
(void)0; // Ne rien faire pour la couche pooling
} }
// Une couche de pooling ne nécessite pas de traitement
} }
} }
void reset_d_weights(Network* network) { void reset_d_weights(Network* network) {
int n = network->size; int n = network->size;
int input_depth, input_width, output_depth, output_width;
Kernel* k_i; for (int i=0; i < (n-1); i++) {
Kernel* k_i_1; Kernel* k_i = network->kernel[i];
for (int i=0; i<(n-1); i++) { Kernel* k_i_1 = network->kernel[i+1];
k_i = network->kernel[i];
k_i_1 = network->kernel[i+1]; int input_depth = network->depth[i];
input_depth = network->depth[i]; int input_width = network->width[i];
input_width = network->width[i];
output_depth = network->depth[i+1]; int output_depth = network->depth[i+1];
output_width = network->width[i+1]; int output_width = network->width[i+1];
if (k_i->cnn) { // Convolution if (k_i->cnn) { // Convolution
Kernel_cnn* cnn = k_i_1->cnn; Kernel_cnn* cnn = k_i_1->cnn;
int k_size = cnn->k_size; int k_size = cnn->k_size;
for (int a=0; a<input_depth; a++) {
for (int b=0; b<output_depth; b++) { for (int a=0; a < input_depth; a++) {
for (int c=0; c<k_size; c++) { for (int b=0; b < output_depth; b++) {
for (int d=0; d<k_size; d++) { for (int c=0; c < k_size; c++) {
for (int d=0; d < k_size; d++) {
cnn->d_weights[a][b][c][d] = 0; cnn->d_weights[a][b][c][d] = 0;
} }
} }
@ -141,53 +141,55 @@ void reset_d_weights(Network* network) {
} else if (k_i->nn) { // Full connection } else if (k_i->nn) { // Full connection
if (k_i->linearisation == 0) { // Vecteur -> Vecteur if (k_i->linearisation == 0) { // Vecteur -> Vecteur
Kernel_nn* nn = k_i_1->nn; Kernel_nn* nn = k_i_1->nn;
for (int a=0; a<input_width; a++) {
for (int b=0; b<output_width; b++) { for (int a=0; a < input_width; a++) {
for (int b=0; b < output_width; b++) {
nn->d_weights[a][b] = 0; nn->d_weights[a][b] = 0;
} }
} }
} else { // Matrice -> vecteur } else { // Matrice -> vecteur
Kernel_nn* nn = k_i_1->nn; Kernel_nn* nn = k_i_1->nn;
int size_input = input_width*input_width*input_depth; int size_input = input_width*input_width*input_depth;
for (int a=0; a<size_input; a++) {
for (int b=0; b<output_width; b++) { for (int a=0; a < size_input; a++) {
for (int b=0; b < output_width; b++) {
nn->d_weights[a][b] = 0; nn->d_weights[a][b] = 0;
} }
} }
} }
} else { // Pooling
(void)0; // Ne rien faire pour la couche pooling
} }
// Une couche de pooling ne nécessite pas de traitement
} }
} }
void reset_d_bias(Network* network) { void reset_d_bias(Network* network) {
int n = network->size; int n = network->size;
int output_width, output_depth;
Kernel* k_i; for (int i=0; i < (n-1); i++) {
Kernel* k_i_1; Kernel* k_i = network->kernel[i];
for (int i=0; i<(n-1); i++) { Kernel* k_i_1 = network->kernel[i+1];
k_i = network->kernel[i];
k_i_1 = network->kernel[i+1]; int output_width = network->width[i+1];
output_width = network->width[i+1]; int output_depth = network->depth[i+1];
output_depth = network->depth[i+1];
if (k_i->cnn) { // Convolution if (k_i->cnn) { // Convolution
Kernel_cnn* cnn = k_i_1->cnn; Kernel_cnn* cnn = k_i_1->cnn;
for (int a=0; a<output_depth; a++) {
for (int b=0; b<output_width; b++) { for (int a=0; a < output_depth; a++) {
for (int c=0; c<output_width; c++) { for (int b=0; b < output_width; b++) {
for (int c=0; c < output_width; c++) {
cnn->d_bias[a][b][c] = 0; cnn->d_bias[a][b][c] = 0;
} }
} }
} }
} else if (k_i->nn) { // Full connection } else if (k_i->nn) { // Full connection
Kernel_nn* nn = k_i_1->nn; Kernel_nn* nn = k_i_1->nn;
for (int a=0; a<output_width; a++) {
for (int a=0; a < output_width; a++) {
nn->d_bias[a] = 0; nn->d_bias[a] = 0;
} }
} else { // Pooling
(void)0; // Ne rien faire pour la couche pooling
} }
// Une couche de pooling ne nécessite pas de traitement
} }
} }