Loading...
Searching...
No Matches
pooling_layer.h
Go to the documentation of this file.
1// SPDX-License-Identifier: AGPL-3.0-only
2// Copyright (C) 2026 Artificial Intelligence Techniques, S.L. and Roberto López González
3
4#pragma once
5
8
9#ifdef OPENNN_HAS_CUDA
10struct MaxPoolGeometry;
11#endif
12
13namespace opennn
14{
15
17 Index, Index, Index,
18 Index, Index,
19 Index, Index,
20 Index, Index,
21 bool max_pooling);
24 Index, Index, Index,
25 Index, Index,
26 Index, Index,
27 Index, Index,
28 bool max_pooling);
29
31{
32 enum Method { Max, Average };
33
34 Index input_height = 0;
35 Index input_width = 0;
36 Index input_channels = 0;
37
38 Index pool_height = 1;
39 Index pool_width = 1;
40 Index row_stride = 1;
41 Index column_stride = 1;
42 Index padding_height = 0;
43 Index padding_width = 0;
44
46
47 Index get_output_height() const noexcept;
48 Index get_output_width() const noexcept;
49
51
52 void set(Index, Index, Index,
53 Index, Index,
54 Index, Index,
55 Index, Index,
56 Method);
57
58 PoolOperator() = default;
59 PoolOperator(const PoolOperator&) = delete;
60 PoolOperator& operator=(const PoolOperator&) = delete;
61
63 void back_propagate(ForwardPropagation&, BackPropagation&, size_t) const override;
64
65#ifdef OPENNN_HAS_CUDA
67
68 bool own_max_pooling(const TensorView& input, const TensorView& mask) const noexcept;
69 ::MaxPoolGeometry max_pool_geometry(const TensorView& input) const noexcept;
70
71private:
72
74#endif
75};
76
83
86
87class Pooling final : public Layer
88{
89public:
90
91 Pooling(const Shape& = {2, 2, 1},
92 const Shape& = { 2, 2 },
93 const Shape& = { 2, 2 },
94 const Shape& = { 0, 0 },
96 const string& = "pooling_layer");
97
98 Shape get_input_shape() const noexcept override
99 { return {pool.input_height, pool.input_width, pool.input_channels}; }
100 Shape get_output_shape() const override;
101
102 Index get_output_height() const;
103 Index get_output_width() const;
104
105 Index get_pool_height() const noexcept { return pool.pool_height; }
106 Index get_pool_width() const noexcept { return pool.pool_width; }
107
108 Index get_row_stride() const noexcept { return pool.row_stride; }
109 Index get_column_stride() const noexcept { return pool.column_stride; }
110
111 Index get_padding_height() const noexcept { return pool.padding_height; }
112 Index get_padding_width() const noexcept { return pool.padding_width; }
113
116
117 bool is_passthrough() const noexcept;
118
119 vector<TensorSpec> get_forward_specs(Index) const override;
120 vector<TensorSpec> get_backward_specs(Index) const override;
121
123 void back_propagate(ForwardPropagation&, BackPropagation&, size_t) const override;
124
125 void set(const Shape& = { 0, 0, 0 },
126 const Shape& = { 1, 1 },
127 const Shape& = { 1, 1 },
128 const Shape& = { 0, 0 },
130 const string& = "pooling_layer");
131
132 bool accepts_input_rank(Index rank) const override { return is_one_of(rank, 3); }
133
134 void apply_input_shape(const Shape&) override;
136 void set_pooling_method(const string&);
137
138 void read_JSON_body(const Json*) override;
139 void write_JSON_body(JsonWriter&) const override;
140
141private:
142
143 PoolOperator pool;
144
145 enum Forward {Input, MaximalIndices, Output};
146
147 ForwardSlotKind get_forward_slot_kind(size_t slot) const override
148 {
149 return slot == MaximalIndices ? ForwardSlotKind::TrainingOnly : ForwardSlotKind::Pooled;
150 }
151
152 void update_pool_operator();
153};
154
155}
Definition json.h:92
Definition json.h:24
Layer(LayerType type, Trainability trainability=Trainability::Trainable)
Definition layer.h:210
void forward_propagate(ForwardPropagation &, size_t, ForwardPropagationMode) override
bool accepts_input_rank(Index rank) const override
Definition pooling_layer.h:132
Index get_pool_width() const noexcept
Definition pooling_layer.h:106
PoolingMethod get_pooling_method() const noexcept
Definition pooling_layer.h:114
void read_JSON_body(const Json *) override
vector< TensorSpec > get_backward_specs(Index) const override
void set_pooling_method(const string &)
Index get_output_height() const
Index get_padding_height() const noexcept
Definition pooling_layer.h:111
void set_pooling_method(PoolingMethod)
vector< TensorSpec > get_forward_specs(Index) const override
Index get_column_stride() const noexcept
Definition pooling_layer.h:109
void write_JSON_body(JsonWriter &) const override
Index get_row_stride() const noexcept
Definition pooling_layer.h:108
Index get_padding_width() const noexcept
Definition pooling_layer.h:112
Shape get_input_shape() const noexcept override
Definition pooling_layer.h:98
void back_propagate(ForwardPropagation &, BackPropagation &, size_t) const override
void apply_input_shape(const Shape &) override
Index get_pool_height() const noexcept
Definition pooling_layer.h:105
Shape get_output_shape() const override
Index get_output_width() const
void set(const Shape &={ 0, 0, 0 }, const Shape &={ 1, 1 }, const Shape &={ 1, 1 }, const Shape &={ 0, 0 }, PoolingMethod=PoolingMethod::MaxPooling, const string &="pooling_layer")
Pooling(const Shape &={2, 2, 1}, const Shape &={ 2, 2 }, const Shape &={ 2, 2 }, const Shape &={ 0, 0 }, PoolingMethod=PoolingMethod::MaxPooling, const string &="pooling_layer")
bool is_passthrough() const noexcept
Definition configuration.h:10
void pooling_2d_forward(const TensorView &, TensorView &, TensorView &, Index, Index, Index, Index, Index, Index, Index, Index, Index, bool max_pooling)
PoolingMethod string_to_pooling_method(const string &)
cudnnPoolingDescriptor_t DnnPoolingDescriptor
Definition opennn_types.h:153
constexpr bool is_one_of(const T &value, const Candidates &... candidates)
Definition opennn_types.h:273
PoolingMethod
Definition pooling_layer.h:78
@ FirstToken
Definition pooling_layer.h:81
@ MaxPooling
Definition pooling_layer.h:79
@ AveragePooling
Definition pooling_layer.h:80
void pooling_2d_backward(const TensorView &, const TensorView &, TensorView &, Index, Index, Index, Index, Index, Index, Index, Index, Index, bool max_pooling)
const string & pooling_method_to_string(PoolingMethod)
ForwardSlotKind
Definition layer.h:18
@ TrainingOnly
Definition layer.h:18
@ Pooled
Definition layer.h:18
ForwardPropagationMode
Definition forward_propagation.h:33
Definition back_propagation.h:19
Definition device_backend.h:17
Definition forward_propagation.h:52
Definition operator.h:28
Definition pooling_layer.h:31
Index padding_width
Definition pooling_layer.h:43
Index get_output_height() const noexcept
void forward_propagate(ForwardPropagation &, size_t, ForwardPropagationMode) override
Index get_output_width() const noexcept
Method
Definition pooling_layer.h:32
@ Average
Definition pooling_layer.h:32
@ Max
Definition pooling_layer.h:32
Method method
Definition pooling_layer.h:45
::MaxPoolGeometry max_pool_geometry(const TensorView &input) const noexcept
void back_propagate(ForwardPropagation &, BackPropagation &, size_t) const override
Index row_stride
Definition pooling_layer.h:40
Index input_channels
Definition pooling_layer.h:36
DnnPoolingDescriptor get_pooling_descriptor() const
Index input_height
Definition pooling_layer.h:34
Index input_width
Definition pooling_layer.h:35
Index column_stride
Definition pooling_layer.h:41
Index pool_width
Definition pooling_layer.h:39
Index pool_height
Definition pooling_layer.h:38
bool own_max_pooling(const TensorView &input, const TensorView &mask) const noexcept
void set(Index, Index, Index, Index, Index, Index, Index, Index, Index, Method)
Index padding_height
Definition pooling_layer.h:42
Definition tensor_types.h:172
Definition tensor_types.h:281
Definition tensor_types.h:540