tiny_dnn  1.0.0
A header only, dependency-free deep learning framework in C++11
feedforward_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/layers/layer.h"
29 #include "tiny_dnn/activations/activation_function.h"
30 
31 namespace tiny_dnn {
32 
36 template<typename Activation>
37 class feedforward_layer : public layer {
38 public:
39  explicit feedforward_layer(const std::vector<vector_type>& in_data_type)
40  : layer(in_data_type, std_output_order(true)) {}
41  activation::function& activation_function() { return h_; }
42  std::pair<float_t, float_t> out_value_range() const override { return h_.scale(); }
43 
44 public:
45  void forward_activation(tensor_t& a_tensor, tensor_t& out_tensor) {
46  serial_size_t out_dim = out_shape()[0].size();
47 
48  for_i(a_tensor.size(), [&](int sample) {
49  vec_t& out = a_tensor[sample];
50  vec_t& a = out_tensor[sample];
51  out.resize(out_dim);
52  a.resize(out_dim);
53  h_.itef(out, a, out_dim);
54  });
55  }
56 
57  void backward_activation(const tensor_t& prev_delta, const tensor_t& this_out, tensor_t& curr_delta) {
58 
59  // @todo consider parallelism
60  for_i(this_out.size(), [&](serial_size_t sample) {
61  const vec_t& out_vec = this_out[sample];
62  const vec_t& prev_delta_vec = prev_delta[sample];
63  vec_t& curr_delta_vec = curr_delta[sample];
64 
65  const serial_size_t len = static_cast<serial_size_t>(prev_delta_vec.size());
66 
67  if (h_.one_hot()) {
68  for (serial_size_t c = 0; c < len; c++) {
69  curr_delta_vec[c] = prev_delta_vec[c] * h_.df(out_vec[c]);
70  }
71  }
72  else {
73  for (serial_size_t c = 0; c < len; c++) {
74  vec_t df = h_.df(out_vec, c);
75  curr_delta_vec[c] = vectorize::dot(&prev_delta_vec[0], &df[0], len);
76  }
77  }
78  });
79  }
80 
81  Activation h_;
82 };
83 
84 } // namespace tiny_dnn
Definition: activation_function.h:34
single-input, single-output network with activation function
Definition: feedforward_layer.h:37
std::pair< float_t, float_t > out_value_range() const override
return output value range used only for calculating target value from label-id in final(output) layer...
Definition: feedforward_layer.h:42
base class of all kind of NN layers
Definition: layer.h:62
layer(const std::vector< vector_type > &in_type, const std::vector< vector_type > &out_type)
Defaul layer constructor that instantiates a N-input, M-output layer.
Definition: layer.h:76
virtual std::vector< shape3d > out_shape() const =0
array of output shapes (width x height x depth)