\brief load tensor data from file. returns true on success, false for failure. tokens equal to the number of tensor-length are read, rest are discarded. if numbers are less, it returns false. if tokes aren't numbers, it will fail.
| 528 | // if numbers are less, it returns false. |
| 529 | // if tokes aren't numbers, it will fail. |
| 530 | bool read(std::string fileName) { |
| 531 | |
| 532 | std::fstream fs; |
| 533 | fs.open(fileName, std::ios::in); |
| 534 | // parameter file could not be opened. |
| 535 | if (!fs.is_open() || fs.fail()) { |
| 536 | throw std::runtime_error("Could not open file " + fileName + "."); |
| 537 | return false; |
| 538 | } |
| 539 | std::cout << "reading file " << fileName |
| 540 | << (this->name().size() ? " for tensor " + this->name() : "") |
| 541 | << ".\n"; |
| 542 | size_t len = this->length(); |
| 543 | |
| 544 | std::string typedStr; |
| 545 | T fNum; |
| 546 | size_t index = 0; |
| 547 | while (std::getline(fs, typedStr)) { |
| 548 | std::stringstream linestream(typedStr); |
| 549 | while (linestream >> fNum) { |
| 550 | if (index >= len) { |
| 551 | break; |
| 552 | } |
| 553 | _mem_layout[index++] = fNum; |
| 554 | } |
| 555 | if (index >= len) { |
| 556 | break; |
| 557 | } |
| 558 | } |
| 559 | fs.close(); |
| 560 | |
| 561 | // parameter file did not have parametres equal to tensor length. |
| 562 | if (index < len) { |
| 563 | return false; |
| 564 | } |
| 565 | |
| 566 | return true; |
| 567 | } |
| 568 | bool write(std::string fileName) { |
| 569 | |
| 570 | std::fstream fs; |