Guest User

Untitled

a guest
Apr 22nd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. function [out1,out2] = getweights(maxiter, numofhidden, input, y, centers, sigma)
  2. % [out1,out2] = getweights(1000000, 6, input', y, centers, sigma)
  3. % maxiter = maximal number of iterations
  4. % numofhidden = number of hidden neurons in the hidden layer
  5. % input = column vector of input patterns
  6. % y = the true output I am aiming for
  7. % centers = centers computed by C-means clustering
  8. % sigma = used to control smoothness of the interp fcn, also computed before
  9.  
  10. N = 1; % dimension of true_out neurons
  11. M = 1; % dimension of output neurons
  12. num = 100; % number of training true_out
  13. k = numofhidden; % number of hidden neurons
  14. E = zeros(maxiter/1000,1); % energy
  15. true_out = y; % y - matrix(num,M)
  16. train_out = zeros(num,M); % training output
  17. w = zeros(k,M); % weight matrix
  18. r = zeros(k,1); % euclidean norm of input - centers
  19. deltaw = zeros(k,M);% will be computed in update rules
  20. deltac = zeros(k,M);% will be computed in update rules
  21. e = 0;
  22.  
  23. % NORMALIZE THE INPUT DATA
  24. input(:,1)=input(:,1)-mean(input(:,1));
  25. input(:,1)=input(:,1)/std(input(:,1));
  26.  
  27. % INITIALIZE THE PARAMETERS
  28. eta1 = 0.02; % learning rate
  29. eta2 = 0.02; % learning rate
  30. w = 0.5*rand(k,M) - 0.25; % randomly initialize the weight vector
  31. phi = zeros(k,M); % Gaussian RBF fcn
  32. multip = 1; % serves as a counter for plotting the energy
  33.  
  34. for iter=1:maxiter
  35. mu=ceil(rand*num); % randomly pick a pattern
  36. for i=1:M
  37. for j=1:k
  38. r(j) = norm(input(mu,1)-centers(j,1)); % r - matrix (6,1)
  39. end
  40. phi(1:k) = exp(-(r).^2/(2*sigma^2)); % phi - matrix (6,1)
  41. train_out(mu,i) = w' * phi; % train_out - matrix(1,1)
  42. end
  43. e = true_out(mu,i) - train_out(mu,i); % compute the error between the original output and network output
  44. for i=1:M
  45. for j=1:k
  46. deltaw(j,i) = 2*eta1*e*phi(j,1); % deltaw - matrix(6,1)
  47. deltac(j,i) = -2*eta2*w(j,i)*e*phi(j,1)*((input(mu,i)-centers(j,i))/(norm(input(mu,i)-centers(j,i))));
  48. end
  49. end
  50. w = w + deltaw; % update the weights
  51. centers = centers + deltac; % update the centers
  52. if iter == multip*1000 % save energy only every 1000th iteration
  53. E(multip,1) = 1/2 * (sum((true_out-train_out).^2));
  54. multip = multip + 1;
  55. end
  56. end
  57. out1 = w;
  58. out2 = centers;
  59. xx=1:(maxiter/1000);
  60. xx = xx';
  61. plot(xx,E)
  62. end
Add Comment
Please, Sign In to add comment