Main Page   Data Structures   File List   Data Fields   Globals  

nn.h

Go to the documentation of this file.
00001 /*-
00002  * Copyright (c) 2002, 2003 Allan Saddi <allan@saddi.com>
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  *
00014  * THIS SOFTWARE IS PROVIDED BY ALLAN SADDI AND HIS CONTRIBUTORS ``AS IS''
00015  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00016  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00017  * ARE DISCLAIMED.  IN NO EVENT SHALL ALLAN SADDI OR HIS CONTRIBUTORS BE
00018  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00019  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00020  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00021  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00022  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00023  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00024  * POSSIBILITY OF SUCH DAMAGE.
00025  *
00026  * $Id: nn.h 325 2003-02-22 11:39:21Z asaddi $
00027  */
00028 
00029 #ifndef _APS_NN_H
00030 #define _APS_NN_H
00031 
00032 #include <stdio.h>
00033 
00038 struct _nnNode;
00039 
00043 typedef struct _nnLink {
00044   struct _nnNode *source; 
00045   double w, 
00046     del_w; 
00047 } nnLink;
00048 
00052 typedef struct _nnNode {
00053   unsigned long id; 
00055   double a; 
00056   double in; 
00058   double err; 
00059   double del; 
00061   int nlinks; 
00062   nnLink *links; 
00063 } nnNode;
00064 
00068 typedef struct _nnLayer {
00069   int nnodes; 
00070   nnNode *nodes; 
00071 } nnLayer;
00072 
00076 typedef struct _nnNet {
00077   double LearningRate; 
00078   double Momentum; 
00079   double (*ActivationF) (nnNode *node); 
00080   double (*dActivationF) (nnNode *node); 
00083   nnNode *biasNode; 
00085   int nlayers; 
00086   nnLayer *layers; 
00087 } nnNet;
00088 
00092 typedef enum _nnFileType {
00093   nnFileTypeUnknown, 
00094   nnFileTypeBinary, 
00095   nnFileTypeXML 
00096 } nnFileType;
00097 
00101 static inline nnLayer *
00102 nnGetNetworkOutputLayer (nnNet *net)
00103 {
00104   return &net->layers[0];
00105 }
00106 
00110 static inline nnLayer *
00111 nnGetNetworkInputLayer (nnNet *net)
00112 {
00113   return &net->layers[net->nlayers - 1];
00114 }
00115 
00116 /* Core */
00117 double nnSigmoid (nnNode *node);
00118 double nnDSigmoid (nnNode *node);
00119 void nnRunNetwork (nnNet *net);
00120 void nnSetNetworkInputs (nnNet *net, const double inputs[]);
00121 void nnGetNetworkOutputs (nnNet *net, double outputs[]);
00122 
00123 /* Back-propagation */
00124 void nnCalculateOutputLayerError (nnLayer *layer, const double targets[]);
00125 void nnCalculateHiddenLayerError (nnLayer *layer, nnLayer *nextLayer);
00126 void nnUpdateLayer (nnLayer *layer, nnNet *net);
00127 void nnUpdateNetwork (nnNet *net, const double targets[]);
00128 
00129 /* Batch-update back-propagation */
00130 void nnUpdateLayerBatch (nnLayer *layer, nnNet *net);
00131 void nnUpdateNetworkBatch (nnNet *net, double const targets[]);
00132 void nnClearNetworkWeightDeltas (nnNet *net);
00133 void nnUpdateNetworkWeights (nnNet *net);
00134 
00135 /* Network loading/saving */
00136 int nnSaveNetwork (nnNet *net, void *handle,
00137                    int (*nnWrite) (void *handle, void *data, size_t len));
00138 int nnSaveNetworkFile (nnNet *net, FILE *f);
00139 int nnSaveNetworkMem (nnNet *net, void *data, size_t len);
00140 int nnSaveNetworkDynMem (nnNet *net, void **ptr, size_t *len);
00141 nnNet *nnLoadNetwork (void *handle,
00142                       int (*nnRead) (void *handle, void *data, size_t len));
00143 nnNet *nnLoadNetworkFile (FILE *f);
00144 nnNet *nnLoadNetworkMem (void *data, size_t len);
00145 nnFileType nnGuessFileType (FILE *f);
00146 
00147 /* Network loading/saving (XML) */
00148 int nnSaveNetworkXML (nnNet *net, FILE *f);
00149 nnNet *nnLoadNetworkXML (FILE *f);
00150 
00151 /* Utility */
00152 double nnGetLayerError (nnLayer *layer);
00153 double nnGetLayerRMSError (nnLayer *layer);
00154 void nnIterateNetworkNodes (nnNet *net,
00155                             void (*f) (nnLayer *layer, nnNode *node,
00156                                        void *data),
00157                             void *data);
00158 void nnIterateNetworkLinks (nnNet *net,
00159                             void (*f) (nnNode *dest, nnLink *link,
00160                                        void *data),
00161                             void *data);
00162 void nnDestroyNetwork (nnNet *net);
00163 nnNet *nnCreateFullyConnectedNetwork (int nlayers, const int nnodes[]);
00164 nnNet *nnCopyNetwork (nnNet *net);
00165 
00166 #endif /* _APS_NN_H */

Generated on Sat Feb 22 03:39:25 2003 for nn by doxygen1.2.18