tiny_dnn  1.0.0
A header only, dependency-free deep learning framework in C++11
tiny_deconv2d_back_kernel.h
1 /*
2  Copyright (c) 2016, Taiga Nomi, Edgar Riba
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 
29 #include "tiny_dnn/core/params/deconv_params.h"
30 
31 namespace tiny_dnn {
32 namespace core {
33 namespace kernels {
34 
35 inline void tiny_deconv2d_back_kernel(const deconv_params& params,
36  const tensor_t& prev_out,
37  const vec_t& W,
38  tensor_t& dW,
39  tensor_t& db,
40  tensor_t& curr_delta,
41  tensor_t* prev_delta) {
42  // propagate delta to previous layer
43  for_i(prev_out.size(), [&](int sample) {
44  for (serial_size_t inc = 0; inc < params.in.depth_; inc++) {
45  for (serial_size_t outc = 0; outc < params.out.depth_; outc++) {
46  if (!params.tbl.is_connected(outc, inc)) continue;
47 
48  serial_size_t idx = 0;
49  idx = params.in.depth_ * outc + inc;
50  idx = params.weight.get_index(0, 0, idx);
51  const float_t *pw = &W[idx];
52 
53  idx = params.out_unpadded.get_index(0, 0, outc);
54  const float_t *pdelta_src = &curr_delta[sample][idx];
55 
56  idx = params.in.get_index(0, 0, inc);
57  float_t *pdelta_dst = &(*prev_delta)[sample][idx];
58 
59  for (serial_size_t y = 0; y < params.in.height_; y++) {
60  for (serial_size_t x = 0; x < params.in.width_; x++) {
61  const float_t * ppw = pw;
62 
63  float_t * ppdelta_dst = pdelta_dst + y * params.in.width_ + x;
64  float_t sum = float_t(0);
65 
66  for (serial_size_t wy = 0; wy < params.weight.height_; wy++) {
67  for (serial_size_t wx = 0; wx < params.weight.width_; wx++) {
68  idx = (y * params.h_stride + wy) *
69  params.out.width_ + (x *
70  params.w_stride + wx);
71  sum += ppw[wy * params.weight.width_ + wx] *
72  pdelta_src[idx];
73  }
74  }
75  *ppdelta_dst += sum;
76  }
77  }
78  }
79  }
80 
81  // accumulate dw
82  for (serial_size_t inc = 0; inc < params.in.depth_; inc++) {
83  for (serial_size_t outc = 0; outc < params.out.depth_; outc++) {
84  if (!params.tbl.is_connected(outc, inc)) continue;
85 
86  for (serial_size_t wy = 0; wy < params.weight.height_; wy++) {
87  for (serial_size_t wx = 0; wx < params.weight.width_; wx++) {
88  float_t dst = float_t(0);
89 
90  serial_size_t idx = 0;
91  idx = params.in.get_index(0, 0, inc);
92  const float_t * prevo = &prev_out[sample][idx];
93 
94  idx = params.out.get_index(wx, wy, outc);
95  const float_t * delta = &curr_delta[sample][idx];
96 
97  for (serial_size_t y = 0; y < params.in.height_; y++) {
98  dst += vectorize::dot(
99  prevo + y * params.in.width_,
100  delta + y * params.out.width_,
101  params.in.width_);
102  }
103 
104  idx = params.in.depth_ * outc + inc;
105  dW[sample][params.weight.get_index(wx, wy, idx)] += dst;
106  }
107  }
108  }
109  }
110 
111  // accumulate db
112  if (params.has_bias) {
113  //vec_t& db = *in_grad[2];
114 
115  for (serial_size_t outc = 0; outc < params.out.depth_; outc++) {
116  serial_size_t idx = params.out.get_index(0, 0, outc);
117  const float_t * delta = &curr_delta[sample][idx];
118  const float_t * deltaa = delta + params.out.width_ *
119  params.out.height_;
120  db[sample][outc] += std::accumulate(delta, deltaa, float_t(0));
121  }
122  }
123  });
124 }
125 
126 } // namespace kernels
127 } // namespace core
128 } // namespace tiny_dnn