| 1 | package ICC::Support::nNET2; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Carp; | ||
| 5 | |||
| 6 | our $VERSION = 0.21; | ||
| 7 | |||
| 8 | # revised 2016-05-17 | ||
| 9 | # | ||
| 10 | # Copyright © 2004-2018 by William B. Birkett | ||
| 11 | |||
| 12 | # add development directory | ||
| 13 | use lib 'lib'; | ||
| 14 | |||
| 15 | # inherit from Shared | ||
| 16 | use parent qw(ICC::Shared); | ||
| 17 | |||
| 18 | # use POSIX math | ||
| 19 | use POSIX (); | ||
| 20 | |||
| 21 | # enable static variables | ||
| 22 | use feature 'state'; | ||
| 23 | |||
| 24 | # list of functions to export | ||
| 25 | our @EXPORT = qw(rbf_g rbf_iq rbf_mq rbf_imq rbf_phs rbf_tps); | ||
| 26 | |||
| 27 | # list of valid kernel types | ||
| 28 | my @types = qw(CODE ICC::Support::geo1 ICC::Support::geo2); | ||
| 29 | |||
| 30 | # create new nNET object | ||
| 31 | # hash keys are: 'header', 'kernel', 'hidden', 'weight', 'offset', 'init' | ||
| 32 | # 'header' value is a hash reference | ||
| 33 | # 'kernel' value is an array reference of kernel objects -or- CODE references | ||
| 34 | # 'hidden' value is an array reference of CODE references | ||
| 35 | # 'weight' value is an array reference (2-D) -or- Math::Matrix object | ||
| 36 | # 'offset' value is an array reference | ||
| 37 | # 'init' value is a CODE reference | ||
| 38 | # parameters: ([ref_to_attribute_hash]) | ||
| 39 | # returns: (ref_to_object) | ||
| 40 | sub new { | ||
| 41 | |||
| 42 | # get object class | ||
| 43 | my $class = shift; | ||
| 44 | |||
| 45 | # local variable | ||
| 46 | my ($code); | ||
| 47 | |||
| 48 | # create empty nNET object | ||
| 49 | my $self = [ | ||
| 50 | {}, # object header | ||
| 51 | [], # kernel array | ||
| 52 | [], # hidden array | ||
| 53 | [], # weight matrix | ||
| 54 | [] # offset vector | ||
| 55 | ]; | ||
| 56 | |||
| 57 | # if there are parameters | ||
| 58 | if (@_) { | ||
| 59 | |||
| 60 | # if one parameter, a hash reference | ||
| 61 | if (@_ == 1 && ref($_[0]) eq 'HASH') { | ||
| 62 | |||
| 63 | # make new nNET object from attribute hash | ||
| 64 | _new_from_hash($self, shift()); | ||
| 65 | |||
| 66 | # initialize object (if CODE reference defined) | ||
| 67 | (defined($code = $self->[0]{'init'}) && &$code); | ||
| 68 | |||
| 69 | } else { | ||
| 70 | |||
| 71 | # error | ||
| 72 | croak('\'nNET\' parameter must be a hash reference'); | ||
| 73 | |||
| 74 | } | ||
| 75 | |||
| 76 | } | ||
| 77 | |||
| 78 | # bless object | ||
| 79 | bless($self, $class); | ||
| 80 | |||
| 81 | # return object reference | ||
| 82 | return($self); | ||
| 83 | |||
| 84 | } | ||
| 85 | |||
| 86 | # initialize object | ||
| 87 | # calls 'init' CODE reference, if any | ||
| 88 | # used when retrieving an nNET object using Storable | ||
| 89 | sub init { | ||
| 90 | |||
| 91 | # get object reference | ||
| 92 | my $self = shift(); | ||
| 93 | |||
| 94 | # local variable | ||
| 95 | my ($code); | ||
| 96 | |||
| 97 | # initialize object (if CODE reference defined) | ||
| 98 | (defined($code = $self->[0]{'init'}) && &$code); | ||
| 99 | |||
| 100 | } | ||
| 101 | |||
| 102 | # fit nNET object to data | ||
| 103 | # determines optimum 'weight' and 'offset' arrays | ||
| 104 | # kernel and hidden nodes are not modified by this method | ||
| 105 | # uses LAPACK dgelsd function to perform a least-squares fit | ||
| 106 | # fitting is done with or without offset, according to offset_flag | ||
| 107 | # input and output are 2D array references or Math::Matrix objects | ||
| 108 | # parameters: (ref_to_input_array, ref_to_output_array, [offset_flag]) | ||
| 109 | # returns: (dgelsd_info_value) | ||
| 110 | sub fit { | ||
| 111 | |||
| 112 | # get parameters | ||
| 113 | my ($self, $in, $out, $oflag) = @_; | ||
| 114 | |||
| 115 | # local variables | ||
| 116 | my ($info, $hidden, $ab); | ||
| 117 | |||
| 118 | # verify input array | ||
| 119 | (ref($in) eq 'ARRAY' && ref($in->[0]) eq 'ARRAY' && ! ref($in->[0][0])) || (UNIVERSAL::isa($in, 'Math::Matrix')) || croak('fit input not a 2-D array reference'); | ||
| 120 | |||
| 121 | # verify output array | ||
| 122 | (ref($out) eq 'ARRAY' && ref($out->[0]) eq 'ARRAY' && ! ref($out->[0][0])) || (UNIVERSAL::isa($out, 'Math::Matrix')) || croak('fit output not a 2-D array reference'); | ||
| 123 | |||
| 124 | # verify array dimensions | ||
| 125 | ($#{$in} == $#{$out}) || croak('fit input and output arrays have different number of rows'); | ||
| 126 | |||
| 127 | # compute hidden array | ||
| 128 | $hidden = _hidden2($self, $in); | ||
| 129 | |||
| 130 | # fit matrix and offset (hidden values to output values) | ||
| 131 | ($info, $ab) = ICC::Support::Lapack::nNET_fit($hidden, $out, defined($oflag) ? $oflag : 0); | ||
| 132 | |||
| 133 | # check result | ||
| 134 | carp('fit failed - bad parameter when calling dgelsd') if ($info < 0); | ||
| 135 | carp('fit failed - SVD algorithm failed to converge') if ($info > 0); | ||
| 136 | |||
| 137 | # initialize matrix and offset | ||
| 138 | undef($self->[3]); | ||
| 139 | undef($self->[4]); | ||
| 140 | |||
| 141 | # for each output | ||
| 142 | for my $i (0 .. $#{$out->[0]}) { | ||
| 143 | |||
| 144 | # for each input | ||
| 145 | for my $j (0 .. $#{$hidden->[0]}) { | ||
| 146 | |||
| 147 | # set matrix element (transposing) | ||
| 148 | $self->[3][$i][$j] = $ab->[$j][$i]; | ||
| 149 | |||
| 150 | } | ||
| 151 | |||
| 152 | } | ||
| 153 | |||
| 154 | # if offset flag | ||
| 155 | if ($oflag) { | ||
| 156 | |||
| 157 | # set offset | ||
| 158 | $self->[4] = [@{$ab->[$#{$self->[1]} + 1]}]; | ||
| 159 | |||
| 160 | } | ||
| 161 | |||
| 162 | # return info value | ||
| 163 | return($info); | ||
| 164 | |||
| 165 | } | ||
| 166 | |||
| 167 | # get/set reference to header hash | ||
| 168 | # parameters: ([ref_to_new_hash]) | ||
| 169 | # returns: (ref_to_hash) | ||
| 170 | sub header { | ||
| 171 | |||
| 172 | # get parameters | ||
| 173 | my ($self, $header) = @_; | ||
| 174 | |||
| 175 | # if parameter supplied | ||
| 176 | if (defined($header)) { | ||
| 177 | |||
| 178 | # verify a hash reference | ||
| 179 | (ref($header) eq 'HASH') || croak('\'nNET2\' header wrong data type'); | ||
| 180 | |||
| 181 | # copy header hash | ||
| 182 | $self->[0] = {%{$header}}; | ||
| 183 | |||
| 184 | } | ||
| 185 | |||
| 186 | # return hash | ||
| 187 | return($self->[0]); | ||
| 188 | |||
| 189 | } | ||
| 190 | |||
| 191 | # get/set kernel array reference | ||
| 192 | # parameters: ([ref_to_array]) | ||
| 193 | # returns: (ref_to_array) | ||
| 194 | sub kernel { | ||
| 195 | |||
| 196 | # get parameters | ||
| 197 | my ($self, $kernel) = @_; | ||
| 198 | |||
| 199 | # if parameter supplied | ||
| 200 | if (defined($kernel)) { | ||
| 201 | |||
| 202 | # verify an array reference | ||
| 203 | (ref($kernel) eq 'ARRAY') || croak('\'nNET2\' kernel wrong data type'); | ||
| 204 | |||
| 205 | # initialize object array | ||
| 206 | $self->[1] = []; | ||
| 207 | |||
| 208 | # for each kernel element | ||
| 209 | for my $i (0 .. $#{$kernel}) { | ||
| 210 | |||
| 211 | # if a valid kernel type | ||
| 212 | if (grep {ref($kernel->[$i]) eq $_} @types) { | ||
| 213 | |||
| 214 | # copy kernel object | ||
| 215 | $self->[1][$i] = $kernel->[$i]; | ||
| 216 | |||
| 217 | } else { | ||
| 218 | |||
| 219 | # wrong data type | ||
| 220 | croak('\'nNET\' kernel element wrong data type'); | ||
| 221 | |||
| 222 | } | ||
| 223 | |||
| 224 | } | ||
| 225 | |||
| 226 | } | ||
| 227 | |||
| 228 | # return kernel array | ||
| 229 | return($self->[1]); | ||
| 230 | |||
| 231 | } | ||
| 232 | |||
| 233 | # get/set hidden array reference | ||
| 234 | # parameters: ([ref_to_array]) | ||
| 235 | # returns: (ref_to_array) | ||
| 236 | sub hidden { | ||
| 237 | |||
| 238 | # get parameters | ||
| 239 | my ($self, $hidden) = @_; | ||
| 240 | |||
| 241 | # if parameter supplied | ||
| 242 | if (defined($hidden)) { | ||
| 243 | |||
| 244 | # verify an array reference | ||
| 245 | (ref($hidden) eq 'ARRAY') || croak('\'nNET2\' hidden wrong data type'); | ||
| 246 | |||
| 247 | # initialize object array | ||
| 248 | $self->[2] = []; | ||
| 249 | |||
| 250 | # for each hidden element | ||
| 251 | for my $i (0 .. $#{$hidden}) { | ||
| 252 | |||
| 253 | # if a CODE reference | ||
| 254 | if (ref($hidden->[$i]) eq 'CODE') { | ||
| 255 | |||
| 256 | # copy hidden element | ||
| 257 | $self->[2][$i] = $hidden->[$i]; | ||
| 258 | |||
| 259 | } else { | ||
| 260 | |||
| 261 | # wrong data type | ||
| 262 | croak('\'nNET2\' hidden element wrong data type'); | ||
| 263 | |||
| 264 | } | ||
| 265 | |||
| 266 | } | ||
| 267 | |||
| 268 | } | ||
| 269 | |||
| 270 | # return hidden array | ||
| 271 | return($self->[2]); | ||
| 272 | |||
| 273 | } | ||
| 274 | |||
| 275 | # get/set reference to matrix array | ||
| 276 | # parameters: ([ref_to_new_array]) | ||
| 277 | # returns: (ref_to_array) | ||
| 278 | sub matrix { | ||
| 279 | |||
| 280 | # get parameters | ||
| 281 | my ($self, $matrix) = @_; | ||
| 282 | |||
| 283 | # if parameter supplied | ||
| 284 | if (defined($matrix)) { | ||
| 285 | |||
| 286 | # verify a 2-D array reference or Math::Matrix object | ||
| 287 | ((ref($matrix) eq 'ARRAY' && @{$matrix} == grep {ref() eq 'ARRAY'} @{$matrix}) || UNIVERSAL::isa($matrix, 'Math::Matrix')) || croak('\'nNET2\' matrix wrong data type'); | ||
| 288 | |||
| 289 | # copy matrix | ||
| 290 | $self->[3] = Storable::dclone($matrix); | ||
| 291 | |||
| 292 | } | ||
| 293 | |||
| 294 | # return matrix | ||
| 295 | return($self->[3]); | ||
| 296 | |||
| 297 | } | ||
| 298 | |||
| 299 | # get/set reference to offset array | ||
| 300 | # parameters: ([ref_to_new_array]) | ||
| 301 | # returns: (ref_to_array) | ||
| 302 | sub offset { | ||
| 303 | |||
| 304 | # get parameters | ||
| 305 | my ($self, $offset) = @_; | ||
| 306 | |||
| 307 | # if parameter supplied | ||
| 308 | if (defined($offset)) { | ||
| 309 | |||
| 310 | # verify a reference to array of scalars | ||
| 311 | (ref($offset) eq 'ARRAY' && @{$offset} == grep {! ref()} @{$offset}) || croak('\'nNET2\' offset wrong data type'); | ||
| 312 | |||
| 313 | # copy offset | ||
| 314 | $self->[4] = [@{$offset}]; | ||
| 315 | |||
| 316 | } | ||
| 317 | |||
| 318 | # return offset | ||
| 319 | return($self->[4]); | ||
| 320 | |||
| 321 | } | ||
| 322 | |||
| 323 | # add kernel objects | ||
| 324 | # parameters: (ref_to_array_of_objects) | ||
| 325 | # returns: (ref_to_array_of_geo_indices) | ||
| 326 | sub add_kernel { | ||
| 327 | |||
| 328 | # get parameters | ||
| 329 | my ($self, $kernel) = @_; | ||
| 330 | |||
| 331 | # local variables | ||
| 332 | my (@gx); | ||
| 333 | |||
| 334 | # verify add_kernel parameter supplied | ||
| 335 | (defined($kernel)) || carp('\'nNET2\' add_kernel parameter undefined'); | ||
| 336 | |||
| 337 | # wrap if not an array reference | ||
| 338 | $kernel = [$kernel] if (ref($kernel) ne 'ARRAY'); | ||
| 339 | |||
| 340 | # for each kernel element | ||
| 341 | for my $i (0 .. $#{$kernel}) { | ||
| 342 | |||
| 343 | # if a valid kernel type | ||
| 344 | if (grep {ref($kernel->[$i]) eq $_} @types) { | ||
| 345 | |||
| 346 | # add kernel object | ||
| 347 | push(@{$self->[1]}, $kernel->[$i]); | ||
| 348 | |||
| 349 | # add geo index (kernel index + 1) | ||
| 350 | push(@gx, $#{$self->[1]} + 1); | ||
| 351 | |||
| 352 | } else { | ||
| 353 | |||
| 354 | # wrong data type | ||
| 355 | croak('\'nNET\' add_kernel element wrong data type'); | ||
| 356 | |||
| 357 | } | ||
| 358 | |||
| 359 | } | ||
| 360 | |||
| 361 | # return index array ref | ||
| 362 | return(\@gx); | ||
| 363 | |||
| 364 | } | ||
| 365 | |||
| 366 | # add hidden subroutines | ||
| 367 | # parameters: (ref_to_array_of_CODE_refs) | ||
| 368 | # returns: (ref_to_array_of_hidden_indices) | ||
| 369 | sub add_hidden { | ||
| 370 | |||
| 371 | # get parameters | ||
| 372 | my ($self, $hidden) = @_; | ||
| 373 | |||
| 374 | # local variables | ||
| 375 | my (@hx); | ||
| 376 | |||
| 377 | # verify add_hidden parameter supplied | ||
| 378 | (defined($hidden)) || carp('\'nNET2\' add_hidden parameter undefined'); | ||
| 379 | |||
| 380 | # wrap if not an array reference | ||
| 381 | $hidden = [$hidden] if (ref($hidden) ne 'ARRAY'); | ||
| 382 | |||
| 383 | # for each hidden element | ||
| 384 | for my $i (0 .. $#{$hidden}) { | ||
| 385 | |||
| 386 | # if a CODE reference | ||
| 387 | if (ref($hidden->[$i]) eq 'CODE') { | ||
| 388 | |||
| 389 | # add hidden element | ||
| 390 | push(@{$self->[2]}, $hidden->[$i]); | ||
| 391 | |||
| 392 | # add hidden index | ||
| 393 | push(@hx, $#{$self->[2]}); | ||
| 394 | |||
| 395 | } else { | ||
| 396 | |||
| 397 | # wrong data type | ||
| 398 | croak('\'nNET2\' add_hidden element wrong data type'); | ||
| 399 | |||
| 400 | } | ||
| 401 | |||
| 402 | } | ||
| 403 | |||
| 404 | # return index array ref | ||
| 405 | return(\@hx); | ||
| 406 | |||
| 407 | } | ||
| 408 | |||
| 409 | # transform data | ||
| 410 | # supported input types: | ||
| 411 | # parameters: (list, [hash]) | ||
| 412 | # parameters: (vector, [hash]) | ||
| 413 | # parameters: (matrix, [hash]) | ||
| 414 | # parameters: (Math::Matrix_object, [hash]) | ||
| 415 | # parameters: (structure, [hash]) | ||
| 416 | # returns: (same_type_as_input) | ||
| 417 | sub transform { | ||
| 418 | |||
| 419 | # set hash value (0 or 1) | ||
| 420 | my $h = ref($_[-1]) eq 'HASH' ? 1 : 0; | ||
| 421 | |||
| 422 | # if input a 'Math::Matrix' object | ||
| 423 | if (@_ == $h + 2 && UNIVERSAL::isa($_[1], 'Math::Matrix')) { | ||
| 424 | |||
| 425 | # call matrix transform | ||
| 426 | &_trans2; | ||
| 427 | |||
| 428 | # if input an array reference | ||
| 429 | } elsif (@_ == $h + 2 && ref($_[1]) eq 'ARRAY') { | ||
| 430 | |||
| 431 | # if array contains numbers (vector) | ||
| 432 | if (! ref($_[1][0]) && @{$_[1]} == grep {Scalar::Util::looks_like_number($_)} @{$_[1]}) { | ||
| 433 | |||
| 434 | # call vector transform | ||
| 435 | &_trans1; | ||
| 436 | |||
| 437 | # if array contains vectors (2-D array) | ||
| 438 | } elsif (ref($_[1][0]) eq 'ARRAY' && @{$_[1]} == grep {ref($_) eq 'ARRAY' && Scalar::Util::looks_like_number($_->[0])} @{$_[1]}) { | ||
| 439 | |||
| 440 | # call matrix transform | ||
| 441 | &_trans2; | ||
| 442 | |||
| 443 | } else { | ||
| 444 | |||
| 445 | # call structure transform | ||
| 446 | &_trans3; | ||
| 447 | |||
| 448 | } | ||
| 449 | |||
| 450 | # if input a list (of numbers) | ||
| 451 | } elsif (@_ == $h + 1 + grep {Scalar::Util::looks_like_number($_)} @_) { | ||
| 452 | |||
| 453 | # call list transform | ||
| 454 | &_trans0; | ||
| 455 | |||
| 456 | } else { | ||
| 457 | |||
| 458 | # error | ||
| 459 | croak('invalid transform input'); | ||
| 460 | |||
| 461 | } | ||
| 462 | |||
| 463 | } | ||
| 464 | |||
| 465 | # inverse transform | ||
| 466 | # note: number of undefined output values must equal number of defined input values | ||
| 467 | # note: the input and output vectors contain the final solution on return | ||
| 468 | # hash key 'init' specifies initial value vector | ||
| 469 | # parameters: (input_vector, output_vector, [hash]) | ||
| 470 | # returns: (RMS_error_value) | ||
| 471 | sub inverse { | ||
| 472 | |||
| 473 | # get parameters | ||
| 474 | my ($self, $in, $out, $hash) = @_; | ||
| 475 | |||
| 476 | # local variables | ||
| 477 | my ($i, $j, @si, @so, $init); | ||
| 478 | my ($int, $jac, $mat, $delta); | ||
| 479 | my ($max, $elim, $dlim, $accum, $error); | ||
| 480 | |||
| 481 | # initialize indices | ||
| 482 | $i = $j = -1; | ||
| 483 | |||
| 484 | # build slice arrays while validating input and output arrays | ||
| 485 | ((grep {$i++; defined() && push(@si, $i)} @{$in}) == (grep {$j++; ! defined() && push(@so, $j)} @{$out})) || croak('wrong number of undefined values'); | ||
| 486 | |||
| 487 | # get init array | ||
| 488 | $init = $hash->{'init'}; | ||
| 489 | |||
| 490 | # for each undefined output value | ||
| 491 | for my $i (@so) { | ||
| 492 | |||
| 493 | # set to supplied initial value or 0.5 | ||
| 494 | $out->[$i] = defined($init->[$i]) ? $init->[$i] : 0.5; | ||
| 495 | |||
| 496 | } | ||
| 497 | |||
| 498 | # set maximum loop count | ||
| 499 | $max = $hash->{'inv_max'} || 10; | ||
| 500 | |||
| 501 | # loop error limit | ||
| 502 | $elim = $hash->{'inv_elim'} || 1E-6; | ||
| 503 | |||
| 504 | # set delta limit | ||
| 505 | $dlim = $hash->{'inv_dlim'} || 0.5; | ||
| 506 | |||
| 507 | # create empty solution matrix | ||
| 508 | $mat = Math::Matrix->new([]); | ||
| 509 | |||
| 510 | # compute initial transform values | ||
| 511 | ($jac, $int) = jacobian($self, $out, $hash); | ||
| 512 | |||
| 513 | # solution loop | ||
| 514 | for (1 .. $max) { | ||
| 515 | |||
| 516 | # for each input | ||
| 517 | for my $i (0 .. $#si) { | ||
| 518 | |||
| 519 | # for each output | ||
| 520 | for my $j (0 .. $#so) { | ||
| 521 | |||
| 522 | # copy Jacobian value to solution matrix | ||
| 523 | $mat->[$i][$j] = $jac->[$si[$i]][$so[$j]]; | ||
| 524 | |||
| 525 | } | ||
| 526 | |||
| 527 | # save residual value to solution matrix | ||
| 528 | $mat->[$i][$#si + 1] = $in->[$si[$i]] - $int->[$si[$i]]; | ||
| 529 | |||
| 530 | } | ||
| 531 | |||
| 532 | # solve for delta values | ||
| 533 | $delta = $mat->solve; | ||
| 534 | |||
| 535 | # for each output value | ||
| 536 | for my $i (0 .. $#so) { | ||
| 537 | |||
| 538 | # add delta (limited using hyperbolic tangent) | ||
| 539 | $out->[$so[$i]] += POSIX::tanh($delta->[$i][0]/$dlim) * $dlim; | ||
| 540 | |||
| 541 | } | ||
| 542 | |||
| 543 | # compute updated transform values | ||
| 544 | ($jac, $int) = jacobian($self, $out, $hash); | ||
| 545 | |||
| 546 | # initialize error accumulator | ||
| 547 | $accum = 0; | ||
| 548 | |||
| 549 | # for each input | ||
| 550 | for my $i (0 .. $#si) { | ||
| 551 | |||
| 552 | # accumulate delta squared | ||
| 553 | $accum += ($in->[$si[$i]] - $int->[$si[$i]])**2; | ||
| 554 | |||
| 555 | } | ||
| 556 | |||
| 557 | # compute RMS error | ||
| 558 | $error = sqrt($accum/@si); | ||
| 559 | |||
| 560 | # if error less than limit | ||
| 561 | last if ($error < $elim); | ||
| 562 | |||
| 563 | } | ||
| 564 | |||
| 565 | # update input vector with final values | ||
| 566 | @{$in} = @{$int}; | ||
| 567 | |||
| 568 | # return | ||
| 569 | return($error); | ||
| 570 | |||
| 571 | } | ||
| 572 | |||
| 573 | # compute Jacobian matrix | ||
| 574 | # using numerical approximation | ||
| 575 | # parameters: (input_vector, [hash]) | ||
| 576 | # returns: (Jacobian_matrix, [output_vector]) | ||
| 577 | sub jacobian { | ||
| 578 | |||
| 579 | # get parameters | ||
| 580 | my ($self, $in, $hash) = @_; | ||
| 581 | |||
| 582 | # local variables | ||
| 583 | my ($out, $din, $dout, $delta, $jac); | ||
| 584 | |||
| 585 | # get output array | ||
| 586 | $out = _trans1($self, $in); | ||
| 587 | |||
| 588 | # set delta | ||
| 589 | $delta = 1E-9; | ||
| 590 | |||
| 591 | # for each input channel | ||
| 592 | for my $i (0 .. $#{$in}) { | ||
| 593 | |||
| 594 | # copy input array | ||
| 595 | @{$din} = @{$in}; | ||
| 596 | |||
| 597 | # add delta to input channel | ||
| 598 | $din->[$i] += $delta; | ||
| 599 | |||
| 600 | # compute new output values | ||
| 601 | $dout = transform($self, $din); | ||
| 602 | |||
| 603 | # for each output value | ||
| 604 | for my $j (0 .. $#{$out}) { | ||
| 605 | |||
| 606 | # compute Jacobian matrix element | ||
| 607 | $jac->[$j][$i] = ($dout->[$j] - $out->[$j])/$delta; | ||
| 608 | |||
| 609 | } | ||
| 610 | |||
| 611 | } | ||
| 612 | |||
| 613 | # bless Jacobian matrix | ||
| 614 | bless($jac, 'Math::Matrix'); | ||
| 615 | |||
| 616 | # if array wanted | ||
| 617 | if (wantarray) { | ||
| 618 | |||
| 619 | # return Jacobian and output array | ||
| 620 | return($jac, $out); | ||
| 621 | |||
| 622 | } else { | ||
| 623 | |||
| 624 | # return Jacobian | ||
| 625 | return($jac); | ||
| 626 | |||
| 627 | } | ||
| 628 | |||
| 629 | } | ||
| 630 | |||
| 631 | # print object contents to string | ||
| 632 | # format is an array structure | ||
| 633 | # parameter: ([format]) | ||
| 634 | # returns: (string) | ||
| 635 | sub sdump { | ||
| 636 | |||
| 637 | # get parameters | ||
| 638 | my ($self, $p) = @_; | ||
| 639 | |||
| 640 | # local variables | ||
| 641 | my ($s, $fmt); | ||
| 642 | |||
| 643 | # resolve parameter to an array reference | ||
| 644 | $p = defined($p) ? ref($p) eq 'ARRAY' ? $p : [$p] : []; | ||
| 645 | |||
| 646 | # get format string | ||
| 647 | $fmt = defined($p->[0]) && ! ref($p->[0]) ? $p->[0] : 'undef'; | ||
| 648 | |||
| 649 | # set string to object ID | ||
| 650 | $s = sprintf("'%s' object, (0x%x)\n", ref($self), $self); | ||
| 651 | |||
| 652 | # return | ||
| 653 | return($s); | ||
| 654 | |||
| 655 | } | ||
| 656 | |||
| 657 | # radial basis function - Gaussian | ||
| 658 | # ϕ(r) = e^-(εr)² | ||
| 659 | # parameters: (radius, ε) | ||
| 660 | # returns: (ϕ) | ||
| 661 | sub rbf_g { | ||
| 662 | |||
| 663 | # return function value | ||
| 664 | return(exp(-($_[0] * $_[1])**2)); | ||
| 665 | |||
| 666 | } | ||
| 667 | |||
| 668 | # radial basis function - Inverse quadratic | ||
| 669 | # ϕ(r) = 1/(1 + (εr)²) | ||
| 670 | # parameters: (radius, ε) | ||
| 671 | # returns: (ϕ) | ||
| 672 | sub rbf_iq { | ||
| 673 | |||
| 674 | # return function value | ||
| 675 | return(1/(1 + ($_[0] * $_[1])**2)); | ||
| 676 | |||
| 677 | } | ||
| 678 | |||
| 679 | # radial basis function - Multiquadric | ||
| 680 | # ϕ(r) = sqrt(1 + (εr)²) | ||
| 681 | # parameters: (radius, ε) | ||
| 682 | # returns: (ϕ) | ||
| 683 | sub rbf_mq { | ||
| 684 | |||
| 685 | # return function value | ||
| 686 | return(sqrt(1 + ($_[0] * $_[1])**2)); | ||
| 687 | |||
| 688 | } | ||
| 689 | |||
| 690 | # radial basis function - Inverse multiquadric | ||
| 691 | # ϕ(r) = 1/sqrt(1 + (εr)²) | ||
| 692 | # parameters: (radius, ε) | ||
| 693 | # returns: (ϕ) | ||
| 694 | sub rbf_imq { | ||
| 695 | |||
| 696 | # return function value | ||
| 697 | return(1/sqrt(1 + ($_[0] * $_[1])**2)); | ||
| 698 | |||
| 699 | } | ||
| 700 | |||
| 701 | # radial basis function - Polyharmonic spline | ||
| 702 | # ϕ(r) = rᵏ, k = 1, 3, 5, ... | ||
| 703 | # ϕ(r) = rᵏln(r), k = 2, 4, 6, ... | ||
| 704 | # parameters: (radius, k) | ||
| 705 | # returns: (ϕ) | ||
| 706 | sub rbf_phs { | ||
| 707 | |||
| 708 | # return function value | ||
| 709 | return($_[1] % 2 ? $_[0]**$_[1] : $_[0]**$_[1] * log($_[0])); | ||
| 710 | |||
| 711 | } | ||
| 712 | |||
| 713 | # radial basis function - Thin plate spline | ||
| 714 | # ϕ(r) = r²ln(r) | ||
| 715 | # parameters: (radius) | ||
| 716 | # returns: (ϕ) | ||
| 717 | sub rbf_tps { | ||
| 718 | |||
| 719 | # return function value | ||
| 720 | return($_[0]**2 * log($_[0])); | ||
| 721 | |||
| 722 | } | ||
| 723 | |||
| 724 | # transform list | ||
| 725 | # parameters: (object_reference, list, [hash]) | ||
| 726 | # returns: (list) | ||
| 727 | sub _trans0 { | ||
| 728 | |||
| 729 | # local variables | ||
| 730 | my ($self, $hash, @out); | ||
| 731 | |||
| 732 | # get object reference | ||
| 733 | $self = shift(); | ||
| 734 | |||
| 735 | # get optional hash | ||
| 736 | $hash = pop() if (ref($_[-1]) eq 'HASH'); | ||
| 737 | |||
| 738 | # compute output using '_trans1' | ||
| 739 | @out = @{_trans1($self, \@_, $hash)}; | ||
| 740 | |||
| 741 | # return | ||
| 742 | return(@out); | ||
| 743 | |||
| 744 | } | ||
| 745 | |||
| 746 | # transform vector | ||
| 747 | # parameters: (object_reference, vector, [hash]) | ||
| 748 | # returns: (vector) | ||
| 749 | sub _trans1 { | ||
| 750 | |||
| 751 | # get parameters | ||
| 752 | my ($self, $in, $hash) = @_; | ||
| 753 | |||
| 754 | # check if ICC::Support::Lapack module is loaded | ||
| 755 | state $lapack = defined($INC{'ICC/Support/Lapack.pm'}); | ||
| 756 | |||
| 757 | # if ICC::Support::Lapack module is loaded | ||
| 758 | if ($lapack) { | ||
| 759 | |||
| 760 | # call the BLAS dgemv function | ||
| 761 | return(ICC::Support::Lapack::matf_vec_trans(_hidden($self, $in), $self->[3], $self->[4])); | ||
| 762 | |||
| 763 | } else { | ||
| 764 | |||
| 765 | croak('method not yet implemented'); | ||
| 766 | |||
| 767 | } | ||
| 768 | |||
| 769 | } | ||
| 770 | |||
| 771 | # transform matrix (2-D array -or- Math::Matrix object) | ||
| 772 | # parameters: (object_reference, matrix, [hash]) | ||
| 773 | # returns: (matrix) | ||
| 774 | sub _trans2 { | ||
| 775 | |||
| 776 | # get parameters | ||
| 777 | my ($self, $in, $hash) = @_; | ||
| 778 | |||
| 779 | # local variables | ||
| 780 | my ($out); | ||
| 781 | |||
| 782 | # check if ICC::Support::Lapack module is loaded | ||
| 783 | state $lapack = defined($INC{'ICC/Support/Lapack.pm'}); | ||
| 784 | |||
| 785 | # if ICC::Support::Lapack module is loaded | ||
| 786 | if ($lapack) { | ||
| 787 | |||
| 788 | # call the BLAS dgemm function | ||
| 789 | $out = ICC::Support::Lapack::matf_mat_trans(_hidden2($self, $in), $self->[3], $self->[4]); | ||
| 790 | |||
| 791 | } else { | ||
| 792 | |||
| 793 | croak('method not yet implemented'); | ||
| 794 | |||
| 795 | } | ||
| 796 | |||
| 797 | # return | ||
| 798 | return(UNIVERSAL::isa($in, 'Math::Matrix') ? bless($out, 'Math::Matrix') : $out); | ||
| 799 | |||
| 800 | } | ||
| 801 | |||
| 802 | # transform structure | ||
| 803 | # parameters: (object_reference, structure, [hash]) | ||
| 804 | # returns: (structure) | ||
| 805 | sub _trans3 { | ||
| 806 | |||
| 807 | # get parameters | ||
| 808 | my ($self, $in, $hash) = @_; | ||
| 809 | |||
| 810 | # transform the array structure | ||
| 811 | _crawl($self, $in, my $out = [], $hash); | ||
| 812 | |||
| 813 | # return | ||
| 814 | return($out); | ||
| 815 | |||
| 816 | } | ||
| 817 | |||
| 818 | # recursive transform | ||
| 819 | # array structure is traversed until scalar arrays are found and transformed | ||
| 820 | # parameters: (ref_to_object, ref_to_input_array, ref_to_output_array, hash) | ||
| 821 | sub _crawl { | ||
| 822 | |||
| 823 | # get parameters | ||
| 824 | my ($self, $in, $out, $hash) = @_; | ||
| 825 | |||
| 826 | # if input is a vector (reference to a scalar array) | ||
| 827 | if (@{$in} == grep {! ref()} @{$in}) { | ||
| 828 | |||
| 829 | # transform input vector and copy to output | ||
| 830 | @{$out} = @{_trans1($self, $in, $hash)}; | ||
| 831 | |||
| 832 | } else { | ||
| 833 | |||
| 834 | # for each input element | ||
| 835 | for my $i (0 .. $#{$in}) { | ||
| 836 | |||
| 837 | # if an array reference | ||
| 838 | if (ref($in->[$i]) eq 'ARRAY') { | ||
| 839 | |||
| 840 | # transform next level | ||
| 841 | _crawl($self, $in->[$i], $out->[$i] = [], $hash); | ||
| 842 | |||
| 843 | } else { | ||
| 844 | |||
| 845 | # error | ||
| 846 | croak('invalid transform input'); | ||
| 847 | |||
| 848 | } | ||
| 849 | |||
| 850 | } | ||
| 851 | |||
| 852 | } | ||
| 853 | |||
| 854 | } | ||
| 855 | |||
| 856 | # compute hidden node output vector | ||
| 857 | # parameters: (ref_to_object, ref_to_input_vector) | ||
| 858 | # returns: (ref_to_output_vector) | ||
| 859 | sub _hidden { | ||
| 860 | |||
| 861 | # get parameters | ||
| 862 | my ($self, $in) = @_; | ||
| 863 | |||
| 864 | # local variables | ||
| 865 | my (@geo, @hidden); | ||
| 866 | |||
| 867 | # init array | ||
| 868 | @geo = ($in); | ||
| 869 | |||
| 870 | # for each kernel node | ||
| 871 | for my $node (@{$self->[1]}) { | ||
| 872 | |||
| 873 | # if a code reference | ||
| 874 | if (ref($node) eq 'CODE') { | ||
| 875 | |||
| 876 | # call subroutine | ||
| 877 | push(@geo, [&$node($in)]); | ||
| 878 | |||
| 879 | # else an object | ||
| 880 | } else { | ||
| 881 | |||
| 882 | # call transform method | ||
| 883 | push(@geo, [$node->transform($in)]); | ||
| 884 | |||
| 885 | } | ||
| 886 | |||
| 887 | } | ||
| 888 | |||
| 889 | # for each hidden node | ||
| 890 | for my $node (@{$self->[2]}) { | ||
| 891 | |||
| 892 | # add hidden value(s) | ||
| 893 | push(@hidden, &$node(\@geo)); | ||
| 894 | |||
| 895 | } | ||
| 896 | |||
| 897 | # return | ||
| 898 | return([@hidden]); | ||
| 899 | |||
| 900 | } | ||
| 901 | |||
| 902 | # compute hidden node output matrix | ||
| 903 | # parameters: (ref_to_object, ref_to_array_of_input_vectors) | ||
| 904 | # returns: (ref_to_array_of_output_vectors) | ||
| 905 | sub _hidden2 { | ||
| 906 | |||
| 907 | # get parameters | ||
| 908 | my ($self, $array) = @_; | ||
| 909 | |||
| 910 | # local variables | ||
| 911 | my (@geo, @hidden, @out); | ||
| 912 | |||
| 913 | # for each input vector | ||
| 914 | for my $in (@{$array}) { | ||
| 915 | |||
| 916 | # init array | ||
| 917 | @geo = ($in); | ||
| 918 | |||
| 919 | # for each kernel node | ||
| 920 | for my $node (@{$self->[1]}) { | ||
| 921 | |||
| 922 | # if a code reference | ||
| 923 | if (ref($node) eq 'CODE') { | ||
| 924 | |||
| 925 | # call subroutine | ||
| 926 | push(@geo, [&$node($in)]); | ||
| 927 | |||
| 928 | # else an object | ||
| 929 | } else { | ||
| 930 | |||
| 931 | # call transform method | ||
| 932 | push(@geo, [$node->transform($in)]); | ||
| 933 | |||
| 934 | } | ||
| 935 | |||
| 936 | } | ||
| 937 | |||
| 938 | # init array | ||
| 939 | @hidden = (); | ||
| 940 | |||
| 941 | # for each hidden node | ||
| 942 | for my $node (@{$self->[2]}) { | ||
| 943 | |||
| 944 | # add hidden value(s) | ||
| 945 | push(@hidden, &$node(\@geo)); | ||
| 946 | |||
| 947 | } | ||
| 948 | |||
| 949 | # add to output array | ||
| 950 | push(@out, [@hidden]); | ||
| 951 | |||
| 952 | } | ||
| 953 | |||
| 954 | # return | ||
| 955 | return([@out]); | ||
| 956 | |||
| 957 | } | ||
| 958 | |||
| 959 | # make new nNET object from attribute hash | ||
| 960 | # hash may contain pointers to header, kernel, matrix, offset or init | ||
| 961 | # hash keys are: ('header', 'kernel', 'matrix', 'offset', 'init') | ||
| 962 | # object elements not specified in the hash are unchanged | ||
| 963 | # parameters: (ref_to_object, ref_to_attribute_hash) | ||
| 964 | sub _new_from_hash { | ||
| 965 | |||
| 966 | # get parameters | ||
| 967 | my ($self, $hash) = @_; | ||
| 968 | |||
| 969 | # local variables | ||
| 970 | my ($header, $kernel, $hidden, $matrix, $offset, $init); | ||
| 971 | |||
| 972 | # get header | ||
| 973 | if ($header = $hash->{'header'}) { | ||
| 974 | |||
| 975 | # verify a hash reference | ||
| 976 | (ref($header) eq 'HASH') || croak('\'nNET\' header wrong data type'); | ||
| 977 | |||
| 978 | # copy header hash | ||
| 979 | $self->[0] = {%{$header}}; | ||
| 980 | |||
| 981 | } | ||
| 982 | |||
| 983 | # get kernel | ||
| 984 | if ($kernel = $hash->{'kernel'}) { | ||
| 985 | |||
| 986 | # verify an array reference | ||
| 987 | (ref($kernel) eq 'ARRAY') || croak('\'nNET\' kernel wrong data type'); | ||
| 988 | |||
| 989 | # for each kernel element | ||
| 990 | for my $i (0 .. $#{$kernel}) { | ||
| 991 | |||
| 992 | # if a valid kernel type | ||
| 993 | if (grep {ref($kernel->[$i]) eq $_} @types) { | ||
| 994 | |||
| 995 | # copy kernel object | ||
| 996 | $self->[1][$i] = $kernel->[$i]; | ||
| 997 | |||
| 998 | } else { | ||
| 999 | |||
| 1000 | # wrong data type | ||
| 1001 | croak('\'nNET\' kernel element wrong data type'); | ||
| 1002 | |||
| 1003 | } | ||
| 1004 | |||
| 1005 | } | ||
| 1006 | |||
| 1007 | } | ||
| 1008 | |||
| 1009 | # get hidden | ||
| 1010 | if ($hidden = $hash->{'hidden'}) { | ||
| 1011 | |||
| 1012 | # verify an array reference | ||
| 1013 | (ref($hidden) eq 'ARRAY') || croak('\'nNET\' hidden wrong data type'); | ||
| 1014 | |||
| 1015 | # for each hidden element | ||
| 1016 | for my $i (0 .. $#{$hidden}) { | ||
| 1017 | |||
| 1018 | # if a CODE reference | ||
| 1019 | if (ref($hidden->[$i]) eq 'CODE') { | ||
| 1020 | |||
| 1021 | # copy hidden element | ||
| 1022 | $self->[2][$i] = $hidden->[$i]; | ||
| 1023 | |||
| 1024 | } else { | ||
| 1025 | |||
| 1026 | # wrong data type | ||
| 1027 | croak('\'nNET\' hidden element wrong data type'); | ||
| 1028 | |||
| 1029 | } | ||
| 1030 | |||
| 1031 | } | ||
| 1032 | |||
| 1033 | } | ||
| 1034 | |||
| 1035 | # get matrix | ||
| 1036 | if ($matrix = $hash->{'matrix'}) { | ||
| 1037 | |||
| 1038 | # verify a 2-D array reference or Math::Matrix object | ||
| 1039 | ((ref($matrix) eq 'ARRAY' && @{$matrix} == grep {ref() eq 'ARRAY'} @{$matrix}) || UNIVERSAL::isa($matrix, 'Math::Matrix')) || croak('\'nNET\' matrix wrong data type'); | ||
| 1040 | |||
| 1041 | # copy matrix | ||
| 1042 | $self->[3] = Storable::dclone($matrix); | ||
| 1043 | |||
| 1044 | } | ||
| 1045 | |||
| 1046 | # get offset | ||
| 1047 | if ($offset = $hash->{'offset'}) { | ||
| 1048 | |||
| 1049 | # verify a reference to array of scalars | ||
| 1050 | (ref($offset) eq 'ARRAY' && @{$offset} == grep {! ref()} @{$offset}) || croak('\'nNET\' offset wrong data type'); | ||
| 1051 | |||
| 1052 | # copy offset | ||
| 1053 | $self->[4] = [@{$offset}]; | ||
| 1054 | |||
| 1055 | } | ||
| 1056 | |||
| 1057 | # get init | ||
| 1058 | if ($init = $hash->{'init'}) { | ||
| 1059 | |||
| 1060 | # verify a CODE reference | ||
| 1061 | (ref($init) eq 'CODE') || croak('\'nNET\' init wrong data type'); | ||
| 1062 | |||
| 1063 | # set init | ||
| 1064 | $self->[0]{'init'} = $init; | ||
| 1065 | |||
| 1066 | } | ||
| 1067 | |||
| 1068 | } | ||
| 1069 | |||
| 1070 | 1; |