| 1 | /* FFNet_PatternList_Categories.cpp |
| 2 | * |
| 3 | * Copyright (C) 1994-2019 David Weenink |
| 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 20020712 GPL header. |
| 21 | djmw 20020910 changes. |
| 22 | djmw 20030701 Removed non-GPL minimizations. |
| 23 | djmw 20041118 Added FFNet_PatternList_Categories_getCosts. |
| 24 | */ |
| 25 | |
| 26 | #include "FFNet_ActivationList_Categories.h" |
| 27 | #include "FFNet_PatternList_Categories.h" |
| 28 | #include "FFNet_PatternList_ActivationList.h" |
| 29 | |
| 30 | static void _FFNet_PatternList_Categories_checkDimensions (FFNet me, PatternList p, Categories c) { |
| 31 | Melder_require (my numberOfInputs == p -> nx, |
| 32 | U"The PatternList and the FFNet do not match.\nThe number of colums in the PatternList should equal the number of inputs in the FFNet."); |
| 33 | Melder_require (p -> ny == c->size, |
| 34 | U"The PatternList and the categories do not match.\nThe number of rows in the PatternList should equal the number of categories."); |
| 35 | Melder_require (_PatternList_checkElements (p), |
| 36 | U"All PatternList elements should be in the interval [0, 1].\nYou could use \"Formula...\" to scale the PatternList values first."); |
| 37 | } |
| 38 | |
| 39 | double FFNet_PatternList_Categories_getCosts_total (FFNet me, PatternList p, Categories c, int costFunctionType) { |
| 40 | try { |
| 41 | _FFNet_PatternList_Categories_checkDimensions (me, p, c); |
| 42 | autoActivationList activation = FFNet_Categories_to_ActivationList (me, c); |
| 43 | return FFNet_PatternList_ActivationList_getCosts_total (me, p, activation.get(), costFunctionType); |
| 44 | } catch (MelderError) { |
| 45 | return undefined; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | double FFNet_PatternList_Categories_getCosts_average (FFNet me, PatternList p, Categories c, int costFunctionType) { |
| 50 | const double costs = FFNet_PatternList_Categories_getCosts_total (me, p, c, costFunctionType); |
| 51 | return ( isundef (costs) ? undefined : costs / p -> ny ); |
| 52 | } |
| 53 | |
| 54 | void FFNet_PatternList_Categories_learnSD (FFNet me, PatternList p, Categories c, integer maxNumOfEpochs, double tolerance, double learningRate, double momentum, int costFunctionType) { |
| 55 | _FFNet_PatternList_Categories_checkDimensions (me, p, c); |
| 56 | autoActivationList activation = FFNet_Categories_to_ActivationList (me, c); |
| 57 | double min, max; |
| 58 | Matrix_getWindowExtrema (p, 0, 0, 0, 0, & min, & max); |
| 59 | FFNet_PatternList_ActivationList_learnSD (me, p, activation.get(), maxNumOfEpochs, tolerance, learningRate, momentum, costFunctionType); |
| 60 | } |
| 61 | |
| 62 | void FFNet_PatternList_Categories_learnSM (FFNet me, PatternList p, Categories c, integer maxNumOfEpochs, double tolerance, int costFunctionType) { |
| 63 | _FFNet_PatternList_Categories_checkDimensions (me, p, c); |
| 64 | autoActivationList activation = FFNet_Categories_to_ActivationList (me, c); |
| 65 | double min, max; |
| 66 | Matrix_getWindowExtrema (p, 0, 0, 0, 0, & min, & max); |
| 67 | FFNet_PatternList_ActivationList_learnSM (me, p, activation.get(), maxNumOfEpochs, tolerance, costFunctionType); |
| 68 | } |
| 69 | |
| 70 | autoCategories FFNet_PatternList_to_Categories (FFNet me, PatternList thee, int labeling) { |
| 71 | try { |
| 72 | Melder_require (my outputCategories, |
| 73 | U"The FFNet has no output categories."); |
| 74 | Melder_require (my numberOfInputs == thy nx, |
| 75 | U"The number of colums in the PatternList (", thy nx, U") should equal the number of inputs in the FFNet (", my numberOfInputs, U")."); |
| 76 | Melder_require (_PatternList_checkElements (thee), |
| 77 | U"All PatternList elements should be in the interval [0, 1].\nYou could use \"Formula...\" to scale the PatternList values first."); |
| 78 | |
| 79 | autoCategories him = Categories_create (); |
| 80 | |
| 81 | for (integer k = 1; k <= thy ny; k ++) { |
| 82 | FFNet_propagate (me, thy z.row (k), nullptr); |
| 83 | const integer index = FFNet_getWinningUnit (me, labeling); |
| 84 | autoSimpleString item = Data_copy (my outputCategories->at [index]); |
| 85 | his addItem_move (item.move()); |
| 86 | } |
| 87 | return him; |
| 88 | } catch (MelderError) { |
| 89 | Melder_throw (me, U": no Categories created."); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /* End of file FFNet_PatternList_Categories.cpp */ |