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