-
Notifications
You must be signed in to change notification settings - Fork 0
/
layer.cpp
executable file
·293 lines (288 loc) · 7.77 KB
/
layer.cpp
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include"layer.h"
layer::layer(const std::vector<std::vector<double>> &weights_,const std::vector<double> &biases_)
{
int lenc = biases_.size();
biases = Eigen::VectorXd(lenc);
int lenr = weights_[0].size();
weights = Eigen::MatrixXd(lenr,lenc);
#pragma omp parallel for shared(biases,lenc)
for (int i = 0; i < lenc; i++)
{
biases(i) = biases_[i];
for (int j = 0; j < lenr; j++)
{
weights(i,j) = weights_[i][j];
}
}
type = "GENERIC_LAYER";
}
//------------------------------------------------------------------------------
layer::layer(const std::vector<std::vector<double>> &weights_)
{
int lenr = weights_.size();
int lenc = weights_[0].size();
weights = Eigen::MatrixXd(lenr,lenc);
#pragma omp parallel for shared(biases,lenc)
for (int i = 0; i < lenc; i++)
{
for (int j = 0; j < lenr; j++)
{
weights(i,j) = weights_[i][j];
}
}
type = "GENERIC_LAYER";
}
//------------------------------------------------------------------------------
layer::layer(const Eigen::MatrixXd &weights_)
{
weights = weights_;
type = "GENERIC_LAYER";
}
//------------------------------------------------------------------------------
layer::layer(const Eigen::MatrixXd &weights_,const std::vector<double> &biases_)
{
int len = biases_.size();
biases = Eigen::VectorXd(len);
#pragma omp parallel for shared(biases,len)
for (int i = 0; i < len; i++)
{
biases(i) = biases_[i];
}
weights = Eigen::MatrixXd(weights_.rows(),weights_.cols());
weights = weights_;
type = "GENERIC_LAYER";
}
//------------------------------------------------------------------------------
layer::layer(const Eigen::MatrixXd &weights_,const Eigen::VectorXd &biases_)
{
biases = biases_;
weights = weights_;
type = "GENERIC_LAYER";
}
//------------------------------------------------------------------------------
layer::layer(const Eigen::MatrixXd &weights_,const Eigen::VectorXd &biases_, int in_dim, int out_dim)
{
int len = biases_.size();
biases = Eigen::VectorXd(len);
#pragma omp parallel for shared(biases,len)
for (int i = 0; i < len; i++)
{
biases(i) = biases_[i];
}
weights = Eigen::MatrixXd(weights_.rows(),weights_.cols());
weights = weights_;
type = "GENERIC_LAYER";
input_dim = in_dim;
output_dim = out_dim;
}
//------------------------------------------------------------------------------
const Eigen::VectorXd layer::get_biases()
{
return biases;
}
//------------------------------------------------------------------------------
const Eigen::MatrixXd layer::get_weights()
{
return weights;
}
//------------------------------------------------------------------------------
double layer::get_weight(int i, int j)
{
return weights(i,j);
}
//------------------------------------------------------------------------------
double layer::get_bias(int i)
{
return biases(i);
}
//------------------------------------------------------------------------------
const Eigen::MatrixXd layer::get_weights_biases_as_mat()
{
int nrows = weights.rows();
int ncols = weights.cols();
Eigen::MatrixXd res(nrows,ncols+1);
#pragma omp parallel for
for (int i = 0; i < nrows; i++)
{
#pragma omp simd
for (int j = 0; j < ncols; j++)
{
res(i,j) = weights(i,j);
}
res(i,ncols) = biases(i);
}
return res;
}
//------------------------------------------------------------------------------
const Eigen::MatrixXd layer::get_weights_biases_as_vec_col_maj()
{
int len = weights.rows()*(weights.cols()+1);
int wrows = weights.rows();
int wcols = weights.cols();
Eigen::VectorXd res(len);
for (int i = 0; i < wrows; i++)
{
#pragma omp simd
for (int j = 0; j < wcols; j++)
{
res(i+i*wcols+j) = weights(i,j);
}
res(i+i*(wcols)+wcols) = biases(i);
}
return res;
}
//------------------------------------------------------------------------------
const Eigen::MatrixXd layer::get_weights_biases_as_vec_row_maj()
{
const int len = weights.rows()*(weights.cols()+1);
const int wrows = weights.rows();
const int wcols = weights.cols();
Eigen::MatrixXd res(1,len);
for (int i = 0; i < wrows; i++)
{
#pragma omp simd
for (int j = 0; j < wcols; j++)
{
res(0,i+i*wcols+j) = weights(i,j);
}
res(0,i+i*(wcols)+wcols) = biases(i);
}
return res;
}
//------------------------------------------------------------------------------
void layer::set_biases(const std::vector<double> & biases_)
{
int len = biases_.size();
biases.resize(len);
#pragma omp parallel for
for (int i = 0; i < len; i++)
{
biases(i) = biases_[i];
}
}
//------------------------------------------------------------------------------
void layer::set_biases(const Eigen::VectorXd & biases_)
{
biases = biases_;
}
//------------------------------------------------------------------------------
void layer::set_weights(const Eigen::MatrixXd & weights_)
{
weights = weights_;
}
//------------------------------------------------------------------------------
void layer::set_weight(int i, int j, double weight_)
{
weights(i,j) = weight_;
}
//------------------------------------------------------------------------------
void layer::set_bias(int i,double bias_)
{
biases(i) = bias_;
}
//------------------------------------------------------------------------------
void layer::set_weights_biases(Eigen::MatrixXd &source)
{
const int nrows = weights.rows();
const int ncols = weights.cols();
for (int i = 0; i < nrows; i++)
{
#pragma omp simd
for (int j = 0; j < ncols; j++)
{
weights(i,j) = source(0,j+i*(ncols+1));
}
biases(i) = source(0,i*(ncols+1)+ncols);
}
}
//------------------------------------------------------------------------------
void layer::set_weights_biases_row_maj(Eigen::MatrixXd &source)
{
const int nrows = weights.rows();
const int ncols = weights.cols();
for (int i = 0; i < nrows; i++)
{
#pragma omp simd
for (int j = 0; j < ncols; j++)
{
weights(i,j) = source(0,j+i*(ncols+1));
}
biases(i) = source(0,i*(ncols+1)+ncols);
}
}
//------------------------------------------------------------------------------
void layer::set_weights_biases_compact(Eigen::MatrixXd &source)
{
int nrows = weights.rows();
int ncols = weights.cols();
for (int i = 0; i < nrows; i++)
{
for (int j = 0; j < ncols; j++)
{
weights(i,j) = source(i,j);
}
biases(i) = source(ncols,i);
}
}
//------------------------------------------------------------------------------
int layer::get_num_nodes()
{
return output_dim;
}
//------------------------------------------------------------------------------
int layer::get_input_size()
{
return input_dim;
}
//------------------------------------------------------------------------------
int layer::get_weights_rows()
{
return weights.rows();
}
//------------------------------------------------------------------------------
int layer::get_weights_cols()
{
return weights.cols();
}
//------------------------------------------------------------------------------
std::string layer::get_type()
{
return type;
}
//------------------------------------------------------------------------------
void layer::transpose_weights()
{
int nrows = weights.rows();
int ncols = weights.cols();
double temp;
#pragma omp parallel for
for (int i = 0; i < ncols; i++)
{
for (int j = 0; j < nrows; j++)
{
temp = weights(i,j);
weights(i,j) = weights(j,i);
weights(j,i)=temp;
}
}
}
//------------------------------------------------------------------------------
void layer::set_exp_max_cut(double exp_max_cut_)
{
exp_max_cut = exp_max_cut_;
}
//------------------------------------------------------------------------------
void layer::set_exp_zero_approx(double exp_zero_approx_)
{
exp_zero_approx = exp_zero_approx_;
}
//------------------------------------------------------------------------------
double layer::get_exp_max_cut()
{
return exp_max_cut;
}
//------------------------------------------------------------------------------
double layer::get_exp_zero_approx()
{
return exp_zero_approx;
}