| 1 |
#ifndef FANN_CPP_H_INCLUDED |
| 2 |
#define FANN_CPP_H_INCLUDED |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
#include <iostream> |
| 91 |
#include <stdarg.h> |
| 92 |
#include <string> |
| 93 |
#include <iostream> |
| 94 |
#include <fstream> |
| 95 |
#include <vector> |
| 96 |
#include <map> |
| 97 |
#include <cassert> |
| 98 |
#include "base/Vector2d.h" |
| 99 |
#include "config_jiplib.h" |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
namespace FANN |
| 104 |
{ |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
enum error_function_enum { |
| 118 |
ERRORFUNC_LINEAR = FANN_ERRORFUNC_LINEAR, |
| 119 |
ERRORFUNC_TANH |
| 120 |
}; |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
enum stop_function_enum |
| 136 |
{ |
| 137 |
STOPFUNC_MSE = FANN_STOPFUNC_MSE, |
| 138 |
STOPFUNC_BIT |
| 139 |
}; |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
enum training_algorithm_enum { |
| 174 |
TRAIN_INCREMENTAL = FANN_TRAIN_INCREMENTAL, |
| 175 |
TRAIN_BATCH, |
| 176 |
TRAIN_RPROP, |
| 177 |
TRAIN_QUICKPROP |
| 178 |
}; |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
enum activation_function_enum { |
| 273 |
LINEAR = FANN_LINEAR, |
| 274 |
THRESHOLD, |
| 275 |
THRESHOLD_SYMMETRIC, |
| 276 |
SIGMOID, |
| 277 |
SIGMOID_STEPWISE, |
| 278 |
SIGMOID_SYMMETRIC, |
| 279 |
SIGMOID_SYMMETRIC_STEPWISE, |
| 280 |
GAUSSIAN, |
| 281 |
GAUSSIAN_SYMMETRIC, |
| 282 |
GAUSSIAN_STEPWISE, |
| 283 |
ELLIOT, |
| 284 |
ELLIOT_SYMMETRIC, |
| 285 |
LINEAR_PIECE, |
| 286 |
LINEAR_PIECE_SYMMETRIC, |
| 287 |
SIN_SYMMETRIC, |
| 288 |
COS_SYMMETRIC |
| 289 |
}; |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
enum network_type_enum |
| 304 |
{ |
| 305 |
LAYER = FANN_NETTYPE_LAYER, |
| 306 |
SHORTCUT |
| 307 |
}; |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
typedef struct fann_connection connection; |
| 323 |
|
| 324 |
|
| 325 |
class neural_net; |
| 326 |
class training_data; |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
typedef int (*callback_type) (neural_net &net, training_data &train, |
| 359 |
unsigned int max_epochs, unsigned int epochs_between_reports, |
| 360 |
float desired_error, unsigned int epochs, void *user_data); |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
class training_data |
| 370 |
{ |
| 371 |
public: |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
training_data() : train_data(NULL) |
| 378 |
{ |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
training_data(const training_data &data) |
| 387 |
{ |
| 388 |
destroy_train(); |
| 389 |
if (data.train_data != NULL) |
| 390 |
{ |
| 391 |
train_data = fann_duplicate_train_data(data.train_data); |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
#ifdef USE_VIRTUAL_DESTRUCTOR |
| 404 |
virtual |
| 405 |
#endif |
| 406 |
~training_data() |
| 407 |
{ |
| 408 |
destroy_train(); |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
void destroy_train() |
| 419 |
{ |
| 420 |
if (train_data != NULL) |
| 421 |
{ |
| 422 |
fann_destroy_train(train_data); |
| 423 |
train_data = NULL; |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
bool read_train_from_file(const std::string &filename) |
| 448 |
{ |
| 449 |
destroy_train(); |
| 450 |
train_data = fann_read_train_from_file(filename.c_str()); |
| 451 |
return (train_data != NULL); |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
bool save_train(const std::string &filename) |
| 467 |
{ |
| 468 |
if (train_data == NULL) |
| 469 |
{ |
| 470 |
return false; |
| 471 |
} |
| 472 |
if (fann_save_train(train_data, filename.c_str()) == -1) |
| 473 |
{ |
| 474 |
return false; |
| 475 |
} |
| 476 |
return true; |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
bool save_train_to_fixed(const std::string &filename, unsigned int decimal_point) |
| 494 |
{ |
| 495 |
if (train_data == NULL) |
| 496 |
{ |
| 497 |
return false; |
| 498 |
} |
| 499 |
if (fann_save_train_to_fixed(train_data, filename.c_str(), decimal_point) == -1) |
| 500 |
{ |
| 501 |
return false; |
| 502 |
} |
| 503 |
return true; |
| 504 |
} |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
void shuffle_train_data() |
| 514 |
{ |
| 515 |
if (train_data != NULL) |
| 516 |
{ |
| 517 |
fann_shuffle_train_data(train_data); |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
void merge_train_data(const training_data &data) |
| 528 |
{ |
| 529 |
fann_train_data *new_data = fann_merge_train_data(train_data, data.train_data); |
| 530 |
if (new_data != NULL) |
| 531 |
{ |
| 532 |
destroy_train(); |
| 533 |
train_data = new_data; |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
unsigned int length_train_data() |
| 547 |
{ |
| 548 |
if (train_data == NULL) |
| 549 |
{ |
| 550 |
return 0; |
| 551 |
} |
| 552 |
else |
| 553 |
{ |
| 554 |
return fann_length_train_data(train_data); |
| 555 |
} |
| 556 |
} |
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
unsigned int num_input_train_data() |
| 568 |
{ |
| 569 |
if (train_data == NULL) |
| 570 |
{ |
| 571 |
return 0; |
| 572 |
} |
| 573 |
else |
| 574 |
{ |
| 575 |
return fann_num_input_train_data(train_data); |
| 576 |
} |
| 577 |
} |
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
unsigned int num_output_train_data() |
| 589 |
{ |
| 590 |
if (train_data == NULL) |
| 591 |
{ |
| 592 |
return 0; |
| 593 |
} |
| 594 |
else |
| 595 |
{ |
| 596 |
return fann_num_output_train_data(train_data); |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
fann_type **get_input() |
| 613 |
{ |
| 614 |
if (train_data == NULL) |
| 615 |
{ |
| 616 |
return NULL; |
| 617 |
} |
| 618 |
else |
| 619 |
{ |
| 620 |
return train_data->input; |
| 621 |
} |
| 622 |
} |
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
fann_type **get_output() |
| 633 |
{ |
| 634 |
if (train_data == NULL) |
| 635 |
{ |
| 636 |
return NULL; |
| 637 |
} |
| 638 |
else |
| 639 |
{ |
| 640 |
return train_data->output; |
| 641 |
} |
| 642 |
} |
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
void set_train_data(unsigned int num_data, |
| 663 |
unsigned int num_input, fann_type **input, |
| 664 |
unsigned int num_output, fann_type **output) |
| 665 |
{ |
| 666 |
|
| 667 |
struct fann_train_data *data = |
| 668 |
(struct fann_train_data *)malloc(sizeof(struct fann_train_data)); |
| 669 |
data->input = (fann_type **)calloc(num_data, sizeof(fann_type *)); |
| 670 |
data->output = (fann_type **)calloc(num_data, sizeof(fann_type *)); |
| 671 |
|
| 672 |
data->num_data = num_data; |
| 673 |
data->num_input = num_input; |
| 674 |
data->num_output = num_output; |
| 675 |
|
| 676 |
fann_type *data_input = (fann_type *)calloc(num_input*num_data, sizeof(fann_type)); |
| 677 |
fann_type *data_output = (fann_type *)calloc(num_output*num_data, sizeof(fann_type)); |
| 678 |
|
| 679 |
for (unsigned int i = 0; i < num_data; ++i) |
| 680 |
{ |
| 681 |
data->input[i] = data_input; |
| 682 |
data_input += num_input; |
| 683 |
for (unsigned int j = 0; j < num_input; ++j) |
| 684 |
{ |
| 685 |
data->input[i][j] = input[i][j]; |
| 686 |
} |
| 687 |
data->output[i] = data_output; |
| 688 |
data_output += num_output; |
| 689 |
for (unsigned int j = 0; j < num_output; ++j) |
| 690 |
{ |
| 691 |
data->output[i][j] = output[i][j]; |
| 692 |
} |
| 693 |
} |
| 694 |
set_train_data(data); |
| 695 |
} |
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
void set_train_data(const std::vector< std::vector<fann_type> >& input, |
| 700 |
const std::vector< std::vector<fann_type> >& output) |
| 701 |
{ |
| 702 |
unsigned int num_data=input.size(); |
| 703 |
assert(num_data); |
| 704 |
assert(input.size()==output.size()); |
| 705 |
unsigned int num_input=input[0].size(); |
| 706 |
unsigned int num_output=output[0].size(); |
| 707 |
|
| 708 |
|
| 709 |
struct fann_train_data *data = |
| 710 |
(struct fann_train_data *)malloc(sizeof(struct fann_train_data)); |
| 711 |
data->input = (fann_type **)calloc(num_data, sizeof(fann_type *)); |
| 712 |
data->output = (fann_type **)calloc(num_data, sizeof(fann_type *)); |
| 713 |
|
| 714 |
data->num_data = num_data; |
| 715 |
data->num_input = num_input; |
| 716 |
data->num_output = num_output; |
| 717 |
|
| 718 |
fann_type *data_input = (fann_type *)calloc(num_input*num_data, sizeof(fann_type)); |
| 719 |
fann_type *data_output = (fann_type *)calloc(num_output*num_data, sizeof(fann_type)); |
| 720 |
|
| 721 |
for (unsigned int i = 0; i < num_data; ++i) |
| 722 |
{ |
| 723 |
data->input[i] = data_input; |
| 724 |
data_input += num_input; |
| 725 |
for (unsigned int j = 0; j < num_input; ++j) |
| 726 |
{ |
| 727 |
data->input[i][j] = input[i][j]; |
| 728 |
} |
| 729 |
data->output[i] = data_output; |
| 730 |
data_output += num_output; |
| 731 |
for (unsigned int j = 0; j < num_output; ++j) |
| 732 |
{ |
| 733 |
data->output[i][j] = output[i][j]; |
| 734 |
} |
| 735 |
} |
| 736 |
set_train_data(data); |
| 737 |
} |
| 738 |
|
| 739 |
void set_train_data(const std::vector< std::vector< std::vector<fann_type> > >& input, unsigned int num_data) |
| 740 |
{ |
| 741 |
assert(num_data); |
| 742 |
assert(input.size()); |
| 743 |
unsigned int num_class=input.size(); |
| 744 |
assert(input[0].size()); |
| 745 |
unsigned int num_input=input[0][0].size(); |
| 746 |
unsigned int num_output=num_class; |
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
struct fann_train_data *data = |
| 752 |
(struct fann_train_data *)malloc(sizeof(struct fann_train_data)); |
| 753 |
data->input = (fann_type **)calloc(num_data, sizeof(fann_type *)); |
| 754 |
data->output = (fann_type **)calloc(num_data, sizeof(fann_type *)); |
| 755 |
|
| 756 |
data->num_data = num_data; |
| 757 |
data->num_input = num_input; |
| 758 |
data->num_output = num_output; |
| 759 |
|
| 760 |
fann_type *data_input = (fann_type *)calloc(num_input*num_data, sizeof(fann_type)); |
| 761 |
fann_type *data_output = (fann_type *)calloc(num_output*num_data, sizeof(fann_type)); |
| 762 |
|
| 763 |
unsigned int isample=0; |
| 764 |
for(int iclass=0;iclass<num_class;++iclass){ |
| 765 |
for(int csample=0;csample<input[iclass].size();++csample){ |
| 766 |
data->input[isample] = data_input; |
| 767 |
data_input += num_input; |
| 768 |
for(int iband=0;iband<input[iclass][csample].size();++iband){ |
| 769 |
assert(input[iclass][csample].size()==num_input); |
| 770 |
data->input[isample][iband] = input[iclass][csample][iband]; |
| 771 |
} |
| 772 |
data->output[isample] = data_output; |
| 773 |
data_output += num_output; |
| 774 |
for(int ic=0;ic<num_output;++ic){ |
| 775 |
|
| 776 |
|
| 777 |
if(ic==iclass) |
| 778 |
data->output[isample][ic] = 1; |
| 779 |
else |
| 780 |
data->output[isample][ic] = -1; |
| 781 |
} |
| 782 |
++isample; |
| 783 |
} |
| 784 |
} |
| 785 |
set_train_data(data); |
| 786 |
} |
| 787 |
|
| 788 |
void set_train_data(const std::vector< Vector2d<fann_type> >& input, unsigned int num_data) |
| 789 |
{ |
| 790 |
assert(num_data); |
| 791 |
assert(input.size()); |
| 792 |
unsigned int num_class=input.size(); |
| 793 |
assert(input[0].size()); |
| 794 |
unsigned int num_input=input[0][0].size(); |
| 795 |
unsigned int num_output=num_class; |
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
struct fann_train_data *data = |
| 801 |
(struct fann_train_data *)malloc(sizeof(struct fann_train_data)); |
| 802 |
data->input = (fann_type **)calloc(num_data, sizeof(fann_type *)); |
| 803 |
data->output = (fann_type **)calloc(num_data, sizeof(fann_type *)); |
| 804 |
|
| 805 |
data->num_data = num_data; |
| 806 |
data->num_input = num_input; |
| 807 |
data->num_output = num_output; |
| 808 |
|
| 809 |
fann_type *data_input = (fann_type *)calloc(num_input*num_data, sizeof(fann_type)); |
| 810 |
fann_type *data_output = (fann_type *)calloc(num_output*num_data, sizeof(fann_type)); |
| 811 |
|
| 812 |
unsigned int isample=0; |
| 813 |
for(int iclass=0;iclass<num_class;++iclass){ |
| 814 |
for(int csample=0;csample<input[iclass].size();++csample){ |
| 815 |
data->input[isample] = data_input; |
| 816 |
data_input += num_input; |
| 817 |
for(int iband=0;iband<input[iclass][csample].size();++iband){ |
| 818 |
assert(input[iclass][csample].size()==num_input); |
| 819 |
data->input[isample][iband] = input[iclass][csample][iband]; |
| 820 |
} |
| 821 |
data->output[isample] = data_output; |
| 822 |
data_output += num_output; |
| 823 |
for(int ic=0;ic<num_output;++ic){ |
| 824 |
|
| 825 |
|
| 826 |
if(ic==iclass) |
| 827 |
data->output[isample][ic] = 1; |
| 828 |
else |
| 829 |
data->output[isample][ic] = -1; |
| 830 |
} |
| 831 |
++isample; |
| 832 |
} |
| 833 |
} |
| 834 |
set_train_data(data); |
| 835 |
} |
| 836 |
|
| 837 |
|
| 838 |
private: |
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
void set_train_data(struct fann_train_data *data) |
| 843 |
{ |
| 844 |
destroy_train(); |
| 845 |
train_data = data; |
| 846 |
} |
| 847 |
|
| 848 |
public: |
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
void create_train_from_callback(unsigned int num_data, |
| 877 |
unsigned int num_input, |
| 878 |
unsigned int num_output, |
| 879 |
void (FANN_API *user_function)( unsigned int, |
| 880 |
unsigned int, |
| 881 |
unsigned int, |
| 882 |
fann_type * , |
| 883 |
fann_type * )) |
| 884 |
{ |
| 885 |
destroy_train(); |
| 886 |
train_data = fann_create_train_from_callback(num_data, num_input, num_output, user_function); |
| 887 |
} |
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
void scale_input_train_data(fann_type new_min, fann_type new_max) |
| 899 |
{ |
| 900 |
if (train_data != NULL) |
| 901 |
{ |
| 902 |
fann_scale_input_train_data(train_data, new_min, new_max); |
| 903 |
} |
| 904 |
} |
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
void scale_output_train_data(fann_type new_min, fann_type new_max) |
| 916 |
{ |
| 917 |
if (train_data != NULL) |
| 918 |
{ |
| 919 |
fann_scale_output_train_data(train_data, new_min, new_max); |
| 920 |
} |
| 921 |
} |
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
void scale_train_data(fann_type new_min, fann_type new_max) |
| 933 |
{ |
| 934 |
if (train_data != NULL) |
| 935 |
{ |
| 936 |
fann_scale_train_data(train_data, new_min, new_max); |
| 937 |
} |
| 938 |
} |
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
void subset_train_data(unsigned int pos, unsigned int length) |
| 959 |
{ |
| 960 |
if (train_data != NULL) |
| 961 |
{ |
| 962 |
struct fann_train_data *temp = fann_subset_train_data(train_data, pos, length); |
| 963 |
destroy_train(); |
| 964 |
train_data = temp; |
| 965 |
} |
| 966 |
} |
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
protected: |
| 971 |
|
| 972 |
friend class neural_net; |
| 973 |
|
| 974 |
|
| 975 |
struct fann_train_data* train_data; |
| 976 |
}; |
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
class neural_net |
| 986 |
{ |
| 987 |
public: |
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
neural_net() : ann(NULL) |
| 998 |
{ |
| 999 |
} |
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
#ifdef USE_VIRTUAL_DESTRUCTOR |
| 1010 |
virtual |
| 1011 |
#endif |
| 1012 |
~neural_net() |
| 1013 |
{ |
| 1014 |
destroy(); |
| 1015 |
} |
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
void destroy() |
| 1025 |
{ |
| 1026 |
if (ann != NULL) |
| 1027 |
{ |
| 1028 |
user_context *user_data = static_cast<user_context *>(fann_get_user_data(ann)); |
| 1029 |
if (user_data != NULL) |
| 1030 |
delete user_data; |
| 1031 |
|
| 1032 |
fann_destroy(ann); |
| 1033 |
ann = NULL; |
| 1034 |
} |
| 1035 |
} |
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
bool create_standard(unsigned int num_layers, ...) |
| 1069 |
{ |
| 1070 |
va_list layers; |
| 1071 |
std::vector<unsigned int> arr(num_layers); |
| 1072 |
|
| 1073 |
|
| 1074 |
va_start(layers, num_layers); |
| 1075 |
|
| 1076 |
|
| 1077 |
for (unsigned int ii = 0; ii < num_layers; ii++) |
| 1078 |
arr[ii] = va_arg(layers, unsigned int); |
| 1079 |
bool status = create_standard_array(num_layers, &(arr[0])); |
| 1080 |
|
| 1081 |
va_end(layers); |
| 1082 |
return status; |
| 1083 |
} |
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
bool create_standard_array(unsigned int num_layers, const unsigned int * layers) |
| 1097 |
{ |
| 1098 |
destroy(); |
| 1099 |
ann = fann_create_standard_array(num_layers, layers); |
| 1100 |
return (ann != NULL); |
| 1101 |
} |
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
bool create_sparse(float connection_rate, unsigned int num_layers, ...) |
| 1126 |
{ |
| 1127 |
va_list layers; |
| 1128 |
std::vector<unsigned int> arr(num_layers); |
| 1129 |
|
| 1130 |
|
| 1131 |
va_start(layers, num_layers); |
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
for (unsigned int ii = 0; ii < num_layers; ii++) |
| 1136 |
arr[ii] = va_arg(layers, unsigned int); |
| 1137 |
bool status = create_sparse_array(connection_rate, num_layers, &(arr[0])); |
| 1138 |
|
| 1139 |
|
| 1140 |
va_end(layers); |
| 1141 |
return status; |
| 1142 |
} |
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
bool create_sparse_array(float connection_rate, |
| 1157 |
unsigned int num_layers, const unsigned int * layers) |
| 1158 |
{ |
| 1159 |
destroy(); |
| 1160 |
ann = fann_create_sparse_array(connection_rate, num_layers, layers); |
| 1161 |
return (ann != NULL); |
| 1162 |
} |
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
bool create_shortcut(unsigned int num_layers, ...) |
| 1182 |
{ |
| 1183 |
va_list layers; |
| 1184 |
std::vector<unsigned int> arr(num_layers); |
| 1185 |
|
| 1186 |
|
| 1187 |
va_start(layers, num_layers); |
| 1188 |
|
| 1189 |
|
| 1190 |
for (unsigned int ii = 0; ii < num_layers; ii++) |
| 1191 |
arr[ii] = va_arg(layers, unsigned int); |
| 1192 |
bool status = create_shortcut_array(num_layers, &(arr[0])); |
| 1193 |
|
| 1194 |
|
| 1195 |
va_end(layers); |
| 1196 |
return status; |
| 1197 |
} |
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
bool create_shortcut_array(unsigned int num_layers, |
| 1213 |
const unsigned int * layers) |
| 1214 |
{ |
| 1215 |
destroy(); |
| 1216 |
ann = fann_create_shortcut_array(num_layers, layers); |
| 1217 |
return (ann != NULL); |
| 1218 |
} |
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
fann_type* run(fann_type *input) |
| 1231 |
{ |
| 1232 |
if (ann == NULL) |
| 1233 |
{ |
| 1234 |
return NULL; |
| 1235 |
} |
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
return fann_run(ann, input); |
| 1240 |
} |
| 1241 |
|
| 1242 |
std::vector<fann_type> run(std::vector<fann_type> input) |
| 1243 |
{ |
| 1244 |
std::vector<fann_type> vresult; |
| 1245 |
if (ann == NULL) |
| 1246 |
{ |
| 1247 |
return vresult; |
| 1248 |
} |
| 1249 |
vresult.resize(get_num_output()); |
| 1250 |
fann_type* result; |
| 1251 |
result=fann_run(ann,&(input[0])); |
| 1252 |
for(int iout=0;iout<get_num_output();++iout) |
| 1253 |
vresult[iout]=*(result++); |
| 1254 |
return vresult; |
| 1255 |
} |
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
void randomize_weights(fann_type min_weight, fann_type max_weight) |
| 1269 |
{ |
| 1270 |
if (ann != NULL) |
| 1271 |
{ |
| 1272 |
fann_randomize_weights(ann, min_weight, max_weight); |
| 1273 |
} |
| 1274 |
} |
| 1275 |
|
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
void init_weights(const training_data &data) |
| 1296 |
{ |
| 1297 |
if ((ann != NULL) && (data.train_data != NULL)) |
| 1298 |
{ |
| 1299 |
fann_init_weights(ann, data.train_data); |
| 1300 |
} |
| 1301 |
} |
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
void print_connections() |
| 1330 |
{ |
| 1331 |
if (ann != NULL) |
| 1332 |
{ |
| 1333 |
fann_print_connections(ann); |
| 1334 |
} |
| 1335 |
} |
| 1336 |
|
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
|
| 1347 |
bool create_from_file(const std::string &configuration_file) |
| 1348 |
{ |
| 1349 |
destroy(); |
| 1350 |
ann = fann_create_from_file(configuration_file.c_str()); |
| 1351 |
return (ann != NULL); |
| 1352 |
} |
| 1353 |
|
| 1354 |
bool create_from_file(const std::string &configuration_file, std::vector<double> &offset, std::vector<double> &scale, std::map<std::string,short> &classValueMap, std::vector<std::string> &nameVector, std::vector<std::string> &bandNames){ |
| 1355 |
offset.clear(); |
| 1356 |
scale.clear(); |
| 1357 |
classValueMap.clear(); |
| 1358 |
nameVector.clear(); |
| 1359 |
bandNames.clear(); |
| 1360 |
create_from_file(configuration_file.c_str()); |
| 1361 |
short verbose=0; |
| 1362 |
std::ifstream m_ifstream; |
| 1363 |
m_ifstream.open(configuration_file.c_str(),std::ios_base::in); |
| 1364 |
if(!(m_ifstream)){ |
| 1365 |
std::ostringstream errorStream; |
| 1366 |
errorStream << "Error: could not open file" << configuration_file; |
| 1367 |
throw(errorStream.str()); |
| 1368 |
} |
| 1369 |
std::istringstream lineStream; |
| 1370 |
std::istringstream csvstream; |
| 1371 |
int ncol=0; |
| 1372 |
char m_comment='#'; |
| 1373 |
char m_equal='='; |
| 1374 |
char m_fs=','; |
| 1375 |
bool isComment=false; |
| 1376 |
bool isEqual=false; |
| 1377 |
std::string csvRecord; |
| 1378 |
std::string item; |
| 1379 |
size_t pos=std::string::npos; |
| 1380 |
size_t posEqual=std::string::npos; |
| 1381 |
size_t posComment=std::string::npos; |
| 1382 |
|
| 1383 |
bool offsetDone=false; |
| 1384 |
bool scaleDone=false; |
| 1385 |
bool classValueMapDone=false; |
| 1386 |
bool nameVectorDone=false; |
| 1387 |
bool bandNamesDone=false; |
| 1388 |
while(getline(m_ifstream,csvRecord)){ |
| 1389 |
if(!offsetDone){ |
| 1390 |
|
| 1391 |
if(csvRecord.find("offset")==std::string::npos) |
| 1392 |
continue; |
| 1393 |
if(verbose>1) |
| 1394 |
std::cout << csvRecord << std::endl; |
| 1395 |
lineStream.clear(); |
| 1396 |
lineStream.str(csvRecord); |
| 1397 |
isEqual=false; |
| 1398 |
csvstream.clear(); |
| 1399 |
csvstream.str(csvRecord); |
| 1400 |
ncol=0; |
| 1401 |
while(getline(csvstream,item,m_fs)){ |
| 1402 |
if(verbose) |
| 1403 |
std::cout << item << " "; |
| 1404 |
pos=item.find(m_comment); |
| 1405 |
if(pos!=std::string::npos){ |
| 1406 |
isComment=true; |
| 1407 |
if(pos>0) |
| 1408 |
item=item.substr(0,pos-1); |
| 1409 |
else |
| 1410 |
break; |
| 1411 |
if(verbose) |
| 1412 |
std::cout << "comment found, string is " << item << std::endl; |
| 1413 |
} |
| 1414 |
++ncol; |
| 1415 |
if(isComment) |
| 1416 |
break; |
| 1417 |
} |
| 1418 |
|
| 1419 |
lineStream.clear(); |
| 1420 |
lineStream.str(csvRecord); |
| 1421 |
isComment=false; |
| 1422 |
csvstream.clear(); |
| 1423 |
csvstream.str(csvRecord); |
| 1424 |
while(getline(csvstream,item,m_fs)){ |
| 1425 |
if(verbose) |
| 1426 |
std::cout << item << " "; |
| 1427 |
posEqual=item.find(m_equal); |
| 1428 |
posComment=item.find(m_comment); |
| 1429 |
if(!isEqual){ |
| 1430 |
if(posEqual!=std::string::npos){ |
| 1431 |
isEqual=true; |
| 1432 |
if(posEqual>0) |
| 1433 |
item=item.substr(posEqual+1); |
| 1434 |
else |
| 1435 |
break; |
| 1436 |
if(verbose) |
| 1437 |
std::cout << "equal found, string is " << item << std::endl; |
| 1438 |
} |
| 1439 |
else |
| 1440 |
continue; |
| 1441 |
} |
| 1442 |
if(posComment!=std::string::npos){ |
| 1443 |
isComment=true; |
| 1444 |
if(posComment>0) |
| 1445 |
item=item.substr(0,posComment-1); |
| 1446 |
else |
| 1447 |
break; |
| 1448 |
if(verbose) |
| 1449 |
std::cout << "comment found, string is " << item << std::endl; |
| 1450 |
} |
| 1451 |
|
| 1452 |
pos=item.find(','); |
| 1453 |
if(pos!=std::string::npos) |
| 1454 |
item=item.substr(0,pos-1); |
| 1455 |
float value=string2type<float>(item); |
| 1456 |
|
| 1457 |
offset.push_back(value); |
| 1458 |
} |
| 1459 |
if(offset.size()!=ncol){ |
| 1460 |
std::string errorString="Error: reading offset"; |
| 1461 |
throw(errorString); |
| 1462 |
} |
| 1463 |
if(verbose){ |
| 1464 |
std::cout << "offset:" << std::endl; |
| 1465 |
for(int index=0;index<offset.size();++index){ |
| 1466 |
if(index<offset.size()-1) |
| 1467 |
std::cout << offset[index] << ", "; |
| 1468 |
else |
| 1469 |
std::cout << offset[index] << std::endl; |
| 1470 |
} |
| 1471 |
} |
| 1472 |
offsetDone=true; |
| 1473 |
continue; |
| 1474 |
} |
| 1475 |
if(!scaleDone){ |
| 1476 |
|
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
if(verbose>1) |
| 1481 |
std::cout << csvRecord << std::endl; |
| 1482 |
lineStream.clear(); |
| 1483 |
lineStream.str(csvRecord); |
| 1484 |
isEqual=false; |
| 1485 |
csvstream.clear(); |
| 1486 |
csvstream.str(csvRecord); |
| 1487 |
ncol=0; |
| 1488 |
while(getline(csvstream,item,m_fs)){ |
| 1489 |
if(verbose) |
| 1490 |
std::cout << item << " "; |
| 1491 |
pos=item.find(m_comment); |
| 1492 |
if(pos!=std::string::npos){ |
| 1493 |
isComment=true; |
| 1494 |
if(pos>0) |
| 1495 |
item=item.substr(0,pos-1); |
| 1496 |
else |
| 1497 |
break; |
| 1498 |
if(verbose) |
| 1499 |
std::cout << "comment found, string is " << item << std::endl; |
| 1500 |
} |
| 1501 |
++ncol; |
| 1502 |
if(isComment) |
| 1503 |
break; |
| 1504 |
} |
| 1505 |
|
| 1506 |
lineStream.clear(); |
| 1507 |
lineStream.str(csvRecord); |
| 1508 |
isComment=false; |
| 1509 |
while(getline(lineStream,item,m_fs)){ |
| 1510 |
if(verbose) |
| 1511 |
std::cout << item << " "; |
| 1512 |
posEqual=item.find(m_equal); |
| 1513 |
posComment=item.find(m_comment); |
| 1514 |
if(!isEqual){ |
| 1515 |
if(posEqual!=std::string::npos){ |
| 1516 |
isEqual=true; |
| 1517 |
if(posEqual>0) |
| 1518 |
item=item.substr(posEqual+1); |
| 1519 |
else |
| 1520 |
break; |
| 1521 |
if(verbose) |
| 1522 |
std::cout << "equal found, string is " << item << std::endl; |
| 1523 |
} |
| 1524 |
else |
| 1525 |
continue; |
| 1526 |
} |
| 1527 |
if(posComment!=std::string::npos){ |
| 1528 |
isComment=true; |
| 1529 |
if(posComment>0) |
| 1530 |
item=item.substr(0,posComment-1); |
| 1531 |
else |
| 1532 |
break; |
| 1533 |
if(verbose) |
| 1534 |
std::cout << "comment found, string is " << item << std::endl; |
| 1535 |
} |
| 1536 |
|
| 1537 |
pos=item.find(','); |
| 1538 |
if(pos!=std::string::npos) |
| 1539 |
item=item.substr(0,pos-1); |
| 1540 |
float value=string2type<float>(item); |
| 1541 |
scale.push_back(value); |
| 1542 |
} |
| 1543 |
if(scale.size()!=ncol){ |
| 1544 |
std::string errorString="Error: reading scale"; |
| 1545 |
throw(errorString); |
| 1546 |
} |
| 1547 |
if(verbose){ |
| 1548 |
std::cout << "scale:" << std::endl; |
| 1549 |
for(int index=0;index<scale.size();++index){ |
| 1550 |
if(index<scale.size()-1) |
| 1551 |
std::cout << scale[index] << ", "; |
| 1552 |
else |
| 1553 |
std::cout << scale[index] << std::endl; |
| 1554 |
} |
| 1555 |
} |
| 1556 |
scaleDone=true; |
| 1557 |
continue; |
| 1558 |
} |
| 1559 |
if(!classValueMapDone){ |
| 1560 |
|
| 1561 |
|
| 1562 |
|
| 1563 |
if(verbose>1) |
| 1564 |
std::cout << csvRecord << std::endl; |
| 1565 |
lineStream.clear(); |
| 1566 |
lineStream.str(csvRecord); |
| 1567 |
isEqual=false; |
| 1568 |
csvstream.clear(); |
| 1569 |
csvstream.str(csvRecord); |
| 1570 |
ncol=0; |
| 1571 |
while(getline(csvstream,item,m_fs)){ |
| 1572 |
if(verbose) |
| 1573 |
std::cout << item << " "; |
| 1574 |
pos=item.find(m_comment); |
| 1575 |
if(pos!=std::string::npos){ |
| 1576 |
isComment=true; |
| 1577 |
if(pos>0) |
| 1578 |
item=item.substr(0,pos-1); |
| 1579 |
else |
| 1580 |
break; |
| 1581 |
if(verbose) |
| 1582 |
std::cout << "comment found, string is " << item << std::endl; |
| 1583 |
} |
| 1584 |
++ncol; |
| 1585 |
if(isComment) |
| 1586 |
break; |
| 1587 |
} |
| 1588 |
|
| 1589 |
lineStream.clear(); |
| 1590 |
lineStream.str(csvRecord); |
| 1591 |
isComment=false; |
| 1592 |
while(getline(lineStream,item,m_fs)){ |
| 1593 |
if(verbose) |
| 1594 |
std::cout << item << " "; |
| 1595 |
posEqual=item.find(m_equal); |
| 1596 |
posComment=item.find(m_comment); |
| 1597 |
if(!isEqual){ |
| 1598 |
if(posEqual!=std::string::npos){ |
| 1599 |
isEqual=true; |
| 1600 |
if(posEqual>0) |
| 1601 |
item=item.substr(posEqual+1); |
| 1602 |
else |
| 1603 |
break; |
| 1604 |
if(verbose) |
| 1605 |
std::cout << "equal found, string is " << item << std::endl; |
| 1606 |
} |
| 1607 |
else |
| 1608 |
continue; |
| 1609 |
} |
| 1610 |
if(posComment!=std::string::npos){ |
| 1611 |
isComment=true; |
| 1612 |
if(posComment>0) |
| 1613 |
item=item.substr(0,posComment-1); |
| 1614 |
else |
| 1615 |
break; |
| 1616 |
if(verbose) |
| 1617 |
std::cout << "comment found, string is " << item << std::endl; |
| 1618 |
} |
| 1619 |
|
| 1620 |
pos=item.find(','); |
| 1621 |
if(pos!=std::string::npos) |
| 1622 |
item=item.substr(0,pos-1); |
| 1623 |
|
| 1624 |
pos=item.find(':'); |
| 1625 |
std::string key; |
| 1626 |
if(pos!=std::string::npos){ |
| 1627 |
key=item.substr(0,pos); |
| 1628 |
float value=string2type<float>(item.substr(pos+1)); |
| 1629 |
classValueMap[key]=value; |
| 1630 |
} |
| 1631 |
else{ |
| 1632 |
std::string errorString="Error: reading classValueMap (no : found)"; |
| 1633 |
throw(errorString); |
| 1634 |
} |
| 1635 |
} |
| 1636 |
if(verbose){ |
| 1637 |
std::cout << "classValueMap="; |
| 1638 |
std::map<std::string,short>::const_iterator mapit=classValueMap.begin(); |
| 1639 |
while(mapit!=classValueMap.end()){ |
| 1640 |
if(mapit!=--classValueMap.end()) |
| 1641 |
std::cout << mapit->first << ":" << mapit->second << ", "; |
| 1642 |
else |
| 1643 |
std::cout << mapit->first << ":" << mapit->second << std::endl; |
| 1644 |
++mapit; |
| 1645 |
} |
| 1646 |
} |
| 1647 |
if(classValueMap.size()!=ncol){ |
| 1648 |
std::ostringstream errorStream; |
| 1649 |
errorStream << "Error: reading classValueMap (ncol: " << classValueMap.size() << ")"; |
| 1650 |
throw(errorStream.str()); |
| 1651 |
} |
| 1652 |
classValueMapDone=true; |
| 1653 |
continue; |
| 1654 |
} |
| 1655 |
if(!nameVectorDone){ |
| 1656 |
bool nameVectorFound=false; |
| 1657 |
|
| 1658 |
if(csvRecord.find("nameVector")!=std::string::npos) |
| 1659 |
nameVectorFound=true; |
| 1660 |
if(verbose>1) |
| 1661 |
std::cout << csvRecord << std::endl; |
| 1662 |
lineStream.clear(); |
| 1663 |
lineStream.str(csvRecord); |
| 1664 |
isEqual=false; |
| 1665 |
csvstream.clear(); |
| 1666 |
csvstream.str(csvRecord); |
| 1667 |
ncol=0; |
| 1668 |
while(getline(csvstream,item,m_fs)){ |
| 1669 |
if(verbose) |
| 1670 |
std::cout << item << " "; |
| 1671 |
pos=item.find(m_comment); |
| 1672 |
if(pos!=std::string::npos){ |
| 1673 |
isComment=true; |
| 1674 |
if(pos>0) |
| 1675 |
item=item.substr(0,pos-1); |
| 1676 |
else |
| 1677 |
break; |
| 1678 |
if(verbose) |
| 1679 |
std::cout << "comment found, string is " << item << std::endl; |
| 1680 |
} |
| 1681 |
++ncol; |
| 1682 |
if(isComment) |
| 1683 |
break; |
| 1684 |
} |
| 1685 |
|
| 1686 |
lineStream.clear(); |
| 1687 |
lineStream.str(csvRecord); |
| 1688 |
isComment=false; |
| 1689 |
while(getline(lineStream,item,m_fs)){ |
| 1690 |
if(verbose) |
| 1691 |
std::cout << item << " "; |
| 1692 |
posEqual=item.find(m_equal); |
| 1693 |
posComment=item.find(m_comment); |
| 1694 |
if(!isEqual){ |
| 1695 |
if(posEqual!=std::string::npos){ |
| 1696 |
isEqual=true; |
| 1697 |
if(posEqual>0) |
| 1698 |
item=item.substr(posEqual+1); |
| 1699 |
else |
| 1700 |
break; |
| 1701 |
if(verbose) |
| 1702 |
std::cout << "equal found, string is " << item << std::endl; |
| 1703 |
} |
| 1704 |
else |
| 1705 |
continue; |
| 1706 |
} |
| 1707 |
if(posComment!=std::string::npos){ |
| 1708 |
isComment=true; |
| 1709 |
if(posComment>0) |
| 1710 |
item=item.substr(0,posComment-1); |
| 1711 |
else |
| 1712 |
break; |
| 1713 |
if(verbose) |
| 1714 |
std::cout << "comment found, string is " << item << std::endl; |
| 1715 |
} |
| 1716 |
|
| 1717 |
pos=item.find(','); |
| 1718 |
if(pos!=std::string::npos) |
| 1719 |
item=item.substr(0,pos-1); |
| 1720 |
nameVector.push_back(item); |
| 1721 |
} |
| 1722 |
if(nameVector.size()!=ncol){ |
| 1723 |
std::string errorString="Error: reading nameVector"; |
| 1724 |
throw(errorString); |
| 1725 |
} |
| 1726 |
if(verbose){ |
| 1727 |
std::cout << "nameVector:" << std::endl; |
| 1728 |
for(int index=0;index<nameVector.size();++index){ |
| 1729 |
if(index<nameVector.size()-1) |
| 1730 |
std::cout << nameVector[index] << ", "; |
| 1731 |
else |
| 1732 |
std::cout << nameVector[index] << std::endl; |
| 1733 |
} |
| 1734 |
} |
| 1735 |
if(!nameVectorFound) |
| 1736 |
nameVector.clear(); |
| 1737 |
nameVectorDone=true; |
| 1738 |
} |
| 1739 |
if(!bandNamesDone){ |
| 1740 |
bool bandNamesFound=false; |
| 1741 |
|
| 1742 |
if(csvRecord.find("bandNames")!=std::string::npos) |
| 1743 |
bandNamesFound=true; |
| 1744 |
if(verbose>1) |
| 1745 |
std::cout << csvRecord << std::endl; |
| 1746 |
lineStream.clear(); |
| 1747 |
lineStream.str(csvRecord); |
| 1748 |
isEqual=false; |
| 1749 |
csvstream.clear(); |
| 1750 |
csvstream.str(csvRecord); |
| 1751 |
ncol=0; |
| 1752 |
while(getline(csvstream,item,m_fs)){ |
| 1753 |
if(verbose) |
| 1754 |
std::cout << item << " "; |
| 1755 |
pos=item.find(m_comment); |
| 1756 |
if(pos!=std::string::npos){ |
| 1757 |
isComment=true; |
| 1758 |
if(pos>0) |
| 1759 |
item=item.substr(0,pos-1); |
| 1760 |
else |
| 1761 |
break; |
| 1762 |
if(verbose) |
| 1763 |
std::cout << "comment found, string is " << item << std::endl; |
| 1764 |
} |
| 1765 |
++ncol; |
| 1766 |
if(isComment) |
| 1767 |
break; |
| 1768 |
} |
| 1769 |
|
| 1770 |
lineStream.clear(); |
| 1771 |
lineStream.str(csvRecord); |
| 1772 |
isComment=false; |
| 1773 |
while(getline(lineStream,item,m_fs)){ |
| 1774 |
if(verbose) |
| 1775 |
std::cout << item << " "; |
| 1776 |
posEqual=item.find(m_equal); |
| 1777 |
posComment=item.find(m_comment); |
| 1778 |
if(!isEqual){ |
| 1779 |
if(posEqual!=std::string::npos){ |
| 1780 |
isEqual=true; |
| 1781 |
if(posEqual>0) |
| 1782 |
item=item.substr(posEqual+1); |
| 1783 |
else |
| 1784 |
break; |
| 1785 |
if(verbose) |
| 1786 |
std::cout << "equal found, string is " << item << std::endl; |
| 1787 |
} |
| 1788 |
else |
| 1789 |
continue; |
| 1790 |
} |
| 1791 |
if(posComment!=std::string::npos){ |
| 1792 |
isComment=true; |
| 1793 |
if(posComment>0) |
| 1794 |
item=item.substr(0,posComment-1); |
| 1795 |
else |
| 1796 |
break; |
| 1797 |
if(verbose) |
| 1798 |
std::cout << "comment found, string is " << item << std::endl; |
| 1799 |
} |
| 1800 |
|
| 1801 |
pos=item.find(','); |
| 1802 |
if(pos!=std::string::npos) |
| 1803 |
item=item.substr(0,pos-1); |
| 1804 |
bandNames.push_back(item); |
| 1805 |
} |
| 1806 |
if(bandNames.size()!=ncol){ |
| 1807 |
std::string errorString="Error: reading bandNames"; |
| 1808 |
throw(errorString); |
| 1809 |
} |
| 1810 |
if(verbose){ |
| 1811 |
std::cout << "bandNames:" << std::endl; |
| 1812 |
for(int index=0;index<bandNames.size();++index){ |
| 1813 |
if(index<bandNames.size()-1) |
| 1814 |
std::cout << bandNames[index] << ", "; |
| 1815 |
else |
| 1816 |
std::cout << bandNames[index] << std::endl; |
| 1817 |
} |
| 1818 |
} |
| 1819 |
if(!bandNamesFound) |
| 1820 |
bandNames.clear(); |
| 1821 |
bandNamesDone=true; |
| 1822 |
} |
| 1823 |
} |
| 1824 |
m_ifstream.close(); |
| 1825 |
return (ann != NULL); |
| 1826 |
} |
| 1827 |
|
| 1828 |
|
| 1829 |
|
| 1830 |
|
| 1831 |
|
| 1832 |
|
| 1833 |
|
| 1834 |
|
| 1835 |
|
| 1836 |
|
| 1837 |
|
| 1838 |
|
| 1839 |
|
| 1840 |
|
| 1841 |
|
| 1842 |
|
| 1843 |
|
| 1844 |
|
| 1845 |
|
| 1846 |
|
| 1847 |
|
| 1848 |
bool save(const std::string &configuration_file) |
| 1849 |
{ |
| 1850 |
if (ann == NULL) |
| 1851 |
{ |
| 1852 |
return false; |
| 1853 |
} |
| 1854 |
if (fann_save(ann, configuration_file.c_str()) == -1) |
| 1855 |
{ |
| 1856 |
return false; |
| 1857 |
} |
| 1858 |
return true; |
| 1859 |
} |
| 1860 |
|
| 1861 |
int fann_save_internal_fd(std::ostream &outputStream, struct fann *ann, unsigned int save_as_fixed) |
| 1862 |
{ |
| 1863 |
struct fann_layer *layer_it; |
| 1864 |
int calculated_decimal_point = 0; |
| 1865 |
struct fann_neuron *neuron_it, *first_neuron; |
| 1866 |
fann_type *weights; |
| 1867 |
struct fann_neuron **connected_neurons; |
| 1868 |
unsigned int i = 0; |
| 1869 |
|
| 1870 |
#ifndef FIXEDFANN |
| 1871 |
|
| 1872 |
unsigned int decimal_point = 0; |
| 1873 |
unsigned int fixed_multiplier = 0; |
| 1874 |
fann_type max_possible_value = 0; |
| 1875 |
unsigned int bits_used_for_max = 0; |
| 1876 |
fann_type current_max_value = 0; |
| 1877 |
#endif |
| 1878 |
|
| 1879 |
#ifndef FIXEDFANN |
| 1880 |
if(save_as_fixed) |
| 1881 |
{ |
| 1882 |
|
| 1883 |
|
| 1884 |
outputStream << FANN_FIX_VERSION << std::endl; |
| 1885 |
} |
| 1886 |
else |
| 1887 |
{ |
| 1888 |
|
| 1889 |
|
| 1890 |
outputStream << FANN_FLO_VERSION << std::endl; |
| 1891 |
} |
| 1892 |
#else |
| 1893 |
|
| 1894 |
|
| 1895 |
outputStream << FANN_FIX_VERSION << std::endl; |
| 1896 |
#endif |
| 1897 |
|
| 1898 |
#ifndef FIXEDFANN |
| 1899 |
if(save_as_fixed) |
| 1900 |
{ |
| 1901 |
|
| 1902 |
|
| 1903 |
for(layer_it = ann->first_layer + 1; layer_it != ann->last_layer; layer_it++) |
| 1904 |
{ |
| 1905 |
for(neuron_it = layer_it->first_neuron; neuron_it != layer_it->last_neuron; neuron_it++) |
| 1906 |
{ |
| 1907 |
|
| 1908 |
current_max_value = 0; |
| 1909 |
for(i = neuron_it->first_con; i != neuron_it->last_con; i++) |
| 1910 |
{ |
| 1911 |
current_max_value += fann_abs(ann->weights[i]); |
| 1912 |
} |
| 1913 |
|
| 1914 |
if(current_max_value > max_possible_value) |
| 1915 |
{ |
| 1916 |
max_possible_value = current_max_value; |
| 1917 |
} |
| 1918 |
} |
| 1919 |
} |
| 1920 |
|
| 1921 |
for(bits_used_for_max = 0; max_possible_value >= 1; bits_used_for_max++) |
| 1922 |
{ |
| 1923 |
max_possible_value /= 2.0; |
| 1924 |
} |
| 1925 |
|
| 1926 |
|
| 1927 |
|
| 1928 |
|
| 1929 |
|
| 1930 |
|
| 1931 |
|
| 1932 |
calculated_decimal_point = (sizeof(int) * 8 - 2 - bits_used_for_max) / 2; |
| 1933 |
|
| 1934 |
if(calculated_decimal_point < 0) |
| 1935 |
{ |
| 1936 |
decimal_point = 0; |
| 1937 |
} |
| 1938 |
else |
| 1939 |
{ |
| 1940 |
decimal_point = calculated_decimal_point; |
| 1941 |
} |
| 1942 |
|
| 1943 |
fixed_multiplier = 1 << decimal_point; |
| 1944 |
|
| 1945 |
#ifdef DEBUG |
| 1946 |
printf("calculated_decimal_point=%d, decimal_point=%u, bits_used_for_max=%u\n", |
| 1947 |
calculated_decimal_point, decimal_point, bits_used_for_max); |
| 1948 |
#endif |
| 1949 |
|
| 1950 |
|
| 1951 |
|
| 1952 |
outputStream << "decimal_point=" << decimal_point << std::endl; |
| 1953 |
} |
| 1954 |
#else |
| 1955 |
|
| 1956 |
outputStream << "decimal_point=" << ann->decimal_point << std::endl; |
| 1957 |
|
| 1958 |
#endif |
| 1959 |
|
| 1960 |
|
| 1961 |
outputStream << "num_layers=" << (int)(ann->last_layer - ann->first_layer) << std::endl; |
| 1962 |
outputStream << "learning_rate=" << ann->learning_rate << std::endl; |
| 1963 |
outputStream << "connection_rate=" << ann->connection_rate << std::endl; |
| 1964 |
outputStream << "network_type=" << ann->network_type << std::endl; |
| 1965 |
|
| 1966 |
outputStream << "learning_momentum=" << ann->learning_momentum << std::endl; |
| 1967 |
outputStream << "training_algorithm=" << ann->training_algorithm << std::endl; |
| 1968 |
outputStream << "train_error_function=" << ann->train_error_function << std::endl; |
| 1969 |
outputStream << "train_stop_function=" << ann->train_stop_function << std::endl; |
| 1970 |
outputStream << "cascade_output_change_fraction=" << ann->cascade_output_change_fraction << std::endl; |
| 1971 |
outputStream << "quickprop_decay=" << ann->quickprop_decay << std::endl; |
| 1972 |
outputStream << "quickprop_mu=" << ann->quickprop_mu << std::endl; |
| 1973 |
outputStream << "rprop_increase_factor=" << ann->rprop_increase_factor << std::endl; |
| 1974 |
outputStream << "rprop_decrease_factor=" << ann->rprop_decrease_factor << std::endl; |
| 1975 |
outputStream << "rprop_delta_min=" << ann->rprop_delta_min << std::endl; |
| 1976 |
outputStream << "rprop_delta_max=" << ann->rprop_delta_max << std::endl; |
| 1977 |
outputStream << "rprop_delta_zero=" << ann->rprop_delta_zero << std::endl; |
| 1978 |
outputStream << "cascade_output_stagnation_epochs=" << ann->cascade_output_stagnation_epochs << std::endl; |
| 1979 |
outputStream << "cascade_candidate_change_fraction=" << ann->cascade_candidate_change_fraction << std::endl; |
| 1980 |
outputStream << "cascade_candidate_stagnation_epochs=" << ann->cascade_candidate_stagnation_epochs << std::endl; |
| 1981 |
outputStream << "cascade_max_out_epochs=" << ann->cascade_max_out_epochs << std::endl; |
| 1982 |
|
| 1983 |
outputStream << "cascade_min_out_epochs=" << ann->cascade_min_out_epochs << std::endl; |
| 1984 |
|
| 1985 |
outputStream << "cascade_max_cand_epochs=" << ann->cascade_max_cand_epochs << std::endl; |
| 1986 |
|
| 1987 |
outputStream << "cascade_min_cand_epochs=" << ann->cascade_min_cand_epochs << std::endl; |
| 1988 |
|
| 1989 |
outputStream << "cascade_num_candidate_groups=" << ann->cascade_num_candidate_groups << std::endl; |
| 1990 |
|
| 1991 |
#ifndef FIXEDFANN |
| 1992 |
if(save_as_fixed) |
| 1993 |
{ |
| 1994 |
outputStream << "bit_fail_limit=" << (int) floor((ann->bit_fail_limit * fixed_multiplier) + 0.5) << std::endl; |
| 1995 |
outputStream << "cascade_candidate_limit=" << (int) floor((ann->cascade_candidate_limit * fixed_multiplier) + 0.5) << std::endl; |
| 1996 |
outputStream << "cascade_weight_multiplier=" << (int) floor((ann->cascade_weight_multiplier * fixed_multiplier) + 0.5) << std::endl; |
| 1997 |
} |
| 1998 |
else |
| 1999 |
#endif |
| 2000 |
{ |
| 2001 |
outputStream << "bit_fail_limit=" << ann->bit_fail_limit << std::endl; |
| 2002 |
outputStream << "cascade_candidate_limit=" << ann->cascade_candidate_limit << std::endl; |
| 2003 |
outputStream << "cascade_weight_multiplier=" << ann->cascade_weight_multiplier * fixed_multiplier << std::endl; |
| 2004 |
} |
| 2005 |
|
| 2006 |
outputStream << "cascade_activation_functions_count=" << ann->cascade_activation_functions_count << std::endl; |
| 2007 |
outputStream << "cascade_activation_functions="; |
| 2008 |
for(i = 0; i < ann->cascade_activation_functions_count; i++) |
| 2009 |
outputStream << ann->cascade_activation_functions[i] << " "; |
| 2010 |
outputStream << std::endl; |
| 2011 |
|
| 2012 |
outputStream << "cascade_activation_steepnesses_count=" << ann->cascade_activation_steepnesses_count << std::endl; |
| 2013 |
outputStream << "cascade_activation_steepnesses="; |
| 2014 |
for(i = 0; i < ann->cascade_activation_steepnesses_count; i++) |
| 2015 |
{ |
| 2016 |
#ifndef FIXEDFANN |
| 2017 |
if(save_as_fixed) |
| 2018 |
outputStream << (int) floor((ann->cascade_activation_steepnesses[i] * fixed_multiplier) + 0.5) << " "; |
| 2019 |
else |
| 2020 |
#endif |
| 2021 |
|
| 2022 |
|
| 2023 |
|
| 2024 |
|
| 2025 |
outputStream << ann->cascade_activation_steepnesses[i] << " "; |
| 2026 |
} |
| 2027 |
outputStream << std::endl; |
| 2028 |
|
| 2029 |
outputStream << "layer_sizes="; |
| 2030 |
for(layer_it = ann->first_layer; layer_it != ann->last_layer; layer_it++) |
| 2031 |
{ |
| 2032 |
|
| 2033 |
outputStream << (int)(layer_it->last_neuron - layer_it->first_neuron) << " "; |
| 2034 |
} |
| 2035 |
outputStream << std::endl; |
| 2036 |
|
| 2037 |
#ifndef FIXEDFANN |
| 2038 |
|
| 2039 |
#define SCALE_SAVE( what, where ) \ |
| 2040 |
outputStream << #what << "_" << #where << "="; \ |
| 2041 |
for( i = 0; i < ann->num_##where##put; i++ ) \ |
| 2042 |
outputStream << ann->what##_##where[ i ] << " "; \ |
| 2043 |
outputStream << std::endl; |
| 2044 |
|
| 2045 |
if(!save_as_fixed){ |
| 2046 |
if(ann->scale_mean_in != NULL){ |
| 2047 |
outputStream << "scale_included=1" << std::endl; |
| 2048 |
SCALE_SAVE( scale_mean, in ) |
| 2049 |
SCALE_SAVE( scale_deviation, in ) |
| 2050 |
SCALE_SAVE( scale_new_min, in ) |
| 2051 |
SCALE_SAVE( scale_factor, in ) |
| 2052 |
|
| 2053 |
SCALE_SAVE( scale_mean, out ) |
| 2054 |
SCALE_SAVE( scale_deviation, out ) |
| 2055 |
SCALE_SAVE( scale_new_min, out ) |
| 2056 |
SCALE_SAVE( scale_factor, out ) |
| 2057 |
} |
| 2058 |
else |
| 2059 |
outputStream << "scale_included=0" << std::endl; |
| 2060 |
} |
| 2061 |
#undef SCALE_SAVE |
| 2062 |
#endif |
| 2063 |
|
| 2064 |
|
| 2065 |
outputStream << "neurons (num_inputs, activation_function, activation_steepness)="; |
| 2066 |
for(layer_it = ann->first_layer; layer_it != ann->last_layer; layer_it++) |
| 2067 |
{ |
| 2068 |
|
| 2069 |
for(neuron_it = layer_it->first_neuron; neuron_it != layer_it->last_neuron; neuron_it++) |
| 2070 |
{ |
| 2071 |
#ifndef FIXEDFANN |
| 2072 |
if(save_as_fixed) |
| 2073 |
{ |
| 2074 |
outputStream << "(" << neuron_it->last_con - neuron_it->first_con << ", " << neuron_it->activation_function << ", " << (int) floor((neuron_it->activation_steepness * fixed_multiplier) + 0.5) << ") "; |
| 2075 |
} |
| 2076 |
else |
| 2077 |
{ |
| 2078 |
outputStream << "(" << neuron_it->last_con - neuron_it->first_con << ", " << neuron_it->activation_function << ", " << neuron_it->activation_steepness << ") "; |
| 2079 |
} |
| 2080 |
#else |
| 2081 |
outputStream << "(" << neuron_it->last_con - neuron_it->first_con << ", " << neuron_it->activation_function << ", " << neuron_it->activation_steepness << ") "; |
| 2082 |
#endif |
| 2083 |
} |
| 2084 |
} |
| 2085 |
outputStream << std::endl; |
| 2086 |
|
| 2087 |
connected_neurons = ann->connections; |
| 2088 |
weights = ann->weights; |
| 2089 |
first_neuron = ann->first_layer->first_neuron; |
| 2090 |
|
| 2091 |
|
| 2092 |
|
| 2093 |
|
| 2094 |
|
| 2095 |
|
| 2096 |
|
| 2097 |
|
| 2098 |
|
| 2099 |
|
| 2100 |
outputStream << "connections (connected_to_neuron, weight)="; |
| 2101 |
for(i = 0; i < ann->total_connections; i++) |
| 2102 |
{ |
| 2103 |
#ifndef FIXEDFANN |
| 2104 |
if(save_as_fixed) |
| 2105 |
{ |
| 2106 |
|
| 2107 |
outputStream << "(" << (int)(connected_neurons[i] - first_neuron) << ", " << (int) floor((weights[i] * fixed_multiplier) + 0.5) << ") "; |
| 2108 |
} |
| 2109 |
else |
| 2110 |
{ |
| 2111 |
|
| 2112 |
outputStream << "(" << (int)(connected_neurons[i] - first_neuron) << ", " << weights[i] << ") "; |
| 2113 |
} |
| 2114 |
#else |
| 2115 |
|
| 2116 |
outputStream << "(" << (int)(connected_neurons[i] - first_neuron) << ", " << weights[i] << ") "; |
| 2117 |
#endif |
| 2118 |
} |
| 2119 |
outputStream << std::endl; |
| 2120 |
|
| 2121 |
return calculated_decimal_point; |
| 2122 |
} |
| 2123 |
|
| 2124 |
bool save(const std::string &configuration_file, const std::vector<double> &offset, const std::vector<double> &scale, std::map<std::string,short> &classValueMap, const std::vector<std::string> &nameVector, const std::vector<std::string> &bandNames){ |
| 2125 |
bool result=false; |
| 2126 |
std::ofstream fileStream(configuration_file); |
| 2127 |
if(fileStream) |
| 2128 |
result=ann_serialize_model(fileStream, offset, scale, classValueMap, nameVector, bandNames); |
| 2129 |
fileStream.close(); |
| 2130 |
return result; |
| 2131 |
} |
| 2132 |
|
| 2133 |
bool ann_serialize_model(std::ostream &outputStream, const std::vector<double> &offset, const std::vector<double> &scale, std::map<std::string,short> &classValueMap, const std::vector<std::string> &nameVector, const std::vector<std::string> &bandNames) |
| 2134 |
{ |
| 2135 |
short verbose=0; |
| 2136 |
if (ann == NULL) |
| 2137 |
{ |
| 2138 |
return false; |
| 2139 |
} |
| 2140 |
|
| 2141 |
|
| 2142 |
|
| 2143 |
|
| 2144 |
|
| 2145 |
|
| 2146 |
|
| 2147 |
|
| 2148 |
if(fann_save_internal_fd(outputStream, ann, 0)==-1) |
| 2149 |
return(false); |
| 2150 |
if(verbose) |
| 2151 |
std::cout << "writing offset: " << offset[0] << " ..." << std::endl; |
| 2152 |
outputStream << "offset="; |
| 2153 |
for(int index=0;index<offset.size();++index){ |
| 2154 |
if(index<offset.size()-1) |
| 2155 |
outputStream << offset[index] << ", "; |
| 2156 |
else |
| 2157 |
outputStream << offset[index] << std::endl; |
| 2158 |
} |
| 2159 |
if(verbose) |
| 2160 |
std::cout << "writing scale: " << scale[0] << " ..." << std::endl; |
| 2161 |
outputStream << "scale="; |
| 2162 |
for(int index=0;index<scale.size();++index){ |
| 2163 |
if(index<scale.size()-1) |
| 2164 |
outputStream << scale[index] << ", "; |
| 2165 |
else |
| 2166 |
outputStream << scale[index] << std::endl; |
| 2167 |
} |
| 2168 |
|
| 2169 |
if(verbose) |
| 2170 |
std::cout << "writing classValueMap: " << classValueMap.begin()->first << " ..." << std::endl; |
| 2171 |
outputStream << "classValueMap="; |
| 2172 |
std::map<std::string,short>::const_iterator mapit=classValueMap.begin(); |
| 2173 |
while(mapit!=classValueMap.end()){ |
| 2174 |
if(mapit!=--classValueMap.end()) |
| 2175 |
outputStream << mapit->first << ":" << mapit->second << ", "; |
| 2176 |
else |
| 2177 |
outputStream << (mapit->first) << ":" << mapit->second << std::endl; |
| 2178 |
++mapit; |
| 2179 |
} |
| 2180 |
|
| 2181 |
if(verbose) |
| 2182 |
std::cout << "writing nameVector: " << nameVector[0] << " ..." << std::endl; |
| 2183 |
outputStream << "nameVector="; |
| 2184 |
for(int index=0;index<nameVector.size();++index){ |
| 2185 |
if(index<nameVector.size()-1) |
| 2186 |
outputStream << nameVector[index] << ", "; |
| 2187 |
else |
| 2188 |
outputStream << nameVector[index] << std::endl; |
| 2189 |
} |
| 2190 |
|
| 2191 |
if(bandNames.size()){ |
| 2192 |
if(verbose) |
| 2193 |
std::cout << "writing bandNames: " << bandNames[0] << " ..." << std::endl; |
| 2194 |
outputStream << "bandNames="; |
| 2195 |
for(int index=0;index<bandNames.size();++index){ |
| 2196 |
if(index<bandNames.size()-1) |
| 2197 |
outputStream << bandNames[index] << ", "; |
| 2198 |
else |
| 2199 |
outputStream << bandNames[index] << std::endl; |
| 2200 |
} |
| 2201 |
} |
| 2202 |
return true; |
| 2203 |
} |
| 2204 |
|
| 2205 |
|
| 2206 |
|
| 2207 |
|
| 2208 |
|
| 2209 |
|
| 2210 |
|
| 2211 |
|
| 2212 |
|
| 2213 |
|
| 2214 |
|
| 2215 |
|
| 2216 |
|
| 2217 |
|
| 2218 |
|
| 2219 |
|
| 2220 |
|
| 2221 |
|
| 2222 |
|
| 2223 |
|
| 2224 |
|
| 2225 |
|
| 2226 |
|
| 2227 |
|
| 2228 |
|
| 2229 |
|
| 2230 |
|
| 2231 |
|
| 2232 |
|
| 2233 |
|
| 2234 |
|
| 2235 |
|
| 2236 |
|
| 2237 |
int save_to_fixed(const std::string &configuration_file) |
| 2238 |
{ |
| 2239 |
int fixpoint = 0; |
| 2240 |
if (ann != NULL) |
| 2241 |
{ |
| 2242 |
fixpoint = fann_save_to_fixed(ann, configuration_file.c_str()); |
| 2243 |
} |
| 2244 |
return fixpoint; |
| 2245 |
} |
| 2246 |
|
| 2247 |
#ifndef FIXEDFANN |
| 2248 |
|
| 2249 |
|
| 2250 |
|
| 2251 |
|
| 2252 |
|
| 2253 |
|
| 2254 |
|
| 2255 |
|
| 2256 |
|
| 2257 |
|
| 2258 |
|
| 2259 |
|
| 2260 |
|
| 2261 |
|
| 2262 |
|
| 2263 |
|
| 2264 |
void train(fann_type *input, fann_type *desired_output) |
| 2265 |
{ |
| 2266 |
if (ann != NULL) |
| 2267 |
{ |
| 2268 |
fann_train(ann, input, desired_output); |
| 2269 |
} |
| 2270 |
} |
| 2271 |
|
| 2272 |
|
| 2273 |
|
| 2274 |
|
| 2275 |
|
| 2276 |
|
| 2277 |
|
| 2278 |
|
| 2279 |
|
| 2280 |
|
| 2281 |
|
| 2282 |
|
| 2283 |
|
| 2284 |
|
| 2285 |
|
| 2286 |
|
| 2287 |
|
| 2288 |
|
| 2289 |
|
| 2290 |
|
| 2291 |
float train_epoch(const training_data &data) |
| 2292 |
{ |
| 2293 |
float mse = 0.0f; |
| 2294 |
if ((ann != NULL) && (data.train_data != NULL)) |
| 2295 |
{ |
| 2296 |
mse = fann_train_epoch(ann, data.train_data); |
| 2297 |
} |
| 2298 |
return mse; |
| 2299 |
} |
| 2300 |
|
| 2301 |
|
| 2302 |
|
| 2303 |
|
| 2304 |
|
| 2305 |
|
| 2306 |
|
| 2307 |
|
| 2308 |
|
| 2309 |
|
| 2310 |
|
| 2311 |
|
| 2312 |
|
| 2313 |
|
| 2314 |
|
| 2315 |
|
| 2316 |
|
| 2317 |
|
| 2318 |
|
| 2319 |
|
| 2320 |
|
| 2321 |
|
| 2322 |
|
| 2323 |
|
| 2324 |
|
| 2325 |
void train_on_data(const training_data &data, unsigned int max_epochs, |
| 2326 |
unsigned int epochs_between_reports, float desired_error) |
| 2327 |
{ |
| 2328 |
if ((ann != NULL) && (data.train_data != NULL)) |
| 2329 |
{ |
| 2330 |
fann_train_on_data(ann, data.train_data, max_epochs, |
| 2331 |
epochs_between_reports, desired_error); |
| 2332 |
} |
| 2333 |
} |
| 2334 |
|
| 2335 |
|
| 2336 |
|
| 2337 |
void train_on_data(const std::vector< std::vector<fann_type> >& input, |
| 2338 |
const std::vector< std::vector<fann_type> >& output, |
| 2339 |
bool initWeights, |
| 2340 |
unsigned int max_epochs, |
| 2341 |
unsigned int epochs_between_reports, |
| 2342 |
float desired_error) |
| 2343 |
{ |
| 2344 |
if ((ann != NULL)) |
| 2345 |
{ |
| 2346 |
training_data data; |
| 2347 |
data.set_train_data(input,output); |
| 2348 |
if(data.train_data != NULL){ |
| 2349 |
if(initWeights) |
| 2350 |
init_weights(data); |
| 2351 |
fann_train_on_data(ann, data.train_data, max_epochs, |
| 2352 |
epochs_between_reports, desired_error); |
| 2353 |
} |
| 2354 |
} |
| 2355 |
} |
| 2356 |
|
| 2357 |
void train_on_data(const std::vector< Vector2d<fann_type> >& input, |
| 2358 |
unsigned int num_data, |
| 2359 |
bool initWeights, |
| 2360 |
unsigned int max_epochs, |
| 2361 |
unsigned int epochs_between_reports, |
| 2362 |
float desired_error) |
| 2363 |
{ |
| 2364 |
if ((ann != NULL)) |
| 2365 |
{ |
| 2366 |
training_data data; |
| 2367 |
data.set_train_data(input,num_data); |
| 2368 |
if(data.train_data != NULL){ |
| 2369 |
if(initWeights) |
| 2370 |
init_weights(data); |
| 2371 |
fann_train_on_data(ann, data.train_data, max_epochs, |
| 2372 |
epochs_between_reports, desired_error); |
| 2373 |
} |
| 2374 |
} |
| 2375 |
} |
| 2376 |
|
| 2377 |
|
| 2378 |
float cross_validation(std::vector< Vector2d<fann_type> >& trainingFeatures, |
| 2379 |
unsigned int ntraining, |
| 2380 |
unsigned short cv, |
| 2381 |
unsigned int max_epochs, |
| 2382 |
float desired_error, |
| 2383 |
std::vector<unsigned short>& referenceVector, |
| 2384 |
std::vector<unsigned short>& outputVector, |
| 2385 |
short verbose=0) |
| 2386 |
{ |
| 2387 |
referenceVector.clear(); |
| 2388 |
outputVector.clear(); |
| 2389 |
assert(cv<ntraining); |
| 2390 |
int nclass=trainingFeatures.size(); |
| 2391 |
std::vector< Vector2d<float> > testFeatures(nclass); |
| 2392 |
int testclass=0; |
| 2393 |
|
| 2394 |
int nrun=(cv>1)? cv : ntraining; |
| 2395 |
if(nrun>ntraining) |
| 2396 |
nrun=ntraining; |
| 2397 |
for(int irun=0;irun<nrun;++irun){ |
| 2398 |
if(verbose>1) |
| 2399 |
std::cout << "run " << irun << std::endl; |
| 2400 |
|
| 2401 |
if(verbose>1) |
| 2402 |
std::cout << "reset training sample from last run" << std::endl; |
| 2403 |
for(int iclass=0;iclass<nclass;++iclass){ |
| 2404 |
while(testFeatures[iclass].size()){ |
| 2405 |
trainingFeatures[iclass].push_back(testFeatures[iclass].back()); |
| 2406 |
testFeatures[iclass].pop_back(); |
| 2407 |
} |
| 2408 |
if(verbose>1){ |
| 2409 |
std::cout << "training size " << iclass << ": " << trainingFeatures[iclass].size() << std::endl; |
| 2410 |
std::cout << "test size " << iclass << ": " << testFeatures[iclass].size() << std::endl; |
| 2411 |
} |
| 2412 |
assert(trainingFeatures[iclass].size()); |
| 2413 |
} |
| 2414 |
|
| 2415 |
if(verbose>1) |
| 2416 |
std::cout << "create test sample" << std::endl; |
| 2417 |
unsigned int nsample=0; |
| 2418 |
int ntest=(cv>1)? ntraining/cv : 1; |
| 2419 |
while(nsample<ntest){ |
| 2420 |
|
| 2421 |
|
| 2422 |
|
| 2423 |
testFeatures[testclass].push_back(trainingFeatures[testclass][0]); |
| 2424 |
trainingFeatures[testclass].erase(trainingFeatures[testclass].begin()); |
| 2425 |
if(!trainingFeatures[testclass].size()) |
| 2426 |
std::cout << "Error: testclass " << testclass << " has no training" << std::endl; |
| 2427 |
assert(trainingFeatures[testclass].size()); |
| 2428 |
++nsample; |
| 2429 |
if(static_cast<float>(trainingFeatures[testclass].size())/static_cast<float>(testFeatures[testclass].size())<=(cv-1)){ |
| 2430 |
if(verbose>1){ |
| 2431 |
std::cout << "training size " << testclass << ": " << trainingFeatures[testclass].size() << std::endl; |
| 2432 |
std::cout << "test size " << testclass << ": " << testFeatures[testclass].size() << std::endl; |
| 2433 |
} |
| 2434 |
testclass=(testclass+1)%nclass; |
| 2435 |
} |
| 2436 |
} |
| 2437 |
assert(nsample==ntest); |
| 2438 |
|
| 2439 |
if(verbose>1) |
| 2440 |
std::cout << std::endl << "Set training data" << std::endl; |
| 2441 |
bool initWeights=true; |
| 2442 |
unsigned int epochs_between_reports=0; |
| 2443 |
train_on_data(trainingFeatures,ntraining-ntest,initWeights, max_epochs, |
| 2444 |
epochs_between_reports, desired_error); |
| 2445 |
|
| 2446 |
if(verbose>1) |
| 2447 |
std::cout << std::endl << "Cross validation" << std::endl; |
| 2448 |
|
| 2449 |
std::vector<float> result(nclass); |
| 2450 |
int maxClass=-1; |
| 2451 |
for(int iclass=0;iclass<testFeatures.size();++iclass){ |
| 2452 |
assert(trainingFeatures[iclass].size()); |
| 2453 |
for(int isample=0;isample<testFeatures[iclass].size();++isample){ |
| 2454 |
result=run(testFeatures[iclass][isample]); |
| 2455 |
|
| 2456 |
float maxP=-1; |
| 2457 |
for(int ic=0;ic<nclass;++ic){ |
| 2458 |
float pv=(result[ic]+1.0)/2.0; |
| 2459 |
if(pv>maxP){ |
| 2460 |
maxP=pv; |
| 2461 |
maxClass=ic; |
| 2462 |
} |
| 2463 |
} |
| 2464 |
assert(maxP>=0); |
| 2465 |
referenceVector.push_back(iclass); |
| 2466 |
outputVector.push_back(maxClass); |
| 2467 |
} |
| 2468 |
} |
| 2469 |
|
| 2470 |
|
| 2471 |
|
| 2472 |
|
| 2473 |
} |
| 2474 |
|
| 2475 |
|
| 2476 |
for(int iclass=0;iclass<nclass;++iclass){ |
| 2477 |
while(testFeatures[iclass].size()){ |
| 2478 |
trainingFeatures[iclass].push_back(testFeatures[iclass].back()); |
| 2479 |
testFeatures[iclass].pop_back(); |
| 2480 |
} |
| 2481 |
} |
| 2482 |
|
| 2483 |
return 0; |
| 2484 |
} |
| 2485 |
|
| 2486 |
|
| 2487 |
float cross_validation(std::vector< std::vector<fann_type> >& input, |
| 2488 |
std::vector< std::vector<fann_type> >& output, |
| 2489 |
unsigned short cv, |
| 2490 |
unsigned int max_epochs, |
| 2491 |
float desired_error, |
| 2492 |
std::vector< std::vector<fann_type> >& referenceVector, |
| 2493 |
std::vector< std::vector<fann_type> >& outputVector, |
| 2494 |
short verbose=0) |
| 2495 |
{ |
| 2496 |
assert(input.size()); |
| 2497 |
assert(output.size()==input.size()); |
| 2498 |
unsigned int ntraining=input.size(); |
| 2499 |
unsigned int noutput=output[0].size(); |
| 2500 |
referenceVector.clear(); |
| 2501 |
outputVector.clear(); |
| 2502 |
assert(cv<ntraining); |
| 2503 |
std::vector< std::vector<fann_type> > testInput; |
| 2504 |
std::vector< std::vector<fann_type> > testOutput; |
| 2505 |
|
| 2506 |
int nrun=(cv>1)? cv : ntraining; |
| 2507 |
if(nrun>ntraining) |
| 2508 |
nrun=ntraining; |
| 2509 |
for(int irun=0;irun<nrun;++irun){ |
| 2510 |
if(verbose>1) |
| 2511 |
std::cout << "run " << irun << std::endl; |
| 2512 |
|
| 2513 |
if(verbose>1) |
| 2514 |
std::cout << "reset training sample from last run" << std::endl; |
| 2515 |
while(testInput.size()){ |
| 2516 |
input.push_back(testInput.back()); |
| 2517 |
testInput.pop_back(); |
| 2518 |
} |
| 2519 |
while(testOutput.size()){ |
| 2520 |
output.push_back(testOutput.back()); |
| 2521 |
testOutput.pop_back(); |
| 2522 |
} |
| 2523 |
assert(testInput.size()==testOutput.size()); |
| 2524 |
if(verbose>1){ |
| 2525 |
std::cout << "training size: " << input.size() << std::endl; |
| 2526 |
std::cout << "test size: " << testInput.size() << std::endl; |
| 2527 |
} |
| 2528 |
assert(input.size()); |
| 2529 |
|
| 2530 |
if(verbose>1) |
| 2531 |
std::cout << "create test sample" << std::endl; |
| 2532 |
unsigned int nsample=0; |
| 2533 |
int ntest=(cv>1)? ntraining/cv : 1; |
| 2534 |
while(nsample<ntest){ |
| 2535 |
testInput.push_back(input[0]); |
| 2536 |
testOutput.push_back(output[0]); |
| 2537 |
input.erase(input.begin()); |
| 2538 |
output.erase(output.begin()); |
| 2539 |
assert(input.size()); |
| 2540 |
assert(output.size()); |
| 2541 |
assert(input.size()==output.size()); |
| 2542 |
++nsample; |
| 2543 |
} |
| 2544 |
assert(nsample==ntest); |
| 2545 |
assert(testInput.size()==testOutput.size()); |
| 2546 |
|
| 2547 |
if(verbose>1) |
| 2548 |
std::cout << std::endl << "Set training data" << std::endl; |
| 2549 |
bool initWeights=true; |
| 2550 |
unsigned int epochs_between_reports=0; |
| 2551 |
|
| 2552 |
train_on_data(input,output,initWeights, max_epochs, |
| 2553 |
epochs_between_reports, desired_error); |
| 2554 |
|
| 2555 |
if(verbose>1) |
| 2556 |
std::cout << std::endl << "Cross validation" << std::endl; |
| 2557 |
|
| 2558 |
std::vector<fann_type> result(noutput); |
| 2559 |
for(int isample=0;isample<testInput.size();++isample){ |
| 2560 |
result=run(testInput[isample]); |
| 2561 |
referenceVector.push_back(testOutput[isample]); |
| 2562 |
outputVector.push_back(result); |
| 2563 |
} |
| 2564 |
} |
| 2565 |
|
| 2566 |
while(testInput.size()){ |
| 2567 |
input.push_back(testInput.back()); |
| 2568 |
testInput.pop_back(); |
| 2569 |
} |
| 2570 |
while(testOutput.size()){ |
| 2571 |
output.push_back(testOutput.back()); |
| 2572 |
testOutput.pop_back(); |
| 2573 |
} |
| 2574 |
return 0; |
| 2575 |
} |
| 2576 |
|
| 2577 |
|
| 2578 |
|
| 2579 |
|
| 2580 |
|
| 2581 |
|
| 2582 |
|
| 2583 |
|
| 2584 |
|
| 2585 |
|
| 2586 |
void train_on_file(const std::string &filename, unsigned int max_epochs, |
| 2587 |
unsigned int epochs_between_reports, float desired_error) |
| 2588 |
{ |
| 2589 |
if (ann != NULL) |
| 2590 |
{ |
| 2591 |
fann_train_on_file(ann, filename.c_str(), |
| 2592 |
max_epochs, epochs_between_reports, desired_error); |
| 2593 |
} |
| 2594 |
} |
| 2595 |
#endif |
| 2596 |
|
| 2597 |
|
| 2598 |
|
| 2599 |
|
| 2600 |
|
| 2601 |
|
| 2602 |
|
| 2603 |
|
| 2604 |
|
| 2605 |
|
| 2606 |
|
| 2607 |
|
| 2608 |
fann_type * test(fann_type *input, fann_type *desired_output) |
| 2609 |
{ |
| 2610 |
fann_type * output = NULL; |
| 2611 |
if (ann != NULL) |
| 2612 |
{ |
| 2613 |
output = fann_test(ann, input, desired_output); |
| 2614 |
} |
| 2615 |
return output; |
| 2616 |
} |
| 2617 |
|
| 2618 |
|
| 2619 |
|
| 2620 |
|
| 2621 |
|
| 2622 |
|
| 2623 |
|
| 2624 |
|
| 2625 |
|
| 2626 |
|
| 2627 |
|
| 2628 |
|
| 2629 |
float test_data(const training_data &data) |
| 2630 |
{ |
| 2631 |
float mse = 0.0f; |
| 2632 |
if ((ann != NULL) && (data.train_data != NULL)) |
| 2633 |
{ |
| 2634 |
mse = fann_test_data(ann, data.train_data); |
| 2635 |
} |
| 2636 |
return mse; |
| 2637 |
} |
| 2638 |
|
| 2639 |
float test_data(const std::vector< Vector2d<fann_type> >& input, unsigned int num_data) |
| 2640 |
{ |
| 2641 |
assert(num_data); |
| 2642 |
assert(input.size()); |
| 2643 |
unsigned int num_class=input.size(); |
| 2644 |
assert(input[0].size()); |
| 2645 |
unsigned int num_input=input[0][0].size(); |
| 2646 |
unsigned int num_output=num_class; |
| 2647 |
struct fann_train_data *data = |
| 2648 |
(struct fann_train_data *)malloc(sizeof(struct fann_train_data)); |
| 2649 |
data->input = (fann_type **)calloc(num_data, sizeof(fann_type *)); |
| 2650 |
data->output = (fann_type **)calloc(num_data, sizeof(fann_type *)); |
| 2651 |
|
| 2652 |
data->num_data = num_data; |
| 2653 |
data->num_input = num_input; |
| 2654 |
data->num_output = num_output; |
| 2655 |
|
| 2656 |
fann_type *data_input = (fann_type *)calloc(num_input*num_data, sizeof(fann_type)); |
| 2657 |
fann_type *data_output = (fann_type *)calloc(num_output*num_data, sizeof(fann_type)); |
| 2658 |
|
| 2659 |
unsigned int isample=0; |
| 2660 |
for(int iclass=0;iclass<num_class;++iclass){ |
| 2661 |
for(int csample=0;csample<input[iclass].size();++csample){ |
| 2662 |
data->input[isample] = data_input; |
| 2663 |
data_input += num_input; |
| 2664 |
for(int iband=0;iband<input[iclass][csample].size();++iband){ |
| 2665 |
assert(input[iclass][csample].size()==num_input); |
| 2666 |
data->input[isample][iband] = input[iclass][csample][iband]; |
| 2667 |
} |
| 2668 |
data->output[isample] = data_output; |
| 2669 |
data_output += num_output; |
| 2670 |
for(int ic=0;ic<num_output;++ic){ |
| 2671 |
|
| 2672 |
|
| 2673 |
if(ic==iclass) |
| 2674 |
data->output[isample][ic] = 1; |
| 2675 |
else |
| 2676 |
data->output[isample][ic] = -1; |
| 2677 |
} |
| 2678 |
++isample; |
| 2679 |
} |
| 2680 |
} |
| 2681 |
FANN::training_data trainingData; |
| 2682 |
trainingData.train_data = data; |
| 2683 |
return test_data(trainingData); |
| 2684 |
} |
| 2685 |
|
| 2686 |
|
| 2687 |
|
| 2688 |
|
| 2689 |
|
| 2690 |
|
| 2691 |
|
| 2692 |
|
| 2693 |
|
| 2694 |
|
| 2695 |
|
| 2696 |
|
| 2697 |
|
| 2698 |
|
| 2699 |
float get_MSE() |
| 2700 |
{ |
| 2701 |
float mse = 0.0f; |
| 2702 |
if (ann != NULL) |
| 2703 |
{ |
| 2704 |
mse = fann_get_MSE(ann); |
| 2705 |
} |
| 2706 |
return mse; |
| 2707 |
} |
| 2708 |
|
| 2709 |
|
| 2710 |
|
| 2711 |
|
| 2712 |
|
| 2713 |
|
| 2714 |
|
| 2715 |
|
| 2716 |
|
| 2717 |
|
| 2718 |
|
| 2719 |
|
| 2720 |
void reset_MSE() |
| 2721 |
{ |
| 2722 |
if (ann != NULL) |
| 2723 |
{ |
| 2724 |
fann_reset_MSE(ann); |
| 2725 |
} |
| 2726 |
} |
| 2727 |
|
| 2728 |
|
| 2729 |
|
| 2730 |
|
| 2731 |
|
| 2732 |
|
| 2733 |
|
| 2734 |
|
| 2735 |
|
| 2736 |
|
| 2737 |
|
| 2738 |
|
| 2739 |
|
| 2740 |
void set_callback(callback_type callback, void *user_data) |
| 2741 |
{ |
| 2742 |
if (ann != NULL) |
| 2743 |
{ |
| 2744 |
|
| 2745 |
user_context *user_instance = static_cast<user_context *>(fann_get_user_data(ann)); |
| 2746 |
if (user_instance != NULL) |
| 2747 |
delete user_instance; |
| 2748 |
|
| 2749 |
user_instance = new user_context(); |
| 2750 |
user_instance->user_callback = callback; |
| 2751 |
user_instance->user_data = user_data; |
| 2752 |
user_instance->net = this; |
| 2753 |
fann_set_user_data(ann, user_instance); |
| 2754 |
|
| 2755 |
if (callback != NULL) |
| 2756 |
fann_set_callback(ann, &FANN::neural_net::internal_callback); |
| 2757 |
else |
| 2758 |
fann_set_callback(ann, NULL); |
| 2759 |
} |
| 2760 |
} |
| 2761 |
|
| 2762 |
|
| 2763 |
|
| 2764 |
|
| 2765 |
|
| 2766 |
|
| 2767 |
|
| 2768 |
|
| 2769 |
|
| 2770 |
|
| 2771 |
void print_parameters() |
| 2772 |
{ |
| 2773 |
if (ann != NULL) |
| 2774 |
{ |
| 2775 |
fann_print_parameters(ann); |
| 2776 |
} |
| 2777 |
} |
| 2778 |
|
| 2779 |
|
| 2780 |
|
| 2781 |
|
| 2782 |
|
| 2783 |
|
| 2784 |
|
| 2785 |
|
| 2786 |
|
| 2787 |
|
| 2788 |
|
| 2789 |
|
| 2790 |
|
| 2791 |
|
| 2792 |
|
| 2793 |
|
| 2794 |
|
| 2795 |
training_algorithm_enum get_training_algorithm() |
| 2796 |
{ |
| 2797 |
fann_train_enum training_algorithm = FANN_TRAIN_INCREMENTAL; |
| 2798 |
if (ann != NULL) |
| 2799 |
{ |
| 2800 |
training_algorithm = fann_get_training_algorithm(ann); |
| 2801 |
} |
| 2802 |
return static_cast<training_algorithm_enum>(training_algorithm); |
| 2803 |
} |
| 2804 |
|
| 2805 |
|
| 2806 |
|
| 2807 |
|
| 2808 |
|
| 2809 |
|
| 2810 |
|
| 2811 |
|
| 2812 |
|
| 2813 |
void set_training_algorithm(training_algorithm_enum training_algorithm) |
| 2814 |
{ |
| 2815 |
if (ann != NULL) |
| 2816 |
{ |
| 2817 |
fann_set_training_algorithm(ann, |
| 2818 |
static_cast<fann_train_enum>(training_algorithm)); |
| 2819 |
} |
| 2820 |
} |
| 2821 |
|
| 2822 |
|
| 2823 |
|
| 2824 |
|
| 2825 |
|
| 2826 |
|
| 2827 |
|
| 2828 |
|
| 2829 |
|
| 2830 |
|
| 2831 |
|
| 2832 |
|
| 2833 |
|
| 2834 |
|
| 2835 |
|
| 2836 |
|
| 2837 |
|
| 2838 |
float get_learning_rate() |
| 2839 |
{ |
| 2840 |
float learning_rate = 0.0f; |
| 2841 |
if (ann != NULL) |
| 2842 |
{ |
| 2843 |
learning_rate = fann_get_learning_rate(ann); |
| 2844 |
} |
| 2845 |
return learning_rate; |
| 2846 |
} |
| 2847 |
|
| 2848 |
|
| 2849 |
|
| 2850 |
|
| 2851 |
|
| 2852 |
|
| 2853 |
|
| 2854 |
|
| 2855 |
|
| 2856 |
void set_learning_rate(float learning_rate) |
| 2857 |
{ |
| 2858 |
if (ann != NULL) |
| 2859 |
{ |
| 2860 |
fann_set_learning_rate(ann, learning_rate); |
| 2861 |
} |
| 2862 |
} |
| 2863 |
|
| 2864 |
|
| 2865 |
|
| 2866 |
|
| 2867 |
|
| 2868 |
|
| 2869 |
|
| 2870 |
|
| 2871 |
|
| 2872 |
|
| 2873 |
|
| 2874 |
|
| 2875 |
|
| 2876 |
|
| 2877 |
|
| 2878 |
|
| 2879 |
|
| 2880 |
|
| 2881 |
|
| 2882 |
|
| 2883 |
|
| 2884 |
|
| 2885 |
activation_function_enum get_activation_function(int layer, int neuron) |
| 2886 |
{ |
| 2887 |
unsigned int activation_function = 0; |
| 2888 |
if (ann != NULL) |
| 2889 |
{ |
| 2890 |
activation_function = fann_get_activation_function(ann, layer, neuron); |
| 2891 |
} |
| 2892 |
return static_cast<activation_function_enum>(activation_function); |
| 2893 |
} |
| 2894 |
|
| 2895 |
|
| 2896 |
|
| 2897 |
|
| 2898 |
|
| 2899 |
|
| 2900 |
|
| 2901 |
|
| 2902 |
|
| 2903 |
|
| 2904 |
|
| 2905 |
|
| 2906 |
|
| 2907 |
|
| 2908 |
|
| 2909 |
|
| 2910 |
|
| 2911 |
|
| 2912 |
|
| 2913 |
|
| 2914 |
|
| 2915 |
|
| 2916 |
|
| 2917 |
void set_activation_function(activation_function_enum activation_function, int layer, int neuron) |
| 2918 |
{ |
| 2919 |
if (ann != NULL) |
| 2920 |
{ |
| 2921 |
fann_set_activation_function(ann, |
| 2922 |
static_cast<fann_activationfunc_enum>(activation_function), layer, neuron); |
| 2923 |
} |
| 2924 |
} |
| 2925 |
|
| 2926 |
|
| 2927 |
|
| 2928 |
|
| 2929 |
|
| 2930 |
|
| 2931 |
|
| 2932 |
|
| 2933 |
|
| 2934 |
|
| 2935 |
|
| 2936 |
|
| 2937 |
|
| 2938 |
|
| 2939 |
|
| 2940 |
void set_activation_function_layer(activation_function_enum activation_function, int layer) |
| 2941 |
{ |
| 2942 |
if (ann != NULL) |
| 2943 |
{ |
| 2944 |
fann_set_activation_function_layer(ann, |
| 2945 |
static_cast<fann_activationfunc_enum>(activation_function), layer); |
| 2946 |
} |
| 2947 |
} |
| 2948 |
|
| 2949 |
|
| 2950 |
|
| 2951 |
|
| 2952 |
|
| 2953 |
|
| 2954 |
|
| 2955 |
|
| 2956 |
|
| 2957 |
|
| 2958 |
|
| 2959 |
|
| 2960 |
void set_activation_function_hidden(activation_function_enum activation_function) |
| 2961 |
{ |
| 2962 |
if (ann != NULL) |
| 2963 |
{ |
| 2964 |
fann_set_activation_function_hidden(ann, |
| 2965 |
static_cast<fann_activationfunc_enum>(activation_function)); |
| 2966 |
} |
| 2967 |
} |
| 2968 |
|
| 2969 |
|
| 2970 |
|
| 2971 |
|
| 2972 |
|
| 2973 |
|
| 2974 |
|
| 2975 |
|
| 2976 |
|
| 2977 |
|
| 2978 |
|
| 2979 |
|
| 2980 |
void set_activation_function_output(activation_function_enum activation_function) |
| 2981 |
{ |
| 2982 |
if (ann != NULL) |
| 2983 |
{ |
| 2984 |
fann_set_activation_function_output(ann, |
| 2985 |
static_cast<fann_activationfunc_enum>(activation_function)); |
| 2986 |
} |
| 2987 |
} |
| 2988 |
|
| 2989 |
|
| 2990 |
|
| 2991 |
|
| 2992 |
|
| 2993 |
|
| 2994 |
|
| 2995 |
|
| 2996 |
|
| 2997 |
|
| 2998 |
|
| 2999 |
|
| 3000 |
|
| 3001 |
|
| 3002 |
|
| 3003 |
|
| 3004 |
|
| 3005 |
|
| 3006 |
|
| 3007 |
|
| 3008 |
|
| 3009 |
|
| 3010 |
|
| 3011 |
|
| 3012 |
|
| 3013 |
|
| 3014 |
|
| 3015 |
fann_type get_activation_steepness(int layer, int neuron) |
| 3016 |
{ |
| 3017 |
fann_type activation_steepness = 0; |
| 3018 |
if (ann != NULL) |
| 3019 |
{ |
| 3020 |
activation_steepness = fann_get_activation_steepness(ann, layer, neuron); |
| 3021 |
} |
| 3022 |
return activation_steepness; |
| 3023 |
} |
| 3024 |
|
| 3025 |
|
| 3026 |
|
| 3027 |
|
| 3028 |
|
| 3029 |
|
| 3030 |
|
| 3031 |
|
| 3032 |
|
| 3033 |
|
| 3034 |
|
| 3035 |
|
| 3036 |
|
| 3037 |
|
| 3038 |
|
| 3039 |
|
| 3040 |
|
| 3041 |
|
| 3042 |
|
| 3043 |
|
| 3044 |
|
| 3045 |
|
| 3046 |
|
| 3047 |
|
| 3048 |
void set_activation_steepness(fann_type steepness, int layer, int neuron) |
| 3049 |
{ |
| 3050 |
if (ann != NULL) |
| 3051 |
{ |
| 3052 |
fann_set_activation_steepness(ann, steepness, layer, neuron); |
| 3053 |
} |
| 3054 |
} |
| 3055 |
|
| 3056 |
|
| 3057 |
|
| 3058 |
|
| 3059 |
|
| 3060 |
|
| 3061 |
|
| 3062 |
|
| 3063 |
|
| 3064 |
|
| 3065 |
|
| 3066 |
|
| 3067 |
|
| 3068 |
|
| 3069 |
|
| 3070 |
void set_activation_steepness_layer(fann_type steepness, int layer) |
| 3071 |
{ |
| 3072 |
if (ann != NULL) |
| 3073 |
{ |
| 3074 |
fann_set_activation_steepness_layer(ann, steepness, layer); |
| 3075 |
} |
| 3076 |
} |
| 3077 |
|
| 3078 |
|
| 3079 |
|
| 3080 |
|
| 3081 |
|
| 3082 |
|
| 3083 |
|
| 3084 |
|
| 3085 |
|
| 3086 |
|
| 3087 |
|
| 3088 |
|
| 3089 |
void set_activation_steepness_hidden(fann_type steepness) |
| 3090 |
{ |
| 3091 |
if (ann != NULL) |
| 3092 |
{ |
| 3093 |
fann_set_activation_steepness_hidden(ann, steepness); |
| 3094 |
} |
| 3095 |
} |
| 3096 |
|
| 3097 |
|
| 3098 |
|
| 3099 |
|
| 3100 |
|
| 3101 |
|
| 3102 |
|
| 3103 |
|
| 3104 |
|
| 3105 |
|
| 3106 |
|
| 3107 |
|
| 3108 |
void set_activation_steepness_output(fann_type steepness) |
| 3109 |
{ |
| 3110 |
if (ann != NULL) |
| 3111 |
{ |
| 3112 |
fann_set_activation_steepness_output(ann, steepness); |
| 3113 |
} |
| 3114 |
} |
| 3115 |
|
| 3116 |
|
| 3117 |
|
| 3118 |
|
| 3119 |
|
| 3120 |
|
| 3121 |
|
| 3122 |
|
| 3123 |
|
| 3124 |
|
| 3125 |
|
| 3126 |
|
| 3127 |
|
| 3128 |
|
| 3129 |
|
| 3130 |
|
| 3131 |
error_function_enum get_train_error_function() |
| 3132 |
{ |
| 3133 |
fann_errorfunc_enum train_error_function = FANN_ERRORFUNC_LINEAR; |
| 3134 |
if (ann != NULL) |
| 3135 |
{ |
| 3136 |
train_error_function = fann_get_train_error_function(ann); |
| 3137 |
} |
| 3138 |
return static_cast<error_function_enum>(train_error_function); |
| 3139 |
} |
| 3140 |
|
| 3141 |
|
| 3142 |
|
| 3143 |
|
| 3144 |
|
| 3145 |
|
| 3146 |
|
| 3147 |
|
| 3148 |
|
| 3149 |
|
| 3150 |
|
| 3151 |
|
| 3152 |
void set_train_error_function(error_function_enum train_error_function) |
| 3153 |
{ |
| 3154 |
if (ann != NULL) |
| 3155 |
{ |
| 3156 |
fann_set_train_error_function(ann, |
| 3157 |
static_cast<fann_errorfunc_enum>(train_error_function)); |
| 3158 |
} |
| 3159 |
} |
| 3160 |
|
| 3161 |
|
| 3162 |
|
| 3163 |
|
| 3164 |
|
| 3165 |
|
| 3166 |
|
| 3167 |
|
| 3168 |
|
| 3169 |
|
| 3170 |
|
| 3171 |
|
| 3172 |
|
| 3173 |
|
| 3174 |
float get_quickprop_decay() |
| 3175 |
{ |
| 3176 |
float quickprop_decay = 0.0f; |
| 3177 |
if (ann != NULL) |
| 3178 |
{ |
| 3179 |
quickprop_decay = fann_get_quickprop_decay(ann); |
| 3180 |
} |
| 3181 |
return quickprop_decay; |
| 3182 |
} |
| 3183 |
|
| 3184 |
|
| 3185 |
|
| 3186 |
|
| 3187 |
|
| 3188 |
|
| 3189 |
|
| 3190 |
|
| 3191 |
|
| 3192 |
|
| 3193 |
void set_quickprop_decay(float quickprop_decay) |
| 3194 |
{ |
| 3195 |
if (ann != NULL) |
| 3196 |
{ |
| 3197 |
fann_set_quickprop_decay(ann, quickprop_decay); |
| 3198 |
} |
| 3199 |
} |
| 3200 |
|
| 3201 |
|
| 3202 |
|
| 3203 |
|
| 3204 |
|
| 3205 |
|
| 3206 |
|
| 3207 |
|
| 3208 |
|
| 3209 |
|
| 3210 |
|
| 3211 |
|
| 3212 |
|
| 3213 |
|
| 3214 |
float get_quickprop_mu() |
| 3215 |
{ |
| 3216 |
float quickprop_mu = 0.0f; |
| 3217 |
if (ann != NULL) |
| 3218 |
{ |
| 3219 |
quickprop_mu = fann_get_quickprop_mu(ann); |
| 3220 |
} |
| 3221 |
return quickprop_mu; |
| 3222 |
} |
| 3223 |
|
| 3224 |
|
| 3225 |
|
| 3226 |
|
| 3227 |
|
| 3228 |
|
| 3229 |
|
| 3230 |
|
| 3231 |
|
| 3232 |
|
| 3233 |
void set_quickprop_mu(float quickprop_mu) |
| 3234 |
{ |
| 3235 |
if (ann != NULL) |
| 3236 |
{ |
| 3237 |
fann_set_quickprop_mu(ann, quickprop_mu); |
| 3238 |
} |
| 3239 |
} |
| 3240 |
|
| 3241 |
|
| 3242 |
|
| 3243 |
|
| 3244 |
|
| 3245 |
|
| 3246 |
|
| 3247 |
|
| 3248 |
|
| 3249 |
|
| 3250 |
|
| 3251 |
|
| 3252 |
|
| 3253 |
float get_rprop_increase_factor() |
| 3254 |
{ |
| 3255 |
float factor = 0.0f; |
| 3256 |
if (ann != NULL) |
| 3257 |
{ |
| 3258 |
factor = fann_get_rprop_increase_factor(ann); |
| 3259 |
} |
| 3260 |
return factor; |
| 3261 |
} |
| 3262 |
|
| 3263 |
|
| 3264 |
|
| 3265 |
|
| 3266 |
|
| 3267 |
|
| 3268 |
|
| 3269 |
|
| 3270 |
|
| 3271 |
|
| 3272 |
void set_rprop_increase_factor(float rprop_increase_factor) |
| 3273 |
{ |
| 3274 |
if (ann != NULL) |
| 3275 |
{ |
| 3276 |
fann_set_rprop_increase_factor(ann, rprop_increase_factor); |
| 3277 |
} |
| 3278 |
} |
| 3279 |
|
| 3280 |
|
| 3281 |
|
| 3282 |
|
| 3283 |
|
| 3284 |
|
| 3285 |
|
| 3286 |
|
| 3287 |
|
| 3288 |
|
| 3289 |
|
| 3290 |
|
| 3291 |
float get_rprop_decrease_factor() |
| 3292 |
{ |
| 3293 |
float factor = 0.0f; |
| 3294 |
if (ann != NULL) |
| 3295 |
{ |
| 3296 |
factor = fann_get_rprop_decrease_factor(ann); |
| 3297 |
} |
| 3298 |
return factor; |
| 3299 |
} |
| 3300 |
|
| 3301 |
|
| 3302 |
|
| 3303 |
|
| 3304 |
|
| 3305 |
|
| 3306 |
|
| 3307 |
|
| 3308 |
|
| 3309 |
|
| 3310 |
void set_rprop_decrease_factor(float rprop_decrease_factor) |
| 3311 |
{ |
| 3312 |
if (ann != NULL) |
| 3313 |
{ |
| 3314 |
fann_set_rprop_decrease_factor(ann, rprop_decrease_factor); |
| 3315 |
} |
| 3316 |
} |
| 3317 |
|
| 3318 |
|
| 3319 |
|
| 3320 |
|
| 3321 |
|
| 3322 |
|
| 3323 |
|
| 3324 |
|
| 3325 |
|
| 3326 |
|
| 3327 |
|
| 3328 |
|
| 3329 |
float get_rprop_delta_min() |
| 3330 |
{ |
| 3331 |
float delta = 0.0f; |
| 3332 |
if (ann != NULL) |
| 3333 |
{ |
| 3334 |
delta = fann_get_rprop_delta_min(ann); |
| 3335 |
} |
| 3336 |
return delta; |
| 3337 |
} |
| 3338 |
|
| 3339 |
|
| 3340 |
|
| 3341 |
|
| 3342 |
|
| 3343 |
|
| 3344 |
|
| 3345 |
|
| 3346 |
|
| 3347 |
|
| 3348 |
void set_rprop_delta_min(float rprop_delta_min) |
| 3349 |
{ |
| 3350 |
if (ann != NULL) |
| 3351 |
{ |
| 3352 |
fann_set_rprop_delta_min(ann, rprop_delta_min); |
| 3353 |
} |
| 3354 |
} |
| 3355 |
|
| 3356 |
|
| 3357 |
|
| 3358 |
|
| 3359 |
|
| 3360 |
|
| 3361 |
|
| 3362 |
|
| 3363 |
|
| 3364 |
|
| 3365 |
|
| 3366 |
|
| 3367 |
float get_rprop_delta_max() |
| 3368 |
{ |
| 3369 |
float delta = 0.0f; |
| 3370 |
if (ann != NULL) |
| 3371 |
{ |
| 3372 |
delta = fann_get_rprop_delta_max(ann); |
| 3373 |
} |
| 3374 |
return delta; |
| 3375 |
} |
| 3376 |
|
| 3377 |
|
| 3378 |
|
| 3379 |
|
| 3380 |
|
| 3381 |
|
| 3382 |
|
| 3383 |
|
| 3384 |
|
| 3385 |
|
| 3386 |
void set_rprop_delta_max(float rprop_delta_max) |
| 3387 |
{ |
| 3388 |
if (ann != NULL) |
| 3389 |
{ |
| 3390 |
fann_set_rprop_delta_max(ann, rprop_delta_max); |
| 3391 |
} |
| 3392 |
} |
| 3393 |
|
| 3394 |
|
| 3395 |
|
| 3396 |
|
| 3397 |
|
| 3398 |
|
| 3399 |
|
| 3400 |
unsigned int get_num_input() |
| 3401 |
{ |
| 3402 |
unsigned int num_input = 0; |
| 3403 |
if (ann != NULL) |
| 3404 |
{ |
| 3405 |
num_input = fann_get_num_input(ann); |
| 3406 |
} |
| 3407 |
return num_input; |
| 3408 |
} |
| 3409 |
|
| 3410 |
|
| 3411 |
|
| 3412 |
|
| 3413 |
|
| 3414 |
|
| 3415 |
|
| 3416 |
unsigned int get_num_output() |
| 3417 |
{ |
| 3418 |
unsigned int num_output = 0; |
| 3419 |
if (ann != NULL) |
| 3420 |
{ |
| 3421 |
num_output = fann_get_num_output(ann); |
| 3422 |
} |
| 3423 |
return num_output; |
| 3424 |
} |
| 3425 |
|
| 3426 |
|
| 3427 |
|
| 3428 |
|
| 3429 |
|
| 3430 |
|
| 3431 |
|
| 3432 |
|
| 3433 |
unsigned int get_total_neurons() |
| 3434 |
{ |
| 3435 |
if (ann == NULL) |
| 3436 |
{ |
| 3437 |
return 0; |
| 3438 |
} |
| 3439 |
return fann_get_total_neurons(ann); |
| 3440 |
} |
| 3441 |
|
| 3442 |
|
| 3443 |
|
| 3444 |
|
| 3445 |
|
| 3446 |
|
| 3447 |
|
| 3448 |
unsigned int get_total_connections() |
| 3449 |
{ |
| 3450 |
if (ann == NULL) |
| 3451 |
{ |
| 3452 |
return 0; |
| 3453 |
} |
| 3454 |
return fann_get_total_connections(ann); |
| 3455 |
} |
| 3456 |
|
| 3457 |
#ifdef FIXEDFANN |
| 3458 |
|
| 3459 |
|
| 3460 |
|
| 3461 |
|
| 3462 |
|
| 3463 |
|
| 3464 |
|
| 3465 |
|
| 3466 |
|
| 3467 |
|
| 3468 |
|
| 3469 |
|
| 3470 |
|
| 3471 |
|
| 3472 |
unsigned int get_decimal_point() |
| 3473 |
{ |
| 3474 |
if (ann == NULL) |
| 3475 |
{ |
| 3476 |
return 0; |
| 3477 |
} |
| 3478 |
return fann_get_decimal_point(ann); |
| 3479 |
} |
| 3480 |
|
| 3481 |
|
| 3482 |
|
| 3483 |
|
| 3484 |
|
| 3485 |
|
| 3486 |
|
| 3487 |
|
| 3488 |
|
| 3489 |
|
| 3490 |
|
| 3491 |
|
| 3492 |
|
| 3493 |
|
| 3494 |
|
| 3495 |
|
| 3496 |
|
| 3497 |
|
| 3498 |
|
| 3499 |
unsigned int get_multiplier() |
| 3500 |
{ |
| 3501 |
if (ann == NULL) |
| 3502 |
{ |
| 3503 |
return 0; |
| 3504 |
} |
| 3505 |
return fann_get_multiplier(ann); |
| 3506 |
} |
| 3507 |
#endif |
| 3508 |
|
| 3509 |
|
| 3510 |
|
| 3511 |
|
| 3512 |
|
| 3513 |
|
| 3514 |
|
| 3515 |
|
| 3516 |
|
| 3517 |
|
| 3518 |
|
| 3519 |
|
| 3520 |
|
| 3521 |
|
| 3522 |
|
| 3523 |
network_type_enum get_network_type() |
| 3524 |
{ |
| 3525 |
fann_nettype_enum network_type = FANN_NETTYPE_LAYER; |
| 3526 |
if (ann != NULL) |
| 3527 |
{ |
| 3528 |
network_type = fann_get_network_type(ann); |
| 3529 |
} |
| 3530 |
return static_cast<network_type_enum>(network_type); |
| 3531 |
} |
| 3532 |
|
| 3533 |
|
| 3534 |
|
| 3535 |
|
| 3536 |
|
| 3537 |
|
| 3538 |
|
| 3539 |
|
| 3540 |
|
| 3541 |
|
| 3542 |
|
| 3543 |
|
| 3544 |
|
| 3545 |
float get_connection_rate() |
| 3546 |
{ |
| 3547 |
if (ann == NULL) |
| 3548 |
{ |
| 3549 |
return 0; |
| 3550 |
} |
| 3551 |
return fann_get_connection_rate(ann); |
| 3552 |
} |
| 3553 |
|
| 3554 |
|
| 3555 |
|
| 3556 |
|
| 3557 |
|
| 3558 |
|
| 3559 |
|
| 3560 |
|
| 3561 |
|
| 3562 |
|
| 3563 |
|
| 3564 |
|
| 3565 |
|
| 3566 |
unsigned int get_num_layers() |
| 3567 |
{ |
| 3568 |
if (ann == NULL) |
| 3569 |
{ |
| 3570 |
return 0; |
| 3571 |
} |
| 3572 |
return fann_get_num_layers(ann); |
| 3573 |
} |
| 3574 |
|
| 3575 |
|
| 3576 |
|
| 3577 |
|
| 3578 |
|
| 3579 |
|
| 3580 |
|
| 3581 |
|
| 3582 |
|
| 3583 |
|
| 3584 |
|
| 3585 |
|
| 3586 |
|
| 3587 |
|
| 3588 |
|
| 3589 |
void get_layer_array(unsigned int *layers) |
| 3590 |
{ |
| 3591 |
if (ann != NULL) |
| 3592 |
{ |
| 3593 |
fann_get_layer_array(ann, layers); |
| 3594 |
} |
| 3595 |
} |
| 3596 |
|
| 3597 |
void get_layer_array(std::vector<unsigned int> vlayers) |
| 3598 |
{ |
| 3599 |
vlayers.clear(); |
| 3600 |
if (ann != NULL){ |
| 3601 |
vlayers.resize(get_num_layers()); |
| 3602 |
unsigned int* layers=(unsigned int *)calloc(get_num_layers(),sizeof(unsigned int)); |
| 3603 |
fann_get_layer_array(ann, layers); |
| 3604 |
for(int ilayer=0;ilayer<get_num_layers();++ilayer) |
| 3605 |
vlayers[ilayer]=*(layers++); |
| 3606 |
} |
| 3607 |
} |
| 3608 |
|
| 3609 |
|
| 3610 |
|
| 3611 |
|
| 3612 |
|
| 3613 |
|
| 3614 |
|
| 3615 |
|
| 3616 |
|
| 3617 |
|
| 3618 |
|
| 3619 |
|
| 3620 |
|
| 3621 |
void get_bias_array(unsigned int *bias) |
| 3622 |
{ |
| 3623 |
if (ann != NULL) |
| 3624 |
{ |
| 3625 |
fann_get_bias_array(ann, bias); |
| 3626 |
} |
| 3627 |
} |
| 3628 |
|
| 3629 |
|
| 3630 |
|
| 3631 |
|
| 3632 |
|
| 3633 |
|
| 3634 |
|
| 3635 |
|
| 3636 |
|
| 3637 |
|
| 3638 |
|
| 3639 |
|
| 3640 |
|
| 3641 |
void get_connection_array(connection *connections) |
| 3642 |
{ |
| 3643 |
if (ann != NULL) |
| 3644 |
{ |
| 3645 |
fann_get_connection_array(ann, connections); |
| 3646 |
} |
| 3647 |
} |
| 3648 |
|
| 3649 |
void get_connection_array(std::vector<connection>& convector) |
| 3650 |
{ |
| 3651 |
convector.clear(); |
| 3652 |
if (ann != NULL) |
| 3653 |
{ |
| 3654 |
convector.resize(get_total_connections()); |
| 3655 |
connection* connections=(connection*)calloc(get_total_connections(),sizeof(connection)); |
| 3656 |
fann_get_connection_array(ann, connections); |
| 3657 |
for(int icon=0;icon<get_total_connections();++icon) |
| 3658 |
convector[icon]=*(connections++); |
| 3659 |
} |
| 3660 |
} |
| 3661 |
|
| 3662 |
|
| 3663 |
|
| 3664 |
|
| 3665 |
|
| 3666 |
|
| 3667 |
|
| 3668 |
|
| 3669 |
|
| 3670 |
|
| 3671 |
|
| 3672 |
|
| 3673 |
|
| 3674 |
|
| 3675 |
|
| 3676 |
void set_weight_array(connection *connections, unsigned int num_connections) |
| 3677 |
{ |
| 3678 |
if (ann != NULL) |
| 3679 |
{ |
| 3680 |
fann_set_weight_array(ann, connections, num_connections); |
| 3681 |
} |
| 3682 |
} |
| 3683 |
|
| 3684 |
void set_weight_array(std::vector<connection> convector) |
| 3685 |
{ |
| 3686 |
if (ann != NULL) |
| 3687 |
{ |
| 3688 |
unsigned int num_connections=convector.size(); |
| 3689 |
connection* connections=(connection*)calloc(num_connections,sizeof(connection)); |
| 3690 |
connections=&(convector[0]); |
| 3691 |
fann_set_weight_array(ann, connections, num_connections); |
| 3692 |
} |
| 3693 |
} |
| 3694 |
|
| 3695 |
|
| 3696 |
|
| 3697 |
|
| 3698 |
|
| 3699 |
|
| 3700 |
|
| 3701 |
|
| 3702 |
|
| 3703 |
|
| 3704 |
|
| 3705 |
|
| 3706 |
|
| 3707 |
void set_weight(unsigned int from_neuron, unsigned int to_neuron, fann_type weight) |
| 3708 |
{ |
| 3709 |
if (ann != NULL) |
| 3710 |
{ |
| 3711 |
fann_set_weight(ann, from_neuron, to_neuron, weight); |
| 3712 |
} |
| 3713 |
} |
| 3714 |
|
| 3715 |
|
| 3716 |
|
| 3717 |
|
| 3718 |
|
| 3719 |
|
| 3720 |
|
| 3721 |
|
| 3722 |
|
| 3723 |
|
| 3724 |
|
| 3725 |
|
| 3726 |
|
| 3727 |
|
| 3728 |
|
| 3729 |
|
| 3730 |
|
| 3731 |
|
| 3732 |
|
| 3733 |
float get_learning_momentum() |
| 3734 |
{ |
| 3735 |
float learning_momentum = 0.0f; |
| 3736 |
if (ann != NULL) |
| 3737 |
{ |
| 3738 |
learning_momentum = fann_get_learning_momentum(ann); |
| 3739 |
} |
| 3740 |
return learning_momentum; |
| 3741 |
} |
| 3742 |
|
| 3743 |
|
| 3744 |
|
| 3745 |
|
| 3746 |
|
| 3747 |
|
| 3748 |
|
| 3749 |
|
| 3750 |
|
| 3751 |
void set_learning_momentum(float learning_momentum) |
| 3752 |
{ |
| 3753 |
if (ann != NULL) |
| 3754 |
{ |
| 3755 |
fann_set_learning_momentum(ann, learning_momentum); |
| 3756 |
} |
| 3757 |
} |
| 3758 |
|
| 3759 |
|
| 3760 |
|
| 3761 |
|
| 3762 |
|
| 3763 |
|
| 3764 |
|
| 3765 |
|
| 3766 |
|
| 3767 |
|
| 3768 |
|
| 3769 |
|
| 3770 |
|
| 3771 |
|
| 3772 |
stop_function_enum get_train_stop_function() |
| 3773 |
{ |
| 3774 |
enum fann_stopfunc_enum stopfunc = FANN_STOPFUNC_MSE; |
| 3775 |
if (ann != NULL) |
| 3776 |
{ |
| 3777 |
stopfunc = fann_get_train_stop_function(ann); |
| 3778 |
} |
| 3779 |
return static_cast<stop_function_enum>(stopfunc); |
| 3780 |
} |
| 3781 |
|
| 3782 |
|
| 3783 |
|
| 3784 |
|
| 3785 |
|
| 3786 |
|
| 3787 |
|
| 3788 |
|
| 3789 |
|
| 3790 |
|
| 3791 |
|
| 3792 |
|
| 3793 |
void set_train_stop_function(stop_function_enum train_stop_function) |
| 3794 |
{ |
| 3795 |
if (ann != NULL) |
| 3796 |
{ |
| 3797 |
fann_set_train_stop_function(ann, |
| 3798 |
static_cast<enum fann_stopfunc_enum>(train_stop_function)); |
| 3799 |
} |
| 3800 |
} |
| 3801 |
|
| 3802 |
|
| 3803 |
|
| 3804 |
|
| 3805 |
|
| 3806 |
|
| 3807 |
|
| 3808 |
|
| 3809 |
|
| 3810 |
|
| 3811 |
|
| 3812 |
|
| 3813 |
|
| 3814 |
|
| 3815 |
|
| 3816 |
|
| 3817 |
|
| 3818 |
|
| 3819 |
|
| 3820 |
fann_type get_bit_fail_limit() |
| 3821 |
{ |
| 3822 |
fann_type bit_fail_limit = 0.0f; |
| 3823 |
|
| 3824 |
if (ann != NULL) |
| 3825 |
{ |
| 3826 |
bit_fail_limit = fann_get_bit_fail_limit(ann); |
| 3827 |
} |
| 3828 |
return bit_fail_limit; |
| 3829 |
} |
| 3830 |
|
| 3831 |
|
| 3832 |
|
| 3833 |
|
| 3834 |
|
| 3835 |
|
| 3836 |
|
| 3837 |
|
| 3838 |
|
| 3839 |
|
| 3840 |
void set_bit_fail_limit(fann_type bit_fail_limit) |
| 3841 |
{ |
| 3842 |
if (ann != NULL) |
| 3843 |
{ |
| 3844 |
fann_set_bit_fail_limit(ann, bit_fail_limit); |
| 3845 |
} |
| 3846 |
} |
| 3847 |
|
| 3848 |
|
| 3849 |
|
| 3850 |
|
| 3851 |
|
| 3852 |
|
| 3853 |
|
| 3854 |
|
| 3855 |
|
| 3856 |
|
| 3857 |
|
| 3858 |
|
| 3859 |
|
| 3860 |
|
| 3861 |
|
| 3862 |
|
| 3863 |
unsigned int get_bit_fail() |
| 3864 |
{ |
| 3865 |
unsigned int bit_fail = 0; |
| 3866 |
if (ann != NULL) |
| 3867 |
{ |
| 3868 |
bit_fail = fann_get_bit_fail(ann); |
| 3869 |
} |
| 3870 |
return bit_fail; |
| 3871 |
} |
| 3872 |
|
| 3873 |
|
| 3874 |
|
| 3875 |
|
| 3876 |
|
| 3877 |
|
| 3878 |
|
| 3879 |
|
| 3880 |
|
| 3881 |
|
| 3882 |
|
| 3883 |
|
| 3884 |
|
| 3885 |
|
| 3886 |
|
| 3887 |
|
| 3888 |
|
| 3889 |
|
| 3890 |
|
| 3891 |
|
| 3892 |
|
| 3893 |
|
| 3894 |
|
| 3895 |
|
| 3896 |
|
| 3897 |
|
| 3898 |
|
| 3899 |
|
| 3900 |
|
| 3901 |
|
| 3902 |
|
| 3903 |
|
| 3904 |
void cascadetrain_on_data(const training_data &data, unsigned int max_neurons, |
| 3905 |
unsigned int neurons_between_reports, float desired_error) |
| 3906 |
{ |
| 3907 |
if ((ann != NULL) && (data.train_data != NULL)) |
| 3908 |
{ |
| 3909 |
fann_cascadetrain_on_data(ann, data.train_data, max_neurons, |
| 3910 |
neurons_between_reports, desired_error); |
| 3911 |
} |
| 3912 |
} |
| 3913 |
|
| 3914 |
|
| 3915 |
|
| 3916 |
|
| 3917 |
|
| 3918 |
|
| 3919 |
|
| 3920 |
|
| 3921 |
|
| 3922 |
|
| 3923 |
void cascadetrain_on_file(const std::string &filename, unsigned int max_neurons, |
| 3924 |
unsigned int neurons_between_reports, float desired_error) |
| 3925 |
{ |
| 3926 |
if (ann != NULL) |
| 3927 |
{ |
| 3928 |
fann_cascadetrain_on_file(ann, filename.c_str(), |
| 3929 |
max_neurons, neurons_between_reports, desired_error); |
| 3930 |
} |
| 3931 |
} |
| 3932 |
|
| 3933 |
|
| 3934 |
|
| 3935 |
|
| 3936 |
|
| 3937 |
|
| 3938 |
|
| 3939 |
|
| 3940 |
|
| 3941 |
|
| 3942 |
|
| 3943 |
|
| 3944 |
|
| 3945 |
|
| 3946 |
|
| 3947 |
|
| 3948 |
|
| 3949 |
|
| 3950 |
|
| 3951 |
|
| 3952 |
|
| 3953 |
|
| 3954 |
|
| 3955 |
|
| 3956 |
float get_cascade_output_change_fraction() |
| 3957 |
{ |
| 3958 |
float change_fraction = 0.0f; |
| 3959 |
if (ann != NULL) |
| 3960 |
{ |
| 3961 |
change_fraction = fann_get_cascade_output_change_fraction(ann); |
| 3962 |
} |
| 3963 |
return change_fraction; |
| 3964 |
} |
| 3965 |
|
| 3966 |
|
| 3967 |
|
| 3968 |
|
| 3969 |
|
| 3970 |
|
| 3971 |
|
| 3972 |
|
| 3973 |
|
| 3974 |
|
| 3975 |
void set_cascade_output_change_fraction(float cascade_output_change_fraction) |
| 3976 |
{ |
| 3977 |
if (ann != NULL) |
| 3978 |
{ |
| 3979 |
fann_set_cascade_output_change_fraction(ann, cascade_output_change_fraction); |
| 3980 |
} |
| 3981 |
} |
| 3982 |
|
| 3983 |
|
| 3984 |
|
| 3985 |
|
| 3986 |
|
| 3987 |
|
| 3988 |
|
| 3989 |
|
| 3990 |
|
| 3991 |
|
| 3992 |
|
| 3993 |
|
| 3994 |
|
| 3995 |
|
| 3996 |
|
| 3997 |
|
| 3998 |
unsigned int get_cascade_output_stagnation_epochs() |
| 3999 |
{ |
| 4000 |
unsigned int stagnation_epochs = 0; |
| 4001 |
if (ann != NULL) |
| 4002 |
{ |
| 4003 |
stagnation_epochs = fann_get_cascade_output_stagnation_epochs(ann); |
| 4004 |
} |
| 4005 |
return stagnation_epochs; |
| 4006 |
} |
| 4007 |
|
| 4008 |
|
| 4009 |
|
| 4010 |
|
| 4011 |
|
| 4012 |
|
| 4013 |
|
| 4014 |
|
| 4015 |
|
| 4016 |
|
| 4017 |
void set_cascade_output_stagnation_epochs(unsigned int cascade_output_stagnation_epochs) |
| 4018 |
{ |
| 4019 |
if (ann != NULL) |
| 4020 |
{ |
| 4021 |
fann_set_cascade_output_stagnation_epochs(ann, cascade_output_stagnation_epochs); |
| 4022 |
} |
| 4023 |
} |
| 4024 |
|
| 4025 |
|
| 4026 |
|
| 4027 |
|
| 4028 |
|
| 4029 |
|
| 4030 |
|
| 4031 |
|
| 4032 |
|
| 4033 |
|
| 4034 |
|
| 4035 |
|
| 4036 |
|
| 4037 |
|
| 4038 |
|
| 4039 |
|
| 4040 |
|
| 4041 |
|
| 4042 |
|
| 4043 |
|
| 4044 |
|
| 4045 |
|
| 4046 |
|
| 4047 |
|
| 4048 |
float get_cascade_candidate_change_fraction() |
| 4049 |
{ |
| 4050 |
float change_fraction = 0.0f; |
| 4051 |
if (ann != NULL) |
| 4052 |
{ |
| 4053 |
change_fraction = fann_get_cascade_candidate_change_fraction(ann); |
| 4054 |
} |
| 4055 |
return change_fraction; |
| 4056 |
} |
| 4057 |
|
| 4058 |
|
| 4059 |
|
| 4060 |
|
| 4061 |
|
| 4062 |
|
| 4063 |
|
| 4064 |
|
| 4065 |
|
| 4066 |
|
| 4067 |
|
| 4068 |
void set_cascade_candidate_change_fraction(float cascade_candidate_change_fraction) |
| 4069 |
{ |
| 4070 |
if (ann != NULL) |
| 4071 |
{ |
| 4072 |
fann_set_cascade_candidate_change_fraction(ann, cascade_candidate_change_fraction); |
| 4073 |
} |
| 4074 |
} |
| 4075 |
|
| 4076 |
|
| 4077 |
|
| 4078 |
|
| 4079 |
|
| 4080 |
|
| 4081 |
|
| 4082 |
|
| 4083 |
|
| 4084 |
|
| 4085 |
|
| 4086 |
|
| 4087 |
|
| 4088 |
|
| 4089 |
|
| 4090 |
|
| 4091 |
unsigned int get_cascade_candidate_stagnation_epochs() |
| 4092 |
{ |
| 4093 |
unsigned int stagnation_epochs = 0; |
| 4094 |
if (ann != NULL) |
| 4095 |
{ |
| 4096 |
stagnation_epochs = fann_get_cascade_candidate_stagnation_epochs(ann); |
| 4097 |
} |
| 4098 |
return stagnation_epochs; |
| 4099 |
} |
| 4100 |
|
| 4101 |
|
| 4102 |
|
| 4103 |
|
| 4104 |
|
| 4105 |
|
| 4106 |
|
| 4107 |
|
| 4108 |
|
| 4109 |
|
| 4110 |
|
| 4111 |
void set_cascade_candidate_stagnation_epochs(unsigned int cascade_candidate_stagnation_epochs) |
| 4112 |
{ |
| 4113 |
if (ann != NULL) |
| 4114 |
{ |
| 4115 |
fann_set_cascade_candidate_stagnation_epochs(ann, cascade_candidate_stagnation_epochs); |
| 4116 |
} |
| 4117 |
} |
| 4118 |
|
| 4119 |
|
| 4120 |
|
| 4121 |
|
| 4122 |
|
| 4123 |
|
| 4124 |
|
| 4125 |
|
| 4126 |
|
| 4127 |
|
| 4128 |
|
| 4129 |
|
| 4130 |
|
| 4131 |
|
| 4132 |
fann_type get_cascade_weight_multiplier() |
| 4133 |
{ |
| 4134 |
fann_type weight_multiplier = 0; |
| 4135 |
if (ann != NULL) |
| 4136 |
{ |
| 4137 |
weight_multiplier = fann_get_cascade_weight_multiplier(ann); |
| 4138 |
} |
| 4139 |
return weight_multiplier; |
| 4140 |
} |
| 4141 |
|
| 4142 |
|
| 4143 |
|
| 4144 |
|
| 4145 |
|
| 4146 |
|
| 4147 |
|
| 4148 |
|
| 4149 |
|
| 4150 |
|
| 4151 |
void set_cascade_weight_multiplier(fann_type cascade_weight_multiplier) |
| 4152 |
{ |
| 4153 |
if (ann != NULL) |
| 4154 |
{ |
| 4155 |
fann_set_cascade_weight_multiplier(ann, cascade_weight_multiplier); |
| 4156 |
} |
| 4157 |
} |
| 4158 |
|
| 4159 |
|
| 4160 |
|
| 4161 |
|
| 4162 |
|
| 4163 |
|
| 4164 |
|
| 4165 |
|
| 4166 |
|
| 4167 |
|
| 4168 |
|
| 4169 |
|
| 4170 |
|
| 4171 |
|
| 4172 |
|
| 4173 |
|
| 4174 |
fann_type get_cascade_candidate_limit() |
| 4175 |
{ |
| 4176 |
fann_type candidate_limit = 0; |
| 4177 |
if (ann != NULL) |
| 4178 |
{ |
| 4179 |
candidate_limit = fann_get_cascade_candidate_limit(ann); |
| 4180 |
} |
| 4181 |
return candidate_limit; |
| 4182 |
} |
| 4183 |
|
| 4184 |
|
| 4185 |
|
| 4186 |
|
| 4187 |
|
| 4188 |
|
| 4189 |
|
| 4190 |
|
| 4191 |
|
| 4192 |
|
| 4193 |
void set_cascade_candidate_limit(fann_type cascade_candidate_limit) |
| 4194 |
{ |
| 4195 |
if (ann != NULL) |
| 4196 |
{ |
| 4197 |
fann_set_cascade_candidate_limit(ann, cascade_candidate_limit); |
| 4198 |
} |
| 4199 |
} |
| 4200 |
|
| 4201 |
|
| 4202 |
|
| 4203 |
|
| 4204 |
|
| 4205 |
|
| 4206 |
|
| 4207 |
|
| 4208 |
|
| 4209 |
|
| 4210 |
|
| 4211 |
|
| 4212 |
|
| 4213 |
unsigned int get_cascade_max_out_epochs() |
| 4214 |
{ |
| 4215 |
unsigned int max_out_epochs = 0; |
| 4216 |
if (ann != NULL) |
| 4217 |
{ |
| 4218 |
max_out_epochs = fann_get_cascade_max_out_epochs(ann); |
| 4219 |
} |
| 4220 |
return max_out_epochs; |
| 4221 |
} |
| 4222 |
|
| 4223 |
|
| 4224 |
|
| 4225 |
|
| 4226 |
|
| 4227 |
|
| 4228 |
|
| 4229 |
|
| 4230 |
|
| 4231 |
|
| 4232 |
void set_cascade_max_out_epochs(unsigned int cascade_max_out_epochs) |
| 4233 |
{ |
| 4234 |
if (ann != NULL) |
| 4235 |
{ |
| 4236 |
fann_set_cascade_max_out_epochs(ann, cascade_max_out_epochs); |
| 4237 |
} |
| 4238 |
} |
| 4239 |
|
| 4240 |
|
| 4241 |
|
| 4242 |
|
| 4243 |
|
| 4244 |
|
| 4245 |
|
| 4246 |
|
| 4247 |
|
| 4248 |
|
| 4249 |
|
| 4250 |
|
| 4251 |
|
| 4252 |
unsigned int get_cascade_max_cand_epochs() |
| 4253 |
{ |
| 4254 |
unsigned int max_cand_epochs = 0; |
| 4255 |
if (ann != NULL) |
| 4256 |
{ |
| 4257 |
max_cand_epochs = fann_get_cascade_max_cand_epochs(ann); |
| 4258 |
} |
| 4259 |
return max_cand_epochs; |
| 4260 |
} |
| 4261 |
|
| 4262 |
|
| 4263 |
|
| 4264 |
|
| 4265 |
|
| 4266 |
|
| 4267 |
|
| 4268 |
|
| 4269 |
|
| 4270 |
|
| 4271 |
void set_cascade_max_cand_epochs(unsigned int cascade_max_cand_epochs) |
| 4272 |
{ |
| 4273 |
if (ann != NULL) |
| 4274 |
{ |
| 4275 |
fann_set_cascade_max_cand_epochs(ann, cascade_max_cand_epochs); |
| 4276 |
} |
| 4277 |
} |
| 4278 |
|
| 4279 |
|
| 4280 |
|
| 4281 |
|
| 4282 |
|
| 4283 |
|
| 4284 |
|
| 4285 |
|
| 4286 |
|
| 4287 |
|
| 4288 |
|
| 4289 |
|
| 4290 |
|
| 4291 |
|
| 4292 |
|
| 4293 |
|
| 4294 |
|
| 4295 |
|
| 4296 |
|
| 4297 |
|
| 4298 |
|
| 4299 |
|
| 4300 |
|
| 4301 |
|
| 4302 |
unsigned int get_cascade_num_candidates() |
| 4303 |
{ |
| 4304 |
unsigned int num_candidates = 0; |
| 4305 |
if (ann != NULL) |
| 4306 |
{ |
| 4307 |
num_candidates = fann_get_cascade_num_candidates(ann); |
| 4308 |
} |
| 4309 |
return num_candidates; |
| 4310 |
} |
| 4311 |
|
| 4312 |
|
| 4313 |
|
| 4314 |
|
| 4315 |
|
| 4316 |
|
| 4317 |
|
| 4318 |
|
| 4319 |
|
| 4320 |
|
| 4321 |
|
| 4322 |
|
| 4323 |
|
| 4324 |
unsigned int get_cascade_activation_functions_count() |
| 4325 |
{ |
| 4326 |
unsigned int activation_functions_count = 0; |
| 4327 |
if (ann != NULL) |
| 4328 |
{ |
| 4329 |
activation_functions_count = fann_get_cascade_activation_functions_count(ann); |
| 4330 |
} |
| 4331 |
return activation_functions_count; |
| 4332 |
} |
| 4333 |
|
| 4334 |
|
| 4335 |
|
| 4336 |
|
| 4337 |
|
| 4338 |
|
| 4339 |
|
| 4340 |
|
| 4341 |
|
| 4342 |
|
| 4343 |
|
| 4344 |
|
| 4345 |
|
| 4346 |
|
| 4347 |
|
| 4348 |
activation_function_enum * get_cascade_activation_functions() |
| 4349 |
{ |
| 4350 |
enum fann_activationfunc_enum *activation_functions = NULL; |
| 4351 |
if (ann != NULL) |
| 4352 |
{ |
| 4353 |
activation_functions = fann_get_cascade_activation_functions(ann); |
| 4354 |
} |
| 4355 |
return reinterpret_cast<activation_function_enum *>(activation_functions); |
| 4356 |
} |
| 4357 |
|
| 4358 |
|
| 4359 |
|
| 4360 |
|
| 4361 |
|
| 4362 |
|
| 4363 |
|
| 4364 |
|
| 4365 |
|
| 4366 |
|
| 4367 |
|
| 4368 |
|
| 4369 |
|
| 4370 |
|
| 4371 |
|
| 4372 |
void set_cascade_activation_functions(activation_function_enum *cascade_activation_functions, |
| 4373 |
unsigned int cascade_activation_functions_count) |
| 4374 |
{ |
| 4375 |
if (ann != NULL) |
| 4376 |
{ |
| 4377 |
fann_set_cascade_activation_functions(ann, |
| 4378 |
reinterpret_cast<enum fann_activationfunc_enum *>(cascade_activation_functions), |
| 4379 |
cascade_activation_functions_count); |
| 4380 |
} |
| 4381 |
} |
| 4382 |
|
| 4383 |
|
| 4384 |
|
| 4385 |
|
| 4386 |
|
| 4387 |
|
| 4388 |
|
| 4389 |
|
| 4390 |
|
| 4391 |
|
| 4392 |
|
| 4393 |
|
| 4394 |
|
| 4395 |
unsigned int get_cascade_activation_steepnesses_count() |
| 4396 |
{ |
| 4397 |
unsigned int activation_steepness_count = 0; |
| 4398 |
if (ann != NULL) |
| 4399 |
{ |
| 4400 |
activation_steepness_count = fann_get_cascade_activation_steepnesses_count(ann); |
| 4401 |
} |
| 4402 |
return activation_steepness_count; |
| 4403 |
} |
| 4404 |
|
| 4405 |
|
| 4406 |
|
| 4407 |
|
| 4408 |
|
| 4409 |
|
| 4410 |
|
| 4411 |
|
| 4412 |
|
| 4413 |
|
| 4414 |
|
| 4415 |
|
| 4416 |
|
| 4417 |
|
| 4418 |
|
| 4419 |
|
| 4420 |
|
| 4421 |
fann_type *get_cascade_activation_steepnesses() |
| 4422 |
{ |
| 4423 |
fann_type *activation_steepnesses = NULL; |
| 4424 |
if (ann != NULL) |
| 4425 |
{ |
| 4426 |
activation_steepnesses = fann_get_cascade_activation_steepnesses(ann); |
| 4427 |
} |
| 4428 |
return activation_steepnesses; |
| 4429 |
} |
| 4430 |
|
| 4431 |
|
| 4432 |
|
| 4433 |
|
| 4434 |
|
| 4435 |
|
| 4436 |
|
| 4437 |
|
| 4438 |
|
| 4439 |
|
| 4440 |
|
| 4441 |
|
| 4442 |
|
| 4443 |
|
| 4444 |
|
| 4445 |
void set_cascade_activation_steepnesses(fann_type *cascade_activation_steepnesses, |
| 4446 |
unsigned int cascade_activation_steepnesses_count) |
| 4447 |
{ |
| 4448 |
if (ann != NULL) |
| 4449 |
{ |
| 4450 |
fann_set_cascade_activation_steepnesses(ann, |
| 4451 |
cascade_activation_steepnesses, cascade_activation_steepnesses_count); |
| 4452 |
} |
| 4453 |
} |
| 4454 |
|
| 4455 |
|
| 4456 |
|
| 4457 |
|
| 4458 |
|
| 4459 |
|
| 4460 |
|
| 4461 |
|
| 4462 |
|
| 4463 |
|
| 4464 |
|
| 4465 |
|
| 4466 |
|
| 4467 |
|
| 4468 |
|
| 4469 |
|
| 4470 |
|
| 4471 |
|
| 4472 |
unsigned int get_cascade_num_candidate_groups() |
| 4473 |
{ |
| 4474 |
unsigned int num_candidate_groups = 0; |
| 4475 |
if (ann != NULL) |
| 4476 |
{ |
| 4477 |
num_candidate_groups = fann_get_cascade_num_candidate_groups(ann); |
| 4478 |
} |
| 4479 |
return num_candidate_groups; |
| 4480 |
} |
| 4481 |
|
| 4482 |
|
| 4483 |
|
| 4484 |
|
| 4485 |
|
| 4486 |
|
| 4487 |
|
| 4488 |
|
| 4489 |
|
| 4490 |
|
| 4491 |
void set_cascade_num_candidate_groups(unsigned int cascade_num_candidate_groups) |
| 4492 |
{ |
| 4493 |
if (ann != NULL) |
| 4494 |
{ |
| 4495 |
fann_set_cascade_num_candidate_groups(ann, cascade_num_candidate_groups); |
| 4496 |
} |
| 4497 |
} |
| 4498 |
|
| 4499 |
|
| 4500 |
|
| 4501 |
#ifndef FIXEDFANN |
| 4502 |
|
| 4503 |
|
| 4504 |
|
| 4505 |
|
| 4506 |
|
| 4507 |
|
| 4508 |
|
| 4509 |
|
| 4510 |
|
| 4511 |
void scale_train(training_data &data) |
| 4512 |
{ |
| 4513 |
if (ann != NULL) |
| 4514 |
{ |
| 4515 |
fann_scale_train(ann, data.train_data); |
| 4516 |
} |
| 4517 |
} |
| 4518 |
|
| 4519 |
|
| 4520 |
|
| 4521 |
|
| 4522 |
|
| 4523 |
|
| 4524 |
|
| 4525 |
|
| 4526 |
|
| 4527 |
|
| 4528 |
void descale_train(training_data &data) |
| 4529 |
{ |
| 4530 |
if (ann != NULL) |
| 4531 |
{ |
| 4532 |
fann_descale_train(ann, data.train_data); |
| 4533 |
} |
| 4534 |
} |
| 4535 |
|
| 4536 |
|
| 4537 |
|
| 4538 |
|
| 4539 |
|
| 4540 |
|
| 4541 |
|
| 4542 |
|
| 4543 |
|
| 4544 |
|
| 4545 |
bool set_input_scaling_params(const training_data &data, float new_input_min, float new_input_max) |
| 4546 |
{ |
| 4547 |
bool status = false; |
| 4548 |
if (ann != NULL) |
| 4549 |
{ |
| 4550 |
status = (fann_set_input_scaling_params(ann, data.train_data, new_input_min, new_input_max) != -1); |
| 4551 |
} |
| 4552 |
return status; |
| 4553 |
} |
| 4554 |
|
| 4555 |
|
| 4556 |
|
| 4557 |
|
| 4558 |
|
| 4559 |
|
| 4560 |
|
| 4561 |
|
| 4562 |
|
| 4563 |
|
| 4564 |
bool set_output_scaling_params(const training_data &data, float new_output_min, float new_output_max) |
| 4565 |
{ |
| 4566 |
bool status = false; |
| 4567 |
if (ann != NULL) |
| 4568 |
{ |
| 4569 |
status = (fann_set_output_scaling_params(ann, data.train_data, new_output_min, new_output_max) != -1); |
| 4570 |
} |
| 4571 |
return status; |
| 4572 |
} |
| 4573 |
|
| 4574 |
|
| 4575 |
|
| 4576 |
|
| 4577 |
|
| 4578 |
|
| 4579 |
|
| 4580 |
|
| 4581 |
|
| 4582 |
|
| 4583 |
bool set_scaling_params(const training_data &data, |
| 4584 |
float new_input_min, float new_input_max, float new_output_min, float new_output_max) |
| 4585 |
{ |
| 4586 |
bool status = false; |
| 4587 |
if (ann != NULL) |
| 4588 |
{ |
| 4589 |
status = (fann_set_scaling_params(ann, data.train_data, |
| 4590 |
new_input_min, new_input_max, new_output_min, new_output_max) != -1); |
| 4591 |
} |
| 4592 |
return status; |
| 4593 |
} |
| 4594 |
|
| 4595 |
|
| 4596 |
|
| 4597 |
|
| 4598 |
|
| 4599 |
|
| 4600 |
|
| 4601 |
|
| 4602 |
|
| 4603 |
|
| 4604 |
bool clear_scaling_params() |
| 4605 |
{ |
| 4606 |
bool status = false; |
| 4607 |
if (ann != NULL) |
| 4608 |
{ |
| 4609 |
status = (fann_clear_scaling_params(ann) != -1); |
| 4610 |
} |
| 4611 |
return status; |
| 4612 |
} |
| 4613 |
|
| 4614 |
|
| 4615 |
|
| 4616 |
|
| 4617 |
|
| 4618 |
|
| 4619 |
|
| 4620 |
|
| 4621 |
|
| 4622 |
|
| 4623 |
void scale_input(fann_type *input_vector) |
| 4624 |
{ |
| 4625 |
if (ann != NULL) |
| 4626 |
{ |
| 4627 |
fann_scale_input(ann, input_vector ); |
| 4628 |
} |
| 4629 |
} |
| 4630 |
|
| 4631 |
|
| 4632 |
|
| 4633 |
|
| 4634 |
|
| 4635 |
|
| 4636 |
|
| 4637 |
|
| 4638 |
|
| 4639 |
|
| 4640 |
void scale_output(fann_type *output_vector) |
| 4641 |
{ |
| 4642 |
if (ann != NULL) |
| 4643 |
{ |
| 4644 |
fann_scale_output(ann, output_vector ); |
| 4645 |
} |
| 4646 |
} |
| 4647 |
|
| 4648 |
|
| 4649 |
|
| 4650 |
|
| 4651 |
|
| 4652 |
|
| 4653 |
|
| 4654 |
|
| 4655 |
|
| 4656 |
|
| 4657 |
void descale_input(fann_type *input_vector) |
| 4658 |
{ |
| 4659 |
if (ann != NULL) |
| 4660 |
{ |
| 4661 |
fann_descale_input(ann, input_vector ); |
| 4662 |
} |
| 4663 |
} |
| 4664 |
|
| 4665 |
|
| 4666 |
|
| 4667 |
|
| 4668 |
|
| 4669 |
|
| 4670 |
|
| 4671 |
|
| 4672 |
|
| 4673 |
|
| 4674 |
void descale_output(fann_type *output_vector) |
| 4675 |
{ |
| 4676 |
if (ann != NULL) |
| 4677 |
{ |
| 4678 |
fann_descale_output(ann, output_vector ); |
| 4679 |
} |
| 4680 |
} |
| 4681 |
|
| 4682 |
#endif |
| 4683 |
|
| 4684 |
|
| 4685 |
|
| 4686 |
|
| 4687 |
|
| 4688 |
|
| 4689 |
|
| 4690 |
|
| 4691 |
|
| 4692 |
|
| 4693 |
|
| 4694 |
|
| 4695 |
|
| 4696 |
|
| 4697 |
|
| 4698 |
|
| 4699 |
|
| 4700 |
|
| 4701 |
|
| 4702 |
|
| 4703 |
|
| 4704 |
void set_error_log(FILE *log_file) |
| 4705 |
{ |
| 4706 |
fann_set_error_log(reinterpret_cast<struct fann_error *>(ann), log_file); |
| 4707 |
} |
| 4708 |
|
| 4709 |
|
| 4710 |
|
| 4711 |
|
| 4712 |
|
| 4713 |
|
| 4714 |
|
| 4715 |
|
| 4716 |
|
| 4717 |
|
| 4718 |
unsigned int get_errno() |
| 4719 |
{ |
| 4720 |
return fann_get_errno(reinterpret_cast<struct fann_error *>(ann)); |
| 4721 |
} |
| 4722 |
|
| 4723 |
|
| 4724 |
|
| 4725 |
|
| 4726 |
|
| 4727 |
|
| 4728 |
|
| 4729 |
void reset_errno() |
| 4730 |
{ |
| 4731 |
fann_reset_errno(reinterpret_cast<struct fann_error *>(ann)); |
| 4732 |
} |
| 4733 |
|
| 4734 |
|
| 4735 |
|
| 4736 |
|
| 4737 |
|
| 4738 |
|
| 4739 |
|
| 4740 |
void reset_errstr() |
| 4741 |
{ |
| 4742 |
fann_reset_errstr(reinterpret_cast<struct fann_error *>(ann)); |
| 4743 |
} |
| 4744 |
|
| 4745 |
|
| 4746 |
|
| 4747 |
|
| 4748 |
|
| 4749 |
|
| 4750 |
|
| 4751 |
|
| 4752 |
|
| 4753 |
std::string get_errstr() |
| 4754 |
{ |
| 4755 |
return std::string(fann_get_errstr(reinterpret_cast<struct fann_error *>(ann))); |
| 4756 |
} |
| 4757 |
|
| 4758 |
|
| 4759 |
|
| 4760 |
|
| 4761 |
|
| 4762 |
|
| 4763 |
|
| 4764 |
void print_error() |
| 4765 |
{ |
| 4766 |
fann_print_error(reinterpret_cast<struct fann_error *>(ann)); |
| 4767 |
} |
| 4768 |
|
| 4769 |
|
| 4770 |
|
| 4771 |
private: |
| 4772 |
|
| 4773 |
typedef struct user_context_type |
| 4774 |
{ |
| 4775 |
callback_type user_callback; |
| 4776 |
void *user_data; |
| 4777 |
neural_net *net; |
| 4778 |
} user_context; |
| 4779 |
|
| 4780 |
|
| 4781 |
static int FANN_API internal_callback(struct fann *ann, struct fann_train_data *train, |
| 4782 |
unsigned int max_epochs, unsigned int epochs_between_reports, float desired_error, unsigned int epochs) |
| 4783 |
{ |
| 4784 |
user_context *user_data = static_cast<user_context *>(fann_get_user_data(ann)); |
| 4785 |
if (user_data != NULL) |
| 4786 |
{ |
| 4787 |
FANN::training_data data; |
| 4788 |
data.train_data = train; |
| 4789 |
|
| 4790 |
int result = (*user_data->user_callback)(*user_data->net, |
| 4791 |
data, max_epochs, epochs_between_reports, desired_error, epochs, user_data); |
| 4792 |
|
| 4793 |
data.train_data = NULL; |
| 4794 |
return result; |
| 4795 |
} |
| 4796 |
else |
| 4797 |
{ |
| 4798 |
return -1; |
| 4799 |
} |
| 4800 |
} |
| 4801 |
protected: |
| 4802 |
|
| 4803 |
struct fann *ann; |
| 4804 |
}; |
| 4805 |
|
| 4806 |
|
| 4807 |
}; |
| 4808 |
|
| 4809 |
#endif |