-
Notifications
You must be signed in to change notification settings - Fork 2
/
Assignment_2_5.m
129 lines (99 loc) · 2.65 KB
/
Assignment_2_5.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
clc;
clear all;
close all;
data = xlsread('dataset.xlsx');
%Input normalization
data(:,1:end-1) = (data(:,1:end-1)-mean(data(:,1:end-1)))./std(data(:,1:end-1));
X = data(:,1:end-1); %Inputs
Y = data(:,end); %Target outputs
for i=1:length(Y)
if Y(i)==1
z(i,:) = [1 0];
else
z(i,:) = [0 1];
end
end
%Randomly divide the dataset into training (70%) and testing (30%) set
%p = randperm(length(Y));
C = cvpartition(Y,'HoldOut',0.3);
tr = C.training;
te = C.test;
xTrain = X(tr,:);
xTest = X(te,:);
yTrain = z(tr,:);
yTest = z(te,:);
X=X';
Y=Y';
xTrain=xTrain';
yTrain=yTrain';
xTest=xTest';
yTest=yTest';
hiddenSize1 = 100;
autoenc1 = trainAutoencoder(xTrain,hiddenSize1, ...
'MaxEpochs',100, ...
'L2WeightRegularization',0.004, ...
'SparsityRegularization',4, ...
'SparsityProportion',0.15, ...
'ScaleData', false,...
'DecoderTransferFunction','logsig',...
'EncoderTransferFunction','logsig');
feat1 = encode(autoenc1,xTrain);
hiddenSize2 = 50;
autoenc2 = trainAutoencoder(feat1,hiddenSize2, ...
'MaxEpochs',100, ...
'L2WeightRegularization',0.002, ...
'SparsityRegularization',4, ...
'SparsityProportion',0.1, ...
'ScaleData', false,...
'DecoderTransferFunction','logsig',...
'EncoderTransferFunction','logsig');
feat2 = encode(autoenc2,feat1);
softnet = trainSoftmaxLayer(feat2,yTrain,'MaxEpochs',400);
stackednet = stack(autoenc1,autoenc2,softnet);
stackednet = train(stackednet,xTrain,yTrain);
yPredicted = stackednet(xTest);%2 X 644 double
yPredicted=yPredicted';%644 X 2 double
xTest=xTest';
for i=1:size(yPredicted,1)
if yPredicted(i,1)>0.5
y(i)=1;
else
y(i)=0;
end
end
C1 = cvpartition(y,'HoldOut',0.3);
tr = C1.training;
te = C1.test;
xtrain = xTest(tr,:);
xtest = xTest(te,:);
ytrain = yPredicted(tr,:);
ytest = yPredicted(te,:);
tic
NumberofHiddenNeurons=100;
NumberofTrainingData=size(xtrain,1);
NumberofTestingData=size(xtest,1);
NumberofInputNeurons=size(xtrain,2);
randommat=randn(NumberofInputNeurons+1,NumberofHiddenNeurons);
tempH=[ones(size(xtrain,1),1) xtrain]*randommat;
H=tanh(tempH);
[m,n]=size(H);
OutputWeight=pinv(H)*ytrain;
testH=[ones(size(xtest,1),1) xtest]*randommat;
Ht=tanh(testH);
yn=Ht*OutputWeight;
for i=1:size(xtest,1)
[~, lp(:,i)]=max(yn(i,:));
[~, lt(:,i)]=max(ytest(i,:));
end
[cmt,order]=confusionmat(lp,lt);
display(cmt);
IA=zeros(1,size(cmt,1));
OA=0;
for i=1:size(cmt,1)
IA(i)=cmt(i,i)/sum(cmt(i,:))*100;
OA=OA+cmt(i,i);
end
OA=OA/sum(cmt(:))*100;
toc
display(IA);
display(OA);