tiny_dnn 1.0.0
A header only, dependency-free deep learning framework in C++11
Loading...
Searching...
No Matches
linear_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/util/util.h"
29#include <algorithm>
30
31
32namespace tiny_dnn {
33
37template<typename Activation>
38class linear_layer : public feedforward_layer<Activation> {
39public:
40 CNN_USE_LAYER_MEMBERS;
41
43
49 explicit linear_layer(serial_size_t dim, float_t scale = float_t(1), float_t bias = float_t(0))
50 : Base({vector_type::data}),
51 dim_(dim), scale_(scale), bias_(bias) {}
52
53 std::vector<shape3d> in_shape() const override {
54 return {shape3d(dim_, 1, 1) };
55 }
56
57 std::vector<shape3d> out_shape() const override {
58 return{ shape3d(dim_, 1, 1), shape3d(dim_, 1, 1) };
59 }
60
61 std::string layer_type() const override { return "linear"; }
62
63 void forward_propagation(const std::vector<tensor_t*>& in_data,
64 std::vector<tensor_t*>& out_data) override {
65 const tensor_t& in = *in_data[0];
66 tensor_t& out = *out_data[0];
67 tensor_t& a = *out_data[1];
68
69 // do nothing
70 CNN_UNREFERENCED_PARAMETER(out);
71
72 // @todo revise the parallelism strategy
73 for_i(parallelize_, dim_, [&](int i) {
74 for (serial_size_t sample = 0, sample_count = static_cast<serial_size_t>(in.size()); sample < sample_count; ++sample)
75 a[sample][i] = scale_ * in[sample][i] + bias_;
76 });
77 this->forward_activation(*out_data[0], *out_data[1]);
78 }
79
80 void back_propagation(const std::vector<tensor_t*>& in_data,
81 const std::vector<tensor_t*>& out_data,
82 std::vector<tensor_t*>& out_grad,
83 std::vector<tensor_t*>& in_grad) override {
84 tensor_t& prev_delta = *in_grad[0];
85 tensor_t& curr_delta = *out_grad[1];
86
87 CNN_UNREFERENCED_PARAMETER(in_data);
88
89 this->backward_activation(*out_grad[0], *out_data[0], curr_delta);
90
91 // @todo revise parallelism strategy
92 for (serial_size_t sample = 0; sample < static_cast<serial_size_t>(prev_delta.size()); ++sample) {
93 for_i(parallelize_, dim_, [&](int i) {
94 prev_delta[sample][i] = curr_delta[sample][i] * scale_;
95 });
96 }
97 }
98
99 template <class Archive>
100 static void load_and_construct(Archive & ar, cereal::construct<linear_layer> & construct) {
101 serial_size_t dim;
102 float_t scale, bias;
103
104 ar(cereal::make_nvp("in_size", dim), cereal::make_nvp("scale", scale), cereal::make_nvp("bias", bias));
105
106 construct(dim, scale, bias);
107 }
108
109 template <class Archive>
110 void serialize(Archive & ar) {
111 layer::serialize_prolog(ar);
112 ar(cereal::make_nvp("in_size", dim_), cereal::make_nvp("scale", scale_), cereal::make_nvp("bias", bias_));
113 }
114
115protected:
116 serial_size_t dim_;
117 float_t scale_, bias_;
118};
119
120} // namespace tiny_dnn
single-input, single-output network with activation function
Definition feedforward_layer.h:37
Simple image utility class.
Definition image.h:94
bool parallelize_
Flag indicating whether the layer/node operations ara paralellized.
Definition layer.h:696
element-wise operation: f(x) = h(scale*x+bias)
Definition linear_layer.h:38
std::vector< shape3d > out_shape() const override
array of output shapes (width x height x depth)
Definition linear_layer.h:57
linear_layer(serial_size_t dim, float_t scale=float_t(1), float_t bias=float_t(0))
Definition linear_layer.h:49
std::string layer_type() const override
name of layer, should be unique for each concrete class
Definition linear_layer.h:61
std::vector< shape3d > in_shape() const override
array of input shapes (width x height x depth)
Definition linear_layer.h:53
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 linear_layer.h:80
void forward_propagation(const std::vector< tensor_t * > &in_data, std::vector< tensor_t * > &out_data) override
Definition linear_layer.h:63