SHARE
TWEET

Untitled

a guest May 4th, 2019 104 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. %%%%%% LOAD DATA %%%%%%%
  2. raw = load('podaci_564.mat');
  3. matrix = (raw.data)';
  4.  
  5.  
  6. input = matrix(:, 1:2);
  7. output = matrix(:,3);
  8.  
  9. negative = input(find(output == 0),:);
  10. positive = input(find(output == 1),:);
  11.  
  12. %a - visualize data classes
  13.  
  14. figure('Name', 'Data classes');
  15. %0 - red crosses
  16. plot(negative(:,1), negative(:,2),'r+');
  17. hold on;
  18. %1 - blue circles
  19. plot(positive(:,1), positive(:,2),'bo');
  20. %pause;
  21. hold off;
  22.  
  23. % %NOTE - first 450 data samples are class 0 , next 114 are class 1,
  24. % %for test randomize data - shuffle
  25. % %b  - make test dataset (15% or 20% of original size)
  26.  
  27. input_len = length(input);
  28. shuffled_array = randperm(input_len);
  29. input = input(shuffled_array,:);
  30. output = output(shuffled_array);
  31.  
  32.  
  33. training_offset = int32(0.7 * input_len);
  34. cross_val_offset = training_offset + int32(0.15 * input_len);
  35.  
  36. training_input = input(1:training_offset, :);
  37. training_output = output(1:training_offset, :);
  38.  
  39. crossval_input = input(training_offset + 1:cross_val_offset, :);
  40. crossval_output = output(training_offset + 1:cross_val_offset, :);
  41.  
  42. test_input = input(cross_val_offset + 1:input_len, :);
  43. test_output = output(cross_val_offset + 1:input_len, :);
  44.  
  45.  
  46.  
  47. %c - neural nwtwork creation
  48. %trainlm default
  49. %net = feedforwardnet([3 2]);
  50. net = feedforwardnet([4 3]);
  51. net.layers{3}.transferFcn = 'tansig';
  52.  
  53. %[trainInd,valInd,testInd] = dividerand(length(input));
  54.  
  55.  
  56. net.divideParam.trainRatio = 1; % training set [%]
  57. net.divideParam.valRatio = 0; % validation set [%]
  58. net.divideParam.testRatio = 0; % test set [%]
  59. % neural network training
  60. [net,tr,Y,E] = train(net,training_input',training_output');
  61. % view nn
  62. view(net);
  63.  
  64. %netoutput_training = sim(net, training_input');
  65. %plotconfusion(training_output,Y);
  66.  
  67. %d - cross validation optimization
  68.  
  69. best_acc =0;
  70. best_structure =[3 3];
  71. best_trainFcn = 'poslin';
  72. best_reg = 0.2;
  73. best_c1_weight = (1);
  74. best_f1 = 0;
  75.  
  76. new_training_input = [training_input; crossval_input];
  77. new_training_output = [training_output; crossval_output];
  78. %transfer_functions = create_tf_combinations();
  79.  
  80. for structure = {[2 2] [2 3] [3 3] [ 3 4] [4 4] [4 5] [5 6] [5 5] [6 6] [8 8] [10 10] [10 12] [12 12]}
  81.     for trainFcn = create_tf_combinations();
  82.         for reg = {0,0.14, 0.05, 0.2 0.3, 0.5, 0.8}
  83.             net = feedforwardnet(structure{1});
  84.             net.trainparam.showWindow = false;
  85.             net.divideParam.trainRatio = 1; % training set [%]
  86.             net.divideParam.valRatio = 0; % validation set [%]
  87.             net.divideParam.testRatio = 0; % test set [%]            net.trainParam.Mu = 10;
  88.             net.performParam.regularization = reg{1};
  89.             net.layers{1}.transferFcn = trainFcn{1}{1};
  90.             net.layers{2}.transferFcn = trainFcn{1}{2};
  91.             net.layers{3}.transferFcn = trainFcn{1}{3};
  92.             fprintf('Searching for the best hyperparameters....\n');
  93. %             [net,tr,Y,E] = train(net,crossval_input',crossval_output');
  94. %             netoutput = sim(net, crossval_input');
  95. %             current_f1 = calculate_f1(crossval_output', netoutput);
  96.  
  97.             [net,tr,Y,E] = train(net,new_training_input',new_training_output');
  98.             %netoutput_training = sim(net, new_training_input');
  99.             %current_f1 = calculate_f1(new_training_output', netoutput_training);
  100.             [precision,recall] = calculate_conf_params(new_training_output', Y);
  101.             current_f1 = 2 * precision * recall / (precision + recall);
  102.             if(current_f1 > best_f1)
  103.                 best_f1 = current_f1;
  104.                 best_structure = structure{1};
  105.                 best_reg = reg{1};
  106.                 best_trainFcn = trainFcn{1};
  107.             end
  108.         end
  109.     end
  110. end
  111. fprintf('-----------Best hyperparameters-----------\n');
  112. fprintf('Best layers structure: layer 1 = %d, layer 2 = %d\n', best_structure(1), best_structure(2));
  113. fprintf('Optimal regularization parameter:%2.2f\n', best_reg);
  114. fprintf('Optimal transfer function(1) = %s, transfer function(2) = %s, transfer function(3) = %s\n'...
  115.     ,best_trainFcn{1}, best_trainFcn{2}, best_trainFcn{3});
  116. fprintf('-----------Best hyperparameters-----------\n');
  117.  
  118.  
  119.  
  120. net = feedforwardnet(best_structure);
  121. net.divideParam.trainRatio = 1; % training set [%]
  122. net.divideParam.valRatio = 0; % validation set [%]
  123. net.divideParam.testRatio = 0; % test set [%]            net.trainParam.Mu = 10;
  124. net.performParam.regularization = best_reg;
  125. net.layers{1}.transferFcn = best_trainFcn{1};
  126. net.layers{2}.transferFcn = best_trainFcn{2};
  127. net.layers{3}.transferFcn = best_trainFcn{3};
  128.  
  129.  
  130. %figure
  131. [net,tr,Y,E] = train(net,new_training_input',new_training_output');
  132. %netoutput_training = sim(net, training_input');
  133. %plotconfusion(training_output', netoutput_training);
  134. %hold on;
  135.  
  136. %[net,tr,Y,E] = train(net,new_training_input',new_training_output');
  137. netoutput_training = sim(net, new_training_input');
  138. [precision,recall] = calculate_conf_params(new_training_output', netoutput_training);
  139. fprintf('Training + validation dataset results: precision = %4.4f, recall = %4.4f\n',...
  140.     precision, recall);
  141.  
  142. %[net,tr,Y,E] = train(net,test_input',test_output');
  143. netoutput_test = sim(net, test_input');
  144. [precision,recall] = calculate_conf_params(test_output', netoutput_test);
  145. fprintf('Test dataset results: precision = %4.4f, recall = %4.4f\n',...
  146.     precision, recall);
  147. plotconfusion(new_training_output', netoutput_training, ...
  148.     'Training + validation',test_output', netoutput_test, 'Test');
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