2022-07-05 08:13:25 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <float.h>
|
2022-09-16 14:53:35 +02:00
|
|
|
#include "include/function.h"
|
2022-07-05 08:13:25 +02:00
|
|
|
|
|
|
|
float max(float a, float b) {
|
2022-09-09 17:39:07 +02:00
|
|
|
return a < b ? b:a;
|
2022-07-05 08:13:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
float sigmoid(float x) {
|
|
|
|
return 1/(1 + exp(-x));
|
|
|
|
}
|
|
|
|
|
|
|
|
float sigmoid_derivative(float x) {
|
|
|
|
float tmp = exp(-x);
|
|
|
|
return tmp/((1+tmp)*(1+tmp));
|
|
|
|
}
|
|
|
|
|
|
|
|
float relu(float x) {
|
|
|
|
return max(0, x);
|
|
|
|
}
|
|
|
|
|
|
|
|
float relu_derivative(float x) {
|
|
|
|
if (x > 0)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
float tanh_(float x) {
|
|
|
|
return tanh(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
float tanh_derivative(float x) {
|
|
|
|
float a = tanh(x);
|
|
|
|
return 1 - a*a;
|
|
|
|
}
|
|
|
|
|
|
|
|
void apply_softmax_input(float ***input, int depth, int rows, int columns) {
|
|
|
|
float m = FLT_MIN;
|
|
|
|
float sum=0;
|
2022-09-09 17:39:07 +02:00
|
|
|
for (int i=0; i < depth; i++) {
|
|
|
|
for (int j=0; j < rows; j++) {
|
|
|
|
for (int k=0; k < columns; k++) {
|
2022-07-05 08:13:25 +02:00
|
|
|
m = max(m, input[i][j][k]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-09 17:39:07 +02:00
|
|
|
for (int i=0; i < depth; i++) {
|
|
|
|
for (int j=0; j < rows; j++) {
|
|
|
|
for (int k=0; k < columns; k++) {
|
2022-07-05 08:13:25 +02:00
|
|
|
input[i][j][k] = exp(m-input[i][j][k]);
|
|
|
|
sum += input[i][j][k];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-09 17:39:07 +02:00
|
|
|
for (int i=0; i < depth; i++) {
|
|
|
|
for (int j=0; j < rows; j++) {
|
|
|
|
for (int k=0; k < columns; k++) {
|
2022-07-05 08:13:25 +02:00
|
|
|
input[i][j][k] = input[i][j][k]/sum;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void apply_function_input(float (*f)(float), float*** input, int depth, int rows, int columns) {
|
2022-09-09 17:39:07 +02:00
|
|
|
for (int i=0; i < depth; i++) {
|
|
|
|
for (int j=0; j < rows; j++) {
|
|
|
|
for (int k=0; k < columns; k++) {
|
2022-07-05 08:13:25 +02:00
|
|
|
input[i][j][k] = (*f)(input[i][j][k]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void choose_apply_function_input(int activation, float*** input, int depth, int rows, int columns) {
|
|
|
|
if (activation == RELU) {
|
|
|
|
apply_function_input(relu, input, depth, rows, columns);
|
|
|
|
}
|
|
|
|
else if (activation == SIGMOID) {
|
|
|
|
apply_function_input(sigmoid, input, depth, rows, columns);
|
|
|
|
}
|
|
|
|
else if (activation == SOFTMAX) {
|
|
|
|
apply_softmax_input(input, depth, rows, columns);
|
|
|
|
}
|
|
|
|
else if (activation == TANH) {
|
|
|
|
apply_function_input(tanh_, input, depth, rows, columns);
|
|
|
|
}
|
|
|
|
else {
|
2022-09-12 17:56:44 +02:00
|
|
|
printf("Erreur, fonction d'activation inconnue: %d\n", activation);
|
2022-07-05 08:13:25 +02:00
|
|
|
}
|
|
|
|
}
|