Laboratorio Matlab: differenze tra le versioni

Da Bioingegneria Elettronica e Informatica.
(Reti Neurali Feed-Forward)
Riga 89: Riga 89:
  
 
%% Test Rete
 
%% Test Rete
 
 
y_test = net(x_test);
 
y_test = net(x_test);
 
figure, plotconfusion(t_test,y_test),title('Test Confusion Matrix')
 
figure, plotconfusion(t_test,y_test),title('Test Confusion Matrix')
  
 
</syntaxhighlight>
 
</syntaxhighlight>

Versione delle 14:33, 21 mag 2019

Reti Neurali Feed-Forward

Esempio di applicazione di reti neurali artificiali.

Creazione dataset.

  1. clear all, close all
  2.  
  3. load cancer_dataset.mat
  4. x = cancerInputs;
  5. t = cancerTargets(1,:);
  6.  
  7. temp = [x;t];
  8. rng(0)
  9. p = randperm(size(temp,2));
  10.  
  11. train_size = floor(size(temp,2)*.8);
  12. p_train = p(1:train_size);
  13. p_test = p(train_size+1:end);
  14.  
  15. x_train = temp(1:9,p_train);
  16. t_train = temp(10,p_train);
  17.  
  18. x_test = temp(1:9,p_test);
  19. t_test = temp(10,p_test);
  20.  
  21. save('dataset2.mat','x_train','t_train','x_test','t_test')

Implementazione della rete neurale:

  1. clear all, close all
  2.  
  3. load dataset2.mat
  4.  
  5. disp(['# Features: ',num2str(size(x_train,1))])
  6. disp(['# Samples: ',num2str(size(x_train,2))])
  7.  
  8. %% Creazione rete
  9.  
  10. % Layers nascosti
  11. % hiddenLayerSize = [20];
  12. % hiddenLayerSize = [50];
  13. hiddenLayerSize = [20,10];
  14.  
  15.  
  16. % Training Function - help nntrain
  17. trainFcn = 'traingdx'; % traingda, traingdm, traingd
  18.  
  19. % Creazione rete
  20. net = patternnet(hiddenLayerSize, trainFcn);
  21.  
  22. % Suddivisione dataset
  23. net.divideFcn = 'dividerand';
  24. net.divideParam.trainRatio = 70/100;
  25. net.divideParam.valRatio = 30/100;
  26. net.divideParam.testRatio = 0/100;
  27.  
  28. % Criteri di stop
  29. net.trainParam.epochs = 5000;;
  30. %net.trainParam.max_fail = 20;
  31. %net.trainParam.min_grad = 0;%10e-5;
  32.  
  33. % Funzione errore
  34. net.performFcn = 'mse';
  35.  
  36. % Funzioni di attivazione
  37. net.layers{end}.transferFcn = 'logsig';
  38.  
  39. % Visualizza rete
  40. view(net)
  41.  
  42. %% Inizializzazione Rete
  43. rng(0)
  44. net = configure(net,x_train,t_train);
  45. net = init(net);
  46. init_LW = net.LW;
  47. init_IW = net.IW;
  48.  
  49. %% Addestramento Rete
  50. [net,tr] = train(net,x_train,t_train);
  51. y_train = net(x_train);
  52.  
  53. % Plots vari
  54. figure, plotperform(tr)
  55. %figure, plottrainstate(tr)
  56. %figure, ploterrhist(e)
  57. figure, plotconfusion(t_train,y_train),title('Training Confusion Matrix')
  58. %figure, plotroc(t,y)
  59.  
  60. %% Test Rete
  61. y_test = net(x_test);
  62. figure, plotconfusion(t_test,y_test),title('Test Confusion Matrix')