tiny_dnn 1.0.0
A header only, dependency-free deep learning framework in C++11
Loading...
Searching...
No Matches
fully_connected_layer.h
1/*
2 Copyright (c) 2013, 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/layers/layer.h"
29
30#include "tiny_dnn/core/kernels/fully_connected_op.h"
31#include "tiny_dnn/core/kernels/fully_connected_grad_op.h"
32
33namespace tiny_dnn {
34
38template<typename Activation>
39class fully_connected_layer : public feedforward_layer<Activation> {
40public:
42 CNN_USE_LAYER_MEMBERS;
43
50 serial_size_t out_dim,
51 bool has_bias = true,
52 backend_t backend_type = core::default_engine())
53 : Base(std_input_order(has_bias)) {
54 set_params(in_dim, out_dim, has_bias);
55 init_backend(backend_type);
56 Base::set_backend_type(backend_type);
57 }
58
59 // move constructor
61 : Base(std::move(other))
62 , params_(std::move(other.params_))
63 , kernel_fwd_(std::move(other.kernel_fwd_))
64 , kernel_back_(std::move(other.kernel_back_)) {
65 init_backend(std::move(other.engine()));
66 }
67
68 serial_size_t fan_in_size() const override {
69 return params_.in_size_;
70 }
71
72 serial_size_t fan_out_size() const override {
73 return params_.out_size_;
74 }
75
76 std::vector<index3d<serial_size_t>> in_shape() const override {
77 if (params_.has_bias_) {
78 return { index3d<serial_size_t>(params_.in_size_, 1, 1),
79 index3d<serial_size_t>(params_.in_size_,
80 params_.out_size_, 1),
81 index3d<serial_size_t>(params_.out_size_, 1, 1) };
82 } else {
83 return { index3d<serial_size_t>(params_.in_size_, 1, 1),
84 index3d<serial_size_t>(params_.in_size_,
85 params_.out_size_, 1) };
86 }
87 }
88
89 std::vector<index3d<serial_size_t>> out_shape() const override {
90 return { index3d<serial_size_t>(params_.out_size_, 1, 1),
91 index3d<serial_size_t>(params_.out_size_, 1, 1) };
92 }
93
94 void forward_propagation(const std::vector<tensor_t*>& in_data,
95 std::vector<tensor_t*>& out_data) override {
96 // forward convolutional op context
97 auto ctx = OpKernelContext(in_data, out_data);
98 ctx.setParallelize(layer::parallelize());
99 ctx.setEngine(layer::engine());
100
101 // launch convolutional kernel
102 kernel_fwd_->compute(ctx);
103
104 // activations
105 this->forward_activation(*out_data[0], *out_data[1]);
106 }
107
108 void back_propagation(const std::vector<tensor_t*>& in_data,
109 const std::vector<tensor_t*>& out_data,
110 std::vector<tensor_t*>& out_grad,
111 std::vector<tensor_t*>& in_grad) override {
112 // activations
113 // TODO(edgar/nyanp): refactor and move activations outside
114 this->backward_activation(*out_grad[0], *out_data[0], *out_grad[1]);
115
116 // backward convolutional op context
117 auto ctx = OpKernelContext(in_data, out_data, out_grad, in_grad);
118 ctx.setParallelize(layer::parallelize());
119 ctx.setEngine(layer::engine());
120
121 // launch convolutional kernel
122 kernel_back_->compute(ctx);
123 }
124
125 std::string layer_type() const override { return "fully-connected"; }
126
127 template <class Archive>
128 static void load_and_construct(Archive & ar, cereal::construct<fully_connected_layer> & construct) {
129 serial_size_t in_dim, out_dim;
130 bool has_bias;
131
132 ar(cereal::make_nvp("in_size", in_dim),
133 cereal::make_nvp("out_size", out_dim),
134 cereal::make_nvp("has_bias", has_bias));
135 construct(in_dim, out_dim, has_bias);
136 }
137
138 template <class Archive>
139 void serialize(Archive & ar) {
140 layer::serialize_prolog(ar);
141 ar(cereal::make_nvp("in_size", params_.in_size_),
142 cereal::make_nvp("out_size", params_.out_size_),
143 cereal::make_nvp("has_bias", params_.has_bias_));
144 }
145
146protected:
147
148 void set_params(const serial_size_t in_size,
149 const serial_size_t out_size,
150 bool has_bias) {
151 params_.in_size_ = in_size;
152 params_.out_size_ = out_size;
153 params_.has_bias_ = has_bias;
154 }
155
156 void init_backend(backend_t backend_type) {
157 core::OpKernelConstruction ctx =
158 core::OpKernelConstruction(layer::device(), &params_);
159
160 if (backend_type == backend_t::internal ||
161 backend_type == backend_t::avx||
162 backend_type == backend_t::nnpack
163 ) {
164
165 kernel_fwd_.reset(new FullyConnectedOp(ctx));
166 kernel_back_.reset(new FullyConnectedGradOp(ctx));
167
168 return;
169 }
170 else {
171 throw nn_error("Not supported engine: " + to_string(backend_type));
172 }
173 }
174
175 private:
176 /* The layer parameters */
177 fully_params params_;
178
179 /* Forward and backward ops */
180 std::shared_ptr<core::OpKernel> kernel_fwd_;
181 std::shared_ptr<core::OpKernel> kernel_back_;
182};
183
184} // namespace tiny_dnn
single-input, single-output network with activation function
Definition feedforward_layer.h:37
compute fully-connected(matmul) operation
Definition fully_connected_layer.h:39
std::string layer_type() const override
name of layer, should be unique for each concrete class
Definition fully_connected_layer.h:125
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 fully_connected_layer.h:108
fully_connected_layer(serial_size_t in_dim, serial_size_t out_dim, bool has_bias=true, backend_t backend_type=core::default_engine())
Definition fully_connected_layer.h:49
std::vector< index3d< serial_size_t > > out_shape() const override
array of output shapes (width x height x depth)
Definition fully_connected_layer.h:89
void forward_propagation(const std::vector< tensor_t * > &in_data, std::vector< tensor_t * > &out_data) override
Definition fully_connected_layer.h:94
serial_size_t fan_out_size() const override
number of outgoing connections for each input unit used only for weight/bias initialization methods w...
Definition fully_connected_layer.h:72
serial_size_t fan_in_size() const override
number of incoming connections for each output unit used only for weight/bias initialization methods ...
Definition fully_connected_layer.h:68
std::vector< index3d< serial_size_t > > in_shape() const override
array of input shapes (width x height x depth)
Definition fully_connected_layer.h:76
Simple image utility class.
Definition image.h:94
serial_size_t out_size() const
!
Definition layer.h:181
serial_size_t in_size() const
!
Definition layer.h:176