SHARE
TWEET

Untitled

a guest May 10th, 2019 88 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:rows(tset)
  22.         % 3. Set desired output of the ANN
  23.         desiredOut = zeros(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.         delta = (desiredOut - olout)'
  39.         deltaOl = (outlw * delta) .* [actdf(olout) 1]
  40.         deltaOl = deltaOl(1:end-1, :)
  41.        
  42.         DELTAOl = delta * [hlact 1]
  43.        
  44.         % 7. Compute delta error of the hidden layer
  45.         % how many delta errors should be computed here?
  46.         deltaHl = (hidlw' * deltaOl) .* [actdf(hlout) 1]
  47.        
  48.         % 8. Update output layer weights
  49.         outlw = outlw + lr * DELTAOl'
  50.        
  51.         % 9. Update hidden layer weights
  52.         hidlw = hidlw + lr * deltaHl
  53.     end
  54.     terr /= rows(tset);
  55. endfunction
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