/
layers-regularization.R
447 lines (432 loc) · 15.2 KB
/
layers-regularization.R
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
#' Layer that applies an update to the cost function based input activity.
#'
#' @description
#'
#' # Input Shape
#' Arbitrary. Use the keyword argument `input_shape`
#' (tuple of integers, does not include the samples axis)
#' when using this layer as the first layer in a model.
#'
#' # Output Shape
#' Same shape as input.
#'
#' @param l1
#' L1 regularization factor (positive float).
#'
#' @param l2
#' L2 regularization factor (positive float).
#'
#' @param object
#' Object to compose the layer with. A tensor, array, or sequential model.
#'
#' @param ...
#' For forward/backward compatability.
#'
#' @inherit layer_dense return
#' @export
#' @family regularization layers
#' @family layers
#' @seealso
#' + <https://keras.io/api/layers/regularization_layers/activity_regularization#activityregularization-class>
# + <https://www.tensorflow.org/api_docs/python/tf/keras/layers/ActivityRegularization>
#' @tether keras.layers.ActivityRegularization
layer_activity_regularization <-
function (object, l1 = 0, l2 = 0, ...)
{
args <- capture_args(list(input_shape = normalize_shape,
batch_size = as_integer, batch_input_shape = normalize_shape),
ignore = "object")
create_layer(keras$layers$ActivityRegularization, object,
args)
}
#' Applies dropout to the input.
#'
#' @description
#' The `Dropout` layer randomly sets input units to 0 with a frequency of
#' `rate` at each step during training time, which helps prevent overfitting.
#' Inputs not set to 0 are scaled up by `1 / (1 - rate)` such that the sum over
#' all inputs is unchanged.
#'
#' Note that the `Dropout` layer only applies when `training` is set to `TRUE`
#' in `call()`, such that no values are dropped during inference.
#' When using `model.fit`, `training` will be appropriately set to `TRUE`
#' automatically. In other contexts, you can set the argument explicitly
#' to `TRUE` when calling the layer.
#'
#' (This is in contrast to setting `trainable=FALSE` for a `Dropout` layer.
#' `trainable` does not affect the layer's behavior, as `Dropout` does
#' not have any variables/weights that can be frozen during training.)
#'
#' # Call Arguments
#' - `inputs`: Input tensor (of any rank).
#' - `training`: Python boolean indicating whether the layer should behave in
#' training mode (adding dropout) or in inference mode (doing nothing).
#'
#' @param rate
#' Float between 0 and 1. Fraction of the input units to drop.
#'
#' @param noise_shape
#' 1D integer tensor representing the shape of the
#' binary dropout mask that will be multiplied with the input.
#' For instance, if your inputs have shape
#' `(batch_size, timesteps, features)` and
#' you want the dropout mask to be the same for all timesteps,
#' you can use `noise_shape=(batch_size, 1, features)`.
#'
#' @param seed
#' An R integer to use as random seed.
#'
#' @param object
#' Object to compose the layer with. A tensor, array, or sequential model.
#'
#' @param ...
#' For forward/backward compatability.
#'
#' @inherit layer_dense return
#' @export
#' @family regularization layers
#' @family layers
#' @seealso
#' + <https://keras.io/api/layers/regularization_layers/dropout#dropout-class>
# + <https://www.tensorflow.org/api_docs/python/tf/keras/layers/Dropout>
#' @tether keras.layers.Dropout
layer_dropout <-
function (object, rate, noise_shape = NULL, seed = NULL, ...)
{
args <- capture_args(list(noise_shape = as_integer, seed = as_integer,
input_shape = normalize_shape, batch_size = as_integer,
batch_input_shape = normalize_shape), ignore = "object")
create_layer(keras$layers$Dropout, object, args)
}
#' Applies Alpha Dropout to the input.
#'
#' @description
#' Alpha Dropout is a `Dropout` that keeps mean and variance of inputs
#' to their original values, in order to ensure the self-normalizing property
#' even after this dropout.
#' Alpha Dropout fits well to Scaled Exponential Linear Units (SELU) by
#' randomly setting activations to the negative saturation value.
#'
#' # Call Arguments
#' - `inputs`: Input tensor (of any rank).
#' - `training`: R boolean indicating whether the layer should behave in
#' training mode (adding alpha dropout) or in inference mode
#' (doing nothing).
#'
#' @param rate
#' Float between 0 and 1. The multiplicative noise will have
#' standard deviation `sqrt(rate / (1 - rate))`.
#'
#' @param noise_shape
#' 1D integer tensor representing the shape of the
#' binary alpha dropout mask that will be multiplied with the input.
#' For instance, if your inputs have shape
#' `(batch_size, timesteps, features)` and
#' you want the alpha dropout mask to be the same for all timesteps,
#' you can use `noise_shape = (batch_size, 1, features)`.
#'
#' @param seed
#' An integer to use as random seed.
#'
#' @param object
#' Object to compose the layer with. A tensor, array, or sequential model.
#'
#' @param ...
#' For forward/backward compatability.
#'
#' @inherit layer_dense return
#' @export
#' @family regularization layers
#' @family layers
#' @tether keras.layers.AlphaDropout
#' @seealso
#' + <https://www.tensorflow.org/api_docs/python/tf/keras/layers/AlphaDropout>
layer_alpha_dropout <-
function (object, rate, noise_shape = NULL, seed = NULL, ...)
{
args <- capture_args(list(noise_shape = as_integer_array, seed = as_integer,
input_shape = normalize_shape, batch_size = as_integer,
batch_input_shape = normalize_shape), ignore = "object")
create_layer(keras$layers$AlphaDropout, object, args)
}
#' Apply multiplicative 1-centered Gaussian noise.
#'
#' @description
#' As it is a regularization layer, it is only active at training time.
#'
#' # Call Arguments
#' - `inputs`: Input tensor (of any rank).
#' - `training`: Python boolean indicating whether the layer should behave in
#' training mode (adding dropout) or in inference mode (doing nothing).
#'
#' @param rate
#' Float, drop probability (as with `Dropout`).
#' The multiplicative noise will have
#' standard deviation `sqrt(rate / (1 - rate))`.
#'
#' @param seed
#' Integer, optional random seed to enable deterministic behavior.
#'
#' @param object
#' Object to compose the layer with. A tensor, array, or sequential model.
#'
#' @param ...
#' For forward/backward compatability.
#'
#' @inherit layer_dense return
#' @export
#' @family regularization layers
#' @family layers
#' @seealso
#' + <https://keras.io/api/layers/regularization_layers/gaussian_dropout#gaussiandropout-class>
# + <https://www.tensorflow.org/api_docs/python/tf/keras/layers/GaussianDropout>
#' @tether keras.layers.GaussianDropout
layer_gaussian_dropout <-
function (object, rate, seed = NULL, ...)
{
args <- capture_args(list(seed = as_integer, input_shape = normalize_shape,
batch_size = as_integer, batch_input_shape = normalize_shape),
ignore = "object")
create_layer(keras$layers$GaussianDropout, object, args)
}
#' Apply additive zero-centered Gaussian noise.
#'
#' @description
#' This is useful to mitigate overfitting
#' (you could see it as a form of random data augmentation).
#' Gaussian Noise (GS) is a natural choice as corruption process
#' for real valued inputs.
#'
#' As it is a regularization layer, it is only active at training time.
#'
#' # Call Arguments
#' - `inputs`: Input tensor (of any rank).
#' - `training`: Python boolean indicating whether the layer should behave in
#' training mode (adding noise) or in inference mode (doing nothing).
#'
#' @param stddev
#' Float, standard deviation of the noise distribution.
#'
#' @param seed
#' Integer, optional random seed to enable deterministic behavior.
#'
#' @param object
#' Object to compose the layer with. A tensor, array, or sequential model.
#'
#' @param ...
#' For forward/backward compatability.
#'
#' @inherit layer_dense return
#' @export
#' @family regularization layers
#' @family layers
#' @seealso
#' + <https://keras.io/api/layers/regularization_layers/gaussian_noise#gaussiannoise-class>
# + <https://www.tensorflow.org/api_docs/python/tf/keras/layers/GaussianNoise>
#' @tether keras.layers.GaussianNoise
layer_gaussian_noise <-
function (object, stddev, seed = NULL, ...)
{
args <- capture_args(list(seed = as_integer, input_shape = normalize_shape,
batch_size = as_integer, batch_input_shape = normalize_shape),
ignore = "object")
create_layer(keras$layers$GaussianNoise, object, args)
}
#' Spatial 1D version of Dropout.
#'
#' @description
#' This layer performs the same function as Dropout, however, it drops
#' entire 1D feature maps instead of individual elements. If adjacent frames
#' within feature maps are strongly correlated (as is normally the case in
#' early convolution layers) then regular dropout will not regularize the
#' activations and will otherwise just result in an effective learning rate
#' decrease. In this case, `SpatialDropout1D` will help promote independence
#' between feature maps and should be used instead.
#'
#' # Call Arguments
#' - `inputs`: A 3D tensor.
#' - `training`: Python boolean indicating whether the layer
#' should behave in training mode (applying dropout)
#' or in inference mode (pass-through).
#'
#' # Input Shape
#' 3D tensor with shape: `(samples, timesteps, channels)`
#'
#' # Output Shape
#' Same as input.
#'
#' # Reference
#' - [Tompson et al., 2014](https://arxiv.org/abs/1411.4280)
#'
#' @param rate
#' Float between 0 and 1. Fraction of the input units to drop.
#'
#' @param object
#' Object to compose the layer with. A tensor, array, or sequential model.
#'
#' @param name
#' String, name for the object
#'
#' @param seed
#' Initial seed for the random number generator
#'
#' @param dtype
#' datatype (e.g., `"float32"`).
#'
#' @inherit layer_dense return
#' @export
#' @family spatial dropout regularization layers
#' @family regularization layers
#' @family layers
#' @seealso
#' + <https://keras.io/api/layers/regularization_layers/spatial_dropout1d#spatialdropout1d-class>
# + <https://www.tensorflow.org/api_docs/python/tf/keras/layers/SpatialDropout1D>
#' @tether keras.layers.SpatialDropout1D
layer_spatial_dropout_1d <-
function (object, rate, seed = NULL, name = NULL, dtype = NULL)
{
args <- capture_args(list(seed = as_integer, input_shape = normalize_shape,
batch_size = as_integer, batch_input_shape = normalize_shape),
ignore = "object")
create_layer(keras$layers$SpatialDropout1D, object, args)
}
#' Spatial 2D version of Dropout.
#'
#' @description
#' This version performs the same function as Dropout, however, it drops
#' entire 2D feature maps instead of individual elements. If adjacent pixels
#' within feature maps are strongly correlated (as is normally the case in
#' early convolution layers) then regular dropout will not regularize the
#' activations and will otherwise just result in an effective learning rate
#' decrease. In this case, `SpatialDropout2D` will help promote independence
#' between feature maps and should be used instead.
#'
#' # Call Arguments
#' - `inputs`: A 4D tensor.
#' - `training`: Python boolean indicating whether the layer
#' should behave in training mode (applying dropout)
#' or in inference mode (pass-through).
#'
#' # Input Shape
#' 4D tensor with shape: `(samples, channels, rows, cols)` if
#' data_format='channels_first'
#' or 4D tensor with shape: `(samples, rows, cols, channels)` if
#' data_format='channels_last'.
#'
#' # Output Shape
#' Same as input.
#'
#' # Reference
#' - [Tompson et al., 2014](https://arxiv.org/abs/1411.4280)
#'
#' @param rate
#' Float between 0 and 1. Fraction of the input units to drop.
#'
#' @param data_format
#' `"channels_first"` or `"channels_last"`.
#' In `"channels_first"` mode, the channels dimension (the depth)
#' is at index 1, in `"channels_last"` mode is it at index 3.
#' It defaults to the `image_data_format` value found in your
#' Keras config file at `~/.keras/keras.json`.
#' If you never set it, then it will be `"channels_last"`.
#'
#' @param object
#' Object to compose the layer with. A tensor, array, or sequential model.
#'
#' @param name
#' String, name for the object
#'
#' @param seed
#' Initial seed for the random number generator
#'
#' @param dtype
#' datatype (e.g., `"float32"`).
#'
#' @inherit layer_dense return
#' @export
#' @family spatial dropout regularization layers
#' @family regularization layers
#' @family layers
#' @seealso
#' + <https://keras.io/api/layers/regularization_layers/spatial_dropout2d#spatialdropout2d-class>
# + <https://www.tensorflow.org/api_docs/python/tf/keras/layers/SpatialDropout2D>
#' @tether keras.layers.SpatialDropout2D
layer_spatial_dropout_2d <-
function (object, rate, data_format = NULL, seed = NULL, name = NULL,
dtype = NULL)
{
args <- capture_args(list(seed = as_integer, input_shape = normalize_shape,
batch_size = as_integer, batch_input_shape = normalize_shape),
ignore = "object")
create_layer(keras$layers$SpatialDropout2D, object, args)
}
#' Spatial 3D version of Dropout.
#'
#' @description
#' This version performs the same function as Dropout, however, it drops
#' entire 3D feature maps instead of individual elements. If adjacent voxels
#' within feature maps are strongly correlated (as is normally the case in
#' early convolution layers) then regular dropout will not regularize the
#' activations and will otherwise just result in an effective learning rate
#' decrease. In this case, SpatialDropout3D will help promote independence
#' between feature maps and should be used instead.
#'
#' # Call Arguments
#' - `inputs`: A 5D tensor.
#' - `training`: Python boolean indicating whether the layer
#' should behave in training mode (applying dropout)
#' or in inference mode (pass-through).
#'
#' # Input Shape
#' 5D tensor with shape: `(samples, channels, dim1, dim2, dim3)` if
#' data_format='channels_first'
#' or 5D tensor with shape: `(samples, dim1, dim2, dim3, channels)` if
#' data_format='channels_last'.
#'
#' # Output Shape
#' Same as input.
#'
#' # Reference
#' - [Tompson et al., 2014](https://arxiv.org/abs/1411.4280)
#'
#' @param rate
#' Float between 0 and 1. Fraction of the input units to drop.
#'
#' @param data_format
#' `"channels_first"` or `"channels_last"`.
#' In `"channels_first"` mode, the channels dimension (the depth)
#' is at index 1, in `"channels_last"` mode is it at index 4.
#' It defaults to the `image_data_format` value found in your
#' Keras config file at `~/.keras/keras.json`.
#' If you never set it, then it will be `"channels_last"`.
#'
#' @param object
#' Object to compose the layer with. A tensor, array, or sequential model.
#'
#' @param name
#' String, name for the object
#'
#' @param seed
#' Initial seed for the random number generator
#'
#' @param dtype
#' datatype (e.g., `"float32"`).
#'
#' @inherit layer_dense return
#' @export
#' @family spatial dropout regularization layers
#' @family regularization layers
#' @family layers
#' @seealso
#' + <https://keras.io/api/layers/regularization_layers/spatial_dropout3d#spatialdropout3d-class>
# + <https://www.tensorflow.org/api_docs/python/tf/keras/layers/SpatialDropout3D>
#' @tether keras.layers.SpatialDropout3D
layer_spatial_dropout_3d <-
function (object, rate, data_format = NULL, seed = NULL, name = NULL,
dtype = NULL)
{
args <- capture_args(list(seed = as_integer, input_shape = normalize_shape,
batch_size = as_integer, batch_input_shape = normalize_shape),
ignore = "object")
create_layer(keras$layers$SpatialDropout3D, object, args)
}