-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
layer.h
672 lines (558 loc) · 22.5 KB
/
layer.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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
/*
Copyright (c) 2013, Taiga Nomi
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the <organization> nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <sstream>
#include <iomanip>
#include <memory>
#include <numeric>
#include <algorithm>
#include <vector>
#include <string>
#include "tiny_cnn/node.h"
#include "tiny_cnn/util/util.h"
#include "tiny_cnn/util/product.h"
#include "tiny_cnn/util/image.h"
#include "tiny_cnn/util/weight_init.h"
#include "tiny_cnn/optimizers/optimizer.h"
#include "tiny_cnn/activations/activation_function.h"
namespace tiny_cnn {
/**
* base class of all kind of NN layers
*
* sub-class should override these methods:
* - forward_propagation ... body of forward-pass calculation
* - back_propagation ... body of backward-pass calculation
* - in_shape ... specify input data shapes
* - out_shape ... specify output data shapes
* - layer_type ... name of layer
**/
class layer : public node {
public:
friend void connection_mismatch(const layer& from,
const layer& to);
virtual ~layer() = default;
/**
* construct N-input, M-output layer
* @param in_type[N] type of input vector (data, weight, bias...)
* @param out_type[M] type of output vector
**/
layer(const std::vector<vector_type>& in_type,
const std::vector<vector_type>& out_type)
: node(in_type.size(), out_type.size()),
initialized_(false),
parallelize_(true),
in_channels_(in_type.size()),
out_channels_(out_type.size()),
in_type_(in_type),
out_type_(out_type) {
weight_init_ = std::make_shared<weight_init::xavier>();
bias_init_ = std::make_shared<weight_init::constant>();
}
layer(const layer&) = default;
layer &operator =(const layer&) = default;
#if !defined(_MSC_VER) || (_MSC_VER >= 1900) // default generation of move constructor is unsupported in VS2013
layer(layer&&) = default;
layer &operator = (layer&&) = default;
#endif
void set_parallelize(bool parallelize) {
parallelize_ = parallelize;
}
/////////////////////////////////////////////////////////////////////////
// getter
///< number of incoming edges in this layer
cnn_size_t in_channels() const { return in_channels_; }
///< number of outgoing edges in this layer
cnn_size_t out_channels() const { return out_channels_; }
cnn_size_t in_data_size() const {
return sumif(in_shape(), [&](int i) { // NOLINT
return in_type_[i] == vector_type::data; }, [](const shape3d& s) {
return s.size(); });
}
cnn_size_t out_data_size() const {
return sumif(out_shape(), [&](int i) { // NOLINT
return out_type_[i] == vector_type::data; }, [](const shape3d& s) {
return s.size(); });
}
std::vector<shape3d> in_data_shape() {
return filter(in_shape(), [&](int i) { return in_type_[i] == vector_type::data; });
}
std::vector<shape3d> out_data_shape() {
return filter(out_shape(), [&](int i) { return out_type_[i] == vector_type::data; });
}
///! @deprecated use in_data_size() instead
cnn_size_t in_size() const {
return in_data_size();
}
///! @deprecated use out_data_size() instead
cnn_size_t out_size() const {
return out_data_size();
}
std::vector<vec_t*> get_weights() const {
std::vector<vec_t*> v;
for (cnn_size_t i = 0; i < in_channels_; i++) {
if (is_trainable_weight(in_type_[i])) {
v.push_back(const_cast<layerptr_t>(this)->ith_in_node(i)->get_data(0));
}
}
return v;
}
std::vector<vec_t*> get_weights() {
std::vector<vec_t*> v;
for (cnn_size_t i = 0; i < in_channels_; i++) {
if (is_trainable_weight(in_type_[i])) {
v.push_back(ith_in_node(i)->get_data(0));
}
}
return v;
}
std::vector<vec_t*> get_weight_grads() {
std::vector<vec_t*> v;
for (cnn_size_t i = 0; i < in_channels_; i++) {
if (is_trainable_weight(in_type_[i])) {
v.push_back(ith_in_node(i)->get_gradient(0));
}
}
return v;
}
std::vector<edgeptr_t> get_inputs() {
std::vector<edgeptr_t> nodes;
for (cnn_size_t i = 0; i < in_channels_; i++) {
nodes.push_back(ith_in_node(i));
}
return nodes;
}
std::vector<edgeptr_t> get_outputs() {
std::vector<edgeptr_t> nodes;
for (cnn_size_t i = 0; i < out_channels_; i++) {
nodes.push_back(ith_out_node(i));
}
return nodes;
}
std::vector<edgeptr_t> get_outputs() const {
std::vector<edgeptr_t> nodes;
for (cnn_size_t i = 0; i < out_channels_; i++) {
nodes.push_back(const_cast<layerptr_t>(this)->ith_out_node(i));
}
return nodes;
}
void set_out_grads(const vec_t* grad,
cnn_size_t gnum, cnn_size_t worker_idx) {
cnn_size_t j = 0;
for (cnn_size_t i = 0; i < out_channels_; i++) {
if (out_type_[i] != vector_type::data) continue;
assert(j < gnum);
*ith_out_node(i)->get_gradient(worker_idx) = grad[j++];
}
}
void set_in_data(const vec_t* data,
cnn_size_t dnum, cnn_size_t worker_idx) {
cnn_size_t j = 0;
for (cnn_size_t i = 0; i < in_channels_; i++) {
if (in_type_[i] != vector_type::data) continue;
assert(j < dnum);
*ith_in_node(i)->get_data(worker_idx) = data[j++];
}
}
std::vector<vec_t> output(int worker_index = 0) const {
std::vector<vec_t> out;
for (cnn_size_t i = 0; i < out_channels_; i++) {
if (out_type_[i] == vector_type::data) {
out.push_back(*(const_cast<layerptr_t>(this))
->ith_out_node(i)->get_data(worker_index));
}
}
return out;
}
std::vector<vector_type> in_types() const { return in_type_; }
std::vector<vector_type> out_types() const { return out_type_; }
/**
* return output value range
* used only for calculating target value from label-id in final(output) layer
* override properly if the layer is intended to be used as output layer
**/
virtual std::pair<float_t, float_t> out_value_range() const { return {0.0, 1.0}; } // NOLINT
/**
* array of input shapes (width x height x depth)
**/
virtual std::vector<shape3d> in_shape() const = 0;
/**
* array of output shapes (width x height x depth)
**/
virtual std::vector<shape3d> out_shape() const = 0;
/**
* name of layer, should be unique for each concrete class
**/
virtual std::string layer_type() const = 0;
/**
* number of incoming connections for each output unit
* used only for weight/bias initialization methods which require fan-in size (e.g. xavier)
* override if the layer has trainable weights, and scale of initialization is important
**/
virtual size_t fan_in_size() const { return in_shape()[0].width_; }
/**
* number of outgoing connections for each input unit
* used only for weight/bias initialization methods which require fan-out size (e.g. xavier)
* override if the layer has trainable weights, and scale of initialization is important
**/
virtual size_t fan_out_size() const { return out_shape()[0].width_; }
/////////////////////////////////////////////////////////////////////////
// setter
template <typename WeightInit>
layer& weight_init(const WeightInit& f) {
weight_init_ = std::make_shared<WeightInit>(f);
return *this;
}
template <typename BiasInit>
layer& bias_init(const BiasInit& f) {
bias_init_ = std::make_shared<BiasInit>(f);
return *this;
}
template <typename WeightInit>
layer& weight_init(std::shared_ptr<WeightInit> f) {
weight_init_ = f;
return *this;
}
template <typename BiasInit>
layer& bias_init(std::shared_ptr<BiasInit> f) {
bias_init_ = f;
return *this;
}
/////////////////////////////////////////////////////////////////////////
// save/load
virtual void save(std::ostream& os) const { // NOLINT
/*if (is_exploded()) {
throw nn_error("failed to save weights because of infinite weight");
}*/
auto all_weights = get_weights();
for (auto& weight : all_weights) {
for (auto w : *weight) os << w << " ";
}
}
virtual void load(std::istream& is) { // NOLINT
auto all_weights = get_weights();
for (auto& weight : all_weights) {
for (auto& w : *weight) is >> w;
}
initialized_ = true;
}
virtual void load(const std::vector<double>& src, int& idx) { // NOLINT
auto all_weights = get_weights();
for (auto& weight : all_weights) {
for (auto& w : *weight) w = src[idx++];
}
initialized_ = true;
}
/////////////////////////////////////////////////////////////////////////
// visualize
///< visualize latest output of this layer
///< default implementation interpret output as 1d-vector,
///< so "visual" layer(like convolutional layer) should override this for better visualization.
virtual image<> output_to_image(size_t channel = 0,
size_t worker_index = 0) const {
const vec_t* output = get_outputs()[channel]->get_data(worker_index);
return vec2image<unsigned char>(*output, out_shape()[channel]);
}
/////////////////////////////////////////////////////////////////////////
// fprop/bprop
/**
* @param worker_index id of current worker-task
* @param in_data input vectors of this layer (data, weight, bias)
* @param out_data output vectors
**/
virtual void forward_propagation(cnn_size_t worker_index,
const std::vector<vec_t*>& in_data,
std::vector<vec_t*>& out_data) = 0;
/**
* return delta of previous layer (delta=\frac{dE}{da}, a=wx in fully-connected layer)
* @param worker_index id of current worker-task
* @param in_data input vectors (same vectors as forward_propagation)
* @param out_data output vectors (same vectors as forward_propagation)
* @param out_grad gradient of output vectors (i-th vector correspond with out_data[i])
* @param in_grad gradient of input vectors (i-th vector correspond with in_data[i])
**/
virtual void back_propagation(cnn_size_t worker_index,
const std::vector<vec_t*>& in_data,
const std::vector<vec_t*>& out_data,
std::vector<vec_t*>& out_grad,
std::vector<vec_t*>& in_grad) = 0;
/**
* return delta2 of previous layer (delta2=\frac{d^2E}{da^2}, diagonal of hessian matrix)
* it is never called if optimizer is hessian-free
**/
//virtual void back_propagation_2nd(const std::vector<vec_t>& delta_in) = 0;
// called afrer updating weight
virtual void post_update() {}
/**
* notify changing context (train <=> test)
**/
virtual void set_context(net_phase ctx) {
CNN_UNREFERENCED_PARAMETER(ctx);
}
std::vector<vec_t> forward(const std::vector<vec_t>& input) { // for test
setup(false);
set_in_data(&input[0], input.size(), 0);
forward(0);
return output(0);
}
std::vector<vec_t> backward(const std::vector<vec_t>& out_grads) { // for test
setup(false);
set_out_grads(&out_grads[0], out_grads.size(), 0);
backward(0);
return map_<vec_t>(get_inputs(), [](edgeptr_t e) { return *e->get_gradient(); });
}
void forward(int worker_index) {
std::vector<vec_t*> in_data, out_data;
// organize input/output vectors from storage
for (cnn_size_t i = 0; i < in_channels_; i++) {
in_data.push_back(ith_in_node(i)->get_data(worker_index));
}
for (cnn_size_t i = 0; i < out_channels_; i++) {
out_data.push_back(ith_out_node(i)->get_data(worker_index));
ith_out_node(i)->clear_grad_onwork(worker_index);
}
forward_propagation(worker_index, in_data, out_data);
}
void backward(int worker_index) {
std::vector<vec_t*> in_data, out_data, in_grad, out_grad;
// organize input/output vectors from storage
for (cnn_size_t i = 0; i < in_channels_; i++) {
in_data.push_back(ith_in_node(i)->get_data(worker_index));
}
for (cnn_size_t i = 0; i < out_channels_; i++) {
out_data.push_back(ith_out_node(i)->get_data(worker_index));
}
for (cnn_size_t i = 0; i < in_channels_; i++) {
in_grad.push_back(ith_in_node(i)->get_gradient(worker_index));
}
for (cnn_size_t i = 0; i < out_channels_; i++) {
out_grad.push_back(ith_out_node(i)->get_gradient(worker_index));
}
back_propagation(worker_index, in_data, out_data, out_grad, in_grad);
}
// allocate & reset weight
void setup(bool reset_weight, int max_task_size = CNN_TASK_SIZE) {
if (in_shape().size() != in_channels_ ||
out_shape().size() != out_channels_) {
throw nn_error("Connection mismatch at setup layer");
}
for (size_t i = 0; i < out_channels_; i++) {
if (!next_[i]) {
next_[i] = std::make_shared<edge>(
this, out_shape()[i], out_type_[i]);
}
}
set_worker_count(max_task_size);
if (reset_weight || !initialized_) {
init_weight();
}
}
void init_weight() {
for (cnn_size_t i = 0; i < in_channels_; i++) {
switch (in_type_[i]) {
case vector_type::weight:
weight_init_->fill(ith_in_node(i)->get_data(),
fan_in_size(), fan_out_size());
break;
case vector_type::bias:
bias_init_->fill(ith_in_node(i)->get_data(),
fan_in_size(), fan_out_size());
break;
default:
break;
}
}
initialized_ = true;
}
void clear_grads(cnn_size_t worker_size) {
for (size_t i = 0; i < in_type_.size(); i++) {
ith_in_node(i)->clear_grads(worker_size);
}
}
void update_weight(optimizer *o,
cnn_size_t worker_size, cnn_size_t batch_size) {
for (size_t i = 0; i < in_type_.size(); i++) {
if (is_trainable_weight(in_type_[i])) {
vec_t diff;
vec_t& target = *ith_in_node(i)->get_data();
ith_in_node(i)->merge_grads(worker_size, &diff);
std::transform(diff.begin(), diff.end(),
diff.begin(), [&](float_t x) { // NOLINT
return x / batch_size; });
o->update(diff, target);
}
}
clear_grads(worker_size);
post_update();
}
virtual void set_worker_count(cnn_size_t worker_count) {
for (cnn_size_t i = 0; i < in_channels_; i++) {
ith_in_node(i)->set_worker_size(worker_count);
}
for (cnn_size_t i = 0; i < out_channels_; i++) {
ith_out_node(i)->set_worker_size(worker_count);
}
}
bool has_same_weights(const layer& rhs, float_t eps) const {
auto w1 = get_weights();
auto w2 = rhs.get_weights();
if (w1.size() != w2.size()) return false;
for (size_t i = 0; i < w1.size(); i++) {
if (w1[i]->size() != w2[i]->size()) return false;
for (size_t j = 0; j < w1[i]->size(); j++) {
if (std::abs(w1[i]->at(j) - w2[i]->at(j)) > eps) return false;
}
}
return true;
}
protected:
bool initialized_;
bool parallelize_;
cnn_size_t in_channels_; // number of input vectors
cnn_size_t out_channels_; // number of output vectors
std::vector<vector_type> in_type_;
std::vector<vector_type> out_type_;
private:
std::shared_ptr<weight_init::function> weight_init_;
std::shared_ptr<weight_init::function> bias_init_;
void alloc_input(cnn_size_t i) const {
//@todo refactoring
prev_[i] = std::make_shared<edge>(nullptr, in_shape()[i], in_type_[i]);
}
void alloc_output(cnn_size_t i) const {
//@todo refactoring
next_[i] = std::make_shared<edge>((layer*)this, out_shape()[i], out_type_[i]);
}
edgeptr_t ith_in_node(cnn_size_t i) {
if (!prev_[i]) alloc_input(i);
return prev()[i];
}
edgeptr_t ith_out_node(cnn_size_t i) {
if (!next_[i]) alloc_output(i);
return next()[i];
}
};
inline void connect(layerptr_t head,
layerptr_t tail,
cnn_size_t head_index = 0,
cnn_size_t tail_index = 0) {
auto out_shape = head->out_shape()[head_index];
auto in_shape = tail->in_shape()[tail_index];
head->setup(false);
if (out_shape.size() != in_shape.size()) {
connection_mismatch(*head, *tail);
}
if (!head->next_[head_index]) {
throw nn_error("output edge must not be null");
}
tail->prev_[tail_index] = head->next_[head_index];
tail->prev_[tail_index]->add_next_node(tail);
}
inline layer& operator << (layer& lhs, layer& rhs) {
connect(&lhs, &rhs);
return rhs;
}
template <typename Char, typename CharTraits>
std::basic_ostream<Char, CharTraits>& operator << (
std::basic_ostream<Char, CharTraits>& os, const layer& v) {
v.save(os);
return os;
}
template <typename Char, typename CharTraits>
std::basic_istream<Char, CharTraits>& operator >> (
std::basic_istream<Char, CharTraits>& os, layer& v) {
v.load(os);
return os;
}
// error message functions
inline void connection_mismatch(const layer& from, const layer& to) {
std::ostringstream os;
os << std::endl;
os << "output size of Nth layer must be equal to input of (N+1)th layer\n";
os << "layerN: " << std::setw(12) << from.layer_type() << " in:"
<< from.in_data_size() << "("
<< from.in_shape() << "), " << "out:"
<< from.out_data_size() << "("
<< from.out_shape() << ")\n";
os << "layerN+1: " << std::setw(12) << to.layer_type() << " in:"
<< to.in_data_size() << "("
<< to.in_shape() << "), " << "out:"
<< to.out_data_size() << "("
<< to.out_shape() << ")\n";
os << from.out_data_size() << " != " << to.in_data_size() << std::endl;
std::string detail_info = os.str();
throw nn_error("layer dimension mismatch!" + detail_info);
}
inline void data_mismatch(const layer& layer, const vec_t& data) {
std::ostringstream os;
os << std::endl;
os << "data dimension: " << data.size() << "\n";
os << "network dimension: " << layer.in_data_size() << "("
<< layer.layer_type() << ":"
<< layer.in_shape() << ")\n";
std::string detail_info = os.str();
throw nn_error("input dimension mismath!" + detail_info);
}
inline void pooling_size_mismatch(cnn_size_t in_width,
cnn_size_t in_height,
cnn_size_t pooling_size) {
std::ostringstream os;
os << std::endl;
os << "WxH:" << in_width << "x" << in_height << std::endl;
os << "pooling-size:" << pooling_size << std::endl;
std::string detail_info = os.str();
throw nn_error("width/height not multiple of pooling size" + detail_info);
}
template <typename T, typename U>
void graph_traverse(layer *root_node, T&& node_callback, U&& edge_callback) {
std::unordered_set<layer*> visited;
std::queue<layer*> S;
S.push(root_node);
while (!S.empty()) {
layer *curr = S.front();
S.pop();
visited.insert(curr);
node_callback(*curr);
auto edges = curr->next();
for (auto e : edges) {
if (e != nullptr)
edge_callback(*e);
}
auto prev = curr->prev_nodes();
for (auto p : prev) {
layer* l = dynamic_cast<layer*>(p); // @todo refactoring
if (visited.find(l) == visited.end()) {
S.push(l);
}
}
auto next = curr->next_nodes();
for (auto n : next) {
layer* l = dynamic_cast<layer*>(n); // @todo refactoring
if (visited.find(l) == visited.end()) {
S.push(l);
}
}
}
}
} // namespace tiny_cnn