This repository was archived by the owner on Jul 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathPooling.swift
535 lines (482 loc) · 19.1 KB
/
Pooling.swift
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
// Copyright 2019 The TensorFlow Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import _Differentiation
/// A max pooling layer for temporal data.
@frozen
public struct MaxPool1D<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
public typealias TangentVector = EmptyTangentVector
/// The size of the sliding reduction window for pooling.
@noDerivative public let poolSize: Int
/// The stride of the sliding window for temporal dimension.
@noDerivative public let stride: Int
/// The padding algorithm for pooling.
@noDerivative public let padding: Padding
/// Creates a max pooling layer.
///
/// - Parameters:
/// - poolSize: The size of the sliding reduction window for pooling.
/// - stride: The stride of the sliding window for temporal dimension.
/// - padding: The padding algorithm for pooling.
public init(poolSize: Int, stride: Int, padding: Padding) {
precondition(poolSize > 0, "The pooling window size must be greater than 0.")
precondition(stride > 0, "The stride must be greater than 0.")
self.poolSize = poolSize
self.stride = stride
self.padding = padding
}
/// Returns the output obtained from applying the layer to the given input.
///
/// - Parameter input: The input to the layer.
/// - Returns: The output.
@differentiable
public func callAsFunction(_ input: Tensor<Scalar>) -> Tensor<Scalar> {
maxPool2D(
input.expandingShape(at: 1),
filterSize: (1, 1, poolSize, 1),
strides: (1, 1, stride, 1),
padding: padding
).squeezingShape(at: 1)
}
}
/// A max pooling layer for spatial data.
@frozen
public struct MaxPool2D<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
public typealias TangentVector = EmptyTangentVector
/// The size of the sliding reduction window for pooling.
@noDerivative public let poolSize: (Int, Int, Int, Int)
/// The strides of the sliding window for each dimension of a 4-D input.
/// Strides in non-spatial dimensions must be `1`.
@noDerivative public let strides: (Int, Int, Int, Int)
/// The padding algorithm for pooling.
@noDerivative public let padding: Padding
/// Creates a max pooling layer.
public init(poolSize: (Int, Int, Int, Int), strides: (Int, Int, Int, Int), padding: Padding) {
precondition(
poolSize.0 > 0 && poolSize.1 > 0 && poolSize.2 > 0 && poolSize.3 > 0,
"Pooling window sizes must be greater than 0.")
precondition(
strides.0 > 0 && strides.1 > 0 && strides.2 > 0 && strides.3 > 0,
"Strides must be greater than 0.")
self.poolSize = poolSize
self.strides = strides
self.padding = padding
}
/// Returns the output obtained from applying the layer to the given input.
///
/// - Parameter input: The input to the layer.
/// - Returns: The output.
@differentiable
public func callAsFunction(_ input: Tensor<Scalar>) -> Tensor<Scalar> {
maxPool2D(input, filterSize: poolSize, strides: strides, padding: padding)
}
}
extension MaxPool2D {
/// Creates a max pooling layer.
///
/// - Parameters:
/// - poolSize: Vertical and horizontal factors by which to downscale.
/// - strides: The strides.
/// - padding: The padding.
public init(poolSize: (Int, Int), strides: (Int, Int), padding: Padding = .valid) {
self.init(
poolSize: (1, poolSize.0, poolSize.1, 1),
strides: (1, strides.0, strides.1, 1),
padding: padding)
}
}
/// A max pooling layer for spatial or spatio-temporal data.
@frozen
public struct MaxPool3D<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
public typealias TangentVector = EmptyTangentVector
/// The size of the sliding reduction window for pooling.
@noDerivative public let poolSize: (Int, Int, Int, Int, Int)
/// The strides of the sliding window for each dimension of a 5-D input.
/// Strides in non-spatial dimensions must be `1`.
@noDerivative public let strides: (Int, Int, Int, Int, Int)
/// The padding algorithm for pooling.
@noDerivative public let padding: Padding
/// Creates a max pooling layer.
public init(
poolSize: (Int, Int, Int, Int, Int),
strides: (Int, Int, Int, Int, Int),
padding: Padding
) {
precondition(
poolSize.0 > 0 && poolSize.1 > 0 && poolSize.2 > 0 && poolSize.3 > 0 && poolSize.4 > 0,
"Pooling window sizes must be greater than 0."
)
precondition(
strides.0 > 0 && strides.1 > 0 && strides.2 > 0 && strides.3 > 0 && strides.4 > 0,
"Strides must be greater than 0."
)
self.poolSize = poolSize
self.strides = strides
self.padding = padding
}
/// Returns the output obtained from applying the layer to the given input.
///
/// - Parameter input: The input to the layer.
/// - Returns: The output.
@differentiable
public func callAsFunction(_ input: Tensor<Scalar>) -> Tensor<Scalar> {
maxPool3D(input, filterSize: poolSize, strides: strides, padding: padding)
}
}
extension MaxPool3D {
/// Creates a max pooling layer.
///
/// - Parameters:
/// - poolSize: Vertical and horizontal factors by which to downscale.
/// - strides: The strides.
/// - padding: The padding.
public init(poolSize: (Int, Int, Int), strides: (Int, Int, Int), padding: Padding = .valid) {
self.init(
poolSize: (1, poolSize.0, poolSize.1, poolSize.2, 1),
strides: (1, strides.0, strides.1, strides.2, 1),
padding: padding)
}
}
extension MaxPool3D {
/// Creates a max pooling layer with the specified pooling window size and stride. All pooling
/// sizes and strides are the same.
public init(poolSize: Int, stride: Int, padding: Padding = .valid) {
self.init(
poolSize: (poolSize, poolSize, poolSize),
strides: (stride, stride, stride),
padding: padding)
}
}
/// An average pooling layer for temporal data.
@frozen
public struct AvgPool1D<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
public typealias TangentVector = EmptyTangentVector
/// The size of the sliding reduction window for pooling.
@noDerivative public let poolSize: Int
/// The stride of the sliding window for temporal dimension.
@noDerivative public let stride: Int
/// The padding algorithm for pooling.
@noDerivative public let padding: Padding
/// Creates an average pooling layer.
///
/// - Parameters:
/// - poolSize: The size of the sliding reduction window for pooling.
/// - stride: The stride of the sliding window for temporal dimension.
/// - padding: The padding algorithm for pooling.
public init(poolSize: Int, stride: Int, padding: Padding) {
precondition(poolSize > 0, "The pooling window size must be greater than 0.")
precondition(stride > 0, "The stride must be greater than 0.")
self.poolSize = poolSize
self.stride = stride
self.padding = padding
}
/// Returns the output obtained from applying the layer to the given input.
///
/// - Parameter input: The input to the layer.
/// - Returns: The output.
@differentiable
public func callAsFunction(_ input: Tensor<Scalar>) -> Tensor<Scalar> {
avgPool2D(
input.expandingShape(at: 1),
filterSize: (1, 1, poolSize, 1),
strides: (1, 1, stride, 1),
padding: padding
).squeezingShape(at: 1)
}
}
/// An average pooling layer for spatial data.
@frozen
public struct AvgPool2D<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
public typealias TangentVector = EmptyTangentVector
/// The size of the sliding reduction window for pooling.
@noDerivative public let poolSize: (Int, Int, Int, Int)
/// The strides of the sliding window for each dimension of a 4-D input.
/// Strides in non-spatial dimensions must be `1`.
@noDerivative public let strides: (Int, Int, Int, Int)
/// The padding algorithm for pooling.
@noDerivative public let padding: Padding
/// Creates an average pooling layer.
public init(poolSize: (Int, Int, Int, Int), strides: (Int, Int, Int, Int), padding: Padding) {
precondition(
poolSize.0 > 0 && poolSize.1 > 0 && poolSize.2 > 0 && poolSize.3 > 0,
"Pooling window sizes must be greater than 0.")
precondition(
strides.0 > 0 && strides.1 > 0 && strides.2 > 0 && strides.3 > 0,
"Strides must be greater than 0.")
self.poolSize = poolSize
self.strides = strides
self.padding = padding
}
/// Returns the output obtained from applying the layer to the given input.
///
/// - Parameter input: The input to the layer.
/// - Returns: The output.
@differentiable
public func callAsFunction(_ input: Tensor<Scalar>) -> Tensor<Scalar> {
avgPool2D(input, filterSize: poolSize, strides: strides, padding: padding)
}
}
extension AvgPool2D {
/// Creates an average pooling layer.
///
/// - Parameters:
/// - poolSize: Vertical and horizontal factors by which to downscale.
/// - strides: The strides.
/// - padding: The padding.
public init(poolSize: (Int, Int), strides: (Int, Int), padding: Padding = .valid) {
self.init(
poolSize: (1, poolSize.0, poolSize.1, 1),
strides: (1, strides.0, strides.1, 1),
padding: padding)
}
}
/// An average pooling layer for spatial or spatio-temporal data.
@frozen
public struct AvgPool3D<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
public typealias TangentVector = EmptyTangentVector
/// The size of the sliding reduction window for pooling.
@noDerivative public let poolSize: (Int, Int, Int, Int, Int)
/// The strides of the sliding window for each dimension of a 5-D input.
/// Strides in non-spatial dimensions must be `1`.
@noDerivative public let strides: (Int, Int, Int, Int, Int)
/// The padding algorithm for pooling.
@noDerivative public let padding: Padding
/// Creates an average pooling layer.
public init(
poolSize: (Int, Int, Int, Int, Int),
strides: (Int, Int, Int, Int, Int),
padding: Padding
) {
precondition(
poolSize.0 > 0 && poolSize.1 > 0 && poolSize.2 > 0 && poolSize.3 > 0 && poolSize.4 > 0,
"Pooling window sizes must be greater than 0."
)
precondition(
strides.0 > 0 && strides.1 > 0 && strides.2 > 0 && strides.3 > 0 && strides.4 > 0,
"Strides must be greater than 0."
)
self.poolSize = poolSize
self.strides = strides
self.padding = padding
}
/// Returns the output obtained from applying the layer to the given input.
///
/// - Parameter input: The input to the layer.
/// - Returns: The output.
@differentiable
public func callAsFunction(_ input: Tensor<Scalar>) -> Tensor<Scalar> {
avgPool3D(input, filterSize: poolSize, strides: strides, padding: padding)
}
}
extension AvgPool3D {
/// Creates an average pooling layer.
///
/// - Parameters:
/// - poolSize: Vertical and horizontal factors by which to downscale.
/// - strides: The strides.
/// - padding: The padding.
public init(poolSize: (Int, Int, Int), strides: (Int, Int, Int), padding: Padding = .valid) {
self.init(
poolSize: (1, poolSize.0, poolSize.1, poolSize.2, 1),
strides: (1, strides.0, strides.1, strides.2, 1),
padding: padding)
}
}
extension AvgPool3D {
/// Creates an average pooling layer with the specified pooling window size and stride. All
/// pooling sizes and strides are the same.
public init(poolSize: Int, strides: Int, padding: Padding = .valid) {
self.init(
poolSize: (poolSize, poolSize, poolSize),
strides: (strides, strides, strides),
padding: padding)
}
}
/// A global average pooling layer for temporal data.
@frozen
public struct GlobalAvgPool1D<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
public typealias TangentVector = EmptyTangentVector
/// Creates a global average pooling layer.
public init() {}
/// Returns the output obtained from applying the layer to the given input.
///
/// - Parameter input: The input to the layer.
/// - Returns: The output.
@differentiable
public func callAsFunction(_ input: Tensor<Scalar>) -> Tensor<Scalar> {
precondition(input.rank == 3, "The rank of the input must be 3.")
return input.mean(squeezingAxes: 1)
}
}
/// A global average pooling layer for spatial data.
@frozen
public struct GlobalAvgPool2D<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
public typealias TangentVector = EmptyTangentVector
/// Creates a global average pooling layer.
public init() {}
/// Returns the output obtained from applying the layer to the given input.
///
/// - Parameter input: The input to the layer.
/// - Returns: The output.
@differentiable
public func callAsFunction(_ input: Tensor<Scalar>) -> Tensor<Scalar> {
precondition(input.rank == 4, "The rank of the input must be 4.")
return input.mean(squeezingAxes: [1, 2])
}
}
/// A global average pooling layer for spatial and spatio-temporal data.
@frozen
public struct GlobalAvgPool3D<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
public typealias TangentVector = EmptyTangentVector
/// Creates a global average pooling layer.
public init() {}
/// Returns the output obtained from applying the layer to the given input.
///
/// - Parameter input: The input to the layer.
/// - Returns: The output.
@differentiable
public func callAsFunction(_ input: Tensor<Scalar>) -> Tensor<Scalar> {
precondition(input.rank == 5, "The rank of the input must be 5.")
return input.mean(squeezingAxes: [1, 2, 3])
}
}
/// A global max pooling layer for temporal data.
@frozen
public struct GlobalMaxPool1D<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
public typealias TangentVector = EmptyTangentVector
/// Creates a global max pooling layer.
public init() {}
/// Returns the output obtained from applying the layer to the given input.
///
/// - Parameters:
/// - input: The input to the layer.
/// - context: The contextual information for the layer application, e.g. the current learning
/// phase.
/// - Returns: The output.
@differentiable
public func callAsFunction(_ input: Tensor<Scalar>) -> Tensor<Scalar> {
precondition(input.rank == 3, "The rank of the input must be 3.")
return input.max(squeezingAxes: 1)
}
}
/// A global max pooling layer for spatial data.
@frozen
public struct GlobalMaxPool2D<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
public typealias TangentVector = EmptyTangentVector
/// Creates a global max pooling layer.
public init() {}
/// Returns the output obtained from applying the layer to the given input.
///
/// - Parameter input: The input to the layer.
/// - Returns: The output.
@differentiable
public func callAsFunction(_ input: Tensor<Scalar>) -> Tensor<Scalar> {
precondition(input.rank == 4, "The rank of the input must be 4.")
return input.max(squeezingAxes: [1, 2])
}
}
/// A global max pooling layer for spatial and spatio-temporal data.
@frozen
public struct GlobalMaxPool3D<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
public typealias TangentVector = EmptyTangentVector
/// Creates a global max pooling layer.
public init() {}
/// Returns the output obtained from applying the layer to the given input.
///
/// - Parameter input: The input to the layer.
/// - Returns: The output.
@differentiable
public func callAsFunction(_ input: Tensor<Scalar>) -> Tensor<Scalar> {
precondition(input.rank == 5, "The rank of the input must be 5.")
return input.max(squeezingAxes: [1, 2, 3])
}
}
/// A fractional max pooling layer for spatial data.
/// Note: `FractionalMaxPool` does not have an XLA implementation, and thus may have performance implications.
@frozen
public struct FractionalMaxPool2D<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
public typealias TangentVector = EmptyTangentVector
/// Pooling ratios for each dimension of input of shape (batch, height, width, channels).
/// Currently pooling in only height and width is supported.
@noDerivative public let poolingRatio: (Double, Double, Double, Double)
/// Determines whether pooling sequence is generated by pseudorandom fashion.
@noDerivative public let pseudoRandom: Bool
/// Determines whether values at the boundary of adjacent pooling cells are used by both cells
@noDerivative public let overlapping: Bool
/// Determines whether a fixed pooling region will be
/// used when iterating over a FractionalMaxPool2D node in the computation graph.
@noDerivative public let deterministic: Bool
/// Seed for the random number generator
@noDerivative public let seed: Int64
/// A second seed to avoid seed collision
@noDerivative public let seed2: Int64
/// Initializes a `FractionalMaxPool` layer with configurable `poolingRatio`.
public init(
poolingRatio: (Double, Double, Double, Double), pseudoRandom: Bool = false,
overlapping: Bool = false, deterministic: Bool = false, seed: Int64 = 0, seed2: Int64 = 0
) {
precondition(
poolingRatio.0 == 1.0 && poolingRatio.3 == 1.0,
"Pooling on batch and channels dimensions not supported.")
precondition(
poolingRatio.1 >= 1.0 && poolingRatio.2 >= 1.0,
"Pooling ratio for height and width dimensions must be at least 1.0")
self.poolingRatio = poolingRatio
self.pseudoRandom = pseudoRandom
self.overlapping = overlapping
self.deterministic = deterministic
self.seed = seed
self.seed2 = seed2
}
/// Returns the output obtained from applying the layer to the given input.
///
/// - Parameter input: The input to the layer.
/// - Returns: The output.
@differentiable
public func callAsFunction(_ input: Tensor<Scalar>) -> Tensor<Scalar> {
fractionalMaxPool2D(
input,
poolingRatio: poolingRatio,
pseudoRandom: pseudoRandom,
overlapping: overlapping,
deterministic: deterministic,
seed: seed,
seed2: seed2)
}
}
extension FractionalMaxPool2D {
/// Creates a fractional max pooling layer.
///
/// - Parameters:
/// - poolingRatio: Pooling ratio for height and width dimensions of input.
/// - pseudoRandom: Determines wheter the pooling sequence is generated
/// in a pseudorandom fashion.
/// - overlapping: Determines whether values at the boundary of adjacent
/// pooling cells are used by both cells.
/// - deterministic: Determines whether a fixed pooling region will be
/// used when iterating over a FractionalMaxPool2D node in the computation graph.
/// - seed: A seed for random number generator.
/// - seed2: A second seed to avoid seed collision.
public init(
poolingRatio: (Double, Double), pseudoRandom: Bool = false,
overlapping: Bool = false, deterministic: Bool = false, seed: Int64 = 0, seed2: Int64 = 0
) {
self.init(
poolingRatio: (1.0, poolingRatio.0, poolingRatio.1, 1.0),
pseudoRandom: pseudoRandom,
overlapping: overlapping,
deterministic: deterministic,
seed: seed,
seed2: seed2)
}
}