FFNet_ActivationList_Categories.cpp 3.2 KB

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