FFNet_Matrix.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* FFNet_Matrix.cpp
  2. *
  3. * Copyright (C) 1997-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 19950206
  20. djmw 20020712 GPL header
  21. */
  22. #include "FFNet_Matrix.h"
  23. autoMatrix FFNet_weightsToMatrix (FFNet me, integer layer, bool deltaWeights) {
  24. try {
  25. Melder_require (layer > 0 && layer <= my nLayers, U"Layer should be in [1, ", my nLayers, U"].");
  26. autoMatrix thee = Matrix_create (0.5, my nUnitsInLayer [layer] + 0.5, my nUnitsInLayer [layer], 1.0, 1.0,
  27. 0.5, my nUnitsInLayer [layer - 1] + 1 + 0.5, my nUnitsInLayer [layer - 1] + 1, 1.0, 1.0);
  28. integer node = 1;
  29. for (integer i = 0; i < layer; i ++) {
  30. node += my nUnitsInLayer [i] + 1;
  31. }
  32. for (integer i = 1; i <= my nUnitsInLayer [layer]; i ++, node ++) {
  33. integer k = 1;
  34. for (integer j = my wFirst [node]; j <= my wLast [node]; j ++) {
  35. thy z [k ++] [i] = deltaWeights ? my dwi [j] : my w [j];
  36. }
  37. }
  38. return thee;
  39. } catch (MelderError) {
  40. Melder_throw (me, U": no Matrix created.");
  41. }
  42. }
  43. autoFFNet FFNet_weightsFromMatrix (FFNet me, Matrix him, integer layer) {
  44. try {
  45. Melder_require (layer > 0 && layer <= my nLayers, U"Layer should be in [1, ", my nLayers, U"].");
  46. Melder_require (my nUnitsInLayer [layer] == his nx, U"The number of columns (", his nx, U") should equal the number of units (", my nUnitsInLayer [layer], U") in layer ", layer, U".");
  47. integer nunits = my nUnitsInLayer [layer - 1] + 1;
  48. Melder_require (nunits == his ny, U"The number of rows (", his ny, U") should equal the number of units (", nunits , U") in layer ", layer - 1, U".");
  49. autoFFNet thee = Data_copy (me);
  50. integer node = 1;
  51. for (integer i = 0; i < layer; i ++) {
  52. node += thy nUnitsInLayer [i] + 1;
  53. }
  54. for (integer i = 1; i <= thy nUnitsInLayer [layer]; i ++, node ++) {
  55. integer k = 1;
  56. for (integer j = thy wFirst [node]; j <= thy wLast [node]; j ++, k ++) {
  57. thy w [j] = his z [k] [i];
  58. }
  59. }
  60. return thee;
  61. } catch (MelderError) {
  62. Melder_throw (me, U": no FFNet created.");
  63. }
  64. }
  65. /* End of file FFNet_Matrix.cpp */