|
| 1 | +function [xorDesiredOutput,nnOutput,binaryOutput,input] = testXORnonlinear(nnet) |
| 2 | + global filename; |
| 3 | + |
| 4 | + %Number of samples per class = P |
| 5 | + P = 20; |
| 6 | + |
| 7 | + % Generate samples and initialize class index. |
| 8 | + |
| 9 | + dxy=0.02; |
| 10 | + |
| 11 | + k=1; |
| 12 | + for i=0:dxy:1 |
| 13 | + for j=0:dxy:1 |
| 14 | + X(k,:)=[i j]; |
| 15 | + k=k+1; |
| 16 | + end |
| 17 | + end |
| 18 | + |
| 19 | + |
| 20 | + %x1 = rand(P,2); |
| 21 | + %x2 = rand(P,2); |
| 22 | + %x3 = rand(P,2); |
| 23 | + %x4 = rand(P,2); |
| 24 | + |
| 25 | + %xoffset = 1.0; |
| 26 | + %yoffset = 1.0; |
| 27 | + |
| 28 | + %x2 = x2 + repmat([xoffset,0],P,1); |
| 29 | + %x3 = x3 + repmat([xoffset,yoffset],P,1); |
| 30 | + %x4 = x4 + repmat([0,yoffset],P,1); |
| 31 | + |
| 32 | + %X(1:P,1:2) = x1; |
| 33 | + %X(1:P,3) = repmat(1,P,1); |
| 34 | + |
| 35 | + %X(P+1:2*P,1:2) = x2; |
| 36 | + %X(P+1:2*P,3) = repmat(0,P,1); |
| 37 | + |
| 38 | + %X(2*P+1:3*P,1:2) = x3; |
| 39 | + %X(2*P+1:3*P,3) = repmat(1,P,1); |
| 40 | + |
| 41 | + %X(3*P+1:4*P,1:2) = x4; |
| 42 | + %X(3*P+1:4*P,3) = repmat(0,P,1); |
| 43 | + |
| 44 | + [N,col] = size(X); |
| 45 | + %d = col-1; |
| 46 | + d=col; |
| 47 | + |
| 48 | + % Normalize data. |
| 49 | + Xmax = max(max(abs(X(:,1:d)))); |
| 50 | + X(:,1:d) = X(:,1:d)/Xmax; |
| 51 | + |
| 52 | + % Identify elements of class-1 and class-2. |
| 53 | + %i1 = find(X(:,col) == 1); |
| 54 | + %i2 = find(X(:,col) == 0); |
| 55 | + %X1 = X(i1,1:d); |
| 56 | + %X2 = X(i2,1:d); |
| 57 | + |
| 58 | + input = X(:,1:d)' |
| 59 | + [r,c] = size(input); |
| 60 | + |
| 61 | + xorDesiredOutput = X(:,col)'; |
| 62 | + |
| 63 | + output = sim(nnet,input); |
| 64 | + |
| 65 | + xorDesiredOutput = xorDesiredOutput(:) |
| 66 | + nnOutput = output(:) |
| 67 | + binaryOutput = round(nnOutput) |
| 68 | + |
| 69 | + o1 = find(binaryOutput == 1); |
| 70 | + o2 = find(binaryOutput == 0); |
| 71 | + O1 = X(o1,1:d); |
| 72 | + O2 = X(o2,1:d); |
| 73 | + |
| 74 | + testflag = 1; |
| 75 | + if testflag==1 |
| 76 | + |
| 77 | + %%% Plotting the testing samples %%%% |
| 78 | + |
| 79 | + figure; |
| 80 | + axes('FontSize', 14); |
| 81 | + plot(O1(:,1), O1(:,2), 'bv', "markersize", 10); |
| 82 | + hold on; |
| 83 | + plot(O2(:,1), O2(:,2), 'r^', "markersize", 10); |
| 84 | + % line([x1 x2]', [y1 y2]', 'Color', [0 0 0]); |
| 85 | + xlabel('x-coordinate of test sample'); |
| 86 | + ylabel('y-coordinate of test sample'); |
| 87 | + xlim([-0.1 1.2]); |
| 88 | + ylim([-0.2 1.2]); |
| 89 | + end |
| 90 | + |
| 91 | + % Save figure; |
| 92 | + print(strcat(filename, "-2.png")); |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | + return; |
| 97 | + |
0 commit comments