| 1 | #cxx . . $ `<file>/FFNet/FFNet.h`/ifndef _FFNet_h_ |
| 2 | #define _FFNet_h_cxx . . $ `FFNet/FFNet.h:2:9`! |
| 3 | /* FFNet.h |
| 4 | * |
| 5 | * Copyright (C) 1997-2019 David Weenink |
| 6 | * |
| 7 | * This code is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or (at |
| 10 | * your option) any later version. |
| 11 | * |
| 12 | * This code is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | /* |
| 22 | djmw 19970515 |
| 23 | djmw 20020712 GPL header. |
| 24 | djmw 20031025 Added FFNet_getMinimum. |
| 25 | djmw 20040420 FFNet_create and Pattern&Categories: To FFNet changes. |
| 26 | djmw 20040422 FFNet_extractWeights added. |
| 27 | djmw 20040505 FFNet_getNodeNumberFromUnitNumber added. |
| 28 | djmw 20071024 Latest modification. |
| 29 | djmw 20080121 float -> double |
| 30 | */ |
| 31 | |
| 32 | #include "Data.h"cxx . . $ `<file>/sys/Data.h`/ |
| 33 | #include "Categories.h"cxx . . $ `<file>/dwtools/Categories.h`/ |
| 34 | #include "Minimizers.h"cxx . . $ `<file>/dwtools/Minimizers.h`/ |
| 35 | #include "PatternList.h"cxx . . $ `<file>/dwtools/PatternList.h`/ |
| 36 | #include "TableOfReal.h"cxx . . $ `<file>/stat/TableOfReal.h`/ |
| 37 | #include "melder.h"cxx . . $ `<file>/melder/melder.h`/ |
| 38 | |
| 39 | #include "FFNet_def.h"cxx . . $ `<file>/FFNet/FFNet_def.h`/ |
| 40 | |
| 41 | /* Parameters: |
| 42 | * integer numberOfLayers : the #layers in the net (exclusive the inputs) |
| 43 | * numberOfUnitsInLayer : array [1..numberOfLayers] the #units in each layer |
| 44 | * numberOfUnitsInLayer [numberOfLayers] :#outputs |
| 45 | * numberOfWeights : the total #weights in the net (inclusive bias) |
| 46 | * double *w : array [1..numberOfWeights] with the connection strengths |
| 47 | * *activity : array [1..numberOfNodes] with activities |
| 48 | * outputLabels : labels belonging to the outputs |
| 49 | * BOOKKEEPING: |
| 50 | * integer numberOfNodes: total #nodes: bias modelled as unit with constant activity) |
| 51 | * *isbias : array [1..numberOfNodes] set 1 if node is bias else 0 |
| 52 | * *nodeFirst : array [1..numberOfNodes] first node connected to this unit |
| 53 | * *nodeLast: : array [1..numberOfNodes] last node connected to this unit |
| 54 | * *wFirst : array [1..numberOfNodes] first index in *w for this unit |
| 55 | * *wLast : array [1..numberOfNodes] last (inclusive the bias) |
| 56 | * LEARNING: |
| 57 | * int *wSelected : array [1..numberOfWeights] weights selected for minimization |
| 58 | * double *deriv : array [1..numberOfNodes] derivative of nonlinearity at node |
| 59 | * *error : array [1..numberOfNodes] the error at node |
| 60 | * *dw : array [1..numberOfWeights] total derivative for weights |
| 61 | * *dwi : array [1..numberOfWeights] derivative per pattern |
| 62 | * integer dimension : dimension of minimizer space (<= my numberOfWeights) |
| 63 | * integer numberOfPatterns : the #patterns to be learned |
| 64 | * MAT inputPattern: matrix [1..numberOfPatterns] [1..numberOfInputs] |
| 65 | * MAT targetActivation: matrix [1..numberOfPatterns] [1..numberOfOutputs] |
| 66 | * double accumulatedCost : accumulated costs of testing/training with patterns |
| 67 | * |
| 68 | * A network consists of numberOfLayers layers. Layer numbering is from 1...numberOfLayers. |
| 69 | * The highest numbered layer is the output layer |
| 70 | * (numberOfLayers <= 4) |
| 71 | * Each layer consists of a number of units. The biases of all the units in a layer |
| 72 | * are modelled with connections to an extra unit in the lower layer (with constant |
| 73 | * activity 1.0). Nodes refers to 'units' + 'bias units'. |
| 74 | * The variable 'numberOfNodes' is the total number of nodes (inclusive bias nodes). |
| 75 | * E.g. the topology (2,3,4), i.e., 2 inputs, 3 units in the first layer |
| 76 | * and 4 units in the second layer (outputs) is modelled |
| 77 | * with (2+1)+ (3+1)+ (4) = 11 nodes. |
| 78 | * The numbering of the weights is as follows (indices 1..numberOfWeights): |
| 79 | * E.g., topology (I,H,O) (I inputs, H hidden units and O output units) |
| 80 | * There are a total of H* (I+1) + O* (H+1) weights in this net. |
| 81 | * w [1] - w [I] : I (1)->H (1), I (2)->H (1) ... I (I)->H (1) |
| 82 | * w [I+1] : bias->H (1) |
| 83 | * w [ (I+1)+1] - w [2 (I+1)-1] : I (1)->H (2), I (2)->H (2) ... I (I)->H (2) |
| 84 | * w [2 (I+1)] : bias->H (2) |
| 85 | * ... |
| 86 | * w [ (H-1) (I+1)+1] - w [H (I+1)-1] : I (1)->H (H), I (2)->H (H) ... I (I)->H (H) |
| 87 | * w [H (I+1)] : bias->H (H) |
| 88 | * M = H (I+1) |
| 89 | * w [M+1] - w [M+H] : H (1)->O (1), H (2)->O (1) ... H (H)->O (1) |
| 90 | * w [M+H+1] : bias->O (1) |
| 91 | * w [M+ (H+1)+1] - w (M+2 (H+1)-1) : H (1)->O (2), H (2)->O (2) ... H (H)->O (2) |
| 92 | * w [M+2 (H+1)] : bias->O (1) |
| 93 | * ... |
| 94 | * w [M+ (O-1)* (H+1)+1] - w [M+O (H+1)-1] : H (1)->O (O), H (2)->O (O) ... H (H)->O (O) |
| 95 | * w [m+o (h+1)-1] : bias->O (O) |
| 96 | * |
| 97 | * Internals: |
| 98 | * |
| 99 | * A number of auxiliary arrays for efficient calculations have been setup. |
| 100 | * For a node k we need to know: |
| 101 | * 1. isbias [1..numberOfNodes] : usage: if (isbias [k]) ... |
| 102 | * true if node k is a bias node. There are numberOfLayers bias nodes |
| 103 | * 2. nodeFirst [1..numberOfNodes] : usage is j=nodeFirst [k]; |
| 104 | * j is the first node that is connected to k . |
| 105 | * 3. nodeLast [1..numberOfNodes] : usage is j=nodeLast [k] |
| 106 | * j is the last node that is connected to k (bias included). |
| 107 | * For the calculation of the errors, during learning, in unit k we need to |
| 108 | * know which weights from the preceding layer connect to it. |
| 109 | * 4. wFirst [1..numberOfNodes] : usage j=wFirst [k] |
| 110 | * w [j] is first weight to node k. |
| 111 | * 5. wLast [1..numberOfNodes] : usage j=wLast [k] |
| 112 | * w [j] is last weight to node k. |
| 113 | */ |
| 114 | |
| 115 | /* FFNet::copy |
| 116 | * copy everything except minimizer, patterns and inputs. |
| 117 | */ |
| 118 | |
| 119 | void FFNet_initcxx . . $ FFNet_init(6d7aa47a91347af3). (FFNetcxx . . $ FFNet# me, integercxx . . $ integer# numberOfInputs, integercxx . . $ integer# nodesInLayer1, integercxx . . $ integer# nodesInLayer2, |
| 120 | integercxx . . $ integer# numberOfOutputs, bool outputsAreLinear); |
| 121 | |
| 122 | autoFFNetcxx . . $ autoFFNet# FFNet_createcxx . . $ FFNet_create(b4b9df7d991baee1). (integercxx . . $ integer# numberOfInputs, integercxx . . $ integer# numberInLayer1, integercxx . . $ integer# numberInLayer2, |
| 123 | integercxx . . $ integer# numberOfOutputs, bool outputsAreLinear); |
| 124 | |
| 125 | autostring32cxx . . $ autostring32# FFNet_createNameFromTopologycxx . . $ FFNet_createNameFromTopology(fbdb4672e9e45ea3). (FFNetcxx . . $ FFNet# me); |
| 126 | /* Create names as <inputs>-<outputs>, <inputs>-<hidden>-<outputs>, |
| 127 | <inputs>-<hidden1>-<hidden2>-<outputs> for 1, 2 or 3 layer networks. |
| 128 | */ |
| 129 | |
| 130 | #define FFNet_COST_MSEcxx . . $ `FFNet/FFNet.h:130:9`! 1 |
| 131 | |
| 132 | void FFNet_setCostFunctioncxx . . $ FFNet_setCostFunction(dfcbcca279925c1d). (FFNetcxx . . $ FFNet# me, int type); |
| 133 | |
| 134 | #define FFNet_NONLIN_SIGMOIDcxx . . $ `FFNet/FFNet.h:134:9`! 1 |
| 135 | |
| 136 | void FFNet_setNonLinearitycxx . . $ FFNet_setNonLinearity(dfcbcca279925c1d). (FFNetcxx . . $ FFNet# me, int type); |
| 137 | |
| 138 | void FFNet_setOutputCategoriescxx . . $ FFNet_setOutputCategories(2745fb3fa73df096). (FFNetcxx . . $ FFNet# me, Categoriescxx . . $ Categories# thee); |
| 139 | |
| 140 | double FFNet_getBiascxx . . $ FFNet_getBias(817895fdeae8b5e8). (FFNetcxx . . $ FFNet# me, integercxx . . $ integer# layer, integercxx . . $ integer# unit); |
| 141 | |
| 142 | void FFNet_setBiascxx . . $ FFNet_setBias(34542abea2e9fe0). (FFNetcxx . . $ FFNet# me, integercxx . . $ integer# layer, integercxx . . $ integer# node, double value); |
| 143 | |
| 144 | void FFNet_setWeightcxx . . $ FFNet_setWeight(d5a0143623e7172a). (FFNetcxx . . $ FFNet# me, integercxx . . $ integer# later, integercxx . . $ integer# node, integercxx . . $ integer# node_from, double value); |
| 145 | |
| 146 | double FFNet_getWeightcxx . . $ FFNet_getWeight(90cd55f92d15b08c). (FFNetcxx . . $ FFNet# me, integercxx . . $ integer# later, integercxx . . $ integer# node, integercxx . . $ integer# node_from); |
| 147 | |
| 148 | void FFNet_resetcxx . . $ FFNet_reset(18677638c4811baa). (FFNetcxx . . $ FFNet# me, double wrange); |
| 149 | /* reset the neural net: |
| 150 | * initialize all (selected) weights and biases with random numbers from the |
| 151 | * interval (-wrange, wrange). |
| 152 | * forget links with minimizer. |
| 153 | */ |
| 154 | |
| 155 | const char32cxx . . $ char32#* FFNet_getCategoryOfOutputUnitcxx . . $ FFNet_getCategoryOfOutputUnit(6eeb6b2418f592b4). (FFNetcxx . . $ FFNet# me, integercxx . . $ integer# outputUnit); |
| 156 | |
| 157 | integercxx . . $ integer# FFNet_getOutputUnitOfCategorycxx . . $ FFNet_getOutputUnitOfCategory(fcf66cc57df20487). (FFNetcxx . . $ FFNet# me, const char32cxx . . $ char32#* category); |
| 158 | |
| 159 | void FFNet_propagateToLayercxx . . $ FFNet_propagateToLayer(7c404e9ee83353d4). (FFNetcxx . . $ FFNet# me, constVECcxx . . $ constVEC# input, VECcxx . . $ VEC# activity, integercxx . . $ integer# layer); |
| 160 | /* propagate the input through the net to layer and calculate the activities */ |
| 161 | |
| 162 | void FFNet_propagatecxx . . $ FFNet_propagate(66412d333bbbfb3b). (FFNetcxx . . $ FFNet# me, constVECcxx . . $ constVEC# input, autoVECcxx . . $ autoVEC# *output); |
| 163 | /* step (1) feed forward input from "input layer" to "output layer" |
| 164 | * if output != nullptr the output activity is copied into output. |
| 165 | * postcondition: my activities defined |
| 166 | */ |
| 167 | |
| 168 | double FFNet_computeErrorcxx . . $ FFNet_computeError(91ccb74903e539f). (FFNetcxx . . $ FFNet# me, constVECcxx . . $ constVEC# target); |
| 169 | /* step (2) calculate error on output nodes w.r.t. desired output */ |
| 170 | /* step (3) backpropagate this error to previous nodes */ |
| 171 | /* precondition: step (1) */ |
| 172 | |
| 173 | void FFNet_computeDerivativecxx . . $ FFNet_computeDerivative(c48706d70006b0b5). (FFNetcxx . . $ FFNet# me); |
| 174 | /* step (4) compute derivative in my dwi */ |
| 175 | /* Precondition: step (3) */ |
| 176 | |
| 177 | integercxx . . $ integer# FFNet_getWinningUnitcxx . . $ FFNet_getWinningUnit(a1e752f0d53ae155). (FFNetcxx . . $ FFNet# me, integercxx . . $ integer# labeling); |
| 178 | /* labeling = 1 : winner-takes-all */ |
| 179 | /* labeling = 2 : stochastic */ |
| 180 | |
| 181 | void FFNet_selectAllWeightscxx . . $ FFNet_selectAllWeights(c48706d70006b0b5). (FFNetcxx . . $ FFNet# me); |
| 182 | |
| 183 | void FFNet_selectBiasesInLayercxx . . $ FFNet_selectBiasesInLayer(bdd36ac37469be2a). (FFNetcxx . . $ FFNet# me, integercxx . . $ integer# layer); |
| 184 | |
| 185 | integercxx . . $ integer# FFNet_dimensionOfSearchSpacecxx . . $ FFNet_dimensionOfSearchSpace(9ff8c3af4e0e2df3). (FFNetcxx . . $ FFNet# me); |
| 186 | /* count the selected weights */ |
| 187 | |
| 188 | integercxx . . $ integer# FFNet_getNumberOfWeightscxx . . $ FFNet_getNumberOfWeights(9ff8c3af4e0e2df3). (FFNetcxx . . $ FFNet# me); |
| 189 | /* return my numberOfWeights */ |
| 190 | |
| 191 | void FFNet_weightConnectsUnitscxx . . $ FFNet_weightConnectsUnits(77e1e87e3a9f9a4c). (FFNetcxx . . $ FFNet# me, integercxx . . $ integer# index, integercxx . . $ integer# *out_fromUnit, integercxx . . $ integer# *out_toUnit, integercxx . . $ integer# *out_layer); |
| 192 | /* |
| 193 | * w [index] connects unit fromUnit in "layer-1" with unit toUnit in "layer". |
| 194 | * fromUnit returns 0 then w [index] is bias. |
| 195 | */ |
| 196 | |
| 197 | integercxx . . $ integer# FFNet_getNodeNumberFromUnitNumbercxx . . $ FFNet_getNodeNumberFromUnitNumber(5612a7baadb7dc89). (FFNetcxx . . $ FFNet# me, integercxx . . $ integer# unit, integercxx . . $ integer# layer); |
| 198 | |
| 199 | integercxx . . $ integer# FFNet_getNumberOfLayerscxx . . $ FFNet_getNumberOfLayers(9ff8c3af4e0e2df3). (FFNetcxx . . $ FFNet# me); |
| 200 | |
| 201 | integercxx . . $ integer# FFNet_getNumberOfUnitscxx . . $ FFNet_getNumberOfUnits(9ff8c3af4e0e2df3). (FFNetcxx . . $ FFNet# me); |
| 202 | |
| 203 | integercxx . . $ integer# FFNet_getNumberOfHiddenumberOfLayerscxx . . $ FFNet_getNumberOfHiddenumberOfLayers(9ff8c3af4e0e2df3). (FFNetcxx . . $ FFNet# me); |
| 204 | |
| 205 | integercxx . . $ integer# FFNet_getNumberOfUnitsInLayercxx . . $ FFNet_getNumberOfUnitsInLayer(a1e752f0d53ae155). (FFNetcxx . . $ FFNet# me, integercxx . . $ integer# layer); |
| 206 | |
| 207 | double FFNet_getMinimumcxx . . $ FFNet_getMinimum(c565b63288669986). (FFNetcxx . . $ FFNet# me); |
| 208 | |
| 209 | void FFNet_drawTopologycxx . . $ FFNet_drawTopology(1f8f5a95f8d14e97). (FFNetcxx . . $ FFNet# me, Graphicscxx . . $ Graphics# g); |
| 210 | |
| 211 | void FFNet_drawActivationcxx . . $ FFNet_drawActivation(1f8f5a95f8d14e97). (FFNetcxx . . $ FFNet# me, Graphicscxx . . $ Graphics# g); |
| 212 | |
| 213 | void FFNet_drawWeightsToLayercxx . . $ FFNet_drawWeightsToLayer(2f448188820de6aa). (FFNetcxx . . $ FFNet# me, Graphicscxx . . $ Graphics# g, integercxx . . $ integer# toLayer, integercxx . . $ integer# scaling, bool garnish); |
| 214 | /* Deprecated: the strengths of the weights that connect to the nodes in later 'layer' */ |
| 215 | /* are drawn with boxes. The area of each box corresponds to the strength. */ |
| 216 | /* Black boxes have negative strength? */ |
| 217 | |
| 218 | void FFNet_drawCostHistorycxx . . $ FFNet_drawCostHistory(1f22b713dfef5782). (FFNetcxx . . $ FFNet# me, Graphicscxx . . $ Graphics# g, integercxx . . $ integer# from_iteration, integercxx . . $ integer# to_iteration, |
| 219 | double from_cost, double to_cost, bool garnish); |
| 220 | /* draw cost vs epochs */ |
| 221 | |
| 222 | autoCollectioncxx . . $ autoCollection# FFNet_createIrisExamplecxx . . $ FFNet_createIrisExample(19d59450daf2638f). (integercxx . . $ integer# numberOfHidden1, integercxx . . $ integer# numberOfHidden2); |
| 223 | |
| 224 | autoTableOfRealcxx . . $ autoTableOfReal# FFNet_extractWeightscxx . . $ FFNet_extractWeights(94e04f3805c099a). (FFNetcxx . . $ FFNet# me, integercxx . . $ integer# layer); |
| 225 | |
| 226 | void FFNet_drawWeightscxx . . $ FFNet_drawWeights(997d05aa38f4b38). (FFNetcxx . . $ FFNet# me, Graphicscxx . . $ Graphics# g, integercxx . . $ integer# layer, bool garnish); |
| 227 | |
| 228 | autoFFNetcxx . . $ autoFFNet# FFNet_TabelOfReal_to_FFNetcxx . . $ FFNet_TabelOfReal_to_FFNet(3706ba214b605693). (FFNetcxx . . $ FFNet# me, TableOfRealcxx . . $ TableOfReal# him, integercxx . . $ integer# layer); |
| 229 | |
| 230 | autoFFNetcxx . . $ autoFFNet# PatternList_Categories_to_FFNetcxx . . $ PatternList_Categories_to_FFNet(b83e1b8b7bb92144). (PatternListcxx . . $ PatternList# me, Categoriescxx . . $ Categories# you, integercxx . . $ integer# numberOfUnits1, integercxx . . $ integer# numberOfUnits2); |
| 231 | |
| 232 | #endif /* _FFNet_h_ */ |