Learning Curve Plus Plus (LCPP)
algo.h
Go to the documentation of this file.
1 /**
2  * @file algo.h
3  * @author Ozgur Taylan Turan
4  *
5  */
6 
7 #ifndef ALGO_H
8 #define ALGO_H
9 
10 
11 #include "regressors/regressors.h"
13 #include "dimred/dimred.h"
14 #include "nn.h"
15 
16 namespace algo
17 {
18 
19  /*
20  * Save your models easily
21  *
22  * @param filePath -> where to save
23  * @param model -> which model object
24  */
25  template<class MODEL>
26  void save( const std::filesystem::path& filePath,
27  const MODEL& model)
28  {
29  std::ofstream ofs(filePath, std::ios::binary);
30  if (!ofs)
31  throw std::runtime_error("Cannot open file for writing: " + filePath.string());
32 
33  {
34  cereal::BinaryOutputArchive archive(ofs);
35  archive(cereal::make_nvp("model", model));
36  } // archive is destroyed here, ensuring flush
37  }
38 
39  /*
40  * Load your models easily
41  *
42  * @param filePath -> where to save
43  * @param model -> which model object
44  */
45  template<class MODEL>
46  std::shared_ptr<MODEL> load( const std::filesystem::path &filePath )
47  {
48  std::ifstream ifs(filePath, std::ios::binary);
49  if (!ifs)
50  throw std::runtime_error("Cannot open file for reading: " + filePath.string());
51 
52  auto loaded = std::make_shared<MODEL>();
53  {
54  cereal::BinaryInputArchive archive(ifs);
55  archive(cereal::make_nvp("model", *loaded));
56  } // archive destroyed here
57 
58  return loaded;
59  }
60 } //namespace algo
61 #endif