Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.93 KB | None | 0 0
  1. function classifier(trainMatrixEnhanced)
  2. %UNTITLED Summary of this function goes here
  3. %   Detailed explanation goes here
  4.  
  5. % trainfunc,activfunc,learnfunc
  6. trainfunc = "traingd";
  7. activfunc = "hardlim"; % %logsig hardlim purelin
  8. %learnfunc = "learnp";
  9.  
  10. W = rand(10,256); %Weights
  11. b = rand(10,1); %bias
  12.  
  13. % targets = zeros(10, 500);
  14. % for i=1:10
  15. %     for j=1:50
  16. %         targets(i,i) = 1;
  17. %     end
  18. % end
  19.  
  20. target_arrays= [[1 0 0 0 0 0 0 0 0 0];[0 1 0 0 0 0 0 0 0 0];[0 0 1 0 0 0 0 0 0 0];[0 0 0 1 0 0 0 0 0 0];[0 0 0 0 1 0 0 0 0 0];[0 0 0 0 0 1 0 0 0 0];[0 0 0 0 0 0 1 0 0 0];[0 0 0 0 0 0 0 1 0 0];[0 0 0 0 0 0 0 0 1 0];[0 0 0 0 0 0 0 0 0 1]];
  21. %Função fornecida por um colega
  22. targets = [];
  23. A = targets;
  24. for i=1:10
  25.     for j=1:50
  26.         targets= [A target_arrays(1:10,i)];
  27.         A= targets;
  28.     end
  29. end
  30.  
  31. if(activfunc == "hardlim")
  32.     net = perceptron; %Hard limit transfer function (default = 'hardlim') / Perceptron learning rule (default = 'learnp')
  33. else
  34.     net = network();
  35.    
  36.     net.numInputs = 1;
  37.     net.inputs{1}.size = 256;
  38.     net.numLayers = 1;
  39.     net.layers{1}.size = 10;
  40.     net.inputConnect(1) = 1;
  41.     net.biasConnect(1) = 1;
  42.     net.outputConnect(1) = 1;
  43.     net.layers{1}.transferFcn = "purelin";
  44.     net.trainFcn = "traingd"; %gradient / to see all training funcs - help nntrain
  45.    
  46. end
  47.  
  48. view(net)
  49.  
  50. net = configure(net, trainMatrixEnhanced, targets);
  51.  
  52. %Initialization
  53. net.IW{1,1} = W;
  54. net.b{1,1}= b;
  55.  
  56. %Training parameters
  57. net.performParam.lr = 0.5; % learning rate
  58. net.trainParam.epochs = 1000; % maximum epochs
  59. net.trainParam.show = 35; % show
  60. net.trainParam.goal = 1e-6; % goal=objective
  61. net.performFcn = 'sse'; % criterion
  62.  
  63. %Training -> Trains a network net according to net.trainFcn and net.trainParam.
  64. net = train(net, trainMatrixEnhanced, targets);
  65.  
  66. %Access weighs and bias
  67. W = net.IW{1,1};
  68. b = net.b{1,1};
  69.  
  70. load("validateData.mat")
  71. % %Validation
  72. a = sim(net, P, targets)
  73.  
  74. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement