| 1 | package Treex::Tool::Parser::MSTperl::TrainerBase; | ||||||||||
| 2 | { | ||||||||||
| 3 | 4 | $Treex::Tool::Parser::MSTperl::TrainerBase::VERSION = '0.11949'; | |||||||||
| 4 | } | ||||||||||
| 5 | |||||||||||
| 6 | 4 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 4 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 4 | has config => ( | |||||||||
| 10 | isa => 'Treex::Tool::Parser::MSTperl::Config', | ||||||||||
| 11 | is => 'ro', | ||||||||||
| 12 | required => '1', | ||||||||||
| 13 | ); | ||||||||||
| 14 | |||||||||||
| 15 | # to be filled in extending packages! | ||||||||||
| 16 | 4 | has model => ( | |||||||||
| 17 | isa => 'Treex::Tool::Parser::MSTperl::ModelBase', | ||||||||||
| 18 | is => 'rw', | ||||||||||
| 19 | ); | ||||||||||
| 20 | |||||||||||
| 21 | # to be filled in extending packages! | ||||||||||
| 22 | 4 | has featuresControl => ( | |||||||||
| 23 | isa => 'Treex::Tool::Parser::MSTperl::FeaturesControl', | ||||||||||
| 24 | is => 'rw', | ||||||||||
| 25 | ); | ||||||||||
| 26 | |||||||||||
| 27 | # to be filled in extending packages! | ||||||||||
| 28 | 4 | has number_of_iterations => ( | |||||||||
| 29 | isa => 'Int', | ||||||||||
| 30 | is => 'rw', | ||||||||||
| 31 | ); | ||||||||||
| 32 | |||||||||||
| 33 | 4 | has number_of_inner_iterations => ( | |||||||||
| 34 | isa => 'Int', | ||||||||||
| 35 | is => 'rw', | ||||||||||
| 36 | ); | ||||||||||
| 37 | |||||||||||
| 38 | 4 | has skip_scores_averaging => ( | |||||||||
| 39 | is => 'rw', | ||||||||||
| 40 | isa => 'Bool', | ||||||||||
| 41 | default => 0 | ||||||||||
| 42 | ); | ||||||||||
| 43 | |||||||||||
| 44 | # TRAINING COMMON SUBS | ||||||||||
| 45 | |||||||||||
| 46 | 0 | ▶ | sub train_dev { | ||||||||
|
Subroutine
train_dev: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 47 | 0 | my ( $self, $training_data, $dev_data ) = @_; | |||||||||
| 48 | |||||||||||
| 49 | 0 | $self->train( $training_data, 0 ); | |||||||||
| 50 | 0 | my $feature_count = $self->train( $dev_data, 1 ); | |||||||||
| 51 | |||||||||||
| 52 | 0 | return $feature_count; | |||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | 0 | ▶ | sub train_2parts { | ||||||||
|
Subroutine
train_2parts: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 56 | 0 | my ( $self, $training_data, $dev_data ) = @_; | |||||||||
| 57 | |||||||||||
| 58 | 0 | $self->train( $training_data, 0 ); | |||||||||
| 59 | 0 | my $feature_count = $self->train( $dev_data, 0 ); | |||||||||
| 60 | |||||||||||
| 61 | 0 | return $feature_count; | |||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | 2 | ▶ | sub train { | ||||||||
|
Subroutine
train: called CC=12 SCAR=29.3
Pod
undocumented
| |||||||||||
| 65 | |||||||||||
| 66 | # (ArrayRef[Treex::Tool::Parser::MSTperl::Sentence] $training_data | ||||||||||
| 67 | # Bool $unlabelled) | ||||||||||
| 68 | # Training data: T = {(x_t, y_t)} t=1..T | ||||||||||
| 69 | 2 | my ( $self, $training_data, $forbid_new_features ) = @_; | |||||||||
| 70 | |||||||||||
| 71 | # number of sentences in training data | ||||||||||
| 72 | 2 | my $sentence_count = scalar( @{$training_data} ); | |||||||||
| 73 | |||||||||||
| 74 | # how many times $self->mira_update() will be called | ||||||||||
| 75 | 2 | $self->number_of_inner_iterations( | |||||||||
| 76 | $self->number_of_iterations * $sentence_count | ||||||||||
| 77 | ); | ||||||||||
| 78 | |||||||||||
| 79 | # only progress and/or debug info | ||||||||||
| 80 | 2 | ▶ | if ( $self->config->DEBUG >= 1 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | 0 | print "Going to train on $sentence_count sentences with " | |||||||||
| 82 | . $self->number_of_iterations . " iterations.\n"; | ||||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | # precompute features of sentences in training data | ||||||||||
| 86 | # in labelled parsing also gets the list of labels | ||||||||||
| 87 | # and computes the transition probs | ||||||||||
| 88 | 2 | $self->preprocess_sentences($training_data); | |||||||||
| 89 | |||||||||||
| 90 | # do the training | ||||||||||
| 91 | 2 | ▶ | if ( $self->config->DEBUG >= 1 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | 0 | print "Training the model...\n"; | |||||||||
| 93 | } | ||||||||||
| 94 | 2 | my $innerIteration = 0; | |||||||||
| 95 | |||||||||||
| 96 | # for n : 1..N | ||||||||||
| 97 | 2 | ▶ | for ( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | my $iteration = 1; | ||||||||||
| 99 | $iteration <= $self->number_of_iterations; | ||||||||||
| 100 | $iteration++ | ||||||||||
| 101 | ) | ||||||||||
| 102 | { | ||||||||||
| 103 | 4 | ▶ | if ( $self->config->DEBUG >= 1 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 104 | 0 | print " Iteration number $iteration of " | |||||||||
| 105 | . $self->number_of_iterations . "...\n"; | ||||||||||
| 106 | } | ||||||||||
| 107 | 4 | my $sentNo = 0; | |||||||||
| 108 | |||||||||||
| 109 | # for t : 1..T # these are the inner iterations | ||||||||||
| 110 | 4 | foreach my $sentence_correct ( @{$training_data} ) { | |||||||||
| 111 | |||||||||||
| 112 | # weight of weights/scores sum update <N*T .. 1>; | ||||||||||
| 113 | # $sumUpdateWeight denotes number of summands | ||||||||||
| 114 | # in which the new value would appear | ||||||||||
| 115 | # if it were computed according to the definition | ||||||||||
| 116 | 28 | my $sumUpdateWeight = | |||||||||
| 117 | $self->number_of_inner_iterations - $innerIteration; | ||||||||||
| 118 | |||||||||||
| 119 | # update on this instance | ||||||||||
| 120 | 28 | $self->update( $sentence_correct, $sumUpdateWeight, $forbid_new_features ); | |||||||||
| 121 | |||||||||||
| 122 | # $innerIteration = ( $iteration - 1 ) * $sentence_count + $sentNo; | ||||||||||
| 123 | 28 | $innerIteration++; | |||||||||
| 124 | |||||||||||
| 125 | # only progress and/or debug info | ||||||||||
| 126 | 28 | ▶ | if ( $self->config->DEBUG >= 1 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 127 | 0 | $sentNo++; | |||||||||
| 128 | 0 | ▶ | if ( $sentNo % 50 == 0 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 129 | 0 | print " $sentNo/$sentence_count sentences processed " . | |||||||||
| 130 | "(iteration $iteration/" | ||||||||||
| 131 | . $self->number_of_iterations | ||||||||||
| 132 | . ")\n"; | ||||||||||
| 133 | } | ||||||||||
| 134 | } | ||||||||||
| 135 | |||||||||||
| 136 | } # end for inner iterations | ||||||||||
| 137 | } # end for $iteration | ||||||||||
| 138 | |||||||||||
| 139 | # only progress and/or debug info | ||||||||||
| 140 | 2 | ▶ | if ( $self->config->DEBUG >= 1 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 141 | 0 | print "Done.\n"; | |||||||||
| 142 | } | ||||||||||
| 143 | 2 | ▶ | if ( $self->config->DEBUG >= 2 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 144 | 0 | print "FINAL FEATURE WEIGTHS:\n"; | |||||||||
| 145 | } | ||||||||||
| 146 | |||||||||||
| 147 | 2 | ▶ | if ( !$self->skip_scores_averaging ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 148 | |||||||||||
| 149 | # average the model (is said to help overfitting) | ||||||||||
| 150 | 2 | $self->scores_averaging(); | |||||||||
| 151 | } | ||||||||||
| 152 | |||||||||||
| 153 | # only progress and/or debug info | ||||||||||
| 154 | 2 | my $feature_count = $self->model->get_feature_count(); | |||||||||
| 155 | 2 | ▶ | if ( $self->config->DEBUG >= 1 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 156 | 0 | print "Model trained with $feature_count features.\n"; | |||||||||
| 157 | } | ||||||||||
| 158 | |||||||||||
| 159 | 2 | return $feature_count; | |||||||||
| 160 | |||||||||||
| 161 | } # end sub train | ||||||||||
| 162 | |||||||||||
| 163 | # precompute features of sentences in training data | ||||||||||
| 164 | 2 | ▶ | sub preprocess_sentences { | ||||||||
|
Subroutine
preprocess_sentences: called CC=10 SCAR=32.8
Pod
undocumented
| |||||||||||
| 165 | |||||||||||
| 166 | # (ArrayRef[Treex::Tool::Parser::MSTperl::Sentence] $training_data | ||||||||||
| 167 | # Bool $unlabelled) | ||||||||||
| 168 | 2 | my ( $self, $training_data ) = @_; | |||||||||
| 169 | |||||||||||
| 170 | # only progress and/or debug info | ||||||||||
| 171 | 2 | ▶ | if ( $self->config->DEBUG >= 1 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 172 | 0 | print "Computing sentence features...\n"; | |||||||||
| 173 | } | ||||||||||
| 174 | |||||||||||
| 175 | 2 | my $sentence_count = scalar( @{$training_data} ); | |||||||||
| 176 | 2 | my $sentNo = 0; | |||||||||
| 177 | |||||||||||
| 178 | 2 | foreach my $sentence_correct ( @{$training_data} ) { | |||||||||
| 179 | |||||||||||
| 180 | # compute sentence features | ||||||||||
| 181 | # in labelled parsing also gets the list of labels | ||||||||||
| 182 | # and computes the transition probs | ||||||||||
| 183 | 14 | $sentNo++; | |||||||||
| 184 | 14 | $self->preprocess_sentence( | |||||||||
| 185 | $sentence_correct, $sentNo / $sentence_count | ||||||||||
| 186 | ); | ||||||||||
| 187 | |||||||||||
| 188 | # only progress and/or debug info | ||||||||||
| 189 | 14 | ▶ | if ( $self->config->DEBUG >= 1 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 190 | 0 | ▶ | if ( $sentNo % 50 == 0 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 191 | 0 | print " $sentNo/$sentence_count sentences " | |||||||||
| 192 | . "processed (computing features)\n"; | ||||||||||
| 193 | } | ||||||||||
| 194 | } | ||||||||||
| 195 | 14 | ▶ | if ( $self->config->DEBUG >= 3 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 196 | 0 | print "SENTENCE FEATURES:\n"; | |||||||||
| 197 | 0 | foreach my $feature ( @{ $sentence_correct->features } ) { | |||||||||
| 198 | 0 | print "$feature\n"; | |||||||||
| 199 | } | ||||||||||
| 200 | 0 | print "CORRECT EDGES:\n"; | |||||||||
| 201 | 0 | foreach my $edge ( @{ $sentence_correct->edges } ) { | |||||||||
| 202 | 0 | print $edge->parent->ord . " -> " . $edge->child->ord . "\n"; | |||||||||
| 203 | } | ||||||||||
| 204 | 0 | print "CORRECT LABELS:\n"; | |||||||||
| 205 | 0 | foreach my $node ( @{ $sentence_correct->nodes_with_root } ) { | |||||||||
| 206 | 0 | print $node->ord . "/" . $node->label . "\n"; | |||||||||
| 207 | } | ||||||||||
| 208 | } | ||||||||||
| 209 | |||||||||||
| 210 | } | ||||||||||
| 211 | |||||||||||
| 212 | 2 | $self->model->prepare_for_mira($self); | |||||||||
| 213 | |||||||||||
| 214 | 2 | ▶ | if ( $self->config->DEBUG >= 1 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 215 | 0 | print "Done.\n"; | |||||||||
| 216 | } | ||||||||||
| 217 | |||||||||||
| 218 | 2 | return; | |||||||||
| 219 | } | ||||||||||
| 220 | |||||||||||
| 221 | # ABSTRACT TRAINING SUB STUBS (TO BE REDEFINED IN DESCENDED PACKAGES) | ||||||||||
| 222 | |||||||||||
| 223 | # compute the features of the sentence | ||||||||||
| 224 | # in labelling also used to get the list of labels and of transition probs | ||||||||||
| 225 | 0 | ▶ | sub preprocess_sentence { | ||||||||
|
Subroutine
preprocess_sentence: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 226 | |||||||||||
| 227 | # (Treex::Tool::Parser::MSTperl::Sentence $sentence, Num $progress) | ||||||||||
| 228 | # my ( $self, $sentence, $progress ) = @_; | ||||||||||
| 229 | |||||||||||
| 230 | 0 | croak 'TrainerBase::preprocess_sentence is an abstract method,' | |||||||||
| 231 | . ' it must be called' | ||||||||||
| 232 | . ' either from TrainerUnlabelled or TrainerLabelling!'; | ||||||||||
| 233 | } | ||||||||||
| 234 | |||||||||||
| 235 | 0 | ▶ | sub update { | ||||||||
|
Subroutine
update: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 236 | |||||||||||
| 237 | # (Treex::Tool::Parser::MSTperl::Sentence $sentence_correct, | ||||||||||
| 238 | # Int $sumUpdateWeight) | ||||||||||
| 239 | # my ( $self, $sentence_correct, $sumUpdateWeight ) = @_; | ||||||||||
| 240 | |||||||||||
| 241 | 0 | croak 'TrainerBase::update is an abstract method, it must be called' | |||||||||
| 242 | . ' either from TrainerUnlabelled or TrainerLabelling!'; | ||||||||||
| 243 | } | ||||||||||
| 244 | |||||||||||
| 245 | # sub mira_update { | ||||||||||
| 246 | # | ||||||||||
| 247 | # # (Treex::Tool::Parser::MSTperl::Sentence $sentence_correct, | ||||||||||
| 248 | # # Treex::Tool::Parser::MSTperl::Sentence $sentence_best, | ||||||||||
| 249 | # # Int $sumUpdateWeight) | ||||||||||
| 250 | # # my ( $self, $sentence_correct, $sentence_best, $sumUpdateWeight ) = @_; | ||||||||||
| 251 | # | ||||||||||
| 252 | # croak 'TrainerBase::mira_update is an abstract method, it must be called' | ||||||||||
| 253 | # . ' either from TrainerUnlabelled or TrainerLabelling!'; | ||||||||||
| 254 | # } | ||||||||||
| 255 | |||||||||||
| 256 | # recompute feature weights/scores as averages | ||||||||||
| 257 | 0 | ▶ | sub scores_averaging { | ||||||||
|
Subroutine
scores_averaging: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 258 | |||||||||||
| 259 | # my ($self) = @_; | ||||||||||
| 260 | |||||||||||
| 261 | 0 | croak 'TrainerBase::scores_averaging is an abstract method, it ' | |||||||||
| 262 | . 'must be called either from TrainerUnlabelled or TrainerLabelling!'; | ||||||||||
| 263 | |||||||||||
| 264 | } | ||||||||||
| 265 | |||||||||||
| 266 | # MODEL STORING | ||||||||||
| 267 | |||||||||||
| 268 | 0 | ▶ | sub store_model { | ||||||||
|
Subroutine
store_model: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 269 | |||||||||||
| 270 | 0 | my ( $self, $filename ) = @_; | |||||||||
| 271 | |||||||||||
| 272 | 0 | $self->model->store($filename); | |||||||||
| 273 | |||||||||||
| 274 | 0 | return; | |||||||||
| 275 | } | ||||||||||
| 276 | |||||||||||
| 277 | 0 | ▶ | sub store_model_tsv { | ||||||||
|
Subroutine
store_model_tsv: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 278 | |||||||||||
| 279 | 0 | my ( $self, $filename ) = @_; | |||||||||
| 280 | |||||||||||
| 281 | 0 | $self->model->store_tsv($filename); | |||||||||
| 282 | |||||||||||
| 283 | 0 | return; | |||||||||
| 284 | } | ||||||||||
| 285 | |||||||||||
| 286 | 4 | 1; | |||||||||
| 287 | |||||||||||
| 288 | __END__ | ||||||||||