First results in backprop ?

This commit is contained in:
julienChemillier 2022-11-15 12:50:38 +01:00
parent c73d4d5b90
commit d94f61bb77
3 changed files with 23 additions and 16 deletions

View File

@ -10,7 +10,6 @@ void make_average_pooling(float*** input, float*** output, int size, int output_
// output[output_depth][output_dim][output_dim]
float average;
int n = size*size;
for (int i=0; i < output_depth; i++) {
for (int j=0; j < output_dim; j++) {
for (int k=0; k < output_dim; k++) {
@ -20,7 +19,7 @@ void make_average_pooling(float*** input, float*** output, int size, int output_
average += input[i][size*j +a][size*k +b];
}
}
output[i][j][k] = average/n;
output[i][j][k] = average/(float)n;
}
}
}

View File

@ -10,6 +10,7 @@
#include "../include/colors.h"
#include "include/function.h"
#include "include/creation.h"
#include "include/update.h"
#include "include/utils.h"
#include "include/free.h"
#include "include/cnn.h"
@ -45,14 +46,20 @@ void* train_thread(void* parameters) {
int start = param->start;
int nb_images = param->nb_images;
float accuracy = 0.;
int cpt=1;
for (int i=start; i < start+nb_images; i++) {
if (dataset_type == 0) {
write_image_in_network_32(images[i], height, width, network->input[0][0]);
forward_propagation(network);
maxi = indice_max(network, 10);
backward_propagation(network, labels[i]);
maxi = indice_max(network->input[network->size-1][0][0], network->width[network->size-1]);
if (cpt==16) { // Update the network
printf("a\n");
update_weights(network);
update_bias(network);
cpt = 0;
}
cpt++;
if (maxi == labels[i]) {
accuracy += 1.;
}

View File

@ -2,22 +2,25 @@
#include "include/update.h"
#include "include/struct.h"
#include <stdio.h>
void update_weights(Network* network) {
int n = network->size;
int input_depth, input_width, output_depth, output_width;
int input_depth, input_width, output_depth, output_width, k_size;
Kernel* k_i;
Kernel* k_i_1;
for (int i=0; i<(n-1); i++) {
k_i = network->kernel[i];
k_i_1 = network->kernel[i+1];
input_depth = network->depth[i];
input_width = network->width[i];
output_depth = network->depth[i+1];
output_width = network->width[i+1];
if (k_i->cnn) { // Convolution
Kernel_cnn* cnn = k_i_1->cnn;
int k_size = cnn->k_size;
Kernel_cnn* cnn = k_i->cnn; // ERRORS
k_size = cnn->k_size;
for (int a=0; a<input_depth; a++) {
for (int b=0; b<output_depth; b++) {
for (int c=0; c<k_size; c++) {
@ -30,7 +33,7 @@ void update_weights(Network* network) {
}
} else if (k_i->nn) { // Full connection
if (input_depth==1) { // Vecteur -> Vecteur
Kernel_nn* nn = k_i_1->nn;
Kernel_nn* nn = k_i->nn;
for (int a=0; a<input_width; a++) {
for (int b=0; b<output_width; b++) {
nn->weights[a][b] += network->learning_rate * nn->d_weights[a][b];
@ -38,7 +41,7 @@ void update_weights(Network* network) {
}
}
} else { // Matrice -> vecteur
Kernel_nn* nn = k_i_1->nn;
Kernel_nn* nn = k_i->nn;
int input_size = input_width*input_width*input_depth;
for (int a=0; a<input_size; a++) {
for (int b=0; b<output_width; b++) {
@ -57,15 +60,13 @@ void update_bias(Network* network) {
int n = network->size;
int output_width, output_depth;
Kernel* k_i;
Kernel* k_i_1;
for (int i=0; i<(n-1); i++) {
k_i = network->kernel[i];
k_i_1 = network->kernel[i+1];
output_width = network->width[i+1];
output_depth = network->depth[i+1];
if (k_i->cnn) { // Convolution
Kernel_cnn* cnn = k_i_1->cnn;
Kernel_cnn* cnn = k_i->cnn;
for (int a=0; a<output_depth; a++) {
for (int b=0; b<output_width; b++) {
for (int c=0; c<output_width; c++) {
@ -75,7 +76,7 @@ void update_bias(Network* network) {
}
}
} else if (k_i->nn) { // Full connection
Kernel_nn* nn = k_i_1->nn;
Kernel_nn* nn = k_i->nn;
for (int a=0; a<output_width; a++) {
nn->bias[a] += network->learning_rate * nn->d_bias[a];
nn->d_bias[a] = 0;