MSTperl/TrainerUnlabelled.pm

filter: Statementstmt 84.0% 79 / 94 Branchbran 60.0% 12 / 20 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 10 / 10 Podpod 42.8% 3 / 7 totaltotal 77.2% 105 / 136 CC 20 SCAR 18.5
CC
20
Cov
79%
mira_update
9.6
features_diff
8.2
update
6.8
CRAP 6.4
« MSTperl/Parser.pm ↑ Summary MSTperl/Reader.pm »
1 package Treex::Tool::Parser::MSTperl::TrainerUnlabelled;
2 {
3 2 $Treex::Tool::Parser::MSTperl::TrainerUnlabelled::VERSION = '0.11949';
4 }
5  
6 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 extends 'Treex::Tool::Parser::MSTperl::TrainerBase';
9  
10 2 ▶use Treex::Tool::Parser::MSTperl::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 has model => (
13 isa => 'Treex::Tool::Parser::MSTperl::ModelUnlabelled',
14 is => 'rw',
15 );
16  
17 2 has parser => (
18 isa => 'Treex::Tool::Parser::MSTperl::Parser',
19 is => 'rw',
20 );
21  
22 # v
23 # all values of features used during the training summed together
24 # as using average weights instead of final weights
25 # is reported to help avoid overtraining
26 # For labeller has the form of ->{feature}->{label} = weight
27 # instead of ->{feature} = weight
28 has feature_weights_summed => (
29 isa => 'HashRef',
30 is => 'rw',
31 1 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
32 2 );
33  
34 1 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
35 1 my ($self) = @_;
36  
37 1 $self->parser(
38 Treex::Tool::Parser::MSTperl::Parser->new( config => $self->config )
39 );
40 1 $self->model( $self->parser->model );
41 1 $self->featuresControl( $self->config->unlabelledFeaturesControl );
42 1 $self->number_of_iterations( $self->config->number_of_iterations );
43  
44 1 return;
45 }
46  
47 # UNLABELLED TRAINING
48  
49 # compute the features of the sentence
50 7 ▶sub preprocess_sentence {
Subroutine preprocess_sentence: called CC=1 SCAR=0.0
Pod undocumented
51  
52 # (Treex::Tool::Parser::MSTperl::Sentence $sentence)
53 7 my ( $self, $sentence ) = @_;
54  
55 7 $sentence->fill_fields_after_parse();
56  
57 7 return;
58 }
59  
60 14 ▶sub update {
Subroutine update: called CC=4 SCAR=19.2
Pod undocumented
61  
62 # (Treex::Tool::Parser::MSTperl::Sentence $sentence_correct_parse,
63 # Int $sumUpdateWeight)
64 my (
65 14 $self,
66 $sentence_correct_parse,
67 $sumUpdateWeight,
68 $forbid_new_features
69 ) = @_;
70  
71 # reparse the sentence
72 # y' = argmax_y' s(x_t, y')
73 14 my $sentence_best_parse = $self->parser->parse_sentence_internal(
74 $sentence_correct_parse
75 );
76 14 $sentence_best_parse->fill_fields_after_parse();
77  
78 # only progress and/or debug info
79 14 ▶ if ( $self->config->DEBUG >= 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 2) 0 14 14
80 0 print "CORRECT PARSE EDGES:\n";
81 0 foreach my $edge ( @{ $sentence_correct_parse->edges } ) {
82 0 print $edge->parent->ord . " -> "
83 . $edge->child->ord . "\n";
84 }
85 0 print "BEST PARSE EDGES:\n";
86 0 foreach my $edge ( @{ $sentence_best_parse->edges } ) {
87 0 print $edge->parent->ord . " -> "
88 . $edge->child->ord . "\n";
89 }
90 }
91  
92 # min ||w_i+1 - w_i|| s.t. ...
93 $self->mira_update(
94 14 $sentence_correct_parse,
95 $sentence_best_parse,
96 $sumUpdateWeight,
97 $forbid_new_features
98 );
99  
100 14 return;
101  
102 }
103  
104 14 ▶sub mira_update {
Subroutine mira_update: called CC=8 SCAR=22.6
Pod documented
105  
106 # (Treex::Tool::Parser::MSTperl::Sentence $sentence_correct_parse,
107 # Treex::Tool::Parser::MSTperl::Sentence $sentence_best_parse,
108 # Int $sumUpdateWeight)
109 my (
110 14 $self,
111 $sentence_correct_parse,
112 $sentence_best_parse,
113 $sumUpdateWeight,
114 $forbid_new_features
115 ) = @_;
116  
117 # s(x_t, y_t)
118 14 my $score_correct = $self->model->score_sentence($sentence_correct_parse);
119  
120 # s(x_t, y')
121 14 my $score_best = $self->model->score_sentence($sentence_best_parse);
122  
123 # difference in scores should be greater than the margin:
124  
125 # L(y_t, y') number of incorrectly assigned heads
126 14 my $margin = $sentence_best_parse->count_errors_attachement(
127 $sentence_correct_parse
128 );
129  
130 # s(x_t, y_t) - s(x_t, y') this should be zero or less
131 14 my $score_gain = $score_correct - $score_best;
132  
133 # L(y_t, y') - [s(x_t, y_t) - s(x_t, y')]
134 14 my $error = $margin - $score_gain;
135  
136 14 ▶ if ( $error > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error > 0) { } 12 2 14
137 12 my ( $features_diff_correct, $features_diff_best, $features_diff_count )
138 = $self->features_diff(
139 $sentence_correct_parse->features,
140 $sentence_best_parse->features,
141 $forbid_new_features
142 );
143  
144 12 ▶ if ( $features_diff_count == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($features_diff_count == 0) { } 0 12 12
145 0 warn "Features of the best parse and the correct parse do not " .
146 "differ, unable to update the scores. " .
147 "Consider using more features.\n";
148 0 ▶ if ( $self->config->DEBUG >= 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 3) 0 0 0
149 0 print "alpha: 0 on 0 features\n";
150 }
151 } else {
152  
153 # min ||w_i+1 - w_i|| s.t. s(x_t, y_t) - s(x_t, y') >= L(y_t, y')
154 12 my $update = $error / $features_diff_count;
155  
156 #$update is added to features occuring in the correct parse only
157 12 foreach my $feature ( @{$features_diff_correct} ) {
158 961 $self->update_feature_weight(
159 $feature,
160 $update,
161 $sumUpdateWeight
162 );
163 }
164  
165 # and subtracted from features occuring
166 # in the best (and incorrect) parse only
167 12 foreach my $feature ( @{$features_diff_best} ) {
168 1027 $self->update_feature_weight(
169 $feature,
170 -$update,
171 $sumUpdateWeight
172 );
173 }
174 12 ▶ if ( $self->config->DEBUG >= 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 3) 0 12 12
175 0 print "alpha: $update on $features_diff_count features\n";
176 }
177 }
178 } else { #else no need to optimize
179 2 ▶ if ( $self->config->DEBUG >= 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 3) 0 2 2
180 0 print "alpha: 0 on 0 features\n";
181 }
182 }
183  
184 14 return;
185 }
186  
187 12 ▶sub features_diff {
Subroutine features_diff: called CC=8 SCAR=21.0
Pod documented
188  
189 # (ArrayRef[Str] $features_first, ArrayRef[Str] $features_second)
190 12 my ( $self, $features_first, $features_second, $forbid_new_features ) = @_;
191  
192 #get feature counts
193 12 my %feature_counts;
194 12 foreach my $feature ( @{$features_first} ) {
195 3307 $feature_counts{$feature}++;
196 }
197 12 foreach my $feature ( @{$features_second} ) {
198 3483 $feature_counts{$feature}--;
199 }
200  
201 # TODO: try to disregard features which occur in both parses?
202  
203 #do the diff
204 12 my @features_first;
205 my @features_second;
206 12 my $diff_count = 0;
207 12 FF: foreach my $feature ( keys %feature_counts ) {
208 3457 ▶ if ( $forbid_new_features && $self->model->feature_is_unknown($feature) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($forbid_new_features and $self->model->feature_is_unknown($feature)) 0 3457 3457
Condition33%1/3
$forbid_new_features and $self->model->feature_is_unknown($feature)
!ll&&!rl&&r
345700
MC/DC0%0/2
$forbid_new_features and $self->model->feature_is_unknown($feature)
$forbid_new_features $self->model->feature_is_unknown($feature)
Truth table33%1/3
A and B
A= $forbid_new_featuresB= $self->model->feature_is_unknown($feature)
ABresult
0X0
100
111
209 0 next FF;
210 }
211 3457 ▶ if ( $feature_counts{$feature} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($feature_counts{$feature}) 1988 1469 3457
212 1988 my $count = abs( $feature_counts{$feature} );
213  
214 # create arrays of differing features,
215 # each differing feature is included ONCE ONLY
216 # because an optimization of update is not present
217 # and the update makes uniform changes to all differing features,
218 # in which case even repeated features should be updated ONCE ONLY
219  
220 # more often in the first array
221 1988 ▶ if ( $feature_counts{$feature} > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($feature_counts{$feature} > 0) { } 961 1027 1988
222  
223 # for ( my $i = 0; $i < $count; $i++ ) {
224 961 push @features_first, $feature;
225  
226 # }
227  
228 # more often in the second array
229 } else {
230  
231 # for ( my $i = 0; $i < $count; $i++ ) {
232 1027 push @features_second, $feature;
233  
234 # }
235 }
236 1988 $diff_count += $count;
237 } # else same count -> no difference
238 }
239  
240 12 return ( \@features_first, \@features_second, $diff_count );
241 }
242  
243 # update weight of the feature
244 # (also update the sum of feature weights: feature_weights_summed)
245 1988 ▶sub update_feature_weight {
Subroutine update_feature_weight: called CC=1 SCAR=0.0
Pod documented
246  
247 # (Str $feature, Num $update, Num $sumUpdateWeight)
248 1988 my ( $self, $feature, $update, $sumUpdateWeight ) = @_;
249  
250 #adds $update to the current weight of the feature
251 1988 my $result =
252 $self->model->update_feature_weight( $feature, $update );
253  
254 # v = v + w_{i+1}
255 # $sumUpdateWeight denotes number of summands
256 # in which the weight would appear
257 # if it were computed according to the definition
258 1988 my $summed_update = $sumUpdateWeight * $update;
259 1988 $self->feature_weights_summed->{$feature} += $summed_update;
260  
261 1988 return $result;
262 }
263  
264 # recompute weight of $feature as an average
265 # (using feature_weights_summed)
266 1 ▶sub scores_averaging {
Subroutine scores_averaging: called CC=3 SCAR=11.2
Pod undocumented
267  
268 # Str $feature
269 1 my ($self) = @_;
270  
271 1 foreach my $feature ( keys %{ $self->feature_weights_summed } ) {
272  
273 # w = v/(N * T)
274 # see also: my $self->number_of_inner_iterations =
275 # $self->number_of_iterations * $sentence_count;
276  
277 1444 my $weight = $self->feature_weights_summed->{$feature}
278 / $self->number_of_inner_iterations;
279 1444 $self->model->set_feature_weight( $feature, $weight );
280  
281 # only progress and/or debug info
282 1444 ▶ if ( $self->config->DEBUG >= 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 2) 0 1444 1444
283 0 print "$feature\t" . $self->model->get_feature_weight($feature)
284 . "\n";
285  
286 }
287 }
288  
289 1 return;
290 }
291  
292 2 1;
293  
294 __END__
« MSTperl/Parser.pm ↑ Summary MSTperl/Reader.pm »