forked from larryniven/nn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtensor-tree.h
190 lines (120 loc) · 5.36 KB
/
tensor-tree.h
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
178
179
180
181
182
183
184
185
186
187
188
189
190
#ifndef TENSOR_TREE_H
#define TENSOR_TREE_H
#include <vector>
#include <memory>
#include "la/la-cpu.h"
#include "autodiff/autodiff.h"
namespace tensor_tree {
struct vertex {
std::string type;
std::shared_ptr<void> data;
std::vector<std::shared_ptr<vertex>> children;
std::string name;
};
std::shared_ptr<vertex> make_tensor(std::string name = "");
template <class vec>
vec& get_data(std::shared_ptr<vertex> const& t)
{
return *std::static_pointer_cast<vec>(t->data);
}
la::cpu::tensor<double>& get_tensor(std::shared_ptr<vertex> p);
la::tensor_like<double>& get_tensor_like(std::shared_ptr<vertex> p);
std::shared_ptr<autodiff::op_t> get_var(std::shared_ptr<vertex> p);
std::vector<std::shared_ptr<vertex>> leaves_pre_order(std::shared_ptr<vertex> root);
void load_tensor(std::shared_ptr<vertex> root, std::istream& is);
void load_tensor(std::shared_ptr<vertex> root, std::string filename);
void save_tensor(std::shared_ptr<vertex> root, std::ostream& os);
void save_tensor(std::shared_ptr<vertex> root, std::string filename);
void resize_as(std::shared_ptr<vertex> p1, std::shared_ptr<vertex> p2);
void axpy(std::shared_ptr<vertex> p1, double d, std::shared_ptr<vertex> p2);
void zero(std::shared_ptr<vertex> p);
double norm(std::shared_ptr<vertex> root);
bool has_nan(std::shared_ptr<vertex> root);
void const_step_update(std::shared_ptr<vertex> param, std::shared_ptr<vertex> grad,
double step_size);
void const_step_update_momentum(std::shared_ptr<vertex> param, std::shared_ptr<vertex> grad,
std::shared_ptr<vertex> opt_data, double momentum, double step_size);
void adagrad_update(std::shared_ptr<vertex> param, std::shared_ptr<vertex> grad,
std::shared_ptr<vertex> accu_grad_sq, double step_size);
void rmsprop_update(std::shared_ptr<vertex> param, std::shared_ptr<vertex> grad,
std::shared_ptr<vertex> opt_data, double decay, double step_size);
void adam_update(std::shared_ptr<vertex> param,
std::shared_ptr<vertex> grad,
std::shared_ptr<vertex> first_moment,
std::shared_ptr<vertex> second_moment,
int& time, double alpha, double beta1, double beta2);
std::shared_ptr<vertex> make_var_tree(autodiff::computation_graph& g,
std::shared_ptr<vertex> root);
std::shared_ptr<vertex> copy_tree(std::shared_ptr<vertex> root);
std::shared_ptr<vertex> shallow_copy(std::shared_ptr<vertex> root);
std::shared_ptr<vertex> deep_copy(std::shared_ptr<vertex> root);
void copy_grad(std::shared_ptr<vertex> result, std::shared_ptr<vertex> var_tree);
void print_tree(std::shared_ptr<vertex> root);
struct optimizer {
virtual ~optimizer();
virtual void update(std::shared_ptr<vertex> grad) = 0;
virtual void save_opt_data(std::ostream& os) const = 0;
virtual void load_opt_data(std::istream& is) = 0;
};
struct const_step_opt
: public optimizer {
std::shared_ptr<vertex> param;
double step_size;
const_step_opt(std::shared_ptr<vertex> param,
double step_size);
virtual void update(std::shared_ptr<vertex> grad) override;
virtual void save_opt_data(std::ostream& os) const override;
virtual void load_opt_data(std::istream& is) override;
};
struct const_step_momentum_opt
: public optimizer {
std::shared_ptr<vertex> param;
double step_size;
double momentum;
std::shared_ptr<vertex> opt_data;
const_step_momentum_opt(std::shared_ptr<vertex> param,
double step_size, double momentum);
virtual void update(std::shared_ptr<vertex> grad) override;
virtual void save_opt_data(std::ostream& os) const override;
virtual void load_opt_data(std::istream& is) override;
};
struct adagrad_opt
: public optimizer {
std::shared_ptr<vertex> param;
double step_size;
std::shared_ptr<vertex> accu_grad_sq;
adagrad_opt(std::shared_ptr<vertex> param,
double step_size);
virtual void update(std::shared_ptr<vertex> grad) override;
virtual void save_opt_data(std::ostream& os) const override;
virtual void load_opt_data(std::istream& is) override;
};
struct rmsprop_opt
: public optimizer {
std::shared_ptr<vertex> param;
double step_size;
double decay;
std::shared_ptr<vertex> accu_grad_sq;
rmsprop_opt(std::shared_ptr<vertex> param,
double step_size, double decay);
virtual void update(std::shared_ptr<vertex> grad) override;
virtual void save_opt_data(std::ostream& os) const override;
virtual void load_opt_data(std::istream& is) override;
};
struct adam_opt
: public optimizer {
std::shared_ptr<vertex> param;
double alpha;
double beta1;
double beta2;
int time;
std::shared_ptr<vertex> first_moment;
std::shared_ptr<vertex> second_moment;
adam_opt(std::shared_ptr<vertex> param,
double alpha, double beta1, double beta2);
virtual void update(std::shared_ptr<vertex> grad) override;
virtual void save_opt_data(std::ostream& os) const override;
virtual void load_opt_data(std::istream& is) override;
};
}
#endif