MSTperl/TrainerLabelling.pm

filter: Statementstmt 53.2% 130 / 244 Branchbran 36.7% 25 / 68 Conditioncond 24.7% 26 / 105 MC/DCmcdc 0.0% 0 / 45 Subroutinesub 83.3% 15 / 18 Podpod 7.6% 1 / 13 totaltotal 39.9% 197 / 493 CC 90 SCAR 43.3
CC
90
Cov
43%
preprocess_sentence
165.4
mira_tree_update
106.8
mira_update
76.1
CRAP 76.2
« MSTperl/Labeller.pm ↑ Summary MSTperl/ModelAdditional.pm »
1 package Treex::Tool::Parser::MSTperl::TrainerLabelling;
2 {
3 2 $Treex::Tool::Parser::MSTperl::TrainerLabelling::VERSION = '0.11949';
4 }
5  
6 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 extends 'Treex::Tool::Parser::MSTperl::TrainerBase';
10  
11 2 ▶use Treex::Tool::Parser::MSTperl::Labeller;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 has model => (
14 isa => 'Treex::Tool::Parser::MSTperl::ModelLabelling',
15 is => 'rw',
16 );
17  
18 2 has labeller => (
19 isa => 'Treex::Tool::Parser::MSTperl::Labeller',
20 is => 'rw',
21 );
22  
23 # All emissions used during the training summed together
24 # as averaging is reported to help avoid overtraining
25 # (M. Collins. 2002. Discriminative training methods for
26 # hidden Markov models: Theory and experiments with
27 # perceptron algorithms. In Proc. EMNLP.)
28 # Structure:
29 # emissions_summed->{feature}->{label} = score
30 has emissions_summed => (
31 isa => 'HashRef',
32 is => 'rw',
33 1 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
34 2 );
35  
36 # same as emissions_summed but for pairs of labels, structure is
37 # transitions_summed->{feature}->{label_prev}->{label} = score
38 # or
39 # transitions_summed->{label_prev}->{label} = score
40 # depending on the algorithm used
41 has transitions_summed => (
42 isa => 'HashRef',
43 is => 'rw',
44 1 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
45 2 );
46  
47 1 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
48 1 my ($self) = @_;
49  
50 1 $self->labeller(
51 Treex::Tool::Parser::MSTperl::Labeller->new( config => $self->config )
52 );
53 1 $self->model( $self->labeller->model );
54 1 $self->featuresControl( $self->config->labelledFeaturesControl );
55 1 $self->number_of_iterations( $self->config->labeller_number_of_iterations );
56  
57 1 return;
58 }
59  
60 # LABELLING TRAINING
61  
62 # compute the features of the sentence,
63 # build list of existing labels
64 # and compute the transition scores
65 7 ▶sub preprocess_sentence {
Subroutine preprocess_sentence: called CC=22 SCAR=51.1
Pod undocumented
66  
67 # (Treex::Tool::Parser::MSTperl::Sentence $sentence, Num $progress)
68 7 my ( $self, $sentence, $progress ) = @_;
69  
70 # compute edges and their features
71 7 $sentence->fill_fields_before_labelling();
72  
73 # $sentence->fill_fields_after_labelling();
74  
75 7 $self->compute_unigram_counts($sentence);
76  
77 7 my $ALGORITHM = $self->config->labeller_algorithm;
78  
79 # compute transition counts
80  
81 7 ▶ if ($ALGORITHM == 5
Branch: 2 branches
BranchTrueFalseTotal
if ($ALGORITHM == 5 or $ALGORITHM == 12 or $ALGORITHM == 13 or $ALGORITHM == 15 or $ALGORITHM == 16 or $ALGORITHM == 17 or $ALGORITHM == 18 or $ALGORITHM == 19) { } 7 0 7
elsif ($ALGORITHM == 0 or $ALGORITHM == 1 or $ALGORITHM == 2 or $ALGORITHM == 3 or $ALGORITHM == 4 or $ALGORITHM == 9 or $ALGORITHM == 10 or $ALGORITHM == 11 or $ALGORITHM == 14) { } 0 0 0
Condition33%7/21
$ALGORITHM == 5 or $ALGORITHM == 12 or $ALGORITHM == 13 or $ALGORITHM == 15 or $ALGORITHM == 16 or $ALGORITHM == 17 or $ALGORITHM == 18 or $ALGORITHM == 19
l!l&&r!l&&!r
700
$ALGORITHM == 5 or $ALGORITHM == 12 or $ALGORITHM == 13 or $ALGORITHM == 15 or $ALGORITHM == 16 or $ALGORITHM == 17 or $ALGORITHM == 18
l!l&&r!l&&!r
070
$ALGORITHM == 5 or $ALGORITHM == 12 or $ALGORITHM == 13 or $ALGORITHM == 15 or $ALGORITHM == 16 or $ALGORITHM == 17
l!l&&r!l&&!r
007
$ALGORITHM == 5 or $ALGORITHM == 12 or $ALGORITHM == 13 or $ALGORITHM == 15 or $ALGORITHM == 16
l!l&&r!l&&!r
007
$ALGORITHM == 5 or $ALGORITHM == 12 or $ALGORITHM == 13 or $ALGORITHM == 15
l!l&&r!l&&!r
007
$ALGORITHM == 5 or $ALGORITHM == 12 or $ALGORITHM == 13
l!l&&r!l&&!r
007
$ALGORITHM == 5 or $ALGORITHM == 12
l!l&&r!l&&!r
007
MC/DC0%0/8
$ALGORITHM == 5 or $ALGORITHM == 12 or $ALGORITHM == 13 or $ALGORITHM == 15 or $ALGORITHM == 16 or $ALGORITHM == 17 or $ALGORITHM == 18 or $ALGORITHM == 19
$ALGORITHM == 5 $ALGORITHM == 12 $ALGORITHM == 13 $ALGORITHM == 15 $ALGORITHM == 16 $ALGORITHM == 17 $ALGORITHM == 18 $ALGORITHM == 19
Truth table11%1/9
A or B or C or D or E or F or G or H
A= $ALGORITHM == 5B= $ALGORITHM == 12C= $ALGORITHM == 13D= $ALGORITHM == 15E= $ALGORITHM == 16F= $ALGORITHM == 17G= $ALGORITHM == 18H= $ALGORITHM == 19
ABCDEFGHresult
1XXXXXXX1
01XXXXXX1
001XXXXX1
0001XXXX1
00001XXX1
000001XX1
0000001X1
000000011
000000000
82 || $ALGORITHM == 12
83 || $ALGORITHM == 13
84 || $ALGORITHM == 15
85 || $ALGORITHM == 16
86 || $ALGORITHM == 17
87 || $ALGORITHM == 18
88 || $ALGORITHM == 19
89 )
90 {
91  
92 # MLE transitions with smoothing
93 7 ▶ if ( $progress < $self->config->EM_heldout_data_at ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($progress < $self->config->EM_heldout_data_at) { } 6 1 7
94 6 $self->compute_transition_counts( $sentence->getNodeByOrd(0) );
95 } else {
96  
97 # do not use this sentence for transition counts,
98 # it will be used for EM algorithm to compute smoothing params
99 1 push @{ $self->model->EM_heldout_data }, $sentence;
100 }
101 ▶ } elsif (
Condition0%0/24
$ALGORITHM == 0 or $ALGORITHM == 1 or $ALGORITHM == 2 or $ALGORITHM == 3 or $ALGORITHM == 4 or $ALGORITHM == 9 or $ALGORITHM == 10 or $ALGORITHM == 11 or $ALGORITHM == 14
l!l&&r!l&&!r
000
$ALGORITHM == 0 or $ALGORITHM == 1 or $ALGORITHM == 2 or $ALGORITHM == 3 or $ALGORITHM == 4 or $ALGORITHM == 9 or $ALGORITHM == 10 or $ALGORITHM == 11
l!l&&r!l&&!r
000
$ALGORITHM == 0 or $ALGORITHM == 1 or $ALGORITHM == 2 or $ALGORITHM == 3 or $ALGORITHM == 4 or $ALGORITHM == 9 or $ALGORITHM == 10
l!l&&r!l&&!r
000
$ALGORITHM == 0 or $ALGORITHM == 1 or $ALGORITHM == 2 or $ALGORITHM == 3 or $ALGORITHM == 4 or $ALGORITHM == 9
l!l&&r!l&&!r
000
$ALGORITHM == 0 or $ALGORITHM == 1 or $ALGORITHM == 2 or $ALGORITHM == 3 or $ALGORITHM == 4
l!l&&r!l&&!r
000
$ALGORITHM == 0 or $ALGORITHM == 1 or $ALGORITHM == 2 or $ALGORITHM == 3
l!l&&r!l&&!r
000
$ALGORITHM == 0 or $ALGORITHM == 1 or $ALGORITHM == 2
l!l&&r!l&&!r
000
$ALGORITHM == 0 or $ALGORITHM == 1
l!l&&r!l&&!r
000
MC/DC0%0/9
$ALGORITHM == 0 or $ALGORITHM == 1 or $ALGORITHM == 2 or $ALGORITHM == 3 or $ALGORITHM == 4 or $ALGORITHM == 9 or $ALGORITHM == 10 or $ALGORITHM == 11 or $ALGORITHM == 14
$ALGORITHM == 0 $ALGORITHM == 1 $ALGORITHM == 2 $ALGORITHM == 3 $ALGORITHM == 4 $ALGORITHM == 9 $ALGORITHM == 10 $ALGORITHM == 11 $ALGORITHM == 14
Truth table0%0/10
A or B or C or D or E or F or G or H or I
A= $ALGORITHM == 0B= $ALGORITHM == 1C= $ALGORITHM == 2D= $ALGORITHM == 3E= $ALGORITHM == 4F= $ALGORITHM == 9G= $ALGORITHM == 10H= $ALGORITHM == 11I= $ALGORITHM == 14
ABCDEFGHIresult
1XXXXXXXX1
01XXXXXXX1
001XXXXXX1
0001XXXXX1
00001XXXX1
000001XXX1
0000001XX1
00000001X1
0000000011
0000000000
102 $ALGORITHM == 0
103 || $ALGORITHM == 1
104 || $ALGORITHM == 2
105 || $ALGORITHM == 3
106 || $ALGORITHM == 4
107 || $ALGORITHM == 9
108 || $ALGORITHM == 10
109 || $ALGORITHM == 11
110 || $ALGORITHM == 14
111 )
112 {
113  
114 # MLE transitions without smoothing
115 0 $self->compute_transition_counts( $sentence->getNodeByOrd(0) );
116 }
117  
118 # else (8) MLE transition counts not used
119  
120 7 ▶ if ($ALGORITHM == 4
Branch: 1 branch
BranchTrueFalseTotal
if ($ALGORITHM == 4 or $ALGORITHM == 5 or $ALGORITHM == 9) 0 7 7
Condition33%2/6
$ALGORITHM == 4 or $ALGORITHM == 5 or $ALGORITHM == 9
l!l&&r!l&&!r
007
$ALGORITHM == 4 or $ALGORITHM == 5
l!l&&r!l&&!r
007
MC/DC0%0/3
$ALGORITHM == 4 or $ALGORITHM == 5 or $ALGORITHM == 9
$ALGORITHM == 4 $ALGORITHM == 5 $ALGORITHM == 9
Truth table25%1/4
A or B or C
A= $ALGORITHM == 4B= $ALGORITHM == 5C= $ALGORITHM == 9
ABCresult
1XX1
01X1
0011
0000
121 || $ALGORITHM == 5
122 || $ALGORITHM == 9
123 )
124 {
125  
126 # compute MLE emission counts for Viterbi
127 0 $self->compute_emission_counts($sentence);
128 }
129  
130 7 return;
131 }
132  
133 # computes label unigram counts for a sentence
134 7 ▶sub compute_unigram_counts {
Subroutine compute_unigram_counts: called CC=2 SCAR=6.9
Pod undocumented
135  
136 # (Treex::Tool::Parser::MSTperl::Node $parent_node)
137 7 my ( $self, $sentence ) = @_;
138  
139 7 foreach my $edge ( @{ $sentence->edges } ) {
140 92 my $label = $edge->child->label;
141 92 $self->model->add_unigram($label);
142 }
143  
144 # TODO: do this?
145 7 $self->model->add_unigram( $self->config->SEQUENCE_BOUNDARY_LABEL );
146  
147 7 return;
148 }
149  
150 # computes transition counts for a tree (recursively)
151 726 ▶sub compute_transition_counts {
Subroutine compute_transition_counts: called CC=7 SCAR=21.0
Pod undocumented
152  
153 # (Treex::Tool::Parser::MSTperl::Node $parent_node)
154 726 my ( $self, $parent_node ) = @_;
155  
156 # stopping condition
157 726 ▶ if ( scalar( @{ $parent_node->children } ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$parent_node->children;} == 0) 472 254 726
158 472 return;
159 }
160  
161 254 my $ALGORITHM = $self->config->labeller_algorithm;
162  
163 # compute transition counts
164 254 my $last_label = $self->config->SEQUENCE_BOUNDARY_LABEL;
165 254 foreach my $edge ( @{ $parent_node->children } ) {
166 720 my $this_label = $edge->child->label;
167  
168 720 ▶ if ( $ALGORITHM == 9 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ALGORITHM == 9) { } 0 720 720
169 0 my $features = $edge->features;
170 0 foreach my $feature (@$features) {
171 0 $self->model->add_transition(
172 $this_label, $last_label, $feature
173 );
174 }
175 } else {
176 720 $self->model->add_transition( $this_label, $last_label );
177 }
178  
179 720 $last_label = $this_label;
180 720 $self->compute_transition_counts( $edge->child );
181 }
182  
183 # add SEQUENCE_BOUNDARY_LABEL to end of sequence as well
184 # TODO: currently not used in Viterbi
185 254 ▶ if ( $ALGORITHM == 9 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ALGORITHM == 9) { } 0 254 254
186  
187 # TODO: cannot use this since there is actually no such edge
188 # and therefore has no features
189 0 my $features = [];
190 0 foreach my $feature (@$features) {
191 0 $self->model->add_transition(
192 $self->config->SEQUENCE_BOUNDARY_LABEL, $last_label, $feature
193 );
194 }
195 } else {
196 254 $self->model->add_transition(
197 $self->config->SEQUENCE_BOUNDARY_LABEL, $last_label
198 );
199 }
200  
201 254 return;
202 }
203  
204 # algs 4, 5, 9
205 0 ▶sub compute_emission_counts {
Subroutine compute_emission_counts: not called CC=3 SCAR=24.8
Pod undocumented
206  
207 # (Treex::Tool::Parser::MSTperl::Sentence $sentence)
208 0 my ( $self, $sentence ) = @_;
209  
210 0 foreach my $edge ( @{ $sentence->edges } ) {
211 0 my $label = $edge->child->label;
212 0 foreach my $feature ( @{ $edge->features } ) {
213 0 $self->model->add_emission( $feature, $label );
214 }
215 }
216  
217 0 return;
218 }
219  
220 14 ▶sub update {
Subroutine update: called CC=6 SCAR=25.3
Pod undocumented
221  
222 # (Treex::Tool::Parser::MSTperl::Sentence $sentence_correct_labelling,
223 # Int $sumUpdateWeight)
224 my (
225 14 $self,
226 $sentence_correct_labelling,
227 $sumUpdateWeight
228 ) = @_;
229  
230 14 my $ALGORITHM = $self->config->labeller_algorithm;
231  
232 14 ▶ if ( $ALGORITHM == 4 || $ALGORITHM == 5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ALGORITHM == 4 or $ALGORITHM == 5) 0 14 14
Condition33%1/3
$ALGORITHM == 4 or $ALGORITHM == 5
l!l&&r!l&&!r
0014
MC/DC0%0/2
$ALGORITHM == 4 or $ALGORITHM == 5
$ALGORITHM == 4 $ALGORITHM == 5
Truth table33%1/3
A or B
A= $ALGORITHM == 4B= $ALGORITHM == 5
ABresult
1X1
011
000
233  
234 # these are pure MLE setups with no use of MIRA
235 0 return;
236 }
237  
238 # relabel the sentence
239 # l' = argmax_l' s(l', x_t, y_t)
240 14 my $sentence_best_labelling = $self->labeller->label_sentence_internal(
241 $sentence_correct_labelling
242 );
243  
244 # nothing to do now in fill_fields_after_labelling()
245 # $sentence_best_labelling->fill_fields_after_labelling();
246  
247 # only progress and/or debug info
248 14 ▶ if ( $self->config->DEBUG >= 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 2) 0 14 14
249 0 print "CORRECT LABELS:\n";
250 0 foreach my $node ( @{ $sentence_correct_labelling->nodes_with_root } ) {
251 0 print $node->ord . "/" . $node->label . "\n";
252 }
253 0 print "BEST SCORING LABELS:\n";
254 0 foreach my $node ( @{ $sentence_best_labelling->nodes_with_root } ) {
255 0 print $node->ord . "/" . $node->label . "\n";
256 }
257 }
258  
259 # min ||w_i+1 - w_i|| s.t. ...
260 $self->mira_update(
261 14 $sentence_correct_labelling,
262 $sentence_best_labelling,
263 $sumUpdateWeight
264 );
265  
266 14 return;
267  
268 }
269  
270 14 ▶sub mira_update {
Subroutine mira_update: called CC=13 SCAR=43.3
Pod documented
271  
272 # (Treex::Tool::Parser::MSTperl::Sentence $sentence_correct_labelling,
273 # Treex::Tool::Parser::MSTperl::Sentence $sentence_best_labelling,
274 # Int $sumUpdateWeight)
275 my (
276 14 $self,
277 $sentence_correct_labelling,
278 $sentence_best_labelling,
279 $sumUpdateWeight
280 ) = @_;
281  
282 14 my $ALGORITHM = $self->config->labeller_algorithm;
283  
284 14 ▶ if ($ALGORITHM == 8
Branch: 1 branch
BranchTrueFalseTotal
if ($ALGORITHM == 8 or $ALGORITHM == 9 or $ALGORITHM == 14 or $ALGORITHM == 15 or $ALGORITHM == 16 or $ALGORITHM == 17 or $ALGORITHM == 18 or $ALGORITHM == 19 or $ALGORITHM >= 20) { } 14 0 14
Condition33%8/24
$ALGORITHM == 8 or $ALGORITHM == 9 or $ALGORITHM == 14 or $ALGORITHM == 15 or $ALGORITHM == 16 or $ALGORITHM == 17 or $ALGORITHM == 18 or $ALGORITHM == 19 or $ALGORITHM >= 20
l!l&&r!l&&!r
1400
$ALGORITHM == 8 or $ALGORITHM == 9 or $ALGORITHM == 14 or $ALGORITHM == 15 or $ALGORITHM == 16 or $ALGORITHM == 17 or $ALGORITHM == 18 or $ALGORITHM == 19
l!l&&r!l&&!r
1400
$ALGORITHM == 8 or $ALGORITHM == 9 or $ALGORITHM == 14 or $ALGORITHM == 15 or $ALGORITHM == 16 or $ALGORITHM == 17 or $ALGORITHM == 18
l!l&&r!l&&!r
0140
$ALGORITHM == 8 or $ALGORITHM == 9 or $ALGORITHM == 14 or $ALGORITHM == 15 or $ALGORITHM == 16 or $ALGORITHM == 17
l!l&&r!l&&!r
0014
$ALGORITHM == 8 or $ALGORITHM == 9 or $ALGORITHM == 14 or $ALGORITHM == 15 or $ALGORITHM == 16
l!l&&r!l&&!r
0014
$ALGORITHM == 8 or $ALGORITHM == 9 or $ALGORITHM == 14 or $ALGORITHM == 15
l!l&&r!l&&!r
0014
$ALGORITHM == 8 or $ALGORITHM == 9 or $ALGORITHM == 14
l!l&&r!l&&!r
0014
$ALGORITHM == 8 or $ALGORITHM == 9
l!l&&r!l&&!r
0014
MC/DC0%0/9
$ALGORITHM == 8 or $ALGORITHM == 9 or $ALGORITHM == 14 or $ALGORITHM == 15 or $ALGORITHM == 16 or $ALGORITHM == 17 or $ALGORITHM == 18 or $ALGORITHM == 19 or $ALGORITHM >= 20
$ALGORITHM == 8 $ALGORITHM == 9 $ALGORITHM == 14 $ALGORITHM == 15 $ALGORITHM == 16 $ALGORITHM == 17 $ALGORITHM == 18 $ALGORITHM == 19 $ALGORITHM >= 20
Truth table10%1/10
A or B or C or D or E or F or G or H or I
A= $ALGORITHM == 8B= $ALGORITHM == 9C= $ALGORITHM == 14D= $ALGORITHM == 15E= $ALGORITHM == 16F= $ALGORITHM == 17G= $ALGORITHM == 18H= $ALGORITHM == 19I= $ALGORITHM >= 20
ABCDEFGHIresult
1XXXXXXXX1
01XXXXXXX1
001XXXXXX1
0001XXXXX1
00001XXXX1
000001XXX1
0000001XX1
00000001X1
0000000011
0000000000
285 || $ALGORITHM == 9
286 || $ALGORITHM == 14 || $ALGORITHM == 15
287 || $ALGORITHM == 16 || $ALGORITHM == 17
288 || $ALGORITHM == 18 || $ALGORITHM == 19
289 || $ALGORITHM >= 20
290 )
291 {
292 14 $self->mira_tree_update(
293 $sentence_correct_labelling->nodes_with_root->[0],
294 $sentence_best_labelling,
295 $sumUpdateWeight,
296 );
297 } else {
298  
299 # alg in 1 2 3 4 5 6 7 10 11 12 13
300 my @correct_labels =
301 0 map { $_->label } @{ $sentence_correct_labelling->nodes_with_root };
302 my @best_labels =
303 0 map { $_->label } @{ $sentence_best_labelling->nodes_with_root };
304  
305 0 foreach my $edge ( @{ $sentence_correct_labelling->edges } ) {
306  
307 0 my $ord = $edge->child->ord;
308 0 ▶ if ( $correct_labels[$ord] ne $best_labels[$ord] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($correct_labels[$ord] ne $best_labels[$ord]) { } 0 0 0
309  
310 0 my $correct_label = $correct_labels[$ord];
311 0 my $best_label = $best_labels[$ord];
312  
313 0 $self->mira_edge_update(
314 $edge, $correct_label, $best_label, $sumUpdateWeight
315 );
316  
317 } else {
318  
319 # $correct_labels[$ord] eq $best_labels[$ord]
320 0 ▶ if ( $self->config->DEBUG >= 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 3) 0 0 0
321 0 print "label on $ord is correct, no need to optimize\n";
322 }
323 }
324 }
325 }
326  
327 14 return;
328 }
329  
330 # makes an update on the sequence of parent's children
331 # and recurses on their children
332 # alg in 8 9 14 15 16 17 18 19 >=20
333 1806 ▶sub mira_tree_update {
Subroutine mira_tree_update: called CC=24 SCAR=46.7
Pod undocumented
334  
335 # (Treex::Tool::Parser::MSTperl::Node $correct_parent,
336 # Treex::Tool::Parser::MSTperl::Sentence $sentence_best_labelling,
337 # Int $sumUpdateWeight)
338 my (
339 1806 $self,
340 $correct_parent,
341 $sentence_best_labelling,
342 $sumUpdateWeight
343 ) = @_;
344  
345 1806 my @correct_edges = @{ $correct_parent->children };
346  
347 1806 ▶ if ( @correct_edges == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@correct_edges == 0) 1164 642 1806
348 1164 return;
349 }
350  
351 642 my $ALGORITHM = $self->config->labeller_algorithm;
352  
353 642 my $label_prev_correct = $self->config->SEQUENCE_BOUNDARY_LABEL;
354 642 my $label_prev_best = $self->config->SEQUENCE_BOUNDARY_LABEL;
355 642 foreach my $correct_edge (@correct_edges) {
356  
357 1792 my $features = $correct_edge->features_all_labeller();
358 1792 my $label_correct = $correct_edge->child->label;
359 1792 my $label_best = (
360 $sentence_best_labelling->getNodeByOrd(
361 $correct_edge->child->ord
362 )
363 )->label;
364  
365 1792 ▶ if ( $label_correct ne $label_best ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($label_correct ne $label_best) 570 1222 1792
366  
367 # label is incorrect, we have to update the scores
368  
369 # $self->mira_edge_tree_update(
370 # $edge, $correct_label, $best_label, $sumUpdateWeight);
371  
372 570 my $score_correct = $self->model->get_label_score(
373 $label_correct, $label_prev_correct, $features
374 );
375 570 my $score_best = $self->model->get_label_score(
376 $label_best, $label_prev_best, $features
377 );
378  
379 570 ▶ if ( $ALGORITHM == 19 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ALGORITHM == 19) { } 0 570 570
380  
381 0 ▶ if ( $score_correct > $score_best ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($score_correct > $score_best) 0 0 0
382 0 ▶ if ( $self->config->DEBUG >= 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 2) 0 0 0
383 0 print "correct label $label_correct on "
384 . ( $correct_edge->child->ord )
385 . "has higher score than incorrect $label_best "
386 . "but transition scores preferred "
387 . "the incorrect one\n";
388 }
389 0 next;
390 }
391  
392 0 ▶ if ( $score_correct == 0 || $score_best == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($score_correct == 0 or $score_best == 0) 0 0 0
Condition0%0/3
$score_correct == 0 or $score_best == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$score_correct == 0 or $score_best == 0
$score_correct == 0 $score_best == 0
Truth table0%0/3
A or B
A= $score_correct == 0B= $score_best == 0
ABresult
1X1
011
000
393 0 ▶ if ( $self->config->DEBUG >= 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 2) 0 0 0
394 0 print "correct label $label_correct on "
395 . ( $correct_edge->child->ord )
396 . "score correct: $score_correct\n"
397 . "score best: $score_best\n";
398 }
399 0 next;
400 }
401  
402 # inverse sigmoid
403 0 my $em_correct = -log( 1 / $score_correct - 1 )
404 / $self->config->SIGM_LAMBDA;
405 0 my $em_best = -log( 1 / $score_best - 1 )
406 / $self->config->SIGM_LAMBDA;
407  
408 # error to be distributed among features
409 0 my $margin = 1;
410 0 my $error = $em_best - $em_correct + $margin;
411  
412 # the same update is done twice with each feature
413 0 my $features_count = scalar( @{$features} );
414 0 my $update = $error / $features_count / 2;
415  
416 0 foreach my $feature ( @{$features} ) {
417  
418 # positive emission update
419 0 $self->update_feature_score(
420 $feature,
421 $update,
422 $sumUpdateWeight,
423 $label_correct,
424 );
425  
426 # negative emission update
427 0 $self->update_feature_score(
428 $feature,
429 -$update,
430 $sumUpdateWeight,
431 $label_best,
432 );
433 }
434  
435 } else {
436  
437 # this is actually a simple accuracy loss function (number of wrong
438 # labels in the sequence, here for one edge only) as described in
439 # Kevin Gimpel and Shay Cohen (2007):
440 # Discriminative Online Algorithms for
441 # Sequence Labeling - A Comparative Study
442 # which they show gave the best performance from all loss functions
443 # that they had tried
444 570 my $margin = 1;
445  
446 570 my $error = $score_best - $score_correct + $margin;
447  
448 570 ▶ if ( $error < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error < 0) 194 376 570
449 194 ▶ if ( $self->config->DEBUG >= 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 3) 0 194 194
450 0 print "correct label $label_correct on "
451 . ( $correct_edge->child->ord )
452 . "has higher score than incorrect $label_best "
453 . "but transition scores preferred "
454 . "the incorrect one\n";
455 }
456 194 next;
457 }
458  
459 376 my $features_count = scalar( @{$features} );
460  
461 376 ▶ if ( $ALGORITHM == 8 || $ALGORITHM == 9 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ALGORITHM == 8 or $ALGORITHM == 9) { } 0 376 376
elsif ($ALGORITHM == 14 or $ALGORITHM == 15 or $ALGORITHM == 16 or $ALGORITHM == 17 or $ALGORITHM == 18 or $ALGORITHM >= 20) { } 376 0 376
Condition33%1/3
$ALGORITHM == 8 or $ALGORITHM == 9
l!l&&r!l&&!r
00376
MC/DC0%0/2
$ALGORITHM == 8 or $ALGORITHM == 9
$ALGORITHM == 8 $ALGORITHM == 9
Truth table33%1/3
A or B
A= $ALGORITHM == 8B= $ALGORITHM == 9
ABresult
1X1
011
000
462  
463 # the same update is done four times with each feature
464 0 my $update = $error / $features_count / 4;
465  
466 0 foreach my $feature ( @{$features} ) {
467  
468 # TODO: which labels to use in transitions updates?
469 # none of the articles I have read
470 # mentions that specifically
471 # but according to their definitions they use
472 # $label_prev_correct for positive updates
473 # and $label_prev_best for negative updates
474 # (which makes some sense but
475 # several other combinations would
476 # make some sense as well -> let's try them, later)
477  
478 # positive emission update
479 0 $self->update_feature_score(
480 $feature,
481 $update,
482 $sumUpdateWeight,
483 $label_correct,
484 );
485  
486 # positive transition update
487 0 $self->update_feature_score(
488 $feature,
489 $update,
490 $sumUpdateWeight,
491 $label_correct,
492 $label_prev_correct,
493 );
494  
495 # negative emission update
496 0 $self->update_feature_score(
497 $feature,
498 -$update,
499 $sumUpdateWeight,
500 $label_best,
501 );
502  
503 # negative transition update
504 0 $self->update_feature_score(
505 $feature,
506 -$update,
507 $sumUpdateWeight,
508 $label_best,
509 $label_prev_best,
510 );
511 }
512  
513 # end if $ALGORITHM == 8|9
514 ▶ } elsif (
Condition33%5/15
$ALGORITHM == 14 or $ALGORITHM == 15 or $ALGORITHM == 16 or $ALGORITHM == 17 or $ALGORITHM == 18 or $ALGORITHM >= 20
l!l&&r!l&&!r
37600
$ALGORITHM == 14 or $ALGORITHM == 15 or $ALGORITHM == 16 or $ALGORITHM == 17 or $ALGORITHM == 18
l!l&&r!l&&!r
03760
$ALGORITHM == 14 or $ALGORITHM == 15 or $ALGORITHM == 16 or $ALGORITHM == 17
l!l&&r!l&&!r
00376
$ALGORITHM == 14 or $ALGORITHM == 15 or $ALGORITHM == 16
l!l&&r!l&&!r
00376
$ALGORITHM == 14 or $ALGORITHM == 15
l!l&&r!l&&!r
00376
MC/DC0%0/6
$ALGORITHM == 14 or $ALGORITHM == 15 or $ALGORITHM == 16 or $ALGORITHM == 17 or $ALGORITHM == 18 or $ALGORITHM >= 20
$ALGORITHM == 14 $ALGORITHM == 15 $ALGORITHM == 16 $ALGORITHM == 17 $ALGORITHM == 18 $ALGORITHM >= 20
Truth table14%1/7
A or B or C or D or E or F
A= $ALGORITHM == 14B= $ALGORITHM == 15C= $ALGORITHM == 16D= $ALGORITHM == 17E= $ALGORITHM == 18F= $ALGORITHM >= 20
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
515 $ALGORITHM == 14
516 || $ALGORITHM == 15
517 || $ALGORITHM == 16
518 || $ALGORITHM == 17
519 || $ALGORITHM == 18
520 || $ALGORITHM >= 20
521 )
522 {
523  
524 # the same update is done twice with each feature
525 376 my $update = $error / $features_count / 2;
526  
527 376 foreach my $feature ( @{$features} ) {
528  
529 # positive emission update
530 8876 $self->update_feature_score(
531 $feature,
532 $update,
533 $sumUpdateWeight,
534 $label_correct,
535 );
536  
537 # negative emission update
538 8876 $self->update_feature_score(
539 $feature,
540 -$update,
541 $sumUpdateWeight,
542 $label_best,
543 );
544  
545 }
546  
547 # end if $ALGORITHM == 16|17
548 } else {
549 0 croak "TrainerLabelling->mira_tree_update not implemented"
550 . " for algorithm no. $ALGORITHM!";
551 }
552 }
553 }
554  
555 # else label is correct, do not update
556  
557 # shift
558 1598 $label_prev_correct = $label_correct;
559 1598 $label_prev_best = $label_best;
560  
561 } # end foreach $correct_edge
562  
563 # TODO: add SEQUENCE_BOUNDARY_LABEL at the end?
564  
565 # recursion
566 642 foreach my $correct_edge (@correct_edges) {
567 1792 $self->mira_tree_update(
568 $correct_edge->child,
569 $sentence_best_labelling,
570 $sumUpdateWeight,
571 );
572 }
573  
574 642 return;
575 }
576  
577 # alg in 1 2 3 4 5 6 7 10 11 12 13
578 0 ▶sub mira_edge_update {
Subroutine mira_edge_update: not called CC=8 SCAR=42.8
Pod undocumented
579  
580 0 my ( $self, $edge, $correct_label, $best_label, $sumUpdateWeight ) = @_;
581  
582 0 my $label_scores = $self->model->get_emission_scores( $edge->features );
583  
584 # s(l_t, x_t, y_t)
585 0 my $score_correct = $label_scores->{$correct_label};
586 0 ▶ if ( !defined $score_correct ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $score_correct) 0 0 0
587 0 $score_correct = 0;
588 }
589  
590 # s(l', x_t, y_t)
591 0 my $score_best = $label_scores->{$best_label};
592 0 ▶ if ( !defined $score_best ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $score_best) 0 0 0
593 0 $score_best = 0;
594 }
595  
596 # difference in scores should be greater than the margin:
597  
598 # L(l_t, l') number of incorrectly assigned labels
599 # edge-based factorization -> always one error
600 # (in case of an error)
601 0 my $margin = 1;
602  
603 # L(l_t, l') - [s(l_t, x_t, y_t) - s(l', x_t, y_t)]
604 0 my $error = $score_best - $score_correct + $margin;
605  
606 0 ▶ if ( $error > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error > 0) { } 0 0 0
607  
608 # features do not depend on sentence labelling
609 # (TODO: actually they may depend on parent labelling,
610 # but we chose to ignore this as we can assume that the
611 # parent is labelled correctly;
612 # that's why we use edges
613 # from $sentence_correct_labelling here)
614 0 my $features_diff = $edge->features;
615 0 my $features_diff_count = scalar( @{$features_diff} );
616  
617 0 ▶ if ( $features_diff_count > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($features_diff_count > 0) { } 0 0 0
618  
619 # min ||w_i+1 - w_i||
620 # s.t. s(x_t, y_t) - s(x_t, y') >= L(y_t, y')
621 0 my $update = $error / $features_diff_count;
622  
623 0 foreach my $feature ( @{$features_diff} ) {
624  
625 # $update is added to features
626 # of correct labelling
627 0 $self->update_feature_score(
628 $feature,
629 $update,
630 $sumUpdateWeight,
631 $correct_label
632 );
633  
634 # and subtracted from features
635 # of "best" labelling
636 0 $self->update_feature_score(
637 $feature,
638 -$update,
639 $sumUpdateWeight,
640 $best_label
641 );
642 }
643  
644 0 ▶ if ( $self->config->DEBUG >= 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 3) 0 0 0
645 0 print "alpha: $update on $features_diff_count"
646 . " features (correct $correct_label,"
647 . " best $best_label)\n";
648 }
649  
650 } else {
651  
652 # $features_diff_count == 0
653 0 croak "It seems that there are no features!" .
654 "This is somewhat weird.";
655 }
656 } else {
657  
658 # $error <= 0
659 # (correct is better but transition ruled it out)
660 # TODO: incorporate transition scores?
661 0 ▶ if ( $self->config->DEBUG >= 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 3) 0 0 0
662 0 print "correct label $correct_label "
663 . "has higher score than incorrect $best_label "
664 . "but transition scores preferred "
665 . "the incorrect one\n";
666 }
667 }
668  
669 0 return;
670 }
671  
672 # update score of the feature
673 # (also update emissions_summed or transitions_summed)
674 17752 ▶sub update_feature_score {
Subroutine update_feature_score: called CC=2 SCAR=7.1
Pod undocumented
675  
676 # (Str $feature, Num $update, Num $sumUpdateWeight,
677 # Str $label, Maybe[Str] $label_prev)
678 17752 my ( $self, $feature, $update, $sumUpdateWeight, $label, $label_prev ) = @_;
679  
680 # adds $update to the current score of the feature
681 17752 my $result = $self->model->update_feature_score(
682 $feature, $update, $label, $label_prev
683 );
684  
685 # v = v + w_{i+1}
686 # $sumUpdateWeight denotes number of summands
687 # in which the score would appear
688 # if it were computed according to the definition
689 17752 my $summed_update = $sumUpdateWeight * $update;
690 17752 ▶ if ( defined $label_prev ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $label_prev) { } 0 17752 17752
691  
692 # transition score update
693 $self->transitions_summed->{$feature}->{$label_prev}
694 0 ->{$label} += $summed_update;
695 } else {
696  
697 # emission score update
698 17752 $self->emissions_summed->{$feature}->{$label} += $summed_update;
699 }
700  
701 17752 return $result;
702 }
703  
704 # SCORES AVERAGING
705  
706 # recompute feature scores as averages
707 # using emissions_summed and transitions_summed
708 # ( w = v/(N * T) )
709 1 ▶sub scores_averaging {
Subroutine scores_averaging: called CC=5 SCAR=19.1
Pod undocumented
710  
711 1 my ($self) = @_;
712  
713 1 my $ALGORITHM = $self->config->labeller_algorithm;
714  
715 1 ▶ if ( $ALGORITHM == 4 || $ALGORITHM == 5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ALGORITHM == 4 or $ALGORITHM == 5) { } 0 1 1
Condition33%1/3
$ALGORITHM == 4 or $ALGORITHM == 5
l!l&&r!l&&!r
001
MC/DC0%0/2
$ALGORITHM == 4 or $ALGORITHM == 5
$ALGORITHM == 4 $ALGORITHM == 5
Truth table33%1/3
A or B
A= $ALGORITHM == 4B= $ALGORITHM == 5
ABresult
1X1
011
000
716  
717 # nothing to do (MIRA not used)
718  
719 } else {
720  
721 # emissions computed by MIRA
722 1 $self->scores_averaging_emissions();
723  
724 1 ▶ if ( $ALGORITHM == 8 || $ALGORITHM == 9 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ALGORITHM == 8 or $ALGORITHM == 9) 0 1 1
Condition33%1/3
$ALGORITHM == 8 or $ALGORITHM == 9
l!l&&r!l&&!r
001
MC/DC0%0/2
$ALGORITHM == 8 or $ALGORITHM == 9
$ALGORITHM == 8 $ALGORITHM == 9
Truth table33%1/3
A or B
A= $ALGORITHM == 8B= $ALGORITHM == 9
ABresult
1X1
011
000
725  
726 # transitions also computed by MIRA
727 0 $self->scores_averaging_transitions();
728 }
729 }
730  
731 1 return;
732 }
733  
734 1 ▶sub scores_averaging_emissions {
Subroutine scores_averaging_emissions: called CC=4 SCAR=13.9
Pod undocumented
735  
736 1 my ($self) = @_;
737  
738 1 my $divisor = $self->number_of_inner_iterations;
739  
740 1 my @features = keys %{ $self->emissions_summed };
741 1 foreach my $feature (@features) {
742  
743 953 my @labels = keys %{ $self->emissions_summed->{$feature} };
744 953 foreach my $label (@labels) {
745  
746 # get the sum of scores
747 2651 my $score_sum = $self->emissions_summed->{$feature}->{$label};
748  
749 # compute the average score
750 2651 my $new_score = $score_sum / $divisor;
751  
752 # set the new score
753 2651 $self->model->set_feature_score( $feature, $new_score, $label );
754  
755 # only progress and/or debug info
756 2651 ▶ if ( $self->config->DEBUG >= 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 2) 0 2651 2651
757 0 print "$feature\t$label\t$new_score\n";
758 }
759 }
760 }
761  
762 1 return;
763 }
764  
765 0 ▶sub scores_averaging_transitions {
Subroutine scores_averaging_transitions: not called CC=5 SCAR=34.0
Pod undocumented
766  
767 0 my ($self) = @_;
768  
769 0 my $divisor = $self->number_of_inner_iterations;
770  
771 0 my @features = keys %{ $self->transitions_summed };
772 0 foreach my $feature (@features) {
773  
774 0 my @labels = keys %{ $self->transitions_summed->{$feature} };
775 0 foreach my $label_prev (@labels) {
776  
777 my @following_labels = keys %{
778 0 $self->transitions_summed->{$feature}->{$label_prev}
779 };
780 0 foreach my $label (@following_labels) {
781  
782 # get the sum of scores
783 my $score_sum = $self->transitions_summed->
784 0 {$feature}->{$label_prev}->{$label};
785  
786 # compute the average score
787 0 my $new_score = $score_sum / $divisor;
788  
789 # set the new score
790 0 $self->model->set_feature_score(
791 $feature, $new_score, $label, $label_prev
792 );
793  
794 # only progress and/or debug info
795 0 ▶ if ( $self->config->DEBUG >= 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 2) 0 0 0
796 0 print "$feature\t$label_prev\t$label\t$new_score\n";
797 }
798 }
799 }
800 }
801  
802 0 return;
803 }
804  
805 2 1;
806  
807 __END__
« MSTperl/Labeller.pm ↑ Summary MSTperl/ModelAdditional.pm »