Advertisement
Guest User

Untitled

a guest
May 6th, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. load iris_dataset
  2.  
  3. % Number of neurons
  4. n = 4;
  5.  
  6. % Number of attributes and number of classifications
  7. [n_attr, ~] = size(irisInputs);
  8. [n_class, ~] = size(irisTargets);
  9.  
  10. % Initialize neural network
  11. net = feedforwardnet(n);
  12.  
  13. % Configure the neural network for this dataset
  14. net = configure(net, irisInputs, irisTargets); %view(net);
  15.  
  16. fun = @(w) mse_test(w, net, irisInputs, irisTargets);
  17.  
  18. % Add 'Display' option to display result of iterations
  19. ps_opts = psoptimset ( 'CompletePoll', 'off', 'Display', 'iter', 'MaxIter', 100); %, 'TimeLimit', 120 );
  20.  
  21. % There is n_attr attributes in dataset, and there are n neurons so there
  22. % are total of n_attr*n input weights (uniform weight)
  23. initial_il_weights = ones(1, n_attr*n)/(n_attr*n);
  24. % There are n bias values, one for each neuron (random)
  25. initial_il_bias = rand(1, n);
  26. % There is n_class output, so there are total of n_class*n output weights
  27. % (uniform weight)
  28. initial_ol_weights = ones(1, n_class*n)/(n_class*n);
  29. % There are n_class bias values, one for each output neuron (random)
  30. initial_ol_bias = rand(1, n_class);
  31. % starting values
  32. starting_values = [initial_il_weights, initial_il_bias, ...
  33. initial_ol_weights, initial_ol_bias];
  34.  
  35. % alter the patternsearch function with the appropriate constraints, in your case we would change it so that the lower bounds of the weights are zero
  36. [x, fval, flag, output] = patternsearch(fun, starting_values, [], [],[],[], zeros(size(starting_values)), 1e10, ps_opts);
  37.  
  38. function mse_calc = mse_test(x, net, inputs, targets)
  39. net = setwb(net, x');
  40. y = net(inputs);
  41. [row col] = size(y);
  42. mse_calc = sum(sum((y - targets).^2))/(row * col);
  43. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement