tiny_dnn  1.0.0
A header only, dependency-free deep learning framework in C++11
concat_layer.h
1 /*
2  Copyright (c) 2016, Taiga Nomi
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are met:
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the name of the <organization> nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
17  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 #pragma once
28 #include "tiny_dnn/util/util.h"
29 #include "tiny_dnn/layers/layer.h"
30 
31 namespace tiny_dnn {
32 
44 class concat_layer : public layer {
45 public:
49  concat_layer(const std::vector<shape3d>& in_shapes)
50  : layer(std::vector<vector_type>(in_shapes.size(), vector_type::data), {vector_type::data}),
51  in_shapes_(in_shapes) {
52  set_outshape();
53  }
54 
59  concat_layer(serial_size_t num_args, serial_size_t ndim)
60  : layer(std::vector<vector_type>(num_args, vector_type::data), { vector_type::data }),
61  in_shapes_(std::vector<shape3d>(num_args, shape3d(ndim,1,1))) {
62  set_outshape();
63  }
64 
65  void set_outshape() {
66  out_shape_ = in_shapes_.front();
67  for (size_t i = 1; i < in_shapes_.size(); i++) {
68  if (in_shapes_[i].area() != out_shape_.area())
69  throw nn_error("each input shapes to concat must have same WxH size");
70  out_shape_.depth_ += in_shapes_[i].depth_;
71  }
72  }
73 
74  std::string layer_type() const override {
75  return "concat";
76  }
77 
78  std::vector<shape3d> in_shape() const override {
79  return in_shapes_;
80  }
81 
82  std::vector<shape3d> out_shape() const override {
83  return {out_shape_};
84  }
85 
86  void forward_propagation(const std::vector<tensor_t*>& in_data,
87  std::vector<tensor_t*>& out_data) override {
88  serial_size_t num_samples = static_cast<serial_size_t>((*out_data[0]).size());
89 
90  for (serial_size_t s = 0; s < num_samples; s++) {
91  float_t* outs = &(*out_data[0])[s][0];
92 
93  for (serial_size_t i = 0; i < in_shapes_.size(); i++) {
94  const float_t* ins = &(*in_data[i])[s][0];
95  serial_size_t dim = in_shapes_[i].size();
96  outs = std::copy(ins, ins + dim, outs);
97  }
98  }
99  }
100 
101  void back_propagation(const std::vector<tensor_t*>& in_data,
102  const std::vector<tensor_t*>& out_data,
103  std::vector<tensor_t*>& out_grad,
104  std::vector<tensor_t*>& in_grad) override {
105  CNN_UNREFERENCED_PARAMETER(in_data);
106  CNN_UNREFERENCED_PARAMETER(out_data);
107 
108  size_t num_samples = (*out_grad[0]).size();
109 
110  for (size_t s = 0; s < num_samples; s++) {
111  const float_t* outs = &(*out_grad[0])[s][0];
112 
113  for (serial_size_t i = 0; i < in_shapes_.size(); i++) {
114  serial_size_t dim = in_shapes_[i].size();
115  float_t* ins = &(*in_grad[i])[s][0];
116  std::copy(outs, outs + dim, ins);
117  outs += dim;
118  }
119  }
120  }
121 
122  template <class Archive>
123  static void load_and_construct(Archive & ar, cereal::construct<concat_layer> & construct) {
124  std::vector<shape3d> in_shapes;
125 
126  ar(cereal::make_nvp("in_size", in_shapes));
127  construct(in_shapes);
128  }
129 
130  template <class Archive>
131  void serialize(Archive & ar) {
132  layer::serialize_prolog(ar);
133  ar(in_shapes_);
134  }
135 
136 private:
137  std::vector<shape3d> in_shapes_;
138  shape3d out_shape_;
139 };
140 
141 } // namespace tiny_dnn
concat N layers along depth
Definition: concat_layer.h:44
void back_propagation(const std::vector< tensor_t * > &in_data, const std::vector< tensor_t * > &out_data, std::vector< tensor_t * > &out_grad, std::vector< tensor_t * > &in_grad) override
return delta of previous layer (delta=\frac{dE}{da}, a=wx in fully-connected layer)
Definition: concat_layer.h:101
std::vector< shape3d > in_shape() const override
array of input shapes (width x height x depth)
Definition: concat_layer.h:78
std::string layer_type() const override
name of layer, should be unique for each concrete class
Definition: concat_layer.h:74
void forward_propagation(const std::vector< tensor_t * > &in_data, std::vector< tensor_t * > &out_data) override
Definition: concat_layer.h:86
concat_layer(const std::vector< shape3d > &in_shapes)
Definition: concat_layer.h:49
concat_layer(serial_size_t num_args, serial_size_t ndim)
Definition: concat_layer.h:59
std::vector< shape3d > out_shape() const override
array of output shapes (width x height x depth)
Definition: concat_layer.h:82
base class of all kind of NN layers
Definition: layer.h:62