BlackCat_Tensors
A GPU-supported autograd and linear algebra library, designed for neural network construction
layer_loader.h
Go to the documentation of this file.
1 /*
2  * Layer_Loader.h
3  *
4  * Created on: Aug 13, 2019
5  * Author: joseph
6  */
7 
8 #ifndef LAYER_LOADER_H_
9 #define LAYER_LOADER_H_
10 
11 #include <string>
12 #include <ostream>
13 
14 namespace bc {
15 namespace nn {
16 
17 namespace fs = bc::filesystem;
18 
19 struct Layer_Loader {
20 
24 
25  Layer_Loader(string root_directory):
26  root_directory(root_directory) {}
27 
28  void set_current_layer_name(string current_layername) {
29  this->current_layername = current_layername;
30  }
31 
32  void set_current_layer_index(int layer_index) {
33  this->current_layer_index = layer_index;
34  }
35 
36  string current_layer_subdir() const
37  {
38  string index = std::to_string(current_layer_index);
39  string subdir = "l" + index + "_" + current_layername;
40  return fs::make_path(root_directory, subdir);
41  }
42 
43  template<class T>
44  void save_variable(const T& tensor, string variable_name) {
45  std::ofstream output(path_from_args(tensor, variable_name));
46  output << tensor.to_raw_string();
47  }
48 
49  template<class T>
50  void load_variable(T& tensor, string variable_name) {
51  load_variable(tensor, variable_name, Integer<T::tensor_dim>());
52  }
53 
54  template<class Tensor>
55  void load_variable(Tensor& tensor, string variable_name, Integer<1>)
56  {
57  using value_type = typename Tensor::value_type;
58  string filename = path_from_args(tensor, variable_name);
59  auto descriptor = bc::io::csv_descriptor(filename).header(false);
60  tensor = Tensor(bc::io::read_uniform<value_type>(descriptor, tensor.get_allocator()).row(0));
61  }
62 
63  template<class T>
64  void load_variable(T& tensor, string variable_name, Integer<2>) {
65  using value_type = typename T::value_type;
66  auto descriptor = bc::io::csv_descriptor(path_from_args(tensor, variable_name)).header(false);
67  tensor = bc::io::read_uniform<value_type>(descriptor, tensor.get_allocator());
68  }
69 
70  template<class T, int X>
71  void load_variable(T& tensor, string variable_name, Integer<X>) {
72  using value_type = typename T::value_type;
73  auto descriptor = bc::io::csv_descriptor(path_from_args(tensor, variable_name)).header(false);
74  auto csv_mat = bc::io::read_uniform<value_type>(descriptor, tensor.get_allocator());
75  tensor = csv_mat.reshaped(tensor.inner_shape());
76  }
77 
79  {
82  }
83 
84 private:
85 
86  string path_from_args(int dim, string variable_name)
87  {
88  string extension = dim_to_extension(dim);
89  string directory = current_layer_subdir();
90  return fs::make_path(directory, variable_name + "." + extension);
91  }
92 
93  template<class T>
94  string path_from_args(const T& tensor, string variable_name) {
95  return path_from_args(T::tensor_dim, variable_name);
96  }
97 
98 public:
99 
100  bool file_exists(int dim, string filename) {
101  return fs::file_exists(path_from_args(dim, filename));
102  }
103 
104 
105  static string dim_to_extension(int dim) {
106  switch(dim) {
107  case 0: return "scl";
108  case 1: return "vec";
109  case 2: return "mat";
110  case 3: return "cube";
111  default: return "t" + std::to_string(dim);
112  }
113  }
114 };
115 
116 
117 }
118 }
119 
120 
121 
122 #undef BC_USE_EXPERIMENTAL_FILE_SYSTEM
123 #endif /* LAYER_LOADER_H_ */
string current_layer_subdir() const
Definition: layer_loader.h:36
Layer_Loader(string root_directory)
Definition: layer_loader.h:25
int mkdir(const std::string &name)
Definition: filesystem.h:31
Definition: constexpr_int.h:14
Definition: layer_loader.h:19
allocator_type get_allocator() const
Definition: array.h:51
bc::string make_path(const bc::string &path)
Definition: filesystem.h:40
bc::tensors::Tensor_Base< bc::tensors::exprs::Array< bc::Shape< dim >, ValueType, Allocator > > Tensor
Definition: tensors.h:39
bool directory_exists(const std::string &name)
Definition: filesystem.h:21
void save_variable(const T &tensor, string variable_name)
Definition: layer_loader.h:44
BCINLINE auto dim(const Integers &... ints)
Definition: dim.h:336
string root_directory
Definition: layer_loader.h:21
std::string to_string(int precision=8, bool pretty=true, bool sparse=false) const
Definition: tensor_utility.h:34
Definition: io.h:50
int current_layer_index
Definition: layer_loader.h:23
void set_current_layer_index(int layer_index)
Definition: layer_loader.h:32
csv_descriptor & header(bool header)
Definition: io.h:65
bool file_exists(int dim, string filename)
Definition: layer_loader.h:100
Definition: filesystem.h:11
void load_variable(T &tensor, string variable_name, Integer< 2 >)
Definition: layer_loader.h:64
void make_current_directory()
Definition: layer_loader.h:78
bool file_exists(const std::string &name)
Definition: filesystem.h:35
void load_variable(T &tensor, string variable_name)
Definition: layer_loader.h:50
string current_layername
Definition: layer_loader.h:22
void load_variable(Tensor &tensor, string variable_name, Integer< 1 >)
Definition: layer_loader.h:55
void set_current_layer_name(string current_layername)
Definition: layer_loader.h:28
static string dim_to_extension(int dim)
Definition: layer_loader.h:105
The Evaluator determines if an expression needs to be greedily optimized.
Definition: algorithms.h:22
void load_variable(T &tensor, string variable_name, Integer< X >)
Definition: layer_loader.h:71