1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
/*
neurro.c
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*
* Part of: A program using neural networks.
*
* Author: E.BERTIN (IAP)
*
* Contents: run only version of the neural network.
*
* Last modify: 26/11/2003
*
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "define.h"
#include "globals.h"
#include "prefs.h"
#include "neurro.h"
brainstruct *brain;
/******************************** neurinit **********************************/
/*
Initialization of the "brain".
*/
void neurinit()
{
QMALLOC(brain, brainstruct, 1);
return;
}
/********************************* neurend **********************************/
/*
Close the "brain".
*/
void neurclose()
{
free(brain);
return;
}
/******************************** neurresp **********************************/
/*
Neural network response to an input vector.
*/
void neurresp(double *input, double *output)
{
int i, j, l, lastlay = brain->layersnb-1;
double neursum;
for (i=0; i<brain->nn[0]; i++)
brain->n[0][i] = input[i]*brain->inscale[i] + brain->inbias[i];
for (l=0; l<lastlay; l++)
for (j=0; j<brain->nn[l+1]; j++)
{
neursum = brain->b[l][j];
for (i=0; i<brain->nn[l]; i++)
neursum += brain->w[l][i][j] * brain->n[l][i];
brain->n[l+1][j] = f(neursum);
}
for (i=0; i<brain->nn[lastlay]; i++)
output[i] = (brain->n[lastlay][i]-brain->outbias[i])
/ brain->outscale[i];
return;
}
/************************************ f *************************************/
/*
Sigmoid function for a neural network.
*/
double f(double x)
{
return 1.0 / (1.0 + exp(-x));
}
/********************************* getnnw ***********************************/
/*
Read the NNW table that contains the weights.
*/
void getnnw()
{
FILE *infile;
int i, j, k, step;
char str[MAXCHAR], *sstr, *null;
if ((infile = fopen(prefs.nnw_name,"r")) == NULL)
error(EXIT_FAILURE,"*ERROR*: can't read ", prefs.nnw_name);
fgets(str, MAXCHAR, infile);
if (strncmp(str,"NNW",3))
error(EXIT_FAILURE, prefs.nnw_name, " is NOT a NNW table!");
step = 1;
i=j=0; /* To avoid gcc -Wall warnings */
while (fgets(str, MAXCHAR, infile))
{
sstr = &str[(int)strspn(str," \t")];
if (sstr[0]!=(char)'#' && sstr[0]!=(char)'\n')
{
null = sstr;
switch(step)
{
case 1: brain->layersnb = atoi(strtok(sstr, " \t\n"));
for (i=0; i<brain->layersnb; i++)
brain->nn[i] = atoi(strtok(NULL, " \t\n"));
step++;
break;
case 2: for (i=0; i<brain->nn[0]; i++)
{
brain->inbias[i] = atof(strtok(null, " \t\n"));
null = NULL;
}
step++;
break;
case 3: for (i=0; i<brain->nn[0]; i++)
{
brain->inscale[i] = atof(strtok(null, " \t\n"));
null = NULL;
}
i=j=0;
step++;
break;
case 4: if (j == brain->nn[i+1])
{
j = 0;
i++;
}
if (i < brain->layersnb-1)
{
for (k=0; k<brain->nn[i]; k++)
{
brain->w[i][k][j] = atof(strtok(null, " \t\n"));
null = NULL;
}
brain->b[i][j] = atof(strtok(NULL, " \t\n"));
j++;
break;
}
else
step++;
case 5: for (i=0; i<brain->nn[brain->layersnb-1]; i++)
{
brain->outbias[i] = atof(strtok(null, " \t\n"));
null = NULL;
}
step++;
break;
case 6: for (i=0; i<brain->nn[brain->layersnb-1]; i++)
{
brain->outscale[i] = atof(strtok(null, " \t\n"));
null = NULL;
}
step++;
break;
default:error(EXIT_FAILURE, "*Error*: inconsistency in ", prefs.nnw_name);
}
}
}
fclose(infile);
return;
}
|