-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathio.h
More file actions
177 lines (151 loc) · 6.33 KB
/
Copy pathio.h
File metadata and controls
177 lines (151 loc) · 6.33 KB
1
2
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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
174
175
176
177
#ifndef DYNET_IO_H_
#define DYNET_IO_H_
#include <string>
#include <vector>
#include <sstream>
#include <fstream>
#include <iostream>
#include <stdexcept>
#include <unordered_map>
#include <iterator>
#include "dynet/dim.h"
#include "dynet/model.h"
#include "dynet/tensor.h"
#include "dynet/except.h"
#include "dynet/str-util.h"
namespace dynet {
template <class T>
std::ostream& operator<<(std::ostream& os, const std::vector<T> & v) {
for (auto & val : v) os << val << ' ';
return os;
}
template <class T>
std::istream& operator>>(std::istream& is, std::vector<T> & v) {
std::copy(std::istream_iterator<T>(is), std::istream_iterator<T>(), v.begin());
return is;
}
class Saver {
public:
Saver() { }
virtual ~Saver() { }
/**
* @brief Save ParameterCollection
*
* @param model: ParameterCollection object to be saved
* @param key: optional parameter, the name for the ParameterCollection in the saved file. This
* will default to the current name of the ParameterCollection.
* @detail: Let's say we have a ParameterCollection named "/pc1/" containing parameters
* "/pc1/a", "/pc1/b", and "/pc1/c". This will save the parameters with the names as-is
* if `key` is not specified. If `key` is specified as "/pc2/", then the parameters will
* be saved as "/pc2/a", "/pc2/b", and "/pc2/c".
*/
virtual void save(const ParameterCollection & model,
const std::string & key = "") = 0;
/**
* @brief Save Parameter.
*
* @param model: Parameter object to be saved
* @param key: optional parameter, the key for the parameter. This will override the Parameter's
* original name.
*/
virtual void save(const Parameter & param, const std::string & key = "") = 0;
/**
* @brief Save look parameter with key, use internal name if key is not given.
*
* @param model: input LookupParameter object to be saved
* @param key: optional parameter, the key for the parameter. This will override the Parameter's
* original name.
*/
virtual void save(const LookupParameter & param, const std::string & key = "") = 0;
}; // class Saver
class Loader {
public:
Loader() { }
virtual ~Loader() { }
/**
* @brief Populate the parameters of a ParameterCollection.
*
* @param model: The ParameterCollection to be populated.
* @param key: optional parameter, the key corresponding to the ParameterCollection
* @detail: This is the standard way to load parameters of a ParameterCollection from a
* file, and assumes that we have saved an identical ParameterCollection using
* Saver::save(parameter_collection).
* Before calling this function, we assume that the ParameterCollection has
* been fully specified, and all of its Parameters and LookupParameters have been
* created with the proper dimensions. This function will then travel through the
* file and load all parameters with names starting with prefix `key`, and populate
* the Parameters and LookupParameters one-by-one in order. When the function
* terminates, we must have populated all of the parameters in `model`. `key` is
* by default empty, so by default we will load all parameters in the file, but if
* we specify `key` we can load a subset of the parameters.
*
*/
virtual void populate(ParameterCollection & model, const std::string & key = "") = 0;
/**
* @brief Populate independent parameter object with key.
* independent here means it has been saved without a ParameterCollection object
*
* @param param: input/output parameter, the Parameter object to be populated in.
* @param key: optional parameter, the key for loading the parameter
*
*/
virtual void populate(Parameter & param, const std::string & key = "") = 0;
/**
* @brief Populate independent lookup parameter object with key.
* independent here means it has been saved without a LookupParameterCollection object
*
* @param lookup_param: input/output parameter, the LookupParameter object to be populated in.
* @param key: optional parameter, the key for loading the lookup parameter
*
*/
virtual void populate(LookupParameter & lookup_param,
const std::string & key = "") = 0;
/**
* @brief Load parameter into model with key
*
* @param model: input/output parameter, the model to load parameter
* @param key: the key for loading the parameter
* @return: the loaded parameter
*
*/
virtual Parameter load_param(ParameterCollection & model,
const std::string & key) = 0;
/**
* @brief Load lookup parameter into model with key
*
* @param model: input/output parameter, the model to load the lookup parameter
* @param key: the key for loading the lookup parameter
* @return: the loaded lookup parameter
*
*/
virtual LookupParameter load_lookup_param(ParameterCollection & model,
const std::string & key) = 0;
}; // class Loader
class TextFileSaver : public Saver {
public:
TextFileSaver(const std::string & filename, bool append = false);
virtual ~TextFileSaver() { }
void save(const ParameterCollection & model,
const std::string & key = "") override;
void save(const Parameter & param, const std::string & key = "") override;
void save(const LookupParameter & param, const std::string & key = "") override;
protected:
void save(const ParameterStorage & param, const std::string & key = "");
void save(const LookupParameterStorage & param, const std::string & key = "");
std::ofstream datastream;
}; // class TextFileSaver
class TextFileLoader : public Loader {
public:
TextFileLoader(const std::string & filename);
virtual ~TextFileLoader() { }
void populate(ParameterCollection & model, const std::string & key = "") override;
void populate(Parameter & param, const std::string & key = "") override;
void populate(LookupParameter & lookup_param,
const std::string & key = "") override;
Parameter load_param(ParameterCollection & model, const std::string & key) override;
LookupParameter load_lookup_param(ParameterCollection & model, const std::string & key) override;
private:
std::string dataname;
}; // class TextFileLoader
} // namespace dynet
#endif