Laboratorio Matlab

Da Bioingegneria Elettronica e Informatica.

Reti Neurali Feed-Forward

Esempio di applicazione di reti neurali artificiali.

Creazione dataset:

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

Implementazione della rete neurale:

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


Cluster Analysis: Self-Organizing Maps

Implementazione della rete SOM:

  1. load filteredyeastdata.mat
  2. rng(0);
  3. [x,std_settings] = mapstd(yeastvalues');  % Normalize data
  4. [x,pca_settings] = processpca(x,0.15);    % PCA
  5.  
  6. clusters_number = 10;
  7.  
  8. dimensions = [clusters_number];
  9. net = selforgmap(dimensions);
  10. view(net)
  11.  
  12. net.trainParam.epochs = 2000;
  13. net.trainParam.showCommandLine = 1;
  14.  
  15. net = train(net,x);
  16.  
  17. figure,plotsompos(net,x);
  18. figure,plotsomhits(net,x);
  19. figure,plotsomnd(net,x);
  20.  
  21.  
  22. y = net(x);
  23. cluster_indices = vec2ind(y);