| 1 | # -*-Perl-*- | ||
| 2 | # | ||
| 3 | # LTU.pm - A Perl module implementing linear threshold units. | ||
| 4 | # | ||
| 5 | # Copyright (c) 1995 Tom Fawcett. All rights reserved. | ||
| 6 | # This program is free software; you may redistribute it and/or | ||
| 7 | # modify it under the same terms as Perl itself. | ||
| 8 | # | ||
| 9 | # Original C code written by James Callan. | ||
| 10 | # Rewritten for Perl from Version 2.4 by Tom Fawcett. | ||
| 11 | # Bugs, comments, suggestions: Tom Fawcett <fawcett@nynexst.com> | ||
| 12 | # | ||
| 13 | # Description: | ||
| 14 | # | ||
| 15 | # This module contains subroutines for creating, using, and destroying | ||
| 16 | # linear threshold units (LTUs). It offers four different training rules: | ||
| 17 | # | ||
| 18 | # 1) The absolute correction rule (ACR), copied from Nils Nilsson's book | ||
| 19 | # "Learning Machines: Foundations of Trainable Pattern-Classification | ||
| 20 | # Systems," published by McGraw-Hill in 1965. This rule is identical | ||
| 21 | # to the Perceptron rule. Its advantage is that it converges quickly | ||
| 22 | # when the training instances are linearly separable. | ||
| 23 | # 2) The least-mean-square (LMS) rule, devised by Widrow and Hoff in 1960. | ||
| 24 | # See the AI Handbook or Duda & Hart for information on the LMS rule. | ||
| 25 | # The advantage of the LMS rule over absolute correction or fixed | ||
| 26 | # increment is that it tends to minimize the mean-squared error, even | ||
| 27 | # when the classes are not linearly separable. | ||
| 28 | # 3) The recursive least square (RLS) rule, copied from Peter Young's book | ||
| 29 | # "Recursive Estimation and Time-Series Analysis," published by | ||
| 30 | # Springer-Verlag in 1984. This rule is like the LMS rule, but far | ||
| 31 | # superior to it. It's faster, because each instance only needs to | ||
| 32 | # be seen once. It's also more accurate. | ||
| 33 | # 4) The thermal perceptron (TACR) rule, copied from Marcus Frean's PhD | ||
| 34 | # thesis "Learning in Single Perceptrons," published by University of | ||
| 35 | # Edinburgh in 1990. This rule is like the ACR rule, except that its | ||
| 36 | # annealing capabilities enable it to handle classes that are not | ||
| 37 | # linearly separable. | ||
| 38 | # | ||
| 39 | # A "maybe-train" training strategy (i.e. train only when the linear | ||
| 40 | # threshold unit misclassifies an instance) is implicit in the absolute | ||
| 41 | # correction rule, the LMS rule and the thermal perceptron rule. The RLS | ||
| 42 | # rule always trains. | ||
| 43 | # | ||
| 44 | # The training rules are most effective when attribute values are scaled | ||
| 45 | # to a fixed range. When values exceed 1.0, some of the rules (e.g. ACR, | ||
| 46 | # TACR) may allow the magnitude of weights to grow without bound. When | ||
| 47 | # different attributes have different ranges, some of the rules (e.g. RLS) | ||
| 48 | # give greater influence to attributes with larger ranges. Therefore, you | ||
| 49 | # can request automatic scaling, if you desire it, when the LTU is created. | ||
| 50 | # If you enable scaling, your data is automatically scaled, so that its value | ||
| 51 | # does not exceed 1.0. Scales are computed and adjusted when necessary, | ||
| 52 | # without any intervention from you. | ||
| 53 | # | ||
| 54 | # LTU's converge most quickly when the data includes both positive and | ||
| 55 | # negative values. Therefore, when your data is scaled, it is scaled so | ||
| 56 | # that the midpoint of the range is 0.0. | ||
| 57 | # | ||
| 58 | # Each time scales are adjusted, the weights in the LTU become inaccurate. | ||
| 59 | # The scaling procedure cannot compensate for this inaccuracy. Therefore, | ||
| 60 | # the automatic scaling can affect the speed with which the algorithm | ||
| 61 | # converges. However, once the scales "settle down" (i.e. once the extreme | ||
| 62 | # values for each attribute have been seen), the speed of convergence is not | ||
| 63 | # affected. | ||
| 64 | # | ||
| 65 | # Automatic scaling adversely affects the RLS rule, because the RLS rule | ||
| 66 | # implicitly "remembers" each instance. When scales are adjusted, the | ||
| 67 | # instances remembered by the LTU become noisy. If you enable scaling with | ||
| 68 | # the RLS rule, you should cycle through your instances several times, at | ||
| 69 | # least until the extreme values (min/maxs) of each attribute have been seen. | ||
| 70 | # | ||
| 71 | # | ||
| 72 | ############################################################################## | ||
| 73 | # $Log: LTU.pm,v $ | ||
| 74 | # Revision 2.7 1996/02/20 15:15:18 fawcett | ||
| 75 | # Fixed package confusion made in the original version. | ||
| 76 | # Now exports $LTU_MINUS, LTU_PLUS and LTU_THRESHOLD. | ||
| 77 | # Created Makefile.PL so that MakeMaker can handle installation, | ||
| 78 | # cleaning, distribution, etc. | ||
| 79 | # | ||
| 80 | # | ||
| 81 | # Revision 2.5 1995 fawcett | ||
| 82 | # Initial public offering, released as Statistics::LTU.pm. | ||
| 83 | # Based on Jamie's LTU.c version 2.4. | ||
| 84 | # | ||
| 85 | ############################################################################## | ||
| 86 | package Statistics::LTU; | ||
| 87 | my($rcs) = ' $Id: LTU.pm,v 2.7 1996/02/20 15:15:18 fawcett Exp $ ' ; | ||
| 88 | require 5; | ||
| 89 | use Carp; | ||
| 90 | |||
| 91 | require Exporter; | ||
| 92 | @ISA = ('Exporter'); | ||
| 93 | @EXPORT = qw($LTU_PLUS $LTU_MINUS $LTU_THRESHOLD); | ||
| 94 | |||
| 95 | ($Statistics::LTU::VERSION) = $rcs =~ /Id: LTU\.pm.* ([\d\.]+) /; | ||
| 96 | die "Can't determine Statistics::LTU::VERSION from rcs string!" | ||
| 97 | unless $Statistics::LTU::VERSION; | ||
| 98 | |||
| 99 | # We understand LTU's dumped with 2.5 or later | ||
| 100 | my($read_LTU_files_back_to) = 2.5; | ||
| 101 | |||
| 102 | |||
| 103 | ## | ||
| 104 | ## EXPORTED CONSTANTS | ||
| 105 | ## | ||
| 106 | |||
| 107 | $LTU_MINUS = -1.0; | ||
| 108 | $LTU_PLUS = 1.0; | ||
| 109 | $LTU_THRESHOLD = 0; | ||
| 110 | |||
| 111 | ## | ||
| 112 | ## PRIVATE CONSTANTS | ||
| 113 | ## | ||
| 114 | |||
| 115 | my($LTU_ORIGIN_OFF) = 0; | ||
| 116 | my($LTU_ORIGIN_ON) = 1; | ||
| 117 | |||
| 118 | my($LTU_SCALING_OFF) = 0; | ||
| 119 | my($LTU_SCALING_ON) = 1; | ||
| 120 | |||
| 121 | ## | ||
| 122 | ## Data Structure Definitions | ||
| 123 | ## | ||
| 124 | |||
| 125 | ## Field indices for the LTU structure. | ||
| 126 | ## All index names end in "i"; | ||
| 127 | |||
| 128 | my($LTU_LENGTH) = 15; # Number of entries in an LTU | ||
| 129 | |||
| 130 | my($LENGTHi, | ||
| 131 | $SCALINGi, | ||
| 132 | $ORIGIN_RESTRICTIONi, | ||
| 133 | $UNUSEDi, | ||
| 134 | $CYCLES_SINCE_WEIGHT_CHANGEi, | ||
| 135 | # Remainder are references to vectors | ||
| 136 | $WEIGHTi, | ||
| 137 | $WEIGHT_MINi, | ||
| 138 | $WEIGHT_MAXi, | ||
| 139 | $ATTRIB_MINi, | ||
| 140 | $ATTRIB_MAXi, | ||
| 141 | $ATTRIB_SCALEi, | ||
| 142 | $ATTRIBUTEi, | ||
| 143 | $RLS_Pi, | ||
| 144 | $RLS_TMP_1i, | ||
| 145 | $RLS_TMP_2i, | ||
| 146 | ) = | ||
| 147 | (0 .. $LTU_LENGTH-1); | ||
| 148 | |||
| 149 | $LTU_ATTRIBUTE_MIN = -0.5; | ||
| 150 | |||
| 151 | ## | ||
| 152 | ## new creates a new LTU with weights set to 0. | ||
| 153 | ## | ||
| 154 | sub new { | ||
| 155 | my($type, $nvars, $scaling) = @_; | ||
| 156 | |||
| 157 | ## | ||
| 158 | ## First allocate the LTU, and fill in its basic information. | ||
| 159 | ## | ||
| 160 | |||
| 161 | my($self) = [ (0) x $LTU_LENGTH ]; | ||
| 162 | |||
| 163 | my($length) = $nvars + 1; | ||
| 164 | |||
| 165 | $self->[$SCALINGi] = $scaling; | ||
| 166 | $self->[$LENGTHi] = $length; | ||
| 167 | $self->[$ORIGIN_RESTRICTIONi] = $LTU_ORIGIN_OFF; | ||
| 168 | |||
| 169 | ## | ||
| 170 | ## Create vectors used by all types. | ||
| 171 | ## | ||
| 172 | $self->[$WEIGHTi] = [ (0.0) x $length ]; | ||
| 173 | $self->[$WEIGHT_MINi] = [ (0.0) x $length ]; | ||
| 174 | $self->[$WEIGHT_MAXi] = [ (0.0) x $length ]; | ||
| 175 | $self->[$ATTRIBUTEi] = [ (0.0) x $length ]; | ||
| 176 | |||
| 177 | ## | ||
| 178 | ## Allocate scaling vectors if necessary. | ||
| 179 | ## | ||
| 180 | if ($scaling == $LTU_SCALING_ON) { | ||
| 181 | |||
| 182 | ## Signal need to initialize these values by setting attrib_min > max | ||
| 183 | |||
| 184 | $self->[$ATTRIB_MINi] = [ (1.0) x $length ]; | ||
| 185 | $self->[$ATTRIB_MAXi] = [ (-1.0) x $length ]; | ||
| 186 | $self->[$ATTRIB_SCALEi] = [ (1.0) x $length ]; | ||
| 187 | |||
| 188 | }; | ||
| 189 | |||
| 190 | bless $self, $type; # Bless into type | ||
| 191 | } | ||
| 192 | |||
| 193 | |||
| 194 | ## | ||
| 195 | ## COPY creates a copy of itself, which it returns | ||
| 196 | ## | ||
| 197 | sub copy { | ||
| 198 | my($self) = @_; | ||
| 199 | |||
| 200 | ## | ||
| 201 | ## First, create a new LTU. | ||
| 202 | ## | ||
| 203 | |||
| 204 | my($new) = new Statistics::LTU($self->[$LENGTHi]-1, | ||
| 205 | $self->[$SCALINGi]); | ||
| 206 | |||
| 207 | ## | ||
| 208 | ## Copy the basic information that all LTU's have. (The attribute vector | ||
| 209 | ## isn't copied because its contents are temporary.) | ||
| 210 | ## | ||
| 211 | $new->[$CYCLES_SINCE_WEIGHT_CHANGEi] = | ||
| 212 | $self->[$CYCLES_SINCE_WEIGHT_CHANGEi]; | ||
| 213 | $new->[$ORIGIN_RESTRICTIONi] = | ||
| 214 | $self->[$ORIGIN_RESTRICTIONi]; | ||
| 215 | |||
| 216 | $new->[$WEIGHTi] = [@{$self->[$WEIGHTi]}]; | ||
| 217 | $new->[$WEIGHT_MINi] = [@{$self->[$WEIGHT_MINi]}]; | ||
| 218 | $new->[$WEIGHT_MAXi] = [@{$self->[$WEIGHT_MAXi]}]; | ||
| 219 | |||
| 220 | ## | ||
| 221 | ## Copy scaling data, if necessary. | ||
| 222 | ## | ||
| 223 | if ($new->[$SCALINGi] == $LTU_SCALING_ON) { | ||
| 224 | $new->[$ATTRIB_MINi] = $self->[$ATTRIB_MINi]; | ||
| 225 | $new->[$ATTRIB_MAXi] = $self->[$ATTRIB_MAXi]; | ||
| 226 | $new->[$ATTRIB_SCALEi] = $self->[$ATTRIB_SCALEi]; | ||
| 227 | } | ||
| 228 | |||
| 229 | $new; | ||
| 230 | } | ||
| 231 | |||
| 232 | |||
| 233 | ## | ||
| 234 | ## LTU_DESTROY destroys an existing linear threshold unit. | ||
| 235 | ##### I don't know if this is necessary but it can't hurt | ||
| 236 | ## | ||
| 237 | sub destroy { | ||
| 238 | my($self) = @_; | ||
| 239 | |||
| 240 | for (0 .. $LTU_LENGTH-1) { | ||
| 241 | undef $self->[$_]; | ||
| 242 | } | ||
| 243 | undef $self; | ||
| 244 | |||
| 245 | } | ||
| 246 | |||
| 247 | |||
| 248 | ## | ||
| 249 | ## IS_CYCLING returns a boolean value which indicates whether or not the | ||
| 250 | ## LTU's weights are cycling. If they are, further training will not produce | ||
| 251 | ## better results. This judgement is based upon the heuristic that if the | ||
| 252 | ## weights are not cycling, then the min/max's of weights will change fairly | ||
| 253 | ## often. The caller must specify what it thinks is "fairly often". This is | ||
| 254 | ## done with a parameter that specifies how many times the training function | ||
| 255 | ## can adjust weights before the caller would expect at least one weight to | ||
| 256 | ## have its min/max adjusted. Some common values for this parameter are the | ||
| 257 | ## number of attributes, or the log of the number of attributes. | ||
| 258 | ## | ||
| 259 | |||
| 260 | sub is_cycling { | ||
| 261 | my($self, $how_often_weights_must_change) = @_; | ||
| 262 | |||
| 263 | $self->[$CYCLES_SINCE_WEIGHT_CHANGEi] > $how_often_weights_must_change; | ||
| 264 | } | ||
| 265 | |||
| 266 | |||
| 267 | ## | ||
| 268 | ## PRINT describes the LTU and its weights. | ||
| 269 | ## | ||
| 270 | sub print { | ||
| 271 | my($self) = @_; | ||
| 272 | |||
| 273 | print "The linear threshold unit is of type ", ref($self); | ||
| 274 | print " and contains ", $self->[$LENGTHi], " weights.\n"; | ||
| 275 | |||
| 276 | printf("Origin restriction=%d.\n", | ||
| 277 | $self->[$ORIGIN_RESTRICTIONi]); | ||
| 278 | printf("Cycles since last change of a weight min/max: %d.\n", | ||
| 279 | $self->[$CYCLES_SINCE_WEIGHT_CHANGEi]); | ||
| 280 | |||
| 281 | ## | ||
| 282 | ## Print 8 weights to a line, just to make it easier to read. | ||
| 283 | ## | ||
| 284 | |||
| 285 | my($i); | ||
| 286 | |||
| 287 | for $i (0 .. $self->[$LENGTHi]-1) { | ||
| 288 | print $self->[$WEIGHTi]->[$i], " "; | ||
| 289 | |||
| 290 | printf("\n") if $i%8 == 7; | ||
| 291 | }; | ||
| 292 | |||
| 293 | print "\n"; | ||
| 294 | } | ||
| 295 | |||
| 296 | |||
| 297 | ## | ||
| 298 | ## RESTORE restores a linear threshold unit from the specified file. | ||
| 299 | ## | ||
| 300 | sub restore { | ||
| 301 | my($class, $file_name) = @_; | ||
| 302 | |||
| 303 | if (!open(FILE, "<$file_name")) { | ||
| 304 | carp "restore: $file_name: $!\n"; | ||
| 305 | return(0); | ||
| 306 | } | ||
| 307 | |||
| 308 | ## | ||
| 309 | ## Check that the LTU is consistent with this version | ||
| 310 | ## | ||
| 311 | my($ltu_version); | ||
| 312 | $ltu_version = <FILE>; | ||
| 313 | chop($ltu_version); | ||
| 314 | |||
| 315 | if ($ltu_version < $read_LTU_files_back_to) { | ||
| 316 | carp "restore: $file_name written with LTU version $ltu_version.\n"; | ||
| 317 | carp "This is version $Statistics::LTU::VERSION, which only understands\n"; | ||
| 318 | carp "LTU files back to $read_LTU_files_back_to.\n"; | ||
| 319 | carp "LTU not restored!\n"; | ||
| 320 | return(0); | ||
| 321 | } | ||
| 322 | |||
| 323 | ## First, read the type, the number of weights, and whether or not scaling | ||
| 324 | ## is enabled. Then, create a linear threshold unit. Note that the | ||
| 325 | ## number of variables in the training instances is one less than the | ||
| 326 | ## number of weights (one weight is a constant). | ||
| 327 | ## | ||
| 328 | |||
| 329 | my($line); | ||
| 330 | $line = <FILE>; | ||
| 331 | chop($line); | ||
| 332 | my($type, $length, $scaling, $cycles_since_weight_change, $origin) = | ||
| 333 | split(/ /, $line); | ||
| 334 | |||
| 335 | my($new) = $type->new($length-1, $scaling); | ||
| 336 | |||
| 337 | $new->[$CYCLES_SINCE_WEIGHT_CHANGEi] = $cycles_since_weight_change; | ||
| 338 | $new->[$ORIGIN_RESTRICTIONi] = $origin; | ||
| 339 | |||
| 340 | ## | ||
| 341 | ## Now, read the weights, the minimum value of the attributes, and the | ||
| 342 | ## maximum value of the attributes, from the file, and store them in the | ||
| 343 | ## linear threshold unit. | ||
| 344 | |||
| 345 | my($new_weight) = $new->[$WEIGHTi]; | ||
| 346 | my($new_weight_min) = $new->[$WEIGHT_MINi]; | ||
| 347 | my($new_weight_max) = $new->[$WEIGHT_MAXi]; | ||
| 348 | my($new_attrib_min) = $new->[$ATTRIB_MINi]; | ||
| 349 | my($new_attrib_max) = $new->[$ATTRIB_MAXi]; | ||
| 350 | my($new_attrib_scale) = $new->[$ATTRIB_SCALEi]; | ||
| 351 | |||
| 352 | my($i, @fields); | ||
| 353 | |||
| 354 | for $i (0 .. $length-1) { | ||
| 355 | $line = <FILE>; chop($line); | ||
| 356 | |||
| 357 | @fields = split(/ /, $line); | ||
| 358 | |||
| 359 | ($new_weight->[$i], $new_weight_min->[$i], $new_weight_max->[$i]) = | ||
| 360 | @fields; | ||
| 361 | |||
| 362 | if ($scaling == $LTU_SCALING_ON) { | ||
| 363 | |||
| 364 | ($new_attrib_min->[$i], $new_attrib_max->[$i]) = @fields[3,4]; | ||
| 365 | |||
| 366 | if ($new_attrib_max->[$i] > $new_attrib_min->[$i]) { | ||
| 367 | $new_attrib_scale->[$i] = 1.0 / | ||
| 368 | ($new_attrib_max->[$i] - $new_attrib_min->[$i]); | ||
| 369 | } | ||
| 370 | } | ||
| 371 | } | ||
| 372 | |||
| 373 | ##### Pick up final theta value | ||
| 374 | $line = <FILE>; chop($line); | ||
| 375 | $new->[$WEIGHTi]->[$length-1] = $line; | ||
| 376 | |||
| 377 | close(FILE); | ||
| 378 | |||
| 379 | $new; | ||
| 380 | } | ||
| 381 | |||
| 382 | |||
| 383 | |||
| 384 | ## | ||
| 385 | ## SAVE saves the linear threshold unit in the specified file. | ||
| 386 | ## If the file can't be created for some reason, 0 is returned. Otherwise, 1 | ||
| 387 | ## is returned. | ||
| 388 | ## | ||
| 389 | sub save { | ||
| 390 | my($self, $file_name) = @_; | ||
| 391 | my($length) = $self->[$LENGTHi]; | ||
| 392 | |||
| 393 | if (!open(FILE, ">$file_name")) { | ||
| 394 | carp "Statistics::LTU::save: $file_name: $!"; | ||
| 395 | return(0); | ||
| 396 | } | ||
| 397 | |||
| 398 | ## | ||
| 399 | ## Stamp the file with a version number. | ||
| 400 | ## | ||
| 401 | print FILE $Statistics::LTU::VERSION, "\n"; | ||
| 402 | |||
| 403 | ## First, print the type, the number of weights and whether or not scaling | ||
| 404 | ## is enabled. Then, print the weights, the minimum value of the | ||
| 405 | ## attribute, and the maximum value of the attribute. Print 1 | ||
| 406 | ## attribute/line, just to make it easier to read. | ||
| 407 | |||
| 408 | print FILE join(' ', ((ref $self), | ||
| 409 | $self->[$LENGTHi], | ||
| 410 | $self->[$SCALINGi], | ||
| 411 | $self->[$CYCLES_SINCE_WEIGHT_CHANGEi], | ||
| 412 | $self->[$ORIGIN_RESTRICTIONi] | ||
| 413 | )), | ||
| 414 | "\n"; | ||
| 415 | |||
| 416 | my($i); | ||
| 417 | for $i (0 .. $length-1) { | ||
| 418 | |||
| 419 | printf FILE "%lf %lf %lf ", | ||
| 420 | $self->[$WEIGHTi]->[$i], | ||
| 421 | $self->[$WEIGHT_MINi]->[$i], | ||
| 422 | $self->[$WEIGHT_MAXi]->[$i]; | ||
| 423 | |||
| 424 | if ($self->[$SCALINGi] == $LTU_SCALING_ON) { | ||
| 425 | printf FILE "%lf %lf ", | ||
| 426 | $self->[$ATTRIB_MINi]->[$i], | ||
| 427 | $self->[$ATTRIB_MAXi]->[$i]; | ||
| 428 | } | ||
| 429 | |||
| 430 | print FILE "\n"; | ||
| 431 | } | ||
| 432 | |||
| 433 | printf FILE "%lf\n", $self->[$WEIGHTi]->[$length-1]; | ||
| 434 | |||
| 435 | close(FILE); | ||
| 436 | |||
| 437 | 1; | ||
| 438 | } | ||
| 439 | |||
| 440 | |||
| 441 | ## | ||
| 442 | ## TEST computes and returns the result of applying the linear threshold | ||
| 443 | ## unit to the instance_vector. | ||
| 444 | ## | ||
| 445 | |||
| 446 | sub test { | ||
| 447 | my($self, $instance_vector) = @_; | ||
| 448 | |||
| 449 | my($length) = $self->[$LENGTHi]; | ||
| 450 | my($weight) = $self->[$WEIGHTi]; | ||
| 451 | my($attribute) = $self->[$ATTRIBUTEi]; | ||
| 452 | |||
| 453 | $self->_scale_attributes($instance_vector); | ||
| 454 | |||
| 455 | my($result) = $weight->[$length-1]; | ||
| 456 | |||
| 457 | my($i); | ||
| 458 | |||
| 459 | for $i (0 .. $length-2) { | ||
| 460 | $result += $attribute->[$i] * $weight->[$i]; | ||
| 461 | } | ||
| 462 | |||
| 463 | $result; | ||
| 464 | |||
| 465 | } | ||
| 466 | |||
| 467 | |||
| 468 | |||
| 469 | ## | ||
| 470 | ## _SCALE_ATTRIBUTES translates and scales the attributes comprising an | ||
| 471 | ## instance vector. The scaled attributes are stored in the attributes slot | ||
| 472 | ## on the LTU, so that the original instance_vector will not be changed. | ||
| 473 | ## | ||
| 474 | sub _scale_attributes { | ||
| 475 | my($self, $instance_vector) = @_; | ||
| 476 | my($length) = $self->[$LENGTHi]; | ||
| 477 | |||
| 478 | my($attribute) = $self->[$ATTRIBUTEi]; | ||
| 479 | my($attrib_min) = $self->[$ATTRIB_MINi]; | ||
| 480 | my($attrib_scale) = $self->[$ATTRIB_SCALEi]; | ||
| 481 | |||
| 482 | ## | ||
| 483 | ## If scaling is disabled, just copy the instance attribute values into | ||
| 484 | ## the temporary attribute vector. This isolates the input values from | ||
| 485 | ## the caller's storage and converts to double precision. Otherwise | ||
| 486 | ## translate and scale the attributes. When min=max, only one value has | ||
| 487 | ## been seen for the attribute. In that case, the attribute is mapped | ||
| 488 | ## to the minimum value. It is advantageous to use one of the endpoints, | ||
| 489 | ## instead of the midpoint 0.0, because it lets training begin. | ||
| 490 | ## | ||
| 491 | |||
| 492 | my($i); | ||
| 493 | |||
| 494 | if ($self->[$SCALINGi] == $LTU_SCALING_OFF) { | ||
| 495 | |||
| 496 | for $i (0 .. $#$instance_vector) { | ||
| 497 | $attribute->[$i] = $instance_vector->[$i]; | ||
| 498 | } | ||
| 499 | } else { | ||
| 500 | |||
| 501 | for $i (0 .. $#$instance_vector) { | ||
| 502 | $attribute->[$i] = (($instance_vector->[$i] - $attrib_min->[$i]) * | ||
| 503 | $attrib_scale->[$i]) + $LTU_ATTRIBUTE_MIN; | ||
| 504 | } | ||
| 505 | } | ||
| 506 | |||
| 507 | ## | ||
| 508 | ## A constant attribute must be added to the end of the instance vector | ||
| 509 | ## in order to learn some kinds of functions (e.g. f(x)=mx+b, the function | ||
| 510 | ## for a line in 2D space). The constant attribute enables the training | ||
| 511 | ## function to learn a weight that translates the hyperplane. However, | ||
| 512 | ## it is undesirable for other kinds of functions (e.g. f(x)=mx, which is | ||
| 513 | ## what some preference predicates look like). Decide what to do. | ||
| 514 | ## | ||
| 515 | |||
| 516 | if ($self->[$ORIGIN_RESTRICTIONi] == $LTU_ORIGIN_ON) { | ||
| 517 | $attribute->[$length-1] = 0.0; | ||
| 518 | |||
| 519 | } else { | ||
| 520 | $attribute->[$length-1] = 1.0; | ||
| 521 | } | ||
| 522 | |||
| 523 | } | ||
| 524 | |||
| 525 | |||
| 526 | ## | ||
| 527 | ## MAINTAIN_SCALING_FACTORS checks that the specified instance falls within | ||
| 528 | ## the min-max ranges of the specified LTU. If it does, nothing changes. | ||
| 529 | ## If it does not, the min-max ranges and the scaling factors are adjusted | ||
| 530 | ## to accomodate this new instance. | ||
| 531 | ## | ||
| 532 | sub maintain_scaling_factors { | ||
| 533 | my($self, $instance_vector) = @_; | ||
| 534 | |||
| 535 | ## | ||
| 536 | ## If scaling is disabled, don't do anything. | ||
| 537 | ## | ||
| 538 | return if $self->[$SCALINGi] == $LTU_SCALING_OFF; | ||
| 539 | |||
| 540 | my($length) = $self->[$LENGTHi]; | ||
| 541 | my($attrib_min) = $self->[$ATTRIB_MINi]; | ||
| 542 | my($attrib_max) = $self->[$ATTRIB_MAXi]; | ||
| 543 | my($attrib_scale) = $self->[$ATTRIB_SCALEi]; | ||
| 544 | |||
| 545 | my($i); | ||
| 546 | for $i (0 .. $length-2) { | ||
| 547 | if ($attrib_min->[$i] > $attrib_max->[$i]) { | ||
| 548 | |||
| 549 | ## | ||
| 550 | ## Initialization. | ||
| 551 | ## | ||
| 552 | $attrib_min->[$i] = $attrib_max->[$i] = | ||
| 553 | $instance_vector->[$i]; | ||
| 554 | |||
| 555 | } elsif ($instance_vector->[$i] > $attrib_max->[$i]) { | ||
| 556 | |||
| 557 | $attrib_max->[$i] = $instance_vector->[$i]; | ||
| 558 | $attrib_scale->[$i] = | ||
| 559 | 1.0 / ($attrib_max->[$i] - $attrib_min->[$i]); | ||
| 560 | |||
| 561 | } elsif ($instance_vector->[$i] < $self->[$ATTRIB_MINi]->[$i]) { | ||
| 562 | |||
| 563 | $attrib_min->[$i] = $instance_vector->[$i]; | ||
| 564 | $attrib_scale->[$i] = | ||
| 565 | 1.0 / ($attrib_max->[$i] - $attrib_min->[$i]); | ||
| 566 | } | ||
| 567 | } | ||
| 568 | } | ||
| 569 | |||
| 570 | ## | ||
| 571 | ## SET_ORIGIN_RESTRICTION | ||
| 572 | ## | ||
| 573 | sub set_origin_restriction { | ||
| 574 | my($self, $value) = @_; | ||
| 575 | |||
| 576 | if (($value == $LTU_ORIGIN_OFF) or ($value == $LTU_ORIGIN_ON)) { | ||
| 577 | $self->[$ORIGIN_RESTRICTIONi] = $value; | ||
| 578 | } else { | ||
| 579 | carp "$value is an unacceptable value for the origin restriction\n"; | ||
| 580 | } | ||
| 581 | } | ||
| 582 | |||
| 583 | |||
| 584 | ## | ||
| 585 | ## LTU_WEIGHTS returns a ref to copy of the LTU weights. | ||
| 586 | ## | ||
| 587 | sub weights { | ||
| 588 | my($self) = @_; | ||
| 589 | |||
| 590 | [@{$self->[$WEIGHTi]}]; | ||
| 591 | } | ||
| 592 | |||
| 593 | |||
| 594 | ## | ||
| 595 | ## UPDATE_WEIGHT_MIN_MAX updates an LTU's weight min/max's and its | ||
| 596 | ## cycles_since_weight_change field. Although it is simple, it is | ||
| 597 | ## implemented as a separate routine because each training routine | ||
| 598 | ## should perform this function before returning the LTU to the caller. | ||
| 599 | ## | ||
| 600 | sub update_weight_min_max { | ||
| 601 | my($self) = @_; | ||
| 602 | |||
| 603 | my($boundary_changed) = 0; | ||
| 604 | |||
| 605 | my($weight) = $self->[$WEIGHTi]; | ||
| 606 | my($weight_max) = $self->[$WEIGHT_MAXi]; | ||
| 607 | my($weight_min) = $self->[$WEIGHT_MINi]; | ||
| 608 | |||
| 609 | my($i); | ||
| 610 | for $i (0 .. $self->[$LENGTHi]-2) { | ||
| 611 | |||
| 612 | if ($weight_max->[$i] < $weight->[$i]) { | ||
| 613 | $weight_max->[$i] = $weight->[$i]; | ||
| 614 | $boundary_changed = 1; | ||
| 615 | |||
| 616 | } elsif ($weight_min->[$i] > $weight->[$i]) { | ||
| 617 | $weight_min->[$i] = $weight->[$i]; | ||
| 618 | $boundary_changed = 1; | ||
| 619 | } | ||
| 620 | } | ||
| 621 | |||
| 622 | if ($boundary_changed) { | ||
| 623 | $self->[$CYCLES_SINCE_WEIGHT_CHANGEi] = 0; | ||
| 624 | } else { | ||
| 625 | $self->[$CYCLES_SINCE_WEIGHT_CHANGEi]++; | ||
| 626 | } | ||
| 627 | } | ||
| 628 | |||
| 629 | |||
| 630 | |||
| 631 | ## | ||
| 632 | ## $LTU->correctly_classifies($instance, $value) | ||
| 633 | ## Returns 1 iff $instance is on the same side of Statistics::LTU::THRESHOLD | ||
| 634 | ## as $value is. | ||
| 635 | ## | ||
| 636 | sub correctly_classifies { | ||
| 637 | my($self, $instance, $desired_value) = @_; | ||
| 638 | |||
| 639 | my($actual_value) = $self->test($instance); | ||
| 640 | |||
| 641 | ((($actual_value < $LTU_THRESHOLD) and | ||
| 642 | ($desired_value < $LTU_THRESHOLD)) or | ||
| 643 | (($actual_value >= $LTU_THRESHOLD) and | ||
| 644 | ($desired_value >= $LTU_THRESHOLD))); | ||
| 645 | } | ||
| 646 | |||
| 647 | ## | ||
| 648 | ## ltu->eval_on_set(examples) | ||
| 649 | ## | ||
| 650 | ## Evaluates an LTU on a set of example, returning 4 integers: | ||
| 651 | ## True negatives, false positives, false negatives and true positives. | ||
| 652 | ## | ||
| 653 | ## Argument is a ref to a list of examples. | ||
| 654 | ## Each example is a ref to an array of [Vector, Value]. | ||
| 655 | ## Each Vector is a feature vector. | ||
| 656 | ## | ||
| 657 | ## Example: | ||
| 658 | ## @Results = $LTU->eval_on_set([[[1,4,-2],1], [[1,-2,2],-1]]); | ||
| 659 | ## ($TN, $FP, $FN, $TP) = @Results; | ||
| 660 | ## | ||
| 661 | sub eval_on_set { | ||
| 662 | my($self, $Examples) = @_; | ||
| 663 | |||
| 664 | my($TN, $FP, $FN, $TP) = (0,0,0,0); | ||
| 665 | my($example, $Instance, $DesiredValue, $ActualValue); | ||
| 666 | |||
| 667 | foreach $example (@{$Examples}) { | ||
| 668 | ($Instance, $DesiredValue) = @{$example}; | ||
| 669 | my($ActualValue) = $self->test($Instance); | ||
| 670 | if ($ActualValue >= $LTU_THRESHOLD) { | ||
| 671 | if ($DesiredValue >= $LTU_THRESHOLD) { | ||
| 672 | $TP++; | ||
| 673 | } else { | ||
| 674 | $FP++; | ||
| 675 | } | ||
| 676 | } elsif ($DesiredValue >= $LTU_THRESHOLD) { | ||
| 677 | $FN++; | ||
| 678 | } else { | ||
| 679 | $TN++; | ||
| 680 | } | ||
| 681 | } | ||
| 682 | |||
| 683 | ($TN, $FP, $FN, $TP); | ||
| 684 | } | ||
| 685 | |||
| 686 | |||
| 687 | |||
| 688 | ### | ||
| 689 | ### Specific LTU types built upon LTU. | ||
| 690 | ### 1. The absolute correction rule. | ||
| 691 | |||
| 692 | package Statistics::LTU::ACR; | ||
| 693 | @Statistics::LTU::ACR::ISA = qw( Statistics::LTU ); | ||
| 694 | use Statistics::LTU; | ||
| 695 | |||
| 696 | ## | ||
| 697 | ## ACR::TRAIN trains the specified linear threshold unit on a particular | ||
| 698 | ## instance_vector. It returns 1 if the linear threshold unit already | ||
| 699 | ## classified the instance_vector correctly, otherwise it returns 0. | ||
| 700 | ## The training rule is taken from Nilsson's "Learning Machines" book. | ||
| 701 | ## | ||
| 702 | |||
| 703 | sub train { | ||
| 704 | my($self, $instance_vector, $desired_value) = @_; | ||
| 705 | my($length) = $self->[$LENGTHi]; | ||
| 706 | |||
| 707 | ## | ||
| 708 | ## Only train the linear threshold unit if it does not classify correctly. | ||
| 709 | ## | ||
| 710 | |||
| 711 | my($actual_value) = $self->test($instance_vector); | ||
| 712 | |||
| 713 | die "\$Statistics::LTU::LTU_THRESHOLD undefined in ACR" unless defined($Statistics::LTU::LTU_THRESHOLD); | ||
| 714 | |||
| 715 | return(1) if ((($actual_value < $Statistics::LTU::LTU_THRESHOLD) && | ||
| 716 | ($desired_value < $Statistics::LTU::LTU_THRESHOLD)) || | ||
| 717 | (($actual_value >= $Statistics::LTU::LTU_THRESHOLD) && | ||
| 718 | ($desired_value >= $Statistics::LTU::LTU_THRESHOLD))); | ||
| 719 | |||
| 720 | ## The scale factor can only be changed when the weights are being | ||
| 721 | ## changed, because a change to the scale factor invalidates the current | ||
| 722 | ## set of weights. | ||
| 723 | |||
| 724 | $self->maintain_scaling_factors($instance_vector); | ||
| 725 | $self->_scale_attributes($instance_vector); | ||
| 726 | |||
| 727 | ## | ||
| 728 | ## Decide how much to adjust the weights by. The absolute correction rule | ||
| 729 | ## (which provides the fastest learning) requires the dot product of the | ||
| 730 | ## instance vector, so do that first. | ||
| 731 | ## | ||
| 732 | |||
| 733 | my($dot_product) = 0; | ||
| 734 | |||
| 735 | foreach (@{$self->[$ATTRIBUTEi]}) { | ||
| 736 | $dot_product += $_ * $_; | ||
| 737 | } | ||
| 738 | |||
| 739 | my($delta) = $dot_product==0 ? .1 | ||
| 740 | : 1.0 + (int(abs($actual_value) / $dot_product)); | ||
| 741 | $delta = -$delta if $desired_value < $Statistics::LTU::LTU_THRESHOLD; | ||
| 742 | |||
| 743 | ## | ||
| 744 | ## Now, adjust the weights. The last weight is handled differently, | ||
| 745 | ## because it is always applied to the constant 1. | ||
| 746 | ## | ||
| 747 | |||
| 748 | my($weight) = $self->[$WEIGHTi]; | ||
| 749 | my($attribute) = $self->[$ATTRIBUTEi]; | ||
| 750 | |||
| 751 | my($i); | ||
| 752 | for $i (0 .. $length-1) { | ||
| 753 | $weight->[$i] += $delta * $attribute->[$i]; | ||
| 754 | } | ||
| 755 | $self->update_weight_min_max; | ||
| 756 | |||
| 757 | 0; | ||
| 758 | } | ||
| 759 | |||
| 760 | |||
| 761 | |||
| 762 | ### | ||
| 763 | ### Recursive Least Squares linear threshold units | ||
| 764 | ### | ||
| 765 | package Statistics::LTU::RLS; | ||
| 766 | use Statistics::LTU; | ||
| 767 | use Carp; | ||
| 768 | |||
| 769 | @Statistics::LTU::RLS::Inherit::ISA = @Statistics::LTU::RLS::ISA = | ||
| 770 | qw( Statistics::LTU ); | ||
| 771 | |||
| 772 | sub new { | ||
| 773 | my($type, $nvars, $scaling) = @_; | ||
| 774 | |||
| 775 | # I don't know how to get inheritance to work automatically here | ||
| 776 | my($self) = new Statistics::LTU($nvars, $scaling); | ||
| 777 | |||
| 778 | my($length) = $nvars + 1; | ||
| 779 | |||
| 780 | ## | ||
| 781 | ## Allocate temporary space for the RLS rule. | ||
| 782 | ## | ||
| 783 | |||
| 784 | my($p) = [ (0.0) x ($length * $length) ]; | ||
| 785 | $self->[$RLS_Pi] = $p; | ||
| 786 | $self->[$RLS_TMP_1i] = [ (0.0) x $length ]; | ||
| 787 | $self->[$RLS_TMP_2i] = [ (0.0) x $length ]; | ||
| 788 | |||
| 789 | ## [Used to be rls_init_p] | ||
| 790 | ## Initialize the p matrix to an arbitrarily large value along the | ||
| 791 | ## diagonal, and zero everywhere else. Suggested in the algorithm, p 27. | ||
| 792 | ## | ||
| 793 | ## Young says that "large diagonal elements [for the p matrix] (say 10^6 | ||
| 794 | ## in general) will yield convergence and performance comensurate with the | ||
| 795 | ## stage-wise solution of the same problem" (p 27). I have found that | ||
| 796 | ## values larger than 10^6 sometimes converge to more accurate weights; | ||
| 797 | ## however, values that are too large (e.g. 2.0 * 10^15) produce less | ||
| 798 | ## accurate weights. The value defined below is intended to be as large | ||
| 799 | ## as is possible without adversely affecting performance. It was | ||
| 800 | ## determined empirically. | ||
| 801 | ## | ||
| 802 | |||
| 803 | my($DIAGONAL_VALUE) = 10.0e+6; | ||
| 804 | |||
| 805 | my($i, $j); | ||
| 806 | for $i (0 .. $length-1) { | ||
| 807 | |||
| 808 | $p->[$i * $length + $i] = $DIAGONAL_VALUE; | ||
| 809 | |||
| 810 | for $j ($i+1 .. $length-1) { | ||
| 811 | |||
| 812 | $p->[$i * $length + $j] = 0.0; | ||
| 813 | $p->[$j * $length + $i] = 0.0; | ||
| 814 | } | ||
| 815 | } | ||
| 816 | |||
| 817 | bless $self; | ||
| 818 | } | ||
| 819 | |||
| 820 | |||
| 821 | |||
| 822 | sub copy { | ||
| 823 | my($self) = @_; | ||
| 824 | |||
| 825 | my($new) = $self->Statistics::LTU::RLS::Inherit::copy($self); | ||
| 826 | |||
| 827 | ## | ||
| 828 | ## Copy the RLS data. (The tmp_1 and tmp_2 vectors | ||
| 829 | ## aren't copied because their contents are temporary.) | ||
| 830 | ## | ||
| 831 | |||
| 832 | $new->[$RLS_Pi] = [@{$self->[$RLS_Pi]}]; | ||
| 833 | |||
| 834 | bless $new; | ||
| 835 | } | ||
| 836 | |||
| 837 | ## | ||
| 838 | ## TRAIN trains the specified linear threshold unit on a particular | ||
| 839 | ## instance_vector. The training rule is taken from Young's "Recursive | ||
| 840 | ## Estimation and Time-Series Analysis" book, pp26-27. | ||
| 841 | ## Return value is undefined. | ||
| 842 | |||
| 843 | sub train { | ||
| 844 | my($self, $instance_vector, $desired_value) = @_; | ||
| 845 | |||
| 846 | ## | ||
| 847 | ## First, translate and scale the attributes. The scale factor can only | ||
| 848 | ## be changed when the weights are being changed, because a change to the | ||
| 849 | ## scale factor invalidates the current set of weights. | ||
| 850 | ## | ||
| 851 | |||
| 852 | $self->maintain_scaling_factors($instance_vector); | ||
| 853 | $self->_scale_attributes($instance_vector); | ||
| 854 | |||
| 855 | ## | ||
| 856 | ## Now, update the weights. This is done in two parts, | ||
| 857 | ## updating the P matrix and A vector, respectively. | ||
| 858 | ## | ||
| 859 | $self->rls_update_p; | ||
| 860 | $self->rls_update_a($desired_value); | ||
| 861 | |||
| 862 | $self->update_weight_min_max; | ||
| 863 | undef; | ||
| 864 | } | ||
| 865 | |||
| 866 | |||
| 867 | ## | ||
| 868 | ## RLS_UPDATE_P implements equation II(1) on p26. This is the first half of | ||
| 869 | ## the RLS algorithm. | ||
| 870 | ## | ||
| 871 | ## The terminology matches what is in the book. The algorithm computes | ||
| 872 | ## new values for array p at time k, based upon the vector x at time k and | ||
| 873 | ## the array p at time k-1. | ||
| 874 | ## | ||
| 875 | ## The temporary arrays pkm1_xk and xkT_pkm1 are provided by the caller, to | ||
| 876 | ## eliminate the overhead of creating and destroying temporary arrays. | ||
| 877 | ## | ||
| 878 | |||
| 879 | sub rls_update_p { | ||
| 880 | my($self) = @_; | ||
| 881 | |||
| 882 | my($p) = $self->[$RLS_Pi]; | ||
| 883 | my($length) = $self->[$LENGTHi]; | ||
| 884 | my($xk) = $self->[$ATTRIBUTEi]; | ||
| 885 | my($pkm1_xk) = $self->[$RLS_TMP_1i]; | ||
| 886 | my($xkT_pkm1) = $self->[$RLS_TMP_2i]; | ||
| 887 | |||
| 888 | my($i, $j); | ||
| 889 | |||
| 890 | ## | ||
| 891 | ## Multiply pkm1 (the array p at time k-1) by xk (x at time k). | ||
| 892 | ## | ||
| 893 | |||
| 894 | for ($i=0; $i<$length; $i++) { | ||
| 895 | $pkm1_xk->[$i] = 0.0; | ||
| 896 | |||
| 897 | for ($j=0; $j<$length; $j++) { | ||
| 898 | $pkm1_xk->[$i] += $p->[$i * $length + $j] * $xk->[$j]; | ||
| 899 | } | ||
| 900 | } | ||
| 901 | |||
| 902 | ## | ||
| 903 | ## Get the scalar value [1 + xkT_pkm1_xk] ^ -1. Call it scalar_value. | ||
| 904 | ## This can't be done until after pkm1_xk has been computed. | ||
| 905 | ## | ||
| 906 | my($scalar_value) = 1.0; | ||
| 907 | |||
| 908 | for ($i=0; $i<$length; $i++) { | ||
| 909 | $scalar_value += $pkm1_xk->[$i] * $xk->[$i]; | ||
| 910 | } | ||
| 911 | |||
| 912 | $scalar_value = 1.0 / $scalar_value; | ||
| 913 | |||
| 914 | ## | ||
| 915 | ## Fold the scalar_value into pkm1_xk. This is more efficient than doing | ||
| 916 | ## it later, because pkm1_xk is an nx1 array and later arrays will be nxn. | ||
| 917 | ## | ||
| 918 | |||
| 919 | for $i (0 .. $length-1) { | ||
| 920 | $pkm1_xk->[$i] *= $scalar_value; | ||
| 921 | } | ||
| 922 | |||
| 923 | ## | ||
| 924 | ## Multiply xkT (x at time k, transposed) by pkm1 (the array p | ||
| 925 | ## at time k-1). | ||
| 926 | ## | ||
| 927 | |||
| 928 | for $i (0 .. $length-1) { | ||
| 929 | $xkT_pkm1->[$i] = 0.0; | ||
| 930 | |||
| 931 | for $j (0 .. $length-1) { | ||
| 932 | $xkT_pkm1->[$i] += $xk->[$j] * $p->[$j * $length + $i]; | ||
| 933 | } | ||
| 934 | } | ||
| 935 | |||
| 936 | ## | ||
| 937 | ## Multiply pkm1_xk by xkT_pkm1. The result is used to update p, so it | ||
| 938 | ## does not need to be stored explicitly. | ||
| 939 | ## | ||
| 940 | |||
| 941 | for $i (0 .. $length-1) { | ||
| 942 | for $j (0 .. $length-1) { | ||
| 943 | $p->[$i * $length + $j] -= $pkm1_xk->[$i] * $xkT_pkm1->[$j]; | ||
| 944 | } | ||
| 945 | } | ||
| 946 | } | ||
| 947 | |||
| 948 | ## | ||
| 949 | ## RLS_UPDATE_A implements equation II(2) on p26. This is the second half of | ||
| 950 | ## the RLS algorithm. | ||
| 951 | ## | ||
| 952 | sub rls_update_a { | ||
| 953 | my($self, $yk) = @_; | ||
| 954 | |||
| 955 | my($a) = $self->[$WEIGHTi]; | ||
| 956 | my($p) = $self->[$RLS_Pi]; | ||
| 957 | my($length) = $self->[$LENGTHi]; | ||
| 958 | my($xk) = $self->[$ATTRIBUTEi]; | ||
| 959 | |||
| 960 | ## | ||
| 961 | ## Multiply xkT (x at time k, transposed) by akm1 (a at time k-1). The | ||
| 962 | ## result is a scalar value. Subtract yk (y at time k). | ||
| 963 | ## | ||
| 964 | |||
| 965 | my($scalar_value) = 0.0 - $yk; | ||
| 966 | my($i, $j); | ||
| 967 | |||
| 968 | for ($i=0 ; $i<$length ; $i++) { | ||
| 969 | $scalar_value += $xk->[$i] * $a->[$i]; | ||
| 970 | } | ||
| 971 | |||
| 972 | ## | ||
| 973 | ## Multiply pk (p at time k) by xk (x at time k). This is what the | ||
| 974 | ## algorithm calls Kk. Matrix Kk is used to update a, so it does not | ||
| 975 | ## need to be stored explicitly. | ||
| 976 | ## | ||
| 977 | |||
| 978 | my($sum_of_products); | ||
| 979 | |||
| 980 | for $i (0 .. $length-1) { | ||
| 981 | |||
| 982 | $sum_of_products = 0.0; | ||
| 983 | |||
| 984 | for $j (0 .. $length-1) { | ||
| 985 | |||
| 986 | $sum_of_products += $p->[$i * $length + $j] * $xk->[$j]; | ||
| 987 | } | ||
| 988 | |||
| 989 | $a->[$i] -= $sum_of_products * $scalar_value; | ||
| 990 | } | ||
| 991 | } | ||
| 992 | |||
| 993 | |||
| 994 | ## | ||
| 995 | ## RESTORE restores a linear threshold unit from the specified file. | ||
| 996 | ## | ||
| 997 | |||
| 998 | sub restore { | ||
| 999 | my($class, $file_name) = @_; | ||
| 1000 | |||
| 1001 | if (!open(FILE, "<$file_name")) { | ||
| 1002 | carp("restore: $file_name: $!\n"); | ||
| 1003 | return(0); | ||
| 1004 | } | ||
| 1005 | |||
| 1006 | ## | ||
| 1007 | ## Check that the LTU is consistent with this version. | ||
| 1008 | ## | ||
| 1009 | my($ltu_version); | ||
| 1010 | $ltu_version = <FILE>; | ||
| 1011 | chop($ltu_version); | ||
| 1012 | |||
| 1013 | if ($ltu_version < $read_LTU_files_back_to) { | ||
| 1014 | carp "restore: $file_name written with LTU version $ltu_version.\n"; | ||
| 1015 | carp "This is version $Statistics::LTU::VERSION, which only understands\n"; | ||
| 1016 | carp "LTU files back to version $read_LTU_files_back_to\n"; | ||
| 1017 | carp "LTU not restored!\n"; | ||
| 1018 | return(0); | ||
| 1019 | } | ||
| 1020 | |||
| 1021 | ## First, read the type, the number of weights, and whether or not scaling | ||
| 1022 | ## is enabled. Then, create a linear threshold unit. Note that the | ||
| 1023 | ## number of variables in the training instances is one less than the | ||
| 1024 | ## number of weights (one weight is a constant). | ||
| 1025 | ## | ||
| 1026 | |||
| 1027 | my($line); | ||
| 1028 | $line = <FILE>; | ||
| 1029 | chop($line); | ||
| 1030 | my($type, $length, $scaling, $cycles_since_weight_change, $origin) = | ||
| 1031 | split(/ /, $line); | ||
| 1032 | |||
| 1033 | my($new) = $type->new($length-1, $scaling); | ||
| 1034 | |||
| 1035 | $new->[$CYCLES_SINCE_WEIGHT_CHANGEi] = $cycles_since_weight_change; | ||
| 1036 | $new->[$ORIGIN_RESTRICTIONi] = $origin; | ||
| 1037 | |||
| 1038 | ## | ||
| 1039 | ## Now, read the weights, the minimum value of the attributes, and the | ||
| 1040 | ## maximum value of the attributes, from the file, and store them in the | ||
| 1041 | ## linear threshold unit. | ||
| 1042 | |||
| 1043 | my($new_weight) = $new->[$WEIGHTi]; | ||
| 1044 | my($new_weight_min) = $new->[$WEIGHT_MINi]; | ||
| 1045 | my($new_weight_max) = $new->[$WEIGHT_MAXi]; | ||
| 1046 | my($new_attrib_min) = $new->[$ATTRIB_MINi]; | ||
| 1047 | my($new_attrib_max) = $new->[$ATTRIB_MAXi]; | ||
| 1048 | my($new_attrib_scale) = $new->[$ATTRIB_SCALEi]; | ||
| 1049 | |||
| 1050 | my($i, @fields); | ||
| 1051 | |||
| 1052 | for $i (0 .. $length-1) { | ||
| 1053 | $line = <FILE>; chop($line); | ||
| 1054 | |||
| 1055 | @fields = split(/ /, $line); | ||
| 1056 | |||
| 1057 | ($new_weight->[$i], $new_weight_min->[$i], $new_weight_max->[$i]) = | ||
| 1058 | @fields; | ||
| 1059 | |||
| 1060 | if ($scaling == $LTU_SCALING_ON) { | ||
| 1061 | |||
| 1062 | ($new_attrib_min->[$i], $new_attrib_max->[$i]) = @fields[3,4]; | ||
| 1063 | |||
| 1064 | if ($new_attrib_max->[$i] > $new_attrib_min->[$i]) { | ||
| 1065 | $new_attrib_scale->[$i] = 1.0 / | ||
| 1066 | ($new_attrib_max->[$i] - $new_attrib_min->[$i]); | ||
| 1067 | } | ||
| 1068 | } | ||
| 1069 | } | ||
| 1070 | |||
| 1071 | ##### Pick up final theta value | ||
| 1072 | $line = <FILE>; chop($line); | ||
| 1073 | $new->[$WEIGHTi]->[$length-1] = $line; | ||
| 1074 | |||
| 1075 | ## | ||
| 1076 | ## Read information specific to RLS LTU's. | ||
| 1077 | ## | ||
| 1078 | |||
| 1079 | $line = <FILE>; chop($line); | ||
| 1080 | $new->[$RLS_Pi] = [(split(/ /, $line))]; | ||
| 1081 | |||
| 1082 | close(FILE); | ||
| 1083 | |||
| 1084 | $new; | ||
| 1085 | } | ||
| 1086 | |||
| 1087 | |||
| 1088 | |||
| 1089 | ## | ||
| 1090 | ## SAVE saves the linear threshold unit in the specified file. | ||
| 1091 | ## If the file can't be created for some reason, 0 is returned. Otherwise, 1 | ||
| 1092 | ## is returned. | ||
| 1093 | ## | ||
| 1094 | |||
| 1095 | sub save { | ||
| 1096 | my($self, $file_name) = @_; | ||
| 1097 | my($length) = $self->[$LENGTHi]; | ||
| 1098 | |||
| 1099 | if (!open(FILE, ">$file_name")) { | ||
| 1100 | carp("Statistics::LTU::save: $file_name: $!"); | ||
| 1101 | return(0); | ||
| 1102 | } | ||
| 1103 | |||
| 1104 | ## | ||
| 1105 | ## Stamp the file with a version number. | ||
| 1106 | ## | ||
| 1107 | print FILE $Statistics::LTU::VERSION, "\n"; | ||
| 1108 | |||
| 1109 | ## First, print the type, the number of weights and whether or not scaling | ||
| 1110 | ## is enabled. Then, print the weights, the minimum value of the | ||
| 1111 | ## attribute, and the maximum value of the attribute. Print 1 | ||
| 1112 | ## attribute/line, just to make it easier to read. | ||
| 1113 | |||
| 1114 | print FILE join(' ', ((ref $self), | ||
| 1115 | $self->[$LENGTHi], | ||
| 1116 | $self->[$SCALINGi], | ||
| 1117 | $self->[$CYCLES_SINCE_WEIGHT_CHANGEi], | ||
| 1118 | $self->[$ORIGIN_RESTRICTIONi] | ||
| 1119 | )), | ||
| 1120 | "\n"; | ||
| 1121 | |||
| 1122 | my($i); | ||
| 1123 | for $i (0 .. $length-1) { | ||
| 1124 | |||
| 1125 | printf FILE "%lf %lf %lf ", | ||
| 1126 | $self->[$WEIGHTi]->[$i], | ||
| 1127 | $self->[$WEIGHT_MINi]->[$i], | ||
| 1128 | $self->[$WEIGHT_MAXi]->[$i]; | ||
| 1129 | |||
| 1130 | if ($self->[$SCALINGi] == $LTU_SCALING_ON) { | ||
| 1131 | printf FILE "%lf %lf ", | ||
| 1132 | $self->[$ATTRIB_MINi]->[$i], | ||
| 1133 | $self->[$ATTRIB_MAXi]->[$i]; | ||
| 1134 | } | ||
| 1135 | |||
| 1136 | print FILE "\n"; | ||
| 1137 | } | ||
| 1138 | |||
| 1139 | printf FILE "%lf\n", $self->[$WEIGHTi]->[$length-1]; | ||
| 1140 | |||
| 1141 | ## | ||
| 1142 | ## Write out information specific to RLS LTU's. | ||
| 1143 | ## | ||
| 1144 | |||
| 1145 | print FILE join(' ', @{$self->[$RLS_Pi]}), "\n"; | ||
| 1146 | |||
| 1147 | close(FILE); | ||
| 1148 | |||
| 1149 | 1; | ||
| 1150 | } | ||
| 1151 | |||
| 1152 | |||
| 1153 | |||
| 1154 | |||
| 1155 | package Statistics::LTU::LMS; | ||
| 1156 | use Statistics::LTU; | ||
| 1157 | |||
| 1158 | @Statistics::LTU::LMS::ISA = qw( Statistics::LTU ); | ||
| 1159 | |||
| 1160 | ## | ||
| 1161 | ## TRAIN trains the specified linear threshold unit on a particular | ||
| 1162 | ## instance_vector. It returns 1 if the linear threshold unit already | ||
| 1163 | ## classified the instance_vector correctly, otherwise it returns 0. | ||
| 1164 | ## The training rule is the least-mean-square (LMS) rule, devised by | ||
| 1165 | ## Widrow and Hoff in 1960. The advantage of the LMS rule over absolute | ||
| 1166 | ## correction or fixed increment is that it tends to minimize the | ||
| 1167 | ## mean-squared error, even when the classes are not linearly separable. | ||
| 1168 | ## See the AI Handbook or Duda & Hart for more information. | ||
| 1169 | ## | ||
| 1170 | |||
| 1171 | sub train { | ||
| 1172 | my($self, $instance_vector, $desired_value, $rho) = @_; | ||
| 1173 | |||
| 1174 | my($length) = $self->[$LENGTHi]; | ||
| 1175 | my($weight) = $self->[$WEIGHTi]; | ||
| 1176 | my($attribute) = $self->[$ATTRIBUTEi]; | ||
| 1177 | ## | ||
| 1178 | ## Make sure that rho makes sense. If it doesn't, default to 0.2. This | ||
| 1179 | ## number was chosen empirically, on the basis of limited experimentation. | ||
| 1180 | ## | ||
| 1181 | |||
| 1182 | $rho = 0.2 if !defined($rho) or $rho <= 0.0; | ||
| 1183 | |||
| 1184 | ## | ||
| 1185 | ## Only train the linear threshold unit if it does not classify correctly. | ||
| 1186 | ## | ||
| 1187 | |||
| 1188 | my($actual_value) = $self->test($instance_vector); | ||
| 1189 | |||
| 1190 | return(1) if ((($actual_value < $Statistics::LTU::LTU_THRESHOLD) && | ||
| 1191 | ($desired_value < $Statistics::LTU::LTU_THRESHOLD)) || | ||
| 1192 | (($actual_value >= $Statistics::LTU::LTU_THRESHOLD) && | ||
| 1193 | ($desired_value >= $Statistics::LTU::LTU_THRESHOLD))); | ||
| 1194 | |||
| 1195 | ## The scale factor can only be changed when the weights are being | ||
| 1196 | ## changed, because a change to the scale factor invalidates the current | ||
| 1197 | ## set of weights. | ||
| 1198 | |||
| 1199 | $self->maintain_scaling_factors($instance_vector); | ||
| 1200 | $self->_scale_attributes($instance_vector); | ||
| 1201 | |||
| 1202 | ## | ||
| 1203 | ## Decide how much to adjust the weights by. If the actual_value is 0, | ||
| 1204 | ## then the least-mean square rule won't change the weights (it multiplies | ||
| 1205 | ## by 0). Therefore, use the fixed-increment rule (which is slower, but | ||
| 1206 | ## also guaranteed to converge, if convergence is possible) when the | ||
| 1207 | ## actual value is 0. As far as I know, this modification of the LMS rule | ||
| 1208 | ## is original (and harmless). | ||
| 1209 | ## | ||
| 1210 | |||
| 1211 | my($delta) = | ||
| 1212 | ($actual_value == 0.0) ? | ||
| 1213 | $rho * $desired_value | ||
| 1214 | : $rho * (- $actual_value); | ||
| 1215 | |||
| 1216 | ## | ||
| 1217 | ## Now, adjust the weights. The last weight is handled differently, | ||
| 1218 | ## because it is always applied to the constant 1. | ||
| 1219 | ## | ||
| 1220 | |||
| 1221 | my($i); | ||
| 1222 | for $i (0 .. $length-1) { | ||
| 1223 | $weight->[$i] += $delta * $attribute->[$i]; | ||
| 1224 | } | ||
| 1225 | $self->update_weight_min_max; | ||
| 1226 | |||
| 1227 | 0; | ||
| 1228 | |||
| 1229 | } | ||
| 1230 | |||
| 1231 | |||
| 1232 | package Statistics::LTU::TACR; | ||
| 1233 | use Statistics::LTU; | ||
| 1234 | |||
| 1235 | @Statistics::LTU::TACR::ISA = qw( Statistics::LTU ); | ||
| 1236 | |||
| 1237 | ## | ||
| 1238 | ## TRAIN trains the specified linear threshold unit on a particular | ||
| 1239 | ## instance_vector. It returns 1 if the linear threshold unit already | ||
| 1240 | ## classified the instance_vector correctly, otherwise it returns 0. | ||
| 1241 | ## The training rule is the Thermal Absolute Correction Rule, taken from | ||
| 1242 | ## Frean's "Learning in Single Perceptrons" dissertation. | ||
| 1243 | ## | ||
| 1244 | |||
| 1245 | sub train { | ||
| 1246 | my($self, $instance_vector, $desired_value, $temp, $rate) = @_; | ||
| 1247 | |||
| 1248 | my($length) = $self->[$LENGTHi]; | ||
| 1249 | |||
| 1250 | ## | ||
| 1251 | ## Only train the linear threshold unit if it does not classify correctly. | ||
| 1252 | ## | ||
| 1253 | |||
| 1254 | my($actual_value) = $self->test($instance_vector); | ||
| 1255 | |||
| 1256 | return(1) if ((($actual_value < $Statistics::LTU::LTU_THRESHOLD) && | ||
| 1257 | ($desired_value < $Statistics::LTU::LTU_THRESHOLD)) || | ||
| 1258 | (($actual_value >= $Statistics::LTU::LTU_THRESHOLD) && | ||
| 1259 | ($desired_value >= $Statistics::LTU::LTU_THRESHOLD))); | ||
| 1260 | |||
| 1261 | ## | ||
| 1262 | ## If either temp or rate is 0, then no weight adjustment takes place. | ||
| 1263 | ## This shouldn't occur, but if it does, handle it easily and quickly. | ||
| 1264 | ## Note that not handling it allows a divide by zero later. | ||
| 1265 | ## | ||
| 1266 | |||
| 1267 | return(0) if ($temp <= 0) or ($rate <= 0); | ||
| 1268 | |||
| 1269 | ## | ||
| 1270 | ## The scale factor can only be changed when the weights are being | ||
| 1271 | ## changed, because a change to the scale factor invalidates the current | ||
| 1272 | ## set of weights. | ||
| 1273 | ## | ||
| 1274 | |||
| 1275 | $self->maintain_scaling_factors($instance_vector); | ||
| 1276 | $self->_scale_attributes($instance_vector); | ||
| 1277 | |||
| 1278 | ## | ||
| 1279 | ## Decide how much to adjust the weights by. The absolute correction rule | ||
| 1280 | ## (which provides the fastest learning) requires the dot product of the | ||
| 1281 | ## instance vector, so do that first. | ||
| 1282 | ## | ||
| 1283 | my($attribute) = $self->[$ATTRIBUTEi]; | ||
| 1284 | my($dot_product) = 0.0; | ||
| 1285 | my($i); | ||
| 1286 | |||
| 1287 | for $i (0 .. $length-1) { | ||
| 1288 | $dot_product += ($attribute->[$i] * $attribute->[$i]); | ||
| 1289 | } | ||
| 1290 | my($delta) = 1.0 + int(abs($actual_value) / $dot_product); | ||
| 1291 | |||
| 1292 | $delta = -$delta if $desired_value < $Statistics::LTU::LTU_THRESHOLD; | ||
| 1293 | |||
| 1294 | ## | ||
| 1295 | ## This is the thermal part of the rule. | ||
| 1296 | ## | ||
| 1297 | $delta *= $rate * exp((- abs($actual_value) / $temp)); | ||
| 1298 | |||
| 1299 | ## | ||
| 1300 | ## Now, adjust the weights. The last weight is handled differently, | ||
| 1301 | ## because it is always applied to the constant 1. | ||
| 1302 | ## | ||
| 1303 | |||
| 1304 | my($weight) = $self->[$WEIGHTi]; | ||
| 1305 | |||
| 1306 | for $i (0 .. $length-1) { | ||
| 1307 | $weight->[$i] += ($delta * $attribute->[$i]); | ||
| 1308 | } | ||
| 1309 | $self->update_weight_min_max; | ||
| 1310 | |||
| 1311 | 0; | ||
| 1312 | } | ||
| 1313 | |||
| 1314 | |||
| 1315 | |||
| 1316 | { package main; eval join('',<DATA>) || die $@ unless caller(); } | ||
| 1317 | |||
| 1318 | 1;##### End of LTU.pm | ||
| 1319 | __END__ |