/tags/R2008-02-16/main/nnet/inst/__calcperf.m

# · MATLAB · 110 lines · 98 code · 12 blank · 0 comment · 10 complexity · 60410a9ca2ffb768a9e6e84bdc40ed42 MD5 · raw file

  1. ## Copyright (C) 2006 Michel D. Schmid <email: michaelschmid@users.sourceforge.net>
  2. ##
  3. ##
  4. ## This program is free software; you can redistribute it and/or modify it
  5. ## under the terms of the GNU General Public License as published by
  6. ## the Free Software Foundation; either version 2, or (at your option)
  7. ## any later version.
  8. ##
  9. ## This program is distributed in the hope that it will be useful, but
  10. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ## General Public License for more details.
  13. ##
  14. ## You should have received a copy of the GNU General Public License
  15. ## along with this program; see the file COPYING. If not, see
  16. ## <http://www.gnu.org/licenses/>.
  17. ## -*- texinfo -*-
  18. ## @deftypefn {Function File} {}[@var{perf}, @var{Ee}, @var{Aa}, @var{Nn}] = __calcperf (@var{net},@var{xx},@var{Im},@var{Tt})
  19. ## @code{__calcperf} calculates the performance of a multi-layer neural network.
  20. ## PLEASE DON'T USE IT ELSEWHERE, it proparly won't work.
  21. ## @end deftypefn
  22. ## Author: Michel D. Schmid
  23. function [perf,Ee,Aa,Nn] = __calcperf(net,xx,Im,Tt)
  24. ## comment:
  25. ## perf, net performance.. from input to output through the hidden layers
  26. ## Aa, output values of the hidden and last layer (output layer)
  27. ## is used for NEWFF network types
  28. ## calculate bias terms
  29. ## must have the same number of columns like the input matrix Im
  30. [nRows, nColumns] = size(Im);
  31. Btemp = cell(net.numLayers,1); # Btemp: bias matrix
  32. ones1xQ = ones(1,nColumns);
  33. for i= 1:net.numLayers
  34. Btemp{i} = net.b{i}(:,ones1xQ);
  35. endfor
  36. ## shortcuts
  37. IWtemp = cell(net.numLayers,net.numInputs,1);# IW: input weights ...
  38. LWtemp = cell(net.numLayers,net.numLayers,1);# LW: layer weights ...
  39. Aa = cell(net.numLayers,1);# Outputs hidden and output layer
  40. Nn = cell(net.numLayers,1);# outputs before the transfer function
  41. IW = net.IW; # input weights
  42. LW = net.LW; # layer weights
  43. ## calculate the whole network till outputs are reached...
  44. for iLayers = 1:net.numLayers
  45. ## calculate first input weights to weighted inputs..
  46. ## this can be done with matrix calculation...
  47. ## called "dotprod"
  48. ## to do this, there must be a special matrix ...
  49. ## e.g. IW = [1 2 3 4 5; 6 7 8 910] * [ 1 2 3; 4 5 6; 7 8 9; 10 11 12; 1 2 3];
  50. if (iLayers==1)
  51. IWtemp{iLayers,1} = IW{iLayers,1} * Im;
  52. onlyTempVar = [IWtemp(iLayers,1) Btemp(iLayers)];
  53. else
  54. IWtemp{iLayers,1} = [];
  55. endif
  56. ## now calculate layer weights to weighted layer outputs
  57. if (iLayers>1)
  58. Ad = Aa{iLayers-1,1};
  59. LWtemp{iLayers,1} = LW{iLayers,iLayers-1} * Ad;
  60. onlyTempVar = [LWtemp(iLayers,1) Btemp(iLayers)];
  61. else
  62. LWtemp{iLayers,1} = [];
  63. endif
  64. Nn{iLayers,1} = onlyTempVar{1};
  65. for k=2:length(onlyTempVar)
  66. Nn{iLayers,1} = Nn{iLayers,1} + onlyTempVar{k};
  67. endfor
  68. ## now calculate with the transfer functions the layer output
  69. switch net.layers{iLayers}.transferFcn
  70. case "purelin"
  71. Aa{iLayers,1} = purelin(Nn{iLayers,1});
  72. case "tansig"
  73. Aa{iLayers,1} = tansig(Nn{iLayers,1});
  74. case "logsig"
  75. Aa{iLayers,1} = logsig(Nn{iLayers,1});
  76. otherwise
  77. error(["Transfer function: " net.layers{iLayers}.transferFcn " doesn't exist!"])
  78. endswitch
  79. endfor # iLayers = 1:net.numLayers
  80. ## now calc network error
  81. Ee = cell(net.numLayers,1);
  82. for i=net.numLayers
  83. Ee{i,1} = Tt{i,1} - Aa{i,1};# Tt: target
  84. # Ee will be the error vector cell array
  85. endfor
  86. ## now calc network performance
  87. switch(net.performFcn)
  88. case "mse"
  89. perf = __mse(Ee);
  90. otherwise
  91. error("for performance functions, only mse is currently valid!")
  92. endswitch
  93. endfunction