tiny_dnn 1.0.0
A header only, dependency-free deep learning framework in C++11
Loading...
Searching...
No Matches
power_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#include <cmath>
31
32namespace tiny_dnn {
33
34
38class power_layer : public layer {
39public:
40 typedef layer Base;
41
47 power_layer(const shape3d& in_shape, float_t factor, float_t scale=1.0f)
48 : layer({ vector_type::data }, { vector_type::data }),
49 in_shape_(in_shape), factor_(factor), scale_(scale) {
50 }
51
57 power_layer(const layer& prev_layer, float_t factor, float_t scale=1.0f)
58 : layer({ vector_type::data }, { vector_type::data }),
59 in_shape_(prev_layer.out_shape()[0]), factor_(factor), scale_(scale) {
60 }
61
62 std::string layer_type() const override {
63 return "power";
64 }
65
66 std::vector<shape3d> in_shape() const override {
67 return {in_shape_};
68 }
69
70 std::vector<shape3d> out_shape() const override {
71 return {in_shape_};
72 }
73
74 void forward_propagation(const std::vector<tensor_t*>& in_data,
75 std::vector<tensor_t*>& out_data) override {
76 const tensor_t& x = *in_data[0];
77 tensor_t& y = *out_data[0];
78
79 for (serial_size_t i = 0; i < x.size(); i++) {
80 std::transform(x[i].begin(), x[i].end(), y[i].begin(), [=](float_t x) {
81 return scale_*std::pow(x, factor_);
82 });
83 }
84 }
85
86 void back_propagation(const std::vector<tensor_t*>& in_data,
87 const std::vector<tensor_t*>& out_data,
88 std::vector<tensor_t*>& out_grad,
89 std::vector<tensor_t*>& in_grad) override {
90 tensor_t& dx = *in_grad[0];
91 const tensor_t& dy = *out_grad[0];
92 const tensor_t& x = *in_data[0];
93 const tensor_t& y = *out_data[0];
94
95 for (serial_size_t i = 0; i < x.size(); i++) {
96 for (serial_size_t j = 0; j < x[i].size(); j++) {
97 // f(x) = (scale*x)^factor
98 // ->
99 // dx = dy * df(x)
100 // = dy * scale * factor * (scale * x)^(factor - 1)
101 // = dy * scale * factor * (scale * x)^factor * (scale * x)^(-1)
102 // = dy * factor * y / x
103 if (std::abs(x[i][j]) > 1e-10) {
104 dx[i][j] = dy[i][j] * factor_ * y[i][j] / x[i][j];
105 }
106 else {
107 dx[i][j] = dy[i][j] * scale_ * factor_ * std::pow(x[i][j], factor_ - 1.0f);
108 }
109 }
110 }
111 }
112
113 template <class Archive>
114 static void load_and_construct(Archive & ar, cereal::construct<power_layer> & construct) {
116 float_t factor;
117 float_t scale(1.0f);
118
119 ar(cereal::make_nvp("in_size", in_shape), cereal::make_nvp("factor", factor), cereal::make_nvp("scale", scale));
120 construct(in_shape, factor, scale);
121 }
122
123 template <class Archive>
124 void serialize(Archive & ar) {
125 layer::serialize_prolog(ar);
126 ar(cereal::make_nvp("in_size", in_shape_), cereal::make_nvp("factor", factor_), cereal::make_nvp("scale", scale_));
127 }
128
129 float_t factor() const {
130 return factor_;
131 }
132
133 float_t scale() const {
134 return scale_;
135 }
136private:
137
138 shape3d in_shape_;
139 float_t factor_;
140 float_t scale_;
141};
142
143} // namespace tiny_dnn
Simple image utility class.
Definition image.h:94
base class of all kind of NN layers
Definition layer.h:62
element-wise pow: y = scale*x^factor
Definition power_layer.h:38
void forward_propagation(const std::vector< tensor_t * > &in_data, std::vector< tensor_t * > &out_data) override
Definition power_layer.h:74
power_layer(const layer &prev_layer, float_t factor, float_t scale=1.0f)
Definition power_layer.h:57
std::vector< shape3d > in_shape() const override
array of input shapes (width x height x depth)
Definition power_layer.h:66
std::string layer_type() const override
name of layer, should be unique for each concrete class
Definition power_layer.h:62
power_layer(const shape3d &in_shape, float_t factor, float_t scale=1.0f)
Definition power_layer.h:47
std::vector< shape3d > out_shape() const override
array of output shapes (width x height x depth)
Definition power_layer.h:70
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 power_layer.h:86