⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 buildnet.c

📁 各种神经网络C源代码。包括: CPN、BPN、ART1、ART2
💻 C
字号:
/**************************************************************************
 **************************************************************************
 ***									***
 ***				BUILDNET.C				***
 ***									***
 *** Notice:                                                            ***
 ***    This code is copyright (C) 1995 by David M. Skapura.  It may    ***
 ***    be used as is, or modified to suit the requirements of a  	***
 ***    specific application without permission of the author.		***
 ***    There are no royalty fees for use of this code, as long         ***
 ***    as the original author is credited as part of the final		***
 ***    work.  In exchange for this royalty free use, the user    	***
 ***    agrees that no guarantee or warantee is given or implied.	***
 ***									***
 ***									***
 **************************************************************************
 **************************************************************************


 The following functions are used to dynamically allocate a network 
 structure in memory.  The data structures used herein are defined 
 in NETSTRCT.H.


 **************************************************************************
 **************************************************************************/


#include <stdlib.h>
#include <stdarg.h>


int *define_layers (int layers, ...)
 {
  va_list argptr;
  int *l, i;

  l = (int *) calloc (layers+1, sizeof(int));
  l[0] = layers;

  va_start (argptr, layers);
  for (i=1; i<=layers; i++) l[i] = va_arg (argptr, int); 
  va_end (argptr);

  return (l);
 }



layer *build_layer (int units)
 {
  layer *l;

  l = (layer *) calloc (1, sizeof(layer));
  l->units = units;
  l->inputs = 0;
  l->modifier = 0.0;
  l->initval = 1.0;
  l->outputs = (float *) calloc (units, sizeof(float));
  l->connects = NULL;
  l->activation = (afn) linear;       /* default activation function */
  l->propto = (pfn) dot_product;      /* default propagation function */

  #ifdef BPN
   l->lastdelta = NULL;
   l->errors = (float *) calloc (units, sizeof(float));
   l->deriv = (afn) linear_derivative;
   l->eta = 0.5;
   l->alpha = 0.0;
  #endif

  return (l);
 }



void set_activation (layer *l, afn activation, float modifier)
 {
  l->activation = activation;
  l->modifier = modifier;

  #ifdef BPN
    if (activation == SIGMOID)  l->deriv = (afn) sigmoid_derivative;
    if (activation == LINEAR)   l->deriv = (afn) linear_derivative; 
  #endif
 }



void set_propagation (layer *l, pfn netx)
 {
  l->propto = netx;
  set_activation (l, LINEAR, 1.0);    /* reset to default */
 }



#ifdef BPN
 void set_eta (layer *l, float eta)
  {
   l->eta = eta;
  }


 void set_alpha (layer *l, float alpha)
  {
   l->alpha = alpha;
  }
#endif



void connect_layers (layer *inlayer, layer *tolayer)
 {
  register int i;

  tolayer->inputs = inlayer->units;
  tolayer->connects = (float **) calloc (tolayer->units, sizeof(float *));

  for (i=0; i<tolayer->units; i++)
   tolayer->connects[i] = (float *) calloc (tolayer->inputs, sizeof (float));

  #ifdef BPN
   tolayer->lastdelta = (float **) calloc (tolayer->units, sizeof(float *));

   for (i=0; i<tolayer->units; i++)
     tolayer->lastdelta[i] = (float *) calloc (tolayer->inputs, sizeof(float *));
  #endif
 }


void connect (layer *inlayer, layer *tolayer, int how, int init)
 {
  if (how == COMPLETE)
   {
     connect_layers (inlayer, tolayer);
     set_propagation (tolayer, DOT_PRODUCT);
   }
  else
   if (how == ONE_TO_ONE) set_propagation (tolayer, TRANSFER);

  set_weights (tolayer, init);
 }


layer **build_net (int layers, int *sizes)
 {
  layer **n;
  register int i;

  n = (layer **) calloc (layers, sizeof(layer *));

  for (i=0; i<layers; i++)
   n[i] = build_layer (sizes[i]);

  return (n);
 }



void destroy_net (int layers, layer **n)
 {
  int i, j;
  float *wts;
  layer *l;

  for (i=0; i<layers; i++)
   {
    l = n[i];
    if (l->outputs != NULL)  free (l->outputs);

    if (l->connects != NULL)
     for (j=0; j<l->units; j++)
      {
       wts = l->connects[j];
       if (wts != NULL)  free (wts);
      }

    #ifdef BPN
      if (l->lastdelta != NULL)
       for (j=0; j<l->units; j++)
	{
	 wts = l->lastdelta[j];
	 if (wts != NULL)  free (wts);
	}

      if (l->errors != NULL) free (l->errors);
    #endif

    free (l);
   }

  free (n);
 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -