| 1 | #ifndef _FFNet_h_ |
| 2 | #define _FFNet_h_ |
| 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" |
| 33 | #include "Categories.h" |
| 34 | #include "Minimizers.h" |
| 35 | #include "PatternList.h" |
| 36 | #include "TableOfReal.h" |
| 37 | #include "melder.h" |
| 38 | |
| 39 | #include "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_init (FFNet me, integer numberOfInputs, integer nodesInLayer1, integer nodesInLayer2, |
| 120 | integer numberOfOutputs, bool outputsAreLinear); |
| 121 | |
| 122 | autoFFNet FFNet_create (integer numberOfInputs, integer numberInLayer1, integer numberInLayer2, |
| 123 | integer numberOfOutputs, bool outputsAreLinear); |
| 124 | |
| 125 | autostring32 FFNet_createNameFromTopology (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_MSE 1 |
| 131 | |
| 132 | void FFNet_setCostFunction (FFNet me, int type); |
| 133 | |
| 134 | #define FFNet_NONLIN_SIGMOID 1 |
| 135 | |
| 136 | void FFNet_setNonLinearity (FFNet me, int type); |
| 137 | |
| 138 | void FFNet_setOutputCategories (FFNet me, Categories thee); |
| 139 | |
| 140 | double FFNet_getBias (FFNet me, integer layer, integer unit); |
| 141 | |
| 142 | void FFNet_setBias (FFNet me, integer layer, integer node, double value); |
| 143 | |
| 144 | void FFNet_setWeight (FFNet me, integer later, integer node, integer node_from, double value); |
| 145 | |
| 146 | double FFNet_getWeight (FFNet me, integer later, integer node, integer node_from); |
| 147 | |
| 148 | void FFNet_reset (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 char32* FFNet_getCategoryOfOutputUnit (FFNet me, integer outputUnit); |
| 156 | |
| 157 | integer FFNet_getOutputUnitOfCategory (FFNet me, const char32* category); |
| 158 | |
| 159 | void FFNet_propagateToLayer (FFNet me, constVEC input, VEC activity, integer layer); |
| 160 | /* propagate the input through the net to layer and calculate the activities */ |
| 161 | |
| 162 | void FFNet_propagate (FFNet me, constVEC input, 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_computeError (FFNet me, 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_computeDerivative (FFNet me); |
| 174 | /* step (4) compute derivative in my dwi */ |
| 175 | /* Precondition: step (3) */ |
| 176 | |
| 177 | integer FFNet_getWinningUnit (FFNet me, integer labeling); |
| 178 | /* labeling = 1 : winner-takes-all */ |
| 179 | /* labeling = 2 : stochastic */ |
| 180 | |
| 181 | void FFNet_selectAllWeights (FFNet me); |
| 182 | |
| 183 | void FFNet_selectBiasesInLayer (FFNet me, integer layer); |
| 184 | |
| 185 | integer FFNet_dimensionOfSearchSpace (FFNet me); |
| 186 | /* count the selected weights */ |
| 187 | |
| 188 | integer FFNet_getNumberOfWeights (FFNet me); |
| 189 | /* return my numberOfWeights */ |
| 190 | |
| 191 | void FFNet_weightConnectsUnits (FFNet me, integer index, integer *out_fromUnit, integer *out_toUnit, 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 | integer FFNet_getNodeNumberFromUnitNumber (FFNet me, integer unit, integer layer); |
| 198 | |
| 199 | integer FFNet_getNumberOfLayers (FFNet me); |
| 200 | |
| 201 | integer FFNet_getNumberOfUnits (FFNet me); |
| 202 | |
| 203 | integer FFNet_getNumberOfHiddenumberOfLayers (FFNet me); |
| 204 | |
| 205 | integer FFNet_getNumberOfUnitsInLayer (FFNet me, integer layer); |
| 206 | |
| 207 | double FFNet_getMinimum (FFNet me); |
| 208 | |
| 209 | void FFNet_drawTopology (FFNet me, Graphics g); |
| 210 | |
| 211 | void FFNet_drawActivation (FFNet me, Graphics g); |
| 212 | |
| 213 | void FFNet_drawWeightsToLayer (FFNet me, Graphics g, integer toLayer, 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_drawCostHistory (FFNet me, Graphics g, integer from_iteration, integer to_iteration, |
| 219 | double from_cost, double to_cost, bool garnish); |
| 220 | /* draw cost vs epochs */ |
| 221 | |
| 222 | autoCollection FFNet_createIrisExample (integer numberOfHidden1, integer numberOfHidden2); |
| 223 | |
| 224 | autoTableOfReal FFNet_extractWeights (FFNet me, integer layer); |
| 225 | |
| 226 | void FFNet_drawWeights (FFNet me, Graphics g, integer layer, bool garnish); |
| 227 | |
| 228 | autoFFNet FFNet_TabelOfReal_to_FFNet (FFNet me, TableOfReal him, integer layer); |
| 229 | |
| 230 | autoFFNet PatternList_Categories_to_FFNet (PatternList me, Categories you, integer numberOfUnits1, integer numberOfUnits2); |
| 231 | |
| 232 | #endif /* _FFNet_h_ */ |