Debian Codegraph @

FFNet/FFNet_PatternList_ActivationList.cpp

raw
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"
28 #include "FFNet_PatternList_ActivationList.h"
29
30 static double func (Daata object, VEC const& p) {
31 FFNet me = (FFNet) object;
32 const Minimizer thee = my minimizer.get();
33
34 for (integer j = 1, k = 1; k <= my numberOfWeights; k ++) {
35 my dw [k] = 0.0;
36 if (my wSelected [k])
37 my w [k] = p [j ++];
38 }
39 longdouble fp = 0.0;
40 for (integer i = 1; i <= my numberOfPatterns; i ++) {
41 FFNet_propagate (me, my inputPattern.row (i), nullptr);
42 fp += FFNet_computeError (me, my targetActivation.row (i));
43 FFNet_computeDerivative (me);
44 /*
45 Derivative (cumulative)
46 */
47 my dw.part (1, my numberOfWeights) += my dwi.part (1, my numberOfWeights);
48 }
49 thy numberOfFunctionCalls ++;
50 return (double) fp;
51 }
52
53 static void dfunc_optimized (Daata object, VEC const& /* p */, VEC const& dp) {
54 FFNet me = (FFNet) object;
55
56 integer j = 1;
57 for (integer k = 1; k <= my numberOfWeights; k ++) {
58 if (my wSelected [k])
59 dp [j ++] = my dw [k];
60 }
61 }
62
63 static void _FFNet_PatternList_ActivationList_checkDimensions (FFNet me, PatternList p, ActivationList a) {
64 Melder_require (my 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_require (my 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_require (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_require (_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_require (_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_learn (FFNet me, PatternList pattern, ActivationList activation, integer maxNumOfEpochs, double tolerance, int costFunctionType, bool reset) {
77 try {
78 _FFNet_PatternList_ActivationList_checkDimensions (me, pattern, activation);
79 /*
80 Link the things to be learned
81 */
82 my numberOfPatterns = pattern -> ny;
83 my inputPattern = pattern -> z.get();
84 my targetActivation = activation -> z.get();
85 FFNet_setCostFunction (me, costFunctionType);
86
87 if (reset) {
88 autoVEC wbuf = zero_VEC (my dimension);
89 integer k = 1;
90 for (integer i = 1; i <= my numberOfWeights; i ++)
91 if (my wSelected [i])
92 wbuf [k ++] = my w [i];
93 Minimizer_reset (my minimizer.get(), wbuf.get());
94 }
95
96 Minimizer_minimize (my minimizer.get(), maxNumOfEpochs, tolerance, 1);
97
98 // Unlink
99
100 my numberOfPatterns = 0;
101 my inputPattern = MAT();
102 my targetActivation = MAT();
103 } catch (MelderError) {
104 my numberOfPatterns = 0;
105 my inputPattern = MAT();
106 my targetActivation = MAT();
107 }
108 }
109
110
111 void FFNet_PatternList_ActivationList_learnSD (FFNet me, PatternList p, ActivationList a, integer maxNumOfEpochs, double tolerance, double learningRate, double momentum, int costFunctionType) {
112 bool resetMinimizer = false;
113 /*
114 Did we choose another minimizer
115 */
116 if (my minimizer && ! Thing_isa (my minimizer.get(), classSteepestDescentMinimizer)) {
117 my minimizer. reset();
118 resetMinimizer = true;
119 }
120 /*
121 Create the minimizer if it doesn't exist
122 */
123 if (! my minimizer) {
124 resetMinimizer = true;
125 my minimizer = SteepestDescentMinimizer_create (my dimension, me, func, dfunc_optimized);
126 }
127 ((SteepestDescentMinimizer) my minimizer.get()) -> eta = learningRate;
128 ((SteepestDescentMinimizer) my minimizer.get()) -> momentum = momentum;
129 _FFNet_PatternList_ActivationList_learn (me, p, a, maxNumOfEpochs, tolerance, costFunctionType, resetMinimizer);
130 }
131
132 void FFNet_PatternList_ActivationList_learnSM (FFNet me, PatternList p, ActivationList a, integer maxNumOfEpochs, double tolerance, int costFunctionType) {
133 Melder_assert (maxNumOfEpochs > 0);
134 bool resetMinimizer = false;
135 /*
136 Did we choose another minimizer
137 */
138 if (my minimizer.get() && ! Thing_isa (my minimizer.get(), classVDSmagtMinimizer)) {
139 my minimizer. reset();
140 resetMinimizer = true;
141 }
142 /*
143 Create the minimizer if it doesn't exist
144 */
145 if (! my minimizer.get()) {
146 resetMinimizer = true;
147 my minimizer = VDSmagtMinimizer_create (my dimension, me, func, dfunc_optimized);
148 }
149 _FFNet_PatternList_ActivationList_learn (me, p, a, maxNumOfEpochs, tolerance, costFunctionType, resetMinimizer);
150 }
151
152 double FFNet_PatternList_ActivationList_getCosts_total (FFNet me, PatternList p, ActivationList a, int costFunctionType) {
153 try {
154 _FFNet_PatternList_ActivationList_checkDimensions (me, p, a);
155 FFNet_setCostFunction (me, costFunctionType);
156
157 double cost = 0.0;
158 for (integer i = 1; i <= p -> ny; i ++) {
159 FFNet_propagate (me, p -> z.row (i), nullptr);
160 cost += FFNet_computeError (me, a -> z.row (i));
161 }
162 return cost;
163 } catch (MelderError) {
164 return undefined;
165 }
166 }
167
168 double FFNet_PatternList_ActivationList_getCosts_average (FFNet me, PatternList p, ActivationList a, int costFunctionType) {
169 const double costs = FFNet_PatternList_ActivationList_getCosts_total (me, p, a, costFunctionType);
170 return ( isundef (costs) ? undefined : costs / p -> ny );
171 }
172
173 autoActivationList FFNet_PatternList_to_ActivationList (FFNet me, PatternList p, integer layer) {
174 try {
175 if (layer < 1 || layer > my numberOfLayers)
176 layer = my numberOfLayers;
177 Melder_require (my numberOfInputs == p -> nx,
178 U"The number of colums in the PatternList (", p -> nx, U") should equal the number of inputs in the FFNet (", my numberOfInputs, U").");
179 Melder_require (_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 integer numberOfPatterns = p -> ny;
183 autoActivationList thee = ActivationList_create (numberOfPatterns, my numberOfUnitsInLayer [layer]);
184 for (integer i = 1; i <= numberOfPatterns; i ++)
185 FFNet_propagateToLayer (me, p -> z.row (i), thy z.row (i), layer);
186 return thee;
187 } catch (MelderError) {
188 Melder_throw (me, U": no ActivationList created.");
189 }
190 }
191
192 /* End of file FFNet_PatternList_ActivationList.cpp */