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
//! Border layers that add or remove positions at the ends of the spatial axes
//!
//! The family has 2 halves. [`ZeroPadding1D`], [`ZeroPadding2D`], and [`ZeroPadding3D`] add
//! zero positions. [`Cropping1D`], [`Cropping2D`], and [`Cropping3D`] remove positions. Each
//! half is the backward pass of the other half, so both halves share 1 pair of kernels
//!
//! Every layer in the family leaves the batch axis and the channel axis unchanged. Only the
//! axes between them change extent. Under the crate's channels-last layout a rank-4 input is
//! `[batch, height, width, channels]`, so [`ZeroPadding2D`] and [`Cropping2D`] act on axes 1
//! and 2
//!
//! No layer in the family holds a parameter. Each one caches the shape of the most recent
//! forward input, because the backward pass needs that shape to restore it
//!
//! [`Border1D`], [`Border2D`], and [`Border3D`] carry the per-axis amounts. Every constructor
//! takes `impl Into<..>`, so a call site passes a plain integer or a plain tuple
/// 1D cropping layer
/// 2D cropping layer
/// 3D cropping layer
/// Pad and crop kernels shared by every border layer
/// 1D zero-padding layer
/// 2D zero-padding layer
/// 3D zero-padding layer
pub use Cropping1D;
pub use Cropping2D;
pub use Cropping3D;
pub use ZeroPadding1D;
pub use ZeroPadding2D;
pub use ZeroPadding3D;
/// The border amount of a rank-3 border layer, as 1 `(before, after)` pair
///
/// [`ZeroPadding1D::new`] and [`Cropping1D::new`] take `impl Into<Border1D>`. Pass an integer
/// for an equal amount at both ends of the axis. Pass a `(before, after)` pair for an unequal
/// one
///
/// # Examples
///
/// ```rust
/// use rustyml::neural_network::layers::ZeroPadding1D;
///
/// // 2 zero steps before the first step and 2 after the last
/// let even = ZeroPadding1D::new(2);
///
/// // 1 zero step before the first step and 3 after the last
/// let uneven = ZeroPadding1D::new((1, 3));
/// ```
; 1]);
/// The border amounts of a rank-4 border layer, as 1 `(before, after)` pair per spatial axis
///
/// [`ZeroPadding2D::new`] and [`Cropping2D::new`] take `impl Into<Border2D>`. There are 3
/// forms:
///
/// - an integer `n` puts `n` at all 4 ends
/// - a pair `(height, width)` puts an equal amount at both ends of each axis
/// - a pair of pairs `((top, bottom), (left, right))` names all 4 ends
///
/// The 2-integer form gives 1 amount per axis. It does not give the 2 ends of 1 axis
///
/// # Examples
///
/// ```rust
/// use rustyml::neural_network::layers::ZeroPadding2D;
///
/// // 1 zero row and 1 zero column at every edge
/// let ring = ZeroPadding2D::new(1);
///
/// // 2 zero rows at the top and bottom, 3 zero columns at the left and right
/// let per_axis = ZeroPadding2D::new((2, 3));
///
/// // 1 zero row at the top only, and 2 zero columns at the right only
/// let named = ZeroPadding2D::new(((1, 0), (0, 2)));
/// ```
; 2]);
/// The border amounts of a rank-5 border layer, as 1 `(before, after)` pair per spatial axis
///
/// [`ZeroPadding3D::new`] and [`Cropping3D::new`] take `impl Into<Border3D>`. There are 3
/// forms:
///
/// - an integer `n` puts `n` at all 6 ends
/// - a triple `(dim1, dim2, dim3)` puts an equal amount at both ends of each axis
/// - a triple of pairs names all 6 ends
///
/// The 3-integer form gives 1 amount per axis. It does not give the ends of 1 axis
///
/// # Examples
///
/// ```rust
/// use rustyml::neural_network::layers::ZeroPadding3D;
///
/// // 1 zero plane at every one of the 6 faces
/// let shell = ZeroPadding3D::new(1);
///
/// // 1 plane on the first axis, 2 on the second, and none on the third
/// let per_axis = ZeroPadding3D::new((1, 2, 0));
///
/// // Every end named on its own
/// let named = ZeroPadding3D::new(((1, 0), (0, 2), (1, 1)));
/// ```
; 3]);