Skip to content

Commit 15bcf67

Browse files
committed
added backend files
1 parent 7eaf305 commit 15bcf67

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+3815
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
if ($trainFlag == 1) {
3+
if ($numberOfBits == -1 | $numberOfBits == -2) {
4+
echo '<table><tr style="text-align: center;"><td><b>Training data</b></td><td><b>Training error</b></td></tr>';
5+
echo "<td><img name=curimage id=curimage width=600 src=tmp/mlffnntrainingSamples.png> </td>";
6+
echo "<td><img name=curimage id=curimage width=600 src=tmp/mlffnnState.png> </td>";
7+
echo '</tr></table>';
8+
} else
9+
echo "'<table><tr style=\"text-align: center;\"><td><b>Training error</b></td></tr>
10+
<tr><td><img name=curimage id=curimage width=600 src=tmp/mlffnnState.png></td></tr></table>'";
11+
12+
}
13+
14+
if ($testFlag == 1) {
15+
if ($numberOfBits == -1 | $numberOfBits == -2) {
16+
echo '<table><tr style="text-align: center;"><td><b>Training data</b></td><td><b>Classification of test data</b></td></tr>';
17+
echo "<tr><td><img name=curimage id=curimage width=600 src=tmp/mlffnntrainingSamples.png></td>";
18+
echo "<td><img name=curimage id=curimage width=600 src=tmp/mlffnntestingSamples.png> </td></tr></table>";
19+
} else {
20+
echo '<br><b>Result of pattern classification</b><br>';
21+
echo implode('', file("tmp/results.txt"));
22+
//echo nl2br(implode('',file('tmp/results.txt')));
23+
}
24+
}
25+
?>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
% arg is either nbits (testxorcore) or numberofhiddennodes (trainxorcore)
2+
function runexp(type, nbits, nhiddennodes, filepath)
3+
global filename;
4+
filename = filepath;
5+
6+
if type == 1
7+
trainXORcore(nbits, nhiddennodes);
8+
else
9+
testXORcore(nbits);
10+
end
11+
12+
return;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function [xorDesiredOutput,nnOutput,binaryOutput,input] = testXOR2bit(nnet)
2+
3+
input = [0 0; 0 1; 1 0; 1 1]';
4+
[r,c] = size(input);
5+
6+
for i=1:c
7+
xorDesiredOutput(i) = xor(input(1,i), input(2,i));
8+
end
9+
10+
output = sim(nnet, input);
11+
12+
xorDesiredOutput = xorDesiredOutput(:)
13+
nnOutput = output(:)
14+
binaryOutput = round(nnOutput)
15+
16+
return;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function [xorDesiredOutput,nnOutput,binaryOutput,input] = testXOR3bit(nnet)
2+
3+
input = [0 0 0; 0 0 1; 0 1 0; 0 1 1; 1 0 0; 1 0 1; 1 1 0; 1 1 1]';
4+
[r,c] = size(input);
5+
6+
for i=1:c
7+
xorDesiredOutput(i) = xor(input(1,i), xor(input(2,i), input(3,i)) );
8+
end
9+
10+
%input1 = input(1:2,:);
11+
%output1 = sim(nnet, input1);
12+
13+
%input2(1,:) = round((output1(:))');
14+
%input2(2,:) = input(3,:);
15+
16+
%output2 = sim(nnet, input2);
17+
18+
output = sim(nnet,input);
19+
20+
xorDesiredOutput = xorDesiredOutput(:)
21+
%nnOutput = output2(:)
22+
nnOutput = output(:)
23+
binaryOutput = round(nnOutput)
24+
25+
return;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
function [xorDesiredOutput,nnOutput,binaryOutput,input] = testXOR4bit(nnet)
2+
3+
input = [0 0 0 0; 0 0 0 1; 0 0 1 0; 0 0 1 1; 0 1 0 0; 0 1 0 1; 0 1 1 0; 0 1 1 1;1 0 0 0; 1 0 0 1; 1 0 1 0; 1 0 1 1; 1 1 0 0; 1 1 0 1; 1 1 1 0; 1 1 1 1]';
4+
[r,c] = size(input);
5+
6+
for i=1:c
7+
tmpOut = xor(input(1,i), xor(input(2,i), input(3,i)));
8+
xorDesiredOutput(i) = xor(tmpOut, input(4,i));
9+
end
10+
11+
clear tmpOut;
12+
13+
%input1 = input(1:2,:);
14+
%input2 = input(3:4,:);
15+
16+
%output1 = sim(nnet, input1);
17+
%output2 = sim(nnet, input2);
18+
19+
%input3(1,:) = round((output1(:))');
20+
%input3(2,:) = round((output2(:))');
21+
%output3 = sim(nnet, input3);
22+
output = sim(nnet, input);
23+
24+
xorDesiredOutput = xorDesiredOutput(:)
25+
%nnOutput = output3(:)
26+
nnOutput = output(:)
27+
binaryOutput = round(nnOutput)
28+
29+
return;
30+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
function [] = testXORcore(nbits)
2+
global filename;
3+
nb=nbits;
4+
5+
load(strcat(filename, ".dat"));
6+
7+
switch(nb)
8+
case 2
9+
[desout,actout,binout,input]=testXOR2bit(nnet);
10+
case 3
11+
[desout,actout,binout,input]=testXOR3bit(nnet);
12+
case 4
13+
[desout,actout,binout,input]=testXOR4bit(nnet);
14+
case -1
15+
[desout,actout,binout,input]=testXORlinear(nnet);
16+
case -2
17+
[desout,actout,binout,input]=testXORnonlinear(nnet);
18+
end
19+
20+
filepath = strcat(filename, "-result.txt");
21+
fid=fopen(filepath,'w');
22+
fprintf(fid,'<table border=1>\n');
23+
fprintf(fid,'<tr><td> <b>Input pattern</b> </td><td> <b>MLFFNN out</b> </td><td> <b>Binary out</b> </td><td> <b>Desired out</b> </td></tr>\n');
24+
25+
for i=1:length(input)
26+
fprintf(fid,'<tr align=center><td>');
27+
fprintf(fid,'%1d ',input(:,i));
28+
fprintf(fid,'</td>');
29+
fprintf(fid,'<td> %1.4f </td>',actout(i));
30+
31+
if (binout(i)==desout(i))
32+
fprintf(fid,'<td> %1d </td>',binout(i));
33+
else
34+
fprintf(fid,'<td><font color=ff0000> %1d </font></td>',binout(i));
35+
endif
36+
fprintf(fid,'<td> %1d </td>',desout(i));
37+
fprintf(fid,'</tr>\n');
38+
end
39+
40+
fprintf(fid,'</table>\n');
41+
fclose(fid);
42+
43+
return;
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
function [xorDesiredOutput,nnOutput,binaryOutput,input] = testXORlinear(nnet)
2+
global filename;
3+
4+
%Number of samples per class = P
5+
P = 20;
6+
7+
dxy=0.02;
8+
9+
k=1;
10+
for i=0:dxy:1
11+
for j=0:dxy:1
12+
X(k,:)=[i j];
13+
k=k+1;
14+
end
15+
end
16+
17+
% Generate samples and initialize class index.
18+
%x1 = rand(P,2);
19+
%x2 = rand(P,2);
20+
21+
%xoffset = 1.0;
22+
%yoffset = 1.0;
23+
24+
%x2 = x2 + repmat([xoffset,yoffset],P,1);
25+
26+
%X(1:P,1:2) = x1;
27+
%X(1:P,3) = repmat(1,P,1);
28+
29+
%X(P+1:2*P,1:2) = x2;
30+
%X(P+1:2*P,3) = repmat(0,P,1);
31+
32+
[N,col] = size(X);
33+
%d = col-1;
34+
d = col;
35+
36+
% Normalize data.
37+
Xmax = max(max(abs(X(:,1:d))));
38+
X(:,1:d) = X(:,1:d)/Xmax;
39+
40+
% Identify elements of class-1 and class-2.
41+
%i1 = find(X(:,col) == 1);
42+
%i2 = find(X(:,col) == 0);
43+
%X1 = X(i1,1:d);
44+
%X2 = X(i2,1:d);
45+
46+
input = X(:,1:d)'
47+
[r,c] = size(input);
48+
49+
xorDesiredOutput = X(:,col)';
50+
51+
output = sim(nnet,input);
52+
53+
xorDesiredOutput = xorDesiredOutput(:)
54+
nnOutput = output(:)
55+
binaryOutput = round(nnOutput)
56+
57+
o1 = find(binaryOutput == 1);
58+
o2 = find(binaryOutput == 0);
59+
O1 = X(o1,1:d);
60+
O2 = X(o2,1:d);
61+
62+
testflag = 1;
63+
if testflag==1
64+
65+
%%% Plotting the testing samples %%%%
66+
67+
figure;
68+
axes('FontSize', 14);
69+
plot(O1(:,1), O1(:,2), 'b*', "markersize", 10);
70+
hold on;
71+
plot(O2(:,1), O2(:,2), 'r*', "markersize", 10);
72+
% line([x1 x2]', [y1 y2]', 'Color', [0 0 0]);
73+
xlabel('x-coordinate of test sample');
74+
ylabel('y-coordinate of test sample');
75+
xlim([-0.1 1.2]);
76+
ylim([-0.2 1.2]);
77+
end
78+
79+
% Save figure;
80+
print(strcat(filename, "-2.png"));
81+
82+
return;
83+
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
function [nnet] = trainXOR2bit(numberOfhiddenNodes)
2+
global filename;
3+
4+
%inputDim
5+
%numberOfhiddenNodes
6+
7+
input = [0 0; 0 1; 1 0; 1 1]';
8+
[r,c] = size(input);
9+
10+
for i=1:c
11+
xorDesiredOutput(i) = xor(input(1,i), input(2,i));
12+
end
13+
14+
xorDesiredOutput
15+
16+
% num. of input nodes (dimensionality of input).
17+
18+
% min and max values for 2 input elements.
19+
Pr = repmat([0 1], 2, 1);
20+
Pr
21+
% num. of elements in Ss = num. of layers (excluding i/p, including o/p).
22+
Ss = [numberOfhiddenNodes 1];
23+
%a=6;
24+
%Ss = [a 1];
25+
Ss
26+
27+
%trf = {"tansig","purelin"};
28+
trf = {"tansig","tansig"};
29+
%trf = {"tansig","logsig"};
30+
btf = "trainlm";
31+
blf = "learngdm";
32+
pf = "mse";
33+
nnet = newff(Pr, Ss, trf, btf, blf, pf);
34+
35+
figure;
36+
nnet = train(nnet, input, xorDesiredOutput);
37+
38+
%obtainedOutput = sim(nnet,input);
39+
%xorDesiredOutput
40+
%round(obtainedOutput)
41+
42+
43+
xlabel('Number of iterations');
44+
ylabel('Mean squared error');
45+
title('Variation of training error');
46+
47+
clear input r c xorDesiredOutput Pr Ss trf btf blf pf;
48+
49+
% Save figure;
50+
print(strcat(filename, "-2.png"));
51+
52+
% Save state of variables.
53+
save(strcat(filename, ".dat"));
54+
55+
return;
56+

0 commit comments

Comments
 (0)