Bonsoir
Je me lance dans l'écriture de ma première bibliothèque. Pour (ne pas) faire simple, je veux transformer mon nouveau code de perceptron multicouche sur ESP32 et en faire une bibliothèque.
Bien sûr j'ai plein d'erreurs, mais mon plus gros problème est lié à l'allocation de la mémoire.
Sans la bibliothèque, le code alloue des zones de mémoire pour le réseau et les couches, selon des structures:
typedef struct { /* A LAYER OF A NET: */
INT Units; /* - number of neurons in this layer */
REAL* Output; /* - output of ith neuron */
REAL* Error; /* - error term of ith neuron */
REAL** Weight; /* - connection weights to ith neuron */
REAL** WeightSave; /* - saved weights for stopped training */
REAL** dWeight; /* - last weight deltas for momentum */
} LAYER;
typedef struct { /* A NET: */
LAYER** Layer; /* - layers of this net */
LAYER* InputLayer; /* - input layer */
LAYER* OutputLayer; /* - output layer */
REAL Alpha; /* - momentum factor */
REAL Eta; /* - learning rate */
REAL Gain; /* - gain of sigmoid function */
REAL Error; /* - total net error */
REAL EtaSave; /* - saved learning rate */
REAL GainSave; /* - saved gain */
} NET;
L'allocation se fait comme suit :
void GenerateNetwork(NET* Net)
{
INT l, i;
Net->Layer = (LAYER**) calloc(NUM_LAYERS, sizeof(LAYER*));
for (l = 0; l < NUM_LAYERS; l++) {
Net->Layer[l] = (LAYER*) malloc(sizeof(LAYER));
Net->Layer[l]->Units = Units[l];
Net->Layer[l]->Output = (REAL*) calloc(Units[l] + 1, sizeof(REAL));
Net->Layer[l]->Error = (REAL*) calloc(Units[l] + 1, sizeof(REAL));
Net->Layer[l]->Weight = (REAL**) calloc(Units[l] + 1, sizeof(REAL*));
Net->Layer[l]->WeightSave = (REAL**) calloc(Units[l] + 1, sizeof(REAL*));
Net->Layer[l]->dWeight = (REAL**) calloc(Units[l] + 1, sizeof(REAL*));
Net->Layer[l]->Output[0] = BIAS;
if (l != 0) {
for (i = 1; i <= Units[l]; i++) {
Net->Layer[l]->Weight[i] = (REAL*) calloc(Units[l - 1] + 1, sizeof(REAL));
Net->Layer[l]->WeightSave[i] = (REAL*) calloc(Units[l - 1] + 1, sizeof(REAL));
Net->Layer[l]->dWeight[i] = (REAL*) calloc(Units[l - 1] + 1, sizeof(REAL));
}
}
}
Net->InputLayer = Net->Layer[0];
Net->OutputLayer = Net->Layer[NUM_LAYERS - 1];
}
REAL est un alias pour "float" (ce n'est pas mon code ici)
Il faut ensuite que ces emplacements mémoire soient accessibles aux diverses méthodes de ma bibliothèque.
Dans le code sans bibli, je passe le réseau en argument, par exemple :
void RestoreWeights(NET* Net)
Mais ça c'était avant...
Maintenant, avec la bibliothèque, je déclare une instance d'un réseau. Puis je l'initialise via la méthode GenerateNetwork ci-dessus. Mais comment dois-je écrire la méthode RestoreWeights (et l'appeler) dans la bibliothèque ?
Merci de votre aide...