NN_tutorial.m
Upload User: cjw9999
Upload Date: 2007-08-05
Package Size: 1k
Code Size: 4k
Category:

AI-NN-PR

Development Platform:

Matlab

  1. clear memory
  2. clear all
  3. clc
  4. nump=3;  % number of classes
  5.  
  6. n=3;     % number of images per class
  7. % training images reshaped into columns in P 
  8. % image size (3x3) reshaped to (1x9)
  9. % training images 
  10. P=[196    35   234   232    59   244   243    57   226; ...
  11.    188    15   236   244    44   228   251    48   230; ...    % class 1
  12.    246    48   222   225    40   226   208    35   234; ...
  13.    
  14.    255   223   224   255     0   255   249   255   235; ...
  15.    234   255   205   251     0   251   238   253   240; ...    % class 2
  16.    232   255   231   247    38   246   190   236   250; ...
  17.    
  18.    25     53   224   255    15    25   249    55   235; ...
  19.    24     25   205   251    10    25   238    53   240; ...    % class 3
  20.    22     35   231   247    38    24   190    36   250]';
  21. % testing images 
  22. N=[208    16   235   255    44   229   236    34   247; ...
  23.    245    21   213   254    55   252   215    51   249; ...    % class 1
  24.    248    22   225   252    30   240   242    27   244; ...
  25.    
  26.    255   241   208   255    28   255   194   234   188; ...
  27.    237   243   237   237    19   251   227   225   237; ...    % class 2
  28.    224   251   215   245    31   222   233   255   254; ...
  29.    
  30.     25    21   208   255    28    25   194    34   188; ...
  31.     27    23   237   237    19    21   227    25   237; ...    % class 3
  32.     24    49   215   245    31    22   233    55   254]';
  33. % Normalization
  34. P=P/256;
  35. N=N/256;
  36. % display the training images 
  37. figure(1),
  38. for i=1:n*nump
  39.     im=reshape(P(:,i), [3 3]);
  40.     im=imresize(im,20);        % resize the image to make it clear
  41.     subplot(nump,n,i),imshow(im);title(strcat('Train image/Class #', int2str(ceil(i/n))))
  42. end
  43. % display the testing images 
  44. figure,
  45. for i=1:n*nump
  46.     im=reshape(N(:,i), [3 3]);
  47.     im=imresize(im,20);        % resize the image to make it clear 
  48.     subplot(nump,n,i),imshow(im);title(strcat('test image #', int2str(i)))
  49. end
  50. % targets
  51. T=[  1  1   1   0   0   0   0   0   0
  52.      0  0   0   1   1   1   0   0   0
  53.      0  0   0   0   0   0   1   1   1 ];
  54. S1=5;   % numbe of hidden layers
  55. S2=3;   % number of output layers (= number of classes)
  56. [R,Q]=size(P); 
  57. epochs = 10000;      % number of iterations
  58. goal_err = 10e-5;    % goal error
  59. a=0.3;                        % define the range of random variables
  60. b=-0.3;
  61. W1=a + (b-a) *rand(S1,R);     % Weights between Input and Hidden Neurons
  62. W2=a + (b-a) *rand(S2,S1);    % Weights between Hidden and Output Neurons
  63. b1=a + (b-a) *rand(S1,1);     % Weights between Input and Hidden Neurons
  64. b2=a + (b-a) *rand(S2,1);     % Weights between Hidden and Output Neurons
  65. n1=W1*P;
  66. A1=logsig(n1);
  67. n2=W2*A1;
  68. A2=logsig(n2);
  69. e=A2-T;
  70. error =0.5* mean(mean(e.*e));    
  71. nntwarn off
  72. for  itr =1:epochs
  73.     if error <= goal_err 
  74.         break
  75.     else
  76.          for i=1:Q
  77.             df1=dlogsig(n1,A1(:,i));
  78.             df2=dlogsig(n2,A2(:,i));
  79.             s2 = -2*diag(df2) * e(:,i);        
  80.             s1 = diag(df1)* W2'* s2;
  81.             W2 = W2-0.1*s2*A1(:,i)';
  82.             b2 = b2-0.1*s2;
  83.             W1 = W1-0.1*s1*P(:,i)';
  84.             b1 = b1-0.1*s1;
  85.             A1(:,i)=logsig(W1*P(:,i),b1);
  86.             A2(:,i)=logsig(W2*A1(:,i),b2);
  87.          end
  88.             e = T - A2;
  89.             error =0.5*mean(mean(e.*e));
  90.             disp(sprintf('Iteration :%5d        mse :%12.6f%',itr,error));
  91.             mse(itr)=error;
  92.     end
  93. end
  94. threshold=0.9;   % threshold of the system (higher threshold = more accuracy)
  95. % training images result
  96. %TrnOutput=real(A2)
  97. TrnOutput=real(A2>threshold)    
  98. % applying test images to NN
  99. n1=W1*N;
  100. A1=logsig(n1);
  101. n2=W2*A1;
  102. A2test=logsig(n2);
  103. % testing images result
  104. %TstOutput=real(A2test)
  105. TstOutput=real(A2test>threshold)
  106. % recognition rate
  107. wrong=size(find(TstOutput-T),1);
  108. recognition_rate=100*(size(N,2)-wrong)/size(N,2)