-
Notifications
You must be signed in to change notification settings - Fork 0
/
deeptest.m
145 lines (127 loc) · 3.18 KB
/
deeptest.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
clear all;
% load test data
M = csvread("../test1.csv");
% delete id
Newdata = M(:,2:11);
% set aside test data
Fulldata = Newdata;
Newdata = Newdata(1:size(Newdata,1)-30,:);
leng = size(Newdata,1);
% create label and classify
count_o = 1;
count_x = 1;
for i=1:leng
% participate in T2?
if Newdata(i,7)
% yes
PO (1:6,count_o) = Newdata(i,1:6);
PO (7:9,count_o) = Newdata(i,8:10);
count_o = count_o + 1;
else
% no
PX (1:6,count_x) = Newdata(i,1:6);
PX (7:9,count_x) = Newdata(i,8:10);
count_x = count_x + 1;
end
end
% create a cell array
Data{1,1} = PO;
Data{2,1} = PX;
% Labels ={'1','0'};
Labels ={'1';'0'};
Labels = categorical(Labels);
% get the sequence length for obs
numObservations = numel(Data);
for i=1:numObservations
sequence = Data{i};
sequenceLengths(i) = size(sequence,2);
end
% sort data
[sequenceLengths,idx] = sort(sequenceLengths);
Data = Data(idx);
Labels = Labels(idx);
% setting options
% 9 variables
inputSize = 9;
% 100 hidden units
numHiddenUnits = 100;
% 2 classified outputs
numClasses = 2;
% create layers
layers = [ ...
sequenceInputLayer(inputSize)
bilstmLayer(numHiddenUnits,'OutputMode','last')
fullyConnectedLayer(numClasses)
softmaxLayer
classificationLayer];
maxEpochs = 100;
miniBatchSize = 27;
options = trainingOptions('adam', ...
'ExecutionEnvironment','cpu', ...
'GradientThreshold',1, ...
'MaxEpochs',maxEpochs, ...
'MiniBatchSize',miniBatchSize, ...
'SequenceLength','longest', ...
'Shuffle','never', ...
'Verbose',0, ...
'Plots','training-progress');
net = trainNetwork(Data,Labels,layers,options);
% Test dataset
clear TestData;
clear TData;
clear TLabels;
clear TPLabels;
clear TTData;
TestData = Fulldata(size(Newdata,1)-30+1:size(Newdata,1),:);
leng = size(TestData,1);
% create label and classify
count_o = 1;
count_x = 1;
for i=1:leng
% participate in T2?
if Newdata(i,7)
% yes
TPO (1:6,count_o) = TestData(i,1:6);
TPO (7:9,count_o) = TestData(i,8:10);
count_o = count_o + 1;
else
% no
TPX (1:6,count_x) = TestData(i,1:6);
TPX (7:9,count_x) = TestData(i,8:10);
count_x = count_x + 1;
end
TTData (1:6, i) = TestData(i,1:6);
TTData(7:9, i) = TestData(i,8:10);
end
for i=1:leng
TData{i,1} = TTData(:,i);
end
% create a cell array
% TData{1,1} = TPO;
% TData{2,1} = TPX;
% create label list
for i=1:leng
% Labels ={'1','0'};
% TLabels ={'1';'0'};
% TLabels = categorical(TLabels);
if TestData(i,7) == 1
TLabels{i} = '1';
else
TLabels{i} = '0';
end
end
% extract and sort data
% numObservationsTest = numel(TData);
% clear idx;
% for i=1:numObservationsTest
% sequence = TData(i);
% sequenceLengthsTest(i) = size(sequence,2);
% end
% [sequenceLengthsTest,idx] = sort(sequenceLengthsTest);
% TData = TData(idx);
% TLabels = TLabels(idx);
% Test!
miniBatchSize = 15;
TPLabels = classify(net,TData, ...
'MiniBatchSize',miniBatchSize, ...
'SequenceLength','longest');