tiny_dnn 1.0.0
A header only, dependency-free deep learning framework in C++11
Loading...
Searching...
No Matches
quantized_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#include "tiny_dnn/util/product.h"
30
31namespace tiny_dnn {
32
36template<typename Activation>
38public:
40 CNN_USE_LAYER_MEMBERS;
41
48 serial_size_t out_dim,
49 bool has_bias = true,
50 backend_t backend_type = core::backend_t::internal)
51 : Base(std_input_order(has_bias)) {
52 set_params(in_dim, out_dim, has_bias);
53 init_backend(backend_type);
54 }
55
56 // move constructor
58 : Base(std::move(other))
59 , params_(std::move(other.params_)) {
60 init_backend(core::backend_t::internal);
61 }
62
63 serial_size_t fan_in_size() const override {
64 return params_.in_size_;
65 }
66
67 serial_size_t fan_out_size() const override {
68 return params_.out_size_;
69 }
70
71 std::vector<index3d<serial_size_t>> in_shape() const override {
72 if (params_.has_bias_) {
73 return { index3d<serial_size_t>(params_.in_size_, 1, 1),
74 index3d<serial_size_t>(params_.in_size_,
75 params_.out_size_, 1),
76 index3d<serial_size_t>(params_.out_size_, 1, 1) };
77 } else {
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 }
82 }
83
84 std::vector<index3d<serial_size_t>> out_shape() const override {
85 return { index3d<serial_size_t>(params_.out_size_, 1, 1),
86 index3d<serial_size_t>(params_.out_size_, 1, 1) };
87 }
88
89 void forward_propagation(const std::vector<tensor_t*>& in_data,
90 std::vector<tensor_t*>& out_data) override {
91 if (in_data.size() == 2 || in_data.size() == 3) {
92 Base::backend_->fully_q(in_data, out_data);
93
94 // activations
95 this->forward_activation(*out_data[0], *out_data[1]);
96 } else if (in_data.size() == 4 || in_data.size() == 6) {
97 Base::backend_->fully_eq(in_data, out_data);
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 Base::backend_->fully_q(in_data, out_data, out_grad, in_grad);
106 }
107
108 std::string layer_type() const override { return "q_fully-connected"; }
109
110protected:
111 fully_params params_;
112
113 void set_params(const serial_size_t in_size,
114 const serial_size_t out_size,
115 bool has_bias) {
116 params_.in_size_ = in_size;
117 params_.out_size_ = out_size;
118 params_.has_bias_ = has_bias;
119 }
120
121 void init_backend(backend_t backend_type) {
122 std::shared_ptr<core::backend> backend = nullptr;
123
124 // allocate new backend
125 if (backend_type == backend_t::internal) {
126 backend = std::make_shared<core::tiny_backend>(&params_,
127 [this](const tensor_t& p_delta,
128 const tensor_t& out, tensor_t& c_delta) {
129 return Base::backward_activation(p_delta, out, c_delta);
130 });
131 } else if (backend_type == backend_t::nnpack) {
132 backend = std::make_shared<core::nnp_backend>(&params_);
133 } else if (backend_type == backend_t::libdnn) {
134 backend = std::make_shared<core::dnn_backend>();
135 } else {
136 throw nn_error("Not supported backend type.");
137 }
138
139 if (backend) {
140 Base::set_backend(backend);
141 Base::set_backend_type(backend_type);
142 Base::backend_->set_layer(this);
143 } else {
144 throw nn_error("Could not allocate the backend.");
145 }
146 }
147};
148
149} // namespace tiny_dnn
single-input, single-output network with activation function
Definition feedforward_layer.h:37
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
std::shared_ptr< core::backend > backend()
number of incoming edges in this layer
Definition layer.h:143
compute fully-connected(matmul) operation
Definition quantized_fully_connected_layer.h:37
std::vector< index3d< serial_size_t > > out_shape() const override
array of output shapes (width x height x depth)
Definition quantized_fully_connected_layer.h:84
std::string layer_type() const override
name of layer, should be unique for each concrete class
Definition quantized_fully_connected_layer.h:108
serial_size_t fan_in_size() const override
number of incoming connections for each output unit used only for weight/bias initialization methods ...
Definition quantized_fully_connected_layer.h:63
quantized_fully_connected_layer(serial_size_t in_dim, serial_size_t out_dim, bool has_bias=true, backend_t backend_type=core::backend_t::internal)
Definition quantized_fully_connected_layer.h:47
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 quantized_fully_connected_layer.h:67
std::vector< index3d< serial_size_t > > in_shape() const override
array of input shapes (width x height x depth)
Definition quantized_fully_connected_layer.h:71
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 quantized_fully_connected_layer.h:101
void forward_propagation(const std::vector< tensor_t * > &in_data, std::vector< tensor_t * > &out_data) override
Definition quantized_fully_connected_layer.h:89