SHARE
TWEET

Untitled

a guest May 10th, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function [hidlw outlw terr] = backprop(tset, tslb, inihidlw, inioutlw, lr)
  2. % derivative of sigmoid activation function
  3. % tset - training set (every row represents a sample)
  4. % tslb - column vector of labels
  5. % inihidlw - initial hidden layer weight matrix
  6. % inioutlw - initial output layer weight matrix
  7. % lr - learning rate
  8.  
  9. % hidlw - hidden layer weight matrix
  10. % outlw - output layer weight matrix
  11. % terr - total squared error of the ANN
  12.  
  13. % 1. Set output matrices to initial values
  14.     hidlw = inihidlw
  15.     outlw = inioutlw
  16.    
  17. % 2. Set total error to 0
  18.     terr = 0;
  19.    
  20. % foreach sample in the training set
  21.     for i=1:1 % w docelowym to rows(tset)
  22.         % 3. Set desired output of the ANN
  23.         desiredOut = -ones(1, columns(outlw));
  24.         desiredOut(tslb(i)) = 1
  25.  
  26.         % 4. Propagate input forward through the ANN
  27.         % remember to extend input [tset(i, :) 1]
  28.         hlact = [tset(i, :) 1] * hidlw;
  29.         hlout = actf(hlact)
  30.         olact = [hlout 1] * outlw;
  31.         olout = actf(olact)
  32.  
  33.         % 5. Adjust total error (just to know this value)
  34.         terr += 0.5 * sum((desiredOut-olout).^2)
  35.  
  36.         % 6. Compute delta error of the output layer
  37.         % how many delta errors should be computed here?
  38.         deltaOlzk = (desiredOut - olout) .* actdf(olout)
  39.        
  40.         % 7. Compute delta error of the hidden layer
  41.         % how many delta errors should be computed here?
  42.         deltaHlyk = ([tset(i, :) 1]' * (deltaOlzk * outlw(1:end-1, :)')) .* actdf(hlout)
  43.        
  44.         % 8. Update output layer weights
  45.         outlw = outlw + lr * deltaOlzk
  46.        
  47.         % 9. Update hidden layer weights
  48.         hidlw = hidlw + lr * deltaHlyk
  49.     end
  50.     terr /= rows(tset);
  51. endfunction
  52.  
  53. >> [hlnn olnn terr] = backprop(tvec, tlab, hlnn, olnn, 0.005)
  54. hidlw =
  55.    0.078662   0.127638   0.145872  -0.136716
  56.   -0.187663   0.174200  -0.282833   0.286914
  57.    0.218993  -0.019205   0.283381   0.151910
  58. outlw =
  59.   -0.1120277   0.1378272
  60.   -0.1245939  -0.0721922
  61.    0.0058918   0.1056387
  62.    0.2184218   0.1480951
  63.    0.0033430   0.0298463
  64. desiredOut =
  65.    1  -1
  66. hlout =
  67.    0.063535   0.142370   0.087351   0.136194
  68. olout =
  69.    0.0043746   0.0288532
  70. terr =  1.0249
  71. deltaOlzk =
  72.    0.49780  -0.51400
  73. deltaHlyk =
  74.   -0.069355  -0.013426  -0.028035   0.017603
  75.   -0.059897  -0.011595  -0.024212   0.015203
  76.   -0.063050  -0.012206  -0.025487   0.016003
  77. outlw =
  78.   -0.1095386   0.1352572
  79.   -0.1221048  -0.0747622
  80.    0.0083808   0.1030687
  81.    0.2209108   0.1455251
  82.    0.0058321   0.0272763
  83. hidlw =
  84.    0.078315   0.127571   0.145731  -0.136628
  85.   -0.187962   0.174142  -0.282954   0.286990
  86.    0.218678  -0.019267   0.283254   0.151990
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top