Buscando el código fuente C++ del algoritmo de red neuronal de BP

// AnnBP.cpp: implementación de la clase CAnnBP

//

///////////////. /////////////////////////////////////////////////// //// /////

#include "StdAfx.h"

#include "AnnBP.h"

#include "math.h "

/////////////////////////////////////////// /////// /////////////////////////

// Construcción/Destrucción

/////// /////////////////////////////////////////// //////////// /////////////

CAnnBP::CAnnBP()

{

eta1=0.3;

impulso1=0.3

}

CAnnBP::~CAnnBP()

{

}

double CAnnBP::drnd()

{

return ((double) rand() / (double) BIGRND);

}

/*** Devuelve un número aleatorio de doble precisión entre -1,0 y 1,0***/

double CAnnBP::dpn1()

{

return (doble) (rand())/(32767/2)-1

}

/***; Función de acción, actualmente una función en forma de S*** /

double CAnnBP::squash(double x)

{

return (1.0 / (1.0 + exp(-x)));

}

/*** Solicitar matriz de números reales unidimensionales de doble precisión***/

doble* CAnnBP::alloc_1d_dbl(int n)

{

doble *nuevo1

nuevo1 = (doble *) malloc ((sin firmar) (n * sizeof (doble)));

if ( new1 == NULL) {

AfxMessageBox("ALLOC_1D_DBL: No se pudo asignar una matriz de dobles\n");

return (NULL);

}

return (new1);

}

/*** Solicitar un Matriz de números reales bidimensionales de doble precisión***/

double ** CAnnBP::alloc_2d_dbl(int m, int n)

{

int i;

double **new1;

new1 = (double **) malloc ((unsigned) (m * sizeof (double *))); (new1 == NULL) {

AfxMessageBox("ALLOC_2D_DBL: No se pudo asignar la matriz de ptrs dbl\n");

return (NULL); > }

for (i = 0; i < m; i++) {

new1[i] = alloc_1d_dbl(n);

}

return (new1);

}

/*** Pesos de inicialización aleatorios***/

void CAnnBP::bpnn_randomize_weights(double * *w, int m, int n)

{

int i, j

for (i = 0; i <= m; i++) {

for (j = 0; j <= n; j++) {

w[i][j] = dpn1()

}

}

}

/*** 0 pesos de inicialización***/

void CAnnBP::bpnn_zero_weights(double ** w, int m, int n)

{

int i, j

for (i = 0; i <= m; i++) {

for (j = 0; j <= n; j++) {

w[i][j] = 0.0

}

}

}

/*** Establecer semilla de número aleatorio***/

void CAnnBP::bpnn_initialize(int seed)

{

CString msg,s;

msg="Semilla del generador de números aleatorios:"

s.Format("%d",seed );

AfxMessageBox(msg+s);

srand(seed);

}

/*** Crear red BP ** */

BPNN* CAnnBP::bpnn_internal_create(int n_in, int n_hidden, int n_out)

{

BPNN *newnet

newnet = (BPNN *) malloc (tamaño de (BPNN));

if (n)

ewnet == NULL) {

printf("BPNN_CREATE: No se pudo asignar la red neuronal\n");

return (NULL); /p>

newnet->input_n = n_in;

newnet->hidden_n = n_hidden

newnet->output_n = n_out; >input_units = alloc_1d_dbl(n_in + 1);

newnet->hidden_units = alloc_1d_dbl(n_out + 1);

newnet->output_units = alloc_1d_dbl(n_out + 1); p>

p>

newnet->hidden_delta = alloc_1d_dbl(n_hidden + 1);

newnet->output_delta = alloc_1d_dbl(n_out + 1); >objetivo = alloc_1d_dbl( n_out + 1);

newnet->input_weights = alloc_2d_dbl(n_in + 1, n_hidden + 1);

newnet->hidden_weights = alloc_2d_dbl(n_hidden + 1); , n_out + 1)

newnet->input_prev_weights = alloc_2d_dbl(n_in + 1, n_hidden + 1);

newnet->hidden_prev_weights = alloc_2d_dbl(n_hidden + 1, n_out + 1 );

return (newnet);

}

/* Libera el espacio de memoria ocupado por la red BP*/

void. CAnnBP::bpnn_free(BPNN *net)

{

int n1, n2, i

n1 = net->input_n; p> n2 = net- >hidden_n;

free((char *) net->input_units);

free((char *) net->hidden_units); >

free( (char *) net->output_units);

free((char *) net->hidden_delta

free((char *) net-); >output_delta);

p>

gratis((char *) net->target

);

for (i = 0; i <= n1; i++) {

free((char *) net->input_weights[i]);

free((char *) net->input_weights[i]); ->input_prev_weights[i]);

}

libre((char *) neto->input_weights

libre((char *) neto-); >input_prev_weights);

for (i = 0; i <= n2; i++) {

free((char *) net->hidden_weights[i]); >

gratis((char *) net->hidden_prev_weights[i]);

}

gratis((char *) net->hidden_weights

);

gratis((char *) net->hidden_prev_weights);

gratis((char *) net

}

/***); Cree una red BP e inicialice los pesos***/

BPNN* CAnnBP::bpnn_create(int n_in, int n_hidden, int n_out)

{

BPNN *newnet;

newnet = bpnn_internal_create(n_in, n_hidden, n_out);

#ifdef INITZERO

bpnn_zero_weights(newnet->input_weights, n_in, n_hidden )

#else

bpnn_randomize_weights(newnet->input_weights, n_in, n_hidden

#endif

bpnn_randomize_weights(newnet->); Hidden_weights, n_hidden, n_out);

bpnn_zero_weights(newnet->input_prev_weights, n_in, n_hidden);

bpnn_zero_weights(newnet->hidden_prev_weights, n_hidden, n_out); p> return (newnet);

}

void CAnnBP::bpnn_layerforward(double *l1, double *l2, double **conn, int n1, int n2)

p>

{

doble suma

int j, k

/*** Establecer umbral***/

>

l1[0] = 1.0;

/*** Para cada neurona en la segunda capa***/

for (j = 1; j < = n2 ; j++) {

/*** Calcular la suma ponderada de las entradas***/

sum =

for (k = 0; k <= n1; k++) {

suma += conn[k][j] * l1[k]; squash(suma);

}

}

/* Error de salida*/

void CAnnBP::bpnn_output_error(double * delta , doble *objetivo, doble *salida, int nj, doble *err)

{

int j

double o, t, errsum

; p>

suma de error = 0.0;

for (j = 1; j <= nj; j++) {

o = salida[j]; > t = objetivo[j];

delta[j] = o * (1.0 - o) * (t - o )

suma de error += ABS(delta[j ]

}

*err = errsum;

}

/* Error de capa oculta*/

void CAnnBP::bpnn_hidden_error(doble *delta_h, int nh, doble *delta_o, int no, doble **quién, doble *oculto, doble *err)

{

int j, k;

doble h, suma, suma de error;

suma de error = 0,0

para (j = 1; j <= nh; j++ ) {

h = oculto[j];

suma = 0.0

for (k = 1; k <= no; k++) {

suma = 0.0;

p>

suma += delta_o[k] * quién[j][k]

}

delta_h[j] = h * (1.0 - h) * suma ;

errsum += ABS(delta_h[j]);

}

*err = errsum;

}

/* Ajustar peso*/

void CAnnBP::bpnn_adjust_we

derechos(doble *delta, int ndelta, doble *ly, int nly, doble **w, doble **oldw, doble eta, doble impulso)

{

double new_dw;

int k, j;

ly[0] = 1.0;

for (j = 1; j <= ndelta; j++) {

for (k = 0; k <= nly; k++) {

new_dw = ((eta * delta[j] * ly[k]) + (momentum * oldw[k][ j]));

w[k][j] += nuevo_dw;

antiguo[k][j] = nuevo_dw

}

p>

}

}

/* Realizar operación de avance*/

void CAnnBP::bpnn_feedforward(BPNN *net)

{

int dentro, oculto, fuera

in = net->input_n

hid = net->hidden_n;

out = net->output_n;

/*** Activaciones de entrada de avance ***/

bpnn_layerforward(net->input_units, net-. >unidades_ocultas,

net->input_weights, in, hid

bpnn_layerforward(net->hidden_units, net->output_units,

net->hidden_weights , escondido, fuera );

}

/* Tren red BP*/

void CAnnBP::bpnn_train(BPNN *net, doble eta, doble impulso , doble *eo , doble *eh)

{

int in, hid, out

double out_err, hid_err

in = net- >input_n;

hid = net->hidden_n;

out = net->output_n

/*** Activación de entrada directa* **/

bpnn_layerforward(net->input_units, net->hidden_units,

net->input_weights, en

, escondido);

bpnn_layerforward(net->hidden_units, net->output_units,

net->hidden_weights, hid, out

/**); * Calcular errores de capa oculta y capa de salida***/

bpnn_output_error(net->output_delta, net->target, net->output_units,

out, &out_err < /); p>

bpnn_hidden_error(net->hidden_delta, hid, net->output_delta, out,

net->hidden_weights, net->hidden_units, &hid_err

* eo); = out_err;

*eh = hid_err;

/*** Ajustar los pesos de la capa de entrada y la capa oculta***/

bpnn_adjust_weights(net ->output_delta , out, net->hidden_units, hid,

net->hidden_weights, net->hidden_prev_weights, eta, impulse

bpnn_adjust_weights(net->hidden_delta, hid, net-); >input_units, in,

net->input_weights, net->input_prev_weights, eta, impulse

}

/* Guardar red BP */ <); /p>

void CAnnBP::bpnn_save(BPNN *net, char *nombre de archivo)

{

Archivo CFile

char * mem; /p>

int n1, n2, n3, i, j, memcnt;

doble valor, **w

n1 = net->input_n; ->hidden_n; n3 = net->output_n;

printf("Guardando %dx%dx%d red en '%s'\n", n1, n2, n3, nombre de archivo

intente

{

file.Open(filename,CFile::modeWrite|CFile::modeCreate|CFile::modeNoTruncate

}

catch(CFileException* e)

{

e->R

eportError();

e->Delete();

}

archivo.Write(&n1,sizeof(int)); p> archivo.Write(&n2,sizeof(int));

file.Write(&n3,sizeof(int)); > w = net->input_weights;

mem = (char *) malloc ((unsigned) ((n1+1) * (n2+1) * sizeof(double)));

// mem = (char *) malloc (((n1+1) * (n2+1) * sizeof(double))); for (i = 0; i <= n1); ; i++) {

for (j = 0; j <= n2; j++) {

dvalue = w[i][j]; /fastcopy(&mem[memcnt], &dvalue, tamaño de(doble));

fastcopy(&mem[memcnt], &dvalue, tamaño de(doble));

memcnt += tamaño de( doble);

}

}

archivo.Write(mem,sizeof(double)*(n1+1)*(n2+1));

libre(mem);

memcnt = 0;

w = net->hidden_weights

mem = (char *) malloc ((unsigned) ((n2+1) * (n3+1) * sizeof(double)));

// mem = (char *) malloc (((n2+1) * ( n3+1) * tamaño de (doble)));

para (i = 0; i <= n2; i++) {

para (j = 0; j <= n3 ; j++) {

dvalue = w[i][j];

fastcopy(&mem[memcnt], &dvalue, sizeof(double)); // fastcopy(&mem[memcnt], &dvalue, tamaño de(doble));

memcnt += tamaño de

}

}

p>

archivo.Escribir(mem, (n2+1

) * (n3+1) * tamaño de (doble));

// free(mem);

file.Close();

}

/* Leer la red BP desde el archivo*/

BPNN* CAnnBP::bpnn_read(char *nombre de archivo)

{

char *mem;

BPNN *new1

int n1, n2, n3, i, j, memcnt

CFile; file;

intente

{

file.Open(filename,CFile::modeRead|CFile::modeCreate|CFile::modeNoTruncate < /p); >

}

catch(CFileException* e)

{

e->ReportError()

e ->; Eliminar();

}

// printf("Leyendo '%s'\n", nombre de archivo);// fflush(stdout); archivo.Read(&n1, tamaño de(int));

archivo.Read(&n2, tamaño de(int));

new1 = bpnn_internal_create(n1, n2, n3);

// printf("'%s' contiene una red %dx%dx%d\n", nombre de archivo, n1); , n2, n3);

// printf("Leyendo pesos de entrada..."); // fflush(stdout);

memcnt = 0

<; p> mem = (char *) malloc (((n1+1) * (n2+1) * tamaño de (doble)));

file.Read(mem, ((n1+1 )* (n2+1))*tamañode(doble));

para (i = 0; i <= n1; i++) {

para (j = 0; j <= n2; j++) {

//fastcopy(&(new1->input_weights[i][j]), &mem[memcnt], sizeof(double));

fastcopy( &(new1->input_weights[i][j]), &mem[memcnt], sizeof(double));

memcnt += sizeof(double);

}

}

free(mem);

// printf("Listo\nLeyendo pesos ocultos...") ; //fflush(stdout);

memcnt = 0;

mem = (char *) malloc (((n2+1) * (n3+1) * sizeof(double) )));

file.Read(mem, (n2+1) * (n3+1) * sizeof(double)); = n2; i++) {

for (j = 0; j <= n3; j++) {

//fastcopy(&(new1->hidden_weights[i][j] ), &mem[memcnt], tamaño de (doble));

fastcopy(&(new1->hidden_weights[i][j]), &mem[memcnt], tamaño de (doble)); >

memcnt += tamaño de(doble);

}

}

libre(mem);

archivo.Cerrar; ();

printf("Listo\n"); //fflush(stdout);

bpnn_zero_weights(new1->input_prev_weights, n1, n2); p> bpnn_zero_weights(new1->hidden_prev_weights, n2, n3);

return (new1);

}

void CAnnBP::CreateBP(int n_in, int n_hidden, int n_out)

{

net=bpnn_create(n_in,n_hidden,n_out);

}

void CAnnBP: :FreeBP()

{

bpnn_free(net);

}

void CAnnBP::Train(double *input_unit,int input_num, doble *objetivo,int target_num, doble *eo, doble *eh)

{

for(int i=1;i<=input_num;i++)

{

net->input_units[i]=input_unit[i-1];

}

for(int j=1;j< =núm_objetivo;j++)

{

net->target[j]=target[j-1];

}

bpnn_train(net,eta1,momentum1,eo,eh);

}

void CAnnBP::Identify(double *input_unit,int input_num,double *target,int target_num)

{

for(int i=1;i<=input_num;i++)

{

net->input_units[i]=input_unit[i-1]; > }

bpnn_feedforward(net);

for(int j=1;j<=target_num;j++)

{

target[j-1]=net->output_units[j];

}

}

void CAnnBP::Save(char *nombre de archivo)

p>

{

bpnn_save(net,nombredearchivo);

}

void CAnnBP::Read(char *nombre de archivo)

{

net=bpnn_read(filename);

}

void CAnnBP::SetBParm(doble eta, doble impulso)

{

eta1=eta;

momento1=momento;

}

void CAnnBP::Initialize( int semilla)

p>

{

bpnn_initialize(semilla

}

);