-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.m
More file actions
56 lines (49 loc) · 1.83 KB
/
Copy pathquery.m
File metadata and controls
56 lines (49 loc) · 1.83 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% This function is used to support user query based on a particular
%%% sentence and return KL or LR result, plot distribution graphs
%%% To run the query, you must run "gradient_kl.m" or "gradient_lr.m"
%%% firstly
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [] = query(sentenceNum)
global sentenceMap F L A method;
rowIndex = find(strcmp(sentenceMap(:,1), sentenceNum));
confidence = sentenceMap(rowIndex, 2);
realValue = L(rowIndex,:)'
% if it's kl method, we need to normalize to get the final result
if (strcmp(method, 'kl'))
estimatedValue = A*F(rowIndex, :)'/sum(A*F(rowIndex, :)')
elseif(strcmp(method, 'lr'))
estimatedValue = A*F(rowIndex, :)'
else
error('Usage: query(sentenceNumber, method) where method is rl or kl');
end
Y = [realValue estimatedValue];
%% plot basic bars
subplot(2,1,1);
bar(Y);
set(gca, 'XTickLabel', {'del', 'ins', 'r_const', 'r_pred', ...
'r_conn', 'r_mix', 'permute'});
%% plot difference
subplot(2,1,2);
diffY = Y(:,1) - Y(:,2);
bar(diffY, 'g');
set(gca, 'XTickLabel', {'del', 'ins', 'r_const', 'r_pred', ...
'r_conn', 'r_mix', 'permute'});
%% calculate KL distance and log
totalKL = 0;
KL = 0;
changeType = size(realValue);
for x = 1: changeType
KL = KL + estimatedValue(x)*log(estimatedValue(x)/realValue(x));
end
totalKL = totalKL + KL;
if (strcmp(method, 'kl'))
result = ['KL distance: ', num2str(totalKL), 'Confidence: ', ...
confidence];
else
result = ['norm2 distance: ', ...
num2str(norm(realValue - estimatedValue),2), ...
'Confidence: ', confidence];
end
display(result);
end