1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//! 2D average pooling layer
//!
//! [`AveragePooling2D`] slides a window across the height and width axes of a `[batch, height,
//! width, channels]` tensor. The layer replaces each window with the mean of its elements.
//! [`AveragePooling2D::new`] sets the strides to `pool_size` and the padding to
//! [`PaddingType::Valid`].
//! [`AveragePooling2D::with_strides`] and [`AveragePooling2D::with_padding`] override those
//! defaults. The backward pass spreads each output gradient evenly over the in-bounds elements of
//! its window. The forward pass therefore caches only the input shape it must restore.
//!
//! [`PaddingType::Valid`]: crate::neural_network::layers::convolution::PaddingType
use crateError;
use crateParamCounts;
use cratePaddingType;
use crate;
use crate;
use crate;
use cratecalculate_output_shape_2d_pooling;
use cratevalidate_built_input;
use crate;
use crate;
/// 2D average pooling layer
///
/// Computes the mean value within each pooling window across height and width.
/// Input tensor shape: `[batch_size, height, width, channels]`. Output tensor shape:
/// `[batch_size, pooled_height, pooled_width, channels]`. With `Valid` padding,
/// `pooled_height = (height - pool_size_h) / stride_h + 1` and
/// `pooled_width = (width - pool_size_w) / stride_w + 1`. With `Same` padding,
/// `pooled_height = ceil(height / stride_h)` and `pooled_width = ceil(width / stride_w)`.
///
/// # Examples
///
/// ```rust
/// use rustyml::neural_network::Shape;
/// use rustyml::neural_network::sequential::SequentialBuilder;
/// use rustyml::neural_network::layers::*;
/// use rustyml::neural_network::optimizers::*;
/// use rustyml::neural_network::losses::*;
/// use ndarray::Array4;
/// use approx::assert_relative_eq;
///
/// // Input tensor: [batch_size, height, width, channels].
/// // batch size 2, a 4x4 pixel image, 3 input channels.
/// let mut input_data = Array4::zeros((2, 4, 4, 3));
///
/// // Set test data to make the average pooling results predictable.
/// for b in 0..2 {
/// for i in 0..4 {
/// for j in 0..4 {
/// for c in 0..3 {
/// input_data[[b, i, j, c]] = (i + j) as f32;
/// }
/// }
/// }
/// }
///
/// let x = input_data.clone().into_dyn();
///
/// // Test AveragePooling2D with a Sequential model.
/// let mut model = SequentialBuilder::new()
/// // strides default to pool_size (2, 2) and padding defaults to Valid.
/// .add(AveragePooling2D::new((2, 2)))
/// .build(&Shape::known(x.shape()))
/// .unwrap();
/// model.compile(RMSprop::new(0.001, 0.9, 1e-8, 0.0).unwrap(), MeanSquaredError::new());
///
/// // Output shape should be [2, 2, 2, 3].
/// let output = model.predict(&x).unwrap();
/// assert_eq!(output.shape(), &[2, 2, 2, 3]);
///
/// // Verify the pooling results.
/// // A 2x2 window with stride 2 holds the average of its 4 elements.
/// for b in 0..2 {
/// for c in 0..3 {
/// // 1st window (0, 0), (0, 1), (1, 0), (1, 1): average (0 + 1 + 1 + 2) / 4 = 1.0.
/// assert_relative_eq!(output[[b, 0, 0, c]], 1.0);
/// // 2nd window (0, 2), (0, 3), (1, 2), (1, 3): average (2 + 3 + 3 + 4) / 4 = 3.0.
/// assert_relative_eq!(output[[b, 0, 1, c]], 3.0);
/// // 3rd window (2, 0), (2, 1), (3, 0), (3, 1): average (2 + 3 + 3 + 4) / 4 = 3.0.
/// assert_relative_eq!(output[[b, 1, 0, c]], 3.0);
/// // 4th window (2, 2), (2, 3), (3, 2), (3, 3): average (4 + 5 + 5 + 6) / 4 = 5.0.
/// assert_relative_eq!(output[[b, 1, 1, c]], 5.0);
/// }
/// }
/// ```
///
/// # Performance
///
/// The pass runs in parallel when its estimated element count
/// (`batch * out_positions * channels * window taps`) clears the gate in
/// [`tuning::pool`](crate::tuning::pool). The gate does not depend on any fixed shape.