| 1 | /* FFNet_ActivationList_Categories.cpp |
| 2 | * |
| 3 | * Copyright (C) 1997-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 19960322 |
| 21 | djmw 20020712 GPL header |
| 22 | djmw 20040416 Better error messages |
| 23 | djmw 20071014 Melder_error<n> |
| 24 | */ |
| 25 | |
| 26 | #include "FFNet_ActivationList_Categories.h" |
| 27 | |
| 28 | static integer winnerTakesAll (FFNet me, constVEC activation) { |
| 29 | integer pos = 1; |
| 30 | double max = activation [1]; |
| 31 | for (integer i = 2; i <= my numberOfOutputs; i ++) |
| 32 | if (activation [i] > max) |
| 33 | max = activation [pos = i]; |
| 34 | return pos; |
| 35 | } |
| 36 | |
| 37 | static integer stochastic (FFNet me, constVEC activation) { |
| 38 | double range = 0.0, lower = 0.0; |
| 39 | integer i; |
| 40 | for (i = 1; i <= my numberOfOutputs; i ++) |
| 41 | range += activation [i]; |
| 42 | const double number = NUMrandomUniform (0.0, range); |
| 43 | for (i = 1; i <= my numberOfOutputs; i ++) { |
| 44 | lower += activation [i]; |
| 45 | if (number < lower) |
| 46 | break; |
| 47 | } |
| 48 | return i; |
| 49 | } |
| 50 | |
| 51 | autoCategories FFNet_ActivationList_to_Categories (FFNet me, ActivationList activation, int labeling) { |
| 52 | try { |
| 53 | integer (*labelingFunction) (FFNet me, constVEC act); |
| 54 | Melder_require (my outputCategories, |
| 55 | U"No Categories (has the FFNet been trained yet?)."); |
| 56 | Melder_require (my numberOfOutputs == activation -> nx, |
| 57 | U"Number of columns and number of outputs should be equal."); |
| 58 | |
| 59 | autoCategories thee = Categories_create (); |
| 60 | labelingFunction = labeling == 2 ? stochastic : winnerTakesAll; |
| 61 | for (integer i = 1; i <= activation->ny; i ++) { |
| 62 | const integer index = labelingFunction (me, activation -> z.row (i)); |
| 63 | autoSimpleString item = Data_copy (my outputCategories->at [index]); |
| 64 | thy addItem_move (item.move()); |
| 65 | } |
| 66 | return thee; |
| 67 | } catch (MelderError) { |
| 68 | Melder_throw (me, U": no Categories created."); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | autoActivationList FFNet_Categories_to_ActivationList (FFNet me, Categories thee) { |
| 73 | try { |
| 74 | autoCategories uniq = Categories_selectUniqueItems (thee); |
| 75 | Melder_require (my outputCategories, |
| 76 | U"The FFNet does not have categories."); |
| 77 | Melder_require (OrderedOfString_containSameElements (uniq.get(), my outputCategories.get()), |
| 78 | U"The Categories should match the categories of the FFNet."); |
| 79 | |
| 80 | autoActivationList him = ActivationList_create (thy size, my numberOfOutputs); |
| 81 | for (integer i = 1; i <= thy size; i ++) { |
| 82 | const SimpleString category = thy at [i]; |
| 83 | const integer pos = OrderedOfString_indexOfItem (my outputCategories.get(), category -> string.get()); |
| 84 | Melder_require (pos > 0, |
| 85 | U"The FFNet doesn't know the category ", category -> string.get(), U"."); |
| 86 | his z [i] [pos] = 1.0; |
| 87 | } |
| 88 | return him; |
| 89 | } catch (MelderError) { |
| 90 | Melder_throw (me, U": no ActivationList created."); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /* End of file FFNet_ActivationList_Categories.cpp */ |