| 1 | /* FFNet_PatternList_ActivationList.cpp |
| 2 | * |
| 3 | * Copyright (C) 1994-2019 David Weenink, 2015,2017 Paul Boersma |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | * |
| 10 | * This code is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | /* |
| 20 | djmw 19960826 |
| 21 | djmw 20020712 GPL header |
| 22 | djmw 20030701 Removed non-GPL minimizations |
| 23 | djmw 20040416 More precise error messages. |
| 24 | djmw 20041118 Added FFNet_PatternList_Categories_getCosts. |
| 25 | */ |
| 26 | |
| 27 | #include "Graphics.h"cxx . . $ `<file>/sys/Graphics.h`/ |
| 28 | #include "FFNet_PatternList_ActivationList.h"cxx . . $ `<file>/FFNet/FFNet_PatternList_ActivationList.h`/ |
| 29 | |
| 30 | static double funccxx . . $ func(798b806ccf84f768). (Daatacxx . . $ Daata# object, VECcxx . . $ VEC# const& p) { |
| 31 | FFNetcxx . . $ FFNet# me = (FFNetcxx . . $ FFNet#) object; |
| 32 | const Minimizercxx . . $ Minimizer# thee = mycxx . . $ `melder/melder.h:44:9`! minimizercxx . . $ structFFNet#minimizer..getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(); |
| 33 | |
| 34 | for (integercxx . . $ integer# j = 1, k = 1; k <= mycxx . . $ `melder/melder.h:44:9`! numberOfWeightscxx . . $ structFFNet#numberOfWeights.; k ++) { |
| 35 | mycxx . . $ `melder/melder.h:44:9`! dwcxx . . $ structFFNet#dw. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).k] = 0.0; |
| 36 | if (mycxx . . $ `melder/melder.h:44:9`! wSelectedcxx . . $ structFFNet#wSelected. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).k]) |
| 37 | mycxx . . $ `melder/melder.h:44:9`! wcxx . . $ structFFNet#w. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).k] = p [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j ++]; |
| 38 | } |
| 39 | longdoublecxx . . $ longdouble# fp = 0.0; |
| 40 | for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! numberOfPatternscxx . . $ structFFNet#numberOfPatterns.; i ++) { |
| 41 | FFNet_propagatecxx . . $ FFNet_propagate(66412d333bbbfb3b). (me, mycxx . . $ `melder/melder.h:44:9`! inputPatterncxx . . $ structFFNet#inputPattern..rowcxx . . $ matrix#row(2d545b3cb3f8e666). (i), nullptr); |
| 42 | fp += FFNet_computeErrorcxx . . $ FFNet_computeError(91ccb74903e539f). (me, mycxx . . $ `melder/melder.h:44:9`! targetActivationcxx . . $ structFFNet#targetActivation..rowcxx . . $ matrix#row(2d545b3cb3f8e666). (i)); |
| 43 | FFNet_computeDerivativecxx . . $ FFNet_computeDerivative(c48706d70006b0b5). (me); |
| 44 | /* |
| 45 | Derivative (cumulative) |
| 46 | */ |
| 47 | mycxx . . $ `melder/melder.h:44:9`! dwcxx . . $ structFFNet#dw..partcxx . . $ autovector#part(2d8d48654ca64e70). (1, mycxx . . $ `melder/melder.h:44:9`! numberOfWeightscxx . . $ structFFNet#numberOfWeights.) +=cxx . . $ `operator+=`(adea042513329390). mycxx . . $ `melder/melder.h:44:9`! dwicxx . . $ structFFNet#dwi..partcxx . . $ autovector#part(2d8d48654ca64e70). (1, mycxx . . $ `melder/melder.h:44:9`! numberOfWeightscxx . . $ structFFNet#numberOfWeights.); |
| 48 | } |
| 49 | thycxx . . $ `melder/melder.h:45:9`! numberOfFunctionCallscxx . . $ structMinimizer#numberOfFunctionCalls. ++; |
| 50 | return (double) fp; |
| 51 | } |
| 52 | |
| 53 | static void dfunc_optimizedcxx . . $ dfunc_optimized(b88fc210334e1b81). (Daatacxx . . $ Daata# object, VECcxx . . $ VEC# const& /* p */, VECcxx . . $ VEC# const& dp) { |
| 54 | FFNetcxx . . $ FFNet# me = (FFNetcxx . . $ FFNet#) object; |
| 55 | |
| 56 | integercxx . . $ integer# j = 1; |
| 57 | for (integercxx . . $ integer# k = 1; k <= mycxx . . $ `melder/melder.h:44:9`! numberOfWeightscxx . . $ structFFNet#numberOfWeights.; k ++) { |
| 58 | if (mycxx . . $ `melder/melder.h:44:9`! wSelectedcxx . . $ structFFNet#wSelected. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).k]) |
| 59 | dp [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j ++] = mycxx . . $ `melder/melder.h:44:9`! dwcxx . . $ structFFNet#dw. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).k]; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | static void _FFNet_PatternList_ActivationList_checkDimensionscxx . . $ _FFNet_PatternList_ActivationList_checkDimensions(e87547d10e499836). (FFNetcxx . . $ FFNet# me, PatternListcxx . . $ PatternList# p, ActivationListcxx . . $ ActivationList# a) { |
| 64 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (mycxx . . $ `melder/melder.h:44:9`! numberOfInputs == p -> nx, |
| 65 | U"The PatternList and the FFNet do not match.\nThe number of columns in the PatternList must equal the number of inputs in the FFNet."); |
| 66 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (mycxx . . $ `melder/melder.h:44:9`! numberOfOutputs == a -> nx, |
| 67 | U"The Activation and the FFNet do not match.\nThe number of columns in the Activation must equal the number of outputs in the FFNet."); |
| 68 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (p -> ny == a -> ny, |
| 69 | U"The PatternList and the ActivationList do not match.\nThe number of rows in the PatternList must equal the number of rows in the Activation."); |
| 70 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (_PatternList_checkElements (p), |
| 71 | U"All PatternList elements should be in the interval [0, 1].\nYou could use \"Formula...\" to scale the PatternList values first."); |
| 72 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (_ActivationList_checkElements (a), |
| 73 | U"All Activation elements should be in the interval [0, 1].\nYou could use \"Formula...\" to scale the Activation values first."); |
| 74 | } |
| 75 | |
| 76 | static void _FFNet_PatternList_ActivationList_learncxx . . $ _FFNet_PatternList_ActivationList_learn(37ca533ca146da4). (FFNetcxx . . $ FFNet# me, PatternListcxx . . $ PatternList# pattern, ActivationListcxx . . $ ActivationList# activation, integercxx . . $ integer# maxNumOfEpochs, double tolerance, int costFunctionType, bool reset) { |
| 77 | try { |
| 78 | _FFNet_PatternList_ActivationList_checkDimensionscxx . . $ _FFNet_PatternList_ActivationList_checkDimensions(e87547d10e499836). (me, pattern, activation); |
| 79 | /* |
| 80 | Link the things to be learned |
| 81 | */ |
| 82 | mycxx . . $ `melder/melder.h:44:9`! numberOfPatternscxx . . $ structFFNet#numberOfPatterns. = pattern -> nycxx . . $ structSampledXY#ny.; |
| 83 | mycxx . . $ `melder/melder.h:44:9`! inputPatterncxx . . $ structFFNet#inputPattern. =cxx . . $ matrix#`operator=`(4523e359477ec8fe). pattern -> zcxx . . $ structMatrix#z..getcxx . . $ automatrix#get(b2b81450b5bdda66).(); |
| 84 | mycxx . . $ `melder/melder.h:44:9`! targetActivationcxx . . $ structFFNet#targetActivation. =cxx . . $ matrix#`operator=`(4523e359477ec8fe). activation -> zcxx . . $ structMatrix#z..getcxx . . $ automatrix#get(b2b81450b5bdda66).(); |
| 85 | FFNet_setCostFunctioncxx . . $ FFNet_setCostFunction(dfcbcca279925c1d). (me, costFunctionType); |
| 86 | |
| 87 | if (reset) { |
| 88 | autoVECcxx . . $ autoVEC# wbuf = zero_VECcxx . . $ zero_VEC(e4395f764b859b1e). (mycxx . . $ `melder/melder.h:44:9`! dimensioncxx . . $ structFFNet#dimension.); |
| 89 | integercxx . . $ integer# k = 1; |
| 90 | for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! numberOfWeightscxx . . $ structFFNet#numberOfWeights.; i ++) |
| 91 | if (mycxx . . $ `melder/melder.h:44:9`! wSelectedcxx . . $ structFFNet#wSelected. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]) |
| 92 | wbuf [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).k ++] = mycxx . . $ `melder/melder.h:44:9`! wcxx . . $ structFFNet#w. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]; |
| 93 | Minimizer_resetcxx . . $ Minimizer_reset(a31dbe1e2af75008). (mycxx . . $ `melder/melder.h:44:9`! minimizercxx . . $ structFFNet#minimizer..getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), wbufcxx . . $ constvector#constvector(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).()); |
| 94 | } |
| 95 | |
| 96 | Minimizer_minimizecxx . . $ Minimizer_minimize(773a17e8d056decf). (mycxx . . $ `melder/melder.h:44:9`! minimizercxx . . $ structFFNet#minimizer..getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), maxNumOfEpochs, tolerance, 1); |
| 97 | |
| 98 | // Unlink |
| 99 | |
| 100 | mycxx . . $ `melder/melder.h:44:9`! numberOfPatternscxx . . $ structFFNet#numberOfPatterns. = 0; |
| 101 | mycxx . . $ `melder/melder.h:44:9`! inputPatterncxx . . $ structFFNet#inputPattern. =cxx . . $ matrix#`operator=`(4523e359477ec8fe). MATcxx . . $ matrix#matrix(49f6e7a06ebc5aa8).(); |
| 102 | mycxx . . $ `melder/melder.h:44:9`! targetActivationcxx . . $ structFFNet#targetActivation. =cxx . . $ matrix#`operator=`(4523e359477ec8fe). MATcxx . . $ matrix#matrix(49f6e7a06ebc5aa8).(); |
| 103 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 104 | mycxx . . $ `melder/melder.h:44:9`! numberOfPatternscxx . . $ structFFNet#numberOfPatterns. = 0; |
| 105 | mycxx . . $ `melder/melder.h:44:9`! inputPatterncxx . . $ structFFNet#inputPattern. =cxx . . $ matrix#`operator=`(4523e359477ec8fe). MATcxx . . $ matrix#matrix(49f6e7a06ebc5aa8).(); |
| 106 | mycxx . . $ `melder/melder.h:44:9`! targetActivationcxx . . $ structFFNet#targetActivation. =cxx . . $ matrix#`operator=`(4523e359477ec8fe). MATcxx . . $ matrix#matrix(49f6e7a06ebc5aa8).(); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | |
| 111 | void FFNet_PatternList_ActivationList_learnSDcxx . . $ FFNet_PatternList_ActivationList_learnSD(833ed74a47f2a4d2). (FFNetcxx . . $ FFNet# me, PatternListcxx . . $ PatternList# p, ActivationListcxx . . $ ActivationList# a, integercxx . . $ integer# maxNumOfEpochs, double tolerance, double learningRate, double momentum, int costFunctionType) { |
| 112 | bool resetMinimizer = false; |
| 113 | /* |
| 114 | Did we choose another minimizer |
| 115 | */ |
| 116 | if (mycxx . . $ `melder/melder.h:44:9`! minimizercxx . . $ structFFNet#minimizer. && ! Thing_isacxx . . $ Thing_isa(8cbb12e93d03b2a1). (mycxx . . $ `melder/melder.h:44:9`! minimizercxx . . $ structFFNet#minimizer..getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), classSteepestDescentMinimizercxx . . $ classSteepestDescentMinimizer.)) { |
| 117 | mycxx . . $ `melder/melder.h:44:9`! minimizercxx . . $ structFFNet#minimizer.. resetcxx . . $ autoSomeThing#reset(ced63f7c635d850d).(); |
| 118 | resetMinimizer = true; |
| 119 | } |
| 120 | /* |
| 121 | Create the minimizer if it doesn't exist |
| 122 | */ |
| 123 | if (! mycxx . . $ `melder/melder.h:44:9`! minimizercxx . . $ structFFNet#minimizer.) { |
| 124 | resetMinimizer = true; |
| 125 | mycxx . . $ `melder/melder.h:44:9`! minimizercxx . . $ structFFNet#minimizer. =cxx . . $ autoSomeThing#`operator=`(ed6a1a4da6aedae3). SteepestDescentMinimizer_createcxx . . $ SteepestDescentMinimizer_create(ed3ce56bfc3f3e1c). (mycxx . . $ `melder/melder.h:44:9`! dimensioncxx . . $ structFFNet#dimension., me, funccxx . . $ func(798b806ccf84f768)., dfunc_optimizedcxx . . $ dfunc_optimized(b88fc210334e1b81).); |
| 126 | } |
| 127 | ((SteepestDescentMinimizercxx . . $ SteepestDescentMinimizer#) mycxx . . $ `melder/melder.h:44:9`! minimizercxx . . $ structFFNet#minimizer..getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()) -> etacxx . . $ structSteepestDescentMinimizer#eta. = learningRate; |
| 128 | ((SteepestDescentMinimizercxx . . $ SteepestDescentMinimizer#) mycxx . . $ `melder/melder.h:44:9`! minimizercxx . . $ structFFNet#minimizer..getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()) -> momentumcxx . . $ structSteepestDescentMinimizer#momentum. = momentum; |
| 129 | _FFNet_PatternList_ActivationList_learncxx . . $ _FFNet_PatternList_ActivationList_learn(37ca533ca146da4). (me, p, a, maxNumOfEpochs, tolerance, costFunctionType, resetMinimizer); |
| 130 | } |
| 131 | |
| 132 | void FFNet_PatternList_ActivationList_learnSMcxx . . $ FFNet_PatternList_ActivationList_learnSM(204eec4d511c83df). (FFNetcxx . . $ FFNet# me, PatternListcxx . . $ PatternList# p, ActivationListcxx . . $ ActivationList# a, integercxx . . $ integer# maxNumOfEpochs, double tolerance, int costFunctionType) { |
| 133 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (maxNumOfEpochs > 0); |
| 134 | bool resetMinimizer = false; |
| 135 | /* |
| 136 | Did we choose another minimizer |
| 137 | */ |
| 138 | if (mycxx . . $ `melder/melder.h:44:9`! minimizercxx . . $ structFFNet#minimizer..getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).() && ! Thing_isacxx . . $ Thing_isa(8cbb12e93d03b2a1). (mycxx . . $ `melder/melder.h:44:9`! minimizercxx . . $ structFFNet#minimizer..getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), classVDSmagtMinimizercxx . . $ classVDSmagtMinimizer.)) { |
| 139 | mycxx . . $ `melder/melder.h:44:9`! minimizercxx . . $ structFFNet#minimizer.. resetcxx . . $ autoSomeThing#reset(ced63f7c635d850d).(); |
| 140 | resetMinimizer = true; |
| 141 | } |
| 142 | /* |
| 143 | Create the minimizer if it doesn't exist |
| 144 | */ |
| 145 | if (! mycxx . . $ `melder/melder.h:44:9`! minimizercxx . . $ structFFNet#minimizer..getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()) { |
| 146 | resetMinimizer = true; |
| 147 | mycxx . . $ `melder/melder.h:44:9`! minimizercxx . . $ structFFNet#minimizer. =cxx . . $ autoSomeThing#`operator=`(ed6a1a4da6aedae3). VDSmagtMinimizer_createcxx . . $ VDSmagtMinimizer_create(db2e53d812b6eef7). (mycxx . . $ `melder/melder.h:44:9`! dimensioncxx . . $ structFFNet#dimension., me, funccxx . . $ func(798b806ccf84f768)., dfunc_optimizedcxx . . $ dfunc_optimized(b88fc210334e1b81).); |
| 148 | } |
| 149 | _FFNet_PatternList_ActivationList_learncxx . . $ _FFNet_PatternList_ActivationList_learn(37ca533ca146da4). (me, p, a, maxNumOfEpochs, tolerance, costFunctionType, resetMinimizer); |
| 150 | } |
| 151 | |
| 152 | double FFNet_PatternList_ActivationList_getCosts_totalcxx . . $ FFNet_PatternList_ActivationList_getCosts_total(7631ef2e6bcfefc). (FFNetcxx . . $ FFNet# me, PatternListcxx . . $ PatternList# p, ActivationListcxx . . $ ActivationList# a, int costFunctionType) { |
| 153 | try { |
| 154 | _FFNet_PatternList_ActivationList_checkDimensionscxx . . $ _FFNet_PatternList_ActivationList_checkDimensions(e87547d10e499836). (me, p, a); |
| 155 | FFNet_setCostFunctioncxx . . $ FFNet_setCostFunction(dfcbcca279925c1d). (me, costFunctionType); |
| 156 | |
| 157 | double cost = 0.0; |
| 158 | for (integercxx . . $ integer# i = 1; i <= p -> nycxx . . $ structSampledXY#ny.; i ++) { |
| 159 | FFNet_propagatecxx . . $ FFNet_propagate(66412d333bbbfb3b). (me, pcxx . . $ constvector#constvector(eac9d6ec4c4bfecd). -> zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (i), nullptr); |
| 160 | cost += FFNet_computeErrorcxx . . $ FFNet_computeError(91ccb74903e539f). (me, acxx . . $ constvector#constvector(eac9d6ec4c4bfecd). -> zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (i)); |
| 161 | } |
| 162 | return cost; |
| 163 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 164 | return undefinedcxx . . $ undefined.; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | double FFNet_PatternList_ActivationList_getCosts_averagecxx . . $ FFNet_PatternList_ActivationList_getCosts_average(7631ef2e6bcfefc). (FFNetcxx . . $ FFNet# me, PatternListcxx . . $ PatternList# p, ActivationListcxx . . $ ActivationList# a, int costFunctionType) { |
| 169 | const double costs = FFNet_PatternList_ActivationList_getCosts_totalcxx . . $ FFNet_PatternList_ActivationList_getCosts_total(7631ef2e6bcfefc). (me, p, a, costFunctionType); |
| 170 | return ( isundefcxx . . $ isundef(f8197d536af80c96). (costs) ? undefinedcxx . . $ undefined. : costs / p -> nycxx . . $ structSampledXY#ny. ); |
| 171 | } |
| 172 | |
| 173 | autoActivationListcxx . . $ autoActivationList# FFNet_PatternList_to_ActivationListcxx . . $ FFNet_PatternList_to_ActivationList(1cd8b75cf5b58fa3). (FFNetcxx . . $ FFNet# me, PatternListcxx . . $ PatternList# p, integercxx . . $ integer# layer) { |
| 174 | try { |
| 175 | if (layer < 1 || layer > mycxx . . $ `melder/melder.h:44:9`! numberOfLayerscxx . . $ structFFNet#numberOfLayers.) |
| 176 | layer = mycxx . . $ `melder/melder.h:44:9`! numberOfLayerscxx . . $ structFFNet#numberOfLayers.; |
| 177 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (mycxx . . $ `melder/melder.h:44:9`! numberOfInputs == p -> nx, |
| 178 | U"The number of colums in the PatternList (", p -> nx, U") should equal the number of inputs in the FFNet (", mycxx . . $ `melder/melder.h:44:9`! numberOfInputs, U")."); |
| 179 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (_PatternList_checkElements (p), |
| 180 | U"All PatternList elements should be in the interval [0, 1].\nYou could use \"Formula...\" to scale the PatternList values first."); |
| 181 | |
| 182 | const integercxx . . $ integer# numberOfPatterns = p -> nycxx . . $ structSampledXY#ny.; |
| 183 | autoActivationListcxx . . $ autoActivationList# thee = ActivationList_createcxx . . $ ActivationList_create(b83e53fc6a9ddd8f). (numberOfPatterns, mycxx . . $ `melder/melder.h:44:9`! numberOfUnitsInLayercxx . . $ structFFNet#numberOfUnitsInLayer. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).layer]); |
| 184 | for (integercxx . . $ integer# i = 1; i <= numberOfPatterns; i ++) |
| 185 | FFNet_propagateToLayercxx . . $ FFNet_propagateToLayer(7c404e9ee83353d4). (me, pcxx . . $ constvector#constvector(eac9d6ec4c4bfecd). -> zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (i), thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (i), layer); |
| 186 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 187 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 188 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": no ActivationList created."); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | /* End of file FFNet_PatternList_ActivationList.cpp */ |