BlackCat_Tensors
A GPU-supported autograd and linear algebra library, designed for neural network construction
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
max_pooling.h
Go to the documentation of this file.
1 /*
2  * Max_Pooling.h
3  *
4  * Created on: Nov 13, 2019
5  * Author: joseph
6  */
7 
8 #ifndef BLACKCAT_TENSORS_NEURAL_NETWORKS_LAYERS_MAX_POOLING_H_
9 #define BLACKCAT_TENSORS_NEURAL_NETWORKS_LAYERS_MAX_POOLING_H_
10 
11 #include "layer_base.h"
12 
13 namespace bc {
14 namespace nn {
15 
16 template<class SystemTag, class ValueType>
17 struct Max_Pooling:
18  public Layer_Base<
19  Max_Pooling<SystemTag, ValueType>,
20  Tensor_Descriptor<ValueType, SystemTag, Integer<3>>>
21 {
24 
26 
27  using system_tag = SystemTag;
28  using value_type = ValueType;
32 
33  using greedy_evaluate_delta = std::true_type;
34  using requires_extra_cache = std::true_type;
35  using defines_single_predict = std::true_type;
36 
37 private:
38 
44 
47 
48  Dim<2> m_krnl_dims;
49  Dim<2> m_padding;
50  Dim<2> m_strides;
51 
52 public:
53 
55  Dim<3> img_dims,
56  Dim<2> krnl_dims={3,3},
57  Dim<2> padding={0,0},
58  Dim<2> strides={-1,-1}):
59  parent_type(__func__, img_dims),
60  m_krnl_dims(krnl_dims),
61  m_padding(padding),
62  m_strides(strides == Dim<2>{-1,-1} ? krnl_dims : strides)
63  {
64  Dim<2> img_hw = img_dims.template subdim<0,2>();
65  this->m_output_shape = ((img_hw + padding*2)/m_strides ).concat(img_dims[2]);
66 
67  BC_ASSERT((img_dims > 0).all(), "Max_Pooling img_dims must be greater than 0");
68  BC_ASSERT((m_krnl_dims > 0).all(), "Max_Pooling krnl_dims must be greater than 0");
69  BC_ASSERT((m_strides > 0).all(), "Max_Pooling strides must be greater than 0");
70  BC_ASSERT((m_padding >= 0).all(), "Max_Pooling krnl_dims must be greater than 0 or equal to 0");
71 
72  }
73 
74  template<class Image>
75  auto forward_propagation(const Image& image, Cache& cache)
76  {
77  batched_index_tensor_type mask(this->get_batched_output_shape());
78  mask.zero();
79 
80  batched_tensor_type pooled_image(this->get_batched_output_shape());
81  pooled_image.zero();
82 
84  bc::streams::select_on_get_stream(image),
85  image.expression_template(),
86  pooled_image.expression_template(),
87  mask.expression_template(),
88  m_krnl_dims,
89  m_padding,
90  m_strides);
91 
92  cache.store(index_key_type(), mask);
93  return pooled_image;
94  }
95 
96  template<class Image>
97  auto single_predict(const Image& image, Cache& cache)
98  {
99  index_tensor_type mask(this->get_output_shape());
100  mask.zero();
101 
102  tensor_type pooled_image(this->get_output_shape());
103  pooled_image.zero();
104 
106  bc::streams::select_on_get_stream(image),
107  image.expression_template(),
108  pooled_image.expression_template(),
109  mask.expression_template(),
110  m_krnl_dims,
111  m_padding,
112  m_strides);
113 
114  return pooled_image;
115  }
116 
117  template<class Image, class Delta>
119  const Image& image,
120  const Delta& pooled_delta,
121  Cache& cache)
122  {
123  batched_index_tensor_type& mask = cache.load(index_key_type());
125 
127  bc::streams::select_on_get_stream(image),
128  delta_x.expression_template(),
129  pooled_delta.expression_template(),
130  mask.expression_template(),
131  m_krnl_dims,
132  m_padding,
133  m_strides);
134 
135  return delta_x;
136  }
137 };
138 
139 #ifndef BC_CLING_JIT
140 template<class ValueType, class SystemTag>
142  SystemTag system_tag,
143  Dim<3> img_dims,
144  Dim<2> krnl_dims={3,3},
145  Dim<2> padding={0,0},
146  Dim<2> strides={-1,-1})
147 {
149  img_dims, krnl_dims, padding, strides);
150 }
151 
152 #endif
153 
154 template<class SystemTag>
156  SystemTag system_tag,
157  Dim<3> img_dims,
158  Dim<2> krnl_dims={3,3},
159  Dim<2> padding={0,0},
160  Dim<2> strides={-1,-1})
161 {
162  return Max_Pooling<
163  SystemTag,
164  typename SystemTag::default_floating_point_type>(
165  img_dims, krnl_dims, padding, strides);
166 }
167 
169  Dim<3> img_dims,
170  Dim<2> krnl_dims={3,3},
171  Dim<2> padding={0,0},
172  Dim<2> strides={-1,-1})
173 {
175  typename BLACKCAT_DEFAULT_SYSTEM_T::default_floating_point_type>(
176  img_dims, krnl_dims, padding, strides);
177 }
178 
179 
180 }
181 }
182 
183 
184 
185 #endif /* MAX_POOLING_H_ */
Max_Pooling(Dim< 3 > img_dims, Dim< 2 > krnl_dims={3, 3}, Dim< 2 > padding={0, 0}, Dim< 2 > strides={-1,-1})
Definition: max_pooling.h:54
self_type & zero()
Definition: tensor_base.h:13
std::true_type defines_single_predict
Definition: max_pooling.h:35
Definition: constexpr_int.h:14
Definition: layer_base.h:86
#define BLACKCAT_DEFAULT_SYSTEM_T
Definition: common.h:49
A Dictionary designed to store any type using the &#39;store&#39; and &#39;load&#39; functions.
Definition: layer_cache.h:46
Definition: layer_cache.h:33
std::true_type greedy_evaluate_delta
Definition: max_pooling.h:33
std::true_type requires_extra_cache
Definition: max_pooling.h:34
Max_Pooling< SystemTag, ValueType > max_pooling(SystemTag system_tag, Dim< 3 > img_dims, Dim< 2 > krnl_dims={3, 3}, Dim< 2 > padding={0, 0}, Dim< 2 > strides={-1,-1})
Definition: max_pooling.h:141
auto single_predict(const Image &image, Cache &cache)
Definition: max_pooling.h:97
auto back_propagation(const Image &image, const Delta &pooled_delta, Cache &cache)
Definition: max_pooling.h:118
auto & store(key_type< K, V, cache_key_type::inherit > key, U &&expression)
Definition: layer_cache.h:104
Layer_Base< self_type, input_tensor_descriptor_t > parent_type
Definition: max_pooling.h:30
Definition: cmath.h:17
auto & load(key_type< K, V, cache_key_type::inherit > key, int t_modifier=0) const
Definition: layer_cache.h:80
BCINLINE const Derived & expression_template() const
Definition: expression_template_base.h:22
void max_pooling_forward(Stream stream, Image image, ImageOut out, Indexes mask, Dim< 2 > krnl_shape, Dim< 2 > padding=Dim< 2 >().fill(0), Dim< 2 > strides={-1,-1})
Definition: functions.h:19
SystemTag system_tag
Definition: max_pooling.h:27
#define BC_ASSERT(condition, message)
Definition: common.h:185
Definition: max_pooling.h:17
ValueType value_type
Definition: max_pooling.h:28
BCINLINE auto concat(Ints... value) const
Definition: dim.h:105
Definition: any_map.h:22
void max_pooling_backward(Stream stream, Image image, ImageOut delta, Indexes mask, Dim< 2 > krnl_shape, Dim< 2 > padding=Dim< 2 >().fill(0), Dim< 2 > strides=Dim< 2 >().fill(-1))
Definition: functions.h:59
The Evaluator determines if an expression needs to be greedily optimized.
Definition: algorithms.h:22
Definition: recycle_allocator.h:57
auto forward_propagation(const Image &image, Cache &cache)
Definition: max_pooling.h:75