-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAR_WB.m
More file actions
33 lines (31 loc) · 1.34 KB
/
AR_WB.m
File metadata and controls
33 lines (31 loc) · 1.34 KB
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
function [ACC,RANK] = AR_WB(type,TrainIn,TrainOut,TestIn,TestOut)
% White-box algorithm recommendation method
%% LSTM based classification
% Tokenize the reverse Polish expression
TrainIn = cellfun(@(s)num2str(s),TrainIn,'UniformOutput',false);
TrainIn = tokenizedDocument(TrainIn');
TestIn = cellfun(@(s)num2str(s),TestIn,'UniformOutput',false);
TestIn = tokenizedDocument(TestIn');
enc = wordEncoding(TrainIn);
TrainIn = doc2sequence(enc,TrainIn,'Length',27);
TestIn = doc2sequence(enc,TestIn,'Length',27);
[~,TrainOut] = min(cell2mat(TrainOut'),[],2);
TrainOut = categorical(TrainOut);
% Train LSTM
switch type
case 1
layers = [sequenceInputLayer(1)
wordEmbeddingLayer(50,enc.NumWords)
bilstmLayer(50,'OutputMode','last')
fullyConnectedLayer(max(double(TrainOut)))
softmaxLayer
classificationLayer];
options = trainingOptions('adam','MaxEpochs',30,'GradientThreshold',1,'InitialLearnRate',0.01);
net = trainNetwork(TrainIn,TrainOut,layers,options);
save AR_WB net
case 2
load AR_WB net
end
%% Classification
[ACC,RANK] = CalACC(double(classify(net,TestIn)),TestOut);
end