-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimizers.R
More file actions
356 lines (324 loc) Β· 14.5 KB
/
optimizers.R
File metadata and controls
356 lines (324 loc) Β· 14.5 KB
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
#' @title Lazy Adam
#'
#' @param learning_rate A Tensor or a floating point value. or a schedule that is a tf.keras.optimizers.schedules.LearningRateSchedule The learning rate.
#' @param beta_1 A float value or a constant float tensor. The exponential decay rate for the 1st moment estimates.
#' @param beta_2 A float value or a constant float tensor. The exponential decay rate for the 2nd moment estimates.
#' @param epsilon A small constant for numerical stability. This epsilon is "epsilon hat" in Adam: A Method for Stochastic Optimization. Kingma et al., 2014 (in the formula just before Section 2.1), not the epsilon in Algorithm 1 of the paper.
#' @param amsgrad boolean. Whether to apply AMSGrad variant of this algorithm from the paper "On the Convergence of Adam and beyond". Note that this argument is currently not supported and the argument can only be False.
#' @param name Optional name for the operations created when applying gradients. Defaults to "LazyAdam".
#' @param clipnorm is clip gradients by norm;
#' @param clipvalue is clip gradients by value,
#' @param decay is included for backward compatibility to allow time inverse decay of learning rate.
#' @param lr is included for backward compatibility, recommended to use learning_rate instead.
#' @return Optimizer for use with `keras::compile()`
#'
#' @examples
#'
#' \dontrun{
#' keras_model_sequential() %>%
#' layer_dense(32, input_shape = c(784)) %>%
#' compile(
#' optimizer = optimizer_lazy_adam(),
#' loss='binary_crossentropy',
#' metrics='accuracy'
#' )
#' }
#'
#'
#' @export
optimizer_lazy_adam <- function(learning_rate = 0.001, beta_1 = 0.9, beta_2 = 0.999, epsilon = 1e-7,
amsgrad = FALSE, name = "LazyAdam", clipnorm = NULL, clipvalue = NULL,
decay = NULL, lr = NULL) {
args <- list(
learning_rate = learning_rate,
beta_1 = beta_1,
beta_2 = beta_2,
epsilon = epsilon,
amsgrad = amsgrad,
name = name,
clipnorm = clipnorm,
clipvalue = clipvalue,
decay = decay,
lr = lr
)
args$clipnorm <- clipnorm
args$clipvalue <- clipvalue
args$decay <- decay
args$lr <- lr
do.call(tfa$optimizers$LazyAdam, args)
}
attr(optimizer_lazy_adam, "py_function_name") <- "lazy_adam"
#' @title Conditional Gradient
#'
#' @param learning_rate A Tensor or a floating point value, or a schedule that is a tf$keras$optimizers$schedules$LearningRateSchedule The learning rate.
#' @param lambda_ A Tensor or a floating point value. The constraint.
#' @param epsilon A Tensor or a floating point value. A small constant for numerical stability when handling the case of norm of gradient to be zero.
#' @param use_locking If True, use locks for update operations.
#' @param name Optional name prefix for the operations created when applying gradients. Defaults to 'ConditionalGradient'.
#' @param clipnorm is clip gradients by norm.
#' @param clipvalue is clip gradients by value.
#' @param decay is included for backward compatibility to allow time inverse decay of learning rate.
#' @param lr is included for backward compatibility, recommended to use learning_rate instead.
#' @return Optimizer for use with `keras::compile()`
#' @export
optimizer_conditional_gradient <- function(learning_rate, lambda_, epsilon = 1e-07, use_locking = FALSE,
name = 'ConditionalGradient',
clipnorm = NULL, clipvalue = NULL,
decay = NULL, lr = NULL) {
args <- list(
learning_rate = learning_rate,
lambda_ = lambda_,
epsilon = epsilon,
use_locking = use_locking,
name = name,
clipnorm = clipnorm,
clipvalue = clipvalue,
decay = decay,
lr = lr
)
args$clipnorm <- clipnorm
args$clipvalue <- clipvalue
args$decay <- decay
args$lr <- lr
do.call(tfa$optimizers$ConditionalGradient, args)
}
attr(optimizer_conditional_gradient, "py_function_name") <- "conditional_gradient"
#' @title Layer-wise Adaptive Moments
#'
#' @param learning_rate A `Tensor` or a floating point value. or a schedule that is a `tf$keras$optimizers$schedules$LearningRateSchedule` The learning rate.
#' @param beta_1 A `float` value or a constant `float` tensor. The exponential decay rate for the 1st moment estimates.
#' @param beta_2 A `float` value or a constant `float` tensor. The exponential decay rate for the 2nd moment estimates.
#' @param epsilon A small constant for numerical stability.
#' @param weight_decay_rate weight decay rate.
#' @param exclude_from_weight_decay List of regex patterns of variables excluded from weight decay. Variables whose name contain a substring matching the pattern will be excluded.
#' @param exclude_from_layer_adaptation List of regex patterns of variables excluded from layer adaptation. Variables whose name contain a substring matching the pattern will be excluded.
#' @param name Optional name for the operations created when applying gradients. Defaults to "LAMB".
#'
#' @param clipnorm is clip gradients by norm.
#' @param clipvalue is clip gradients by value.
#' @param decay is included for backward compatibility to allow time inverse decay of learning rate.
#' @param lr is included for backward compatibility, recommended to use learning_rate instead.
#' @return Optimizer for use with `keras::compile()`
#'
#' @examples
#'
#' \dontrun{
#' keras_model_sequential() %>%
#' layer_dense(32, input_shape = c(784)) %>%
#' compile(
#' optimizer = optimizer_lamb(),
#' loss='binary_crossentropy',
#' metrics='accuracy'
#' )
#' }
#'
#'
#' @export
optimizer_lamb <- function(learning_rate = 0.001,
beta_1 = 0.9,
beta_2 = 0.999,
epsilon = 1e-6,
weight_decay_rate = 0.0,
exclude_from_weight_decay = NULL,
exclude_from_layer_adaptation = NULL,
name = "LAMB",
clipnorm = NULL, clipvalue = NULL,
decay = NULL, lr = NULL) {
args <- list(
learning_rate = learning_rate,
beta_1 = beta_1,
beta_2 = beta_2,
epsilon = epsilon,
weight_decay_rate = weight_decay_rate,
exclude_from_weight_decay = exclude_from_weight_decay,
exclude_from_layer_adaptation = exclude_from_layer_adaptation,
name = name,
clipnorm = clipnorm,
clipvalue = clipvalue,
decay = decay,
lr = lr
)
args$clipnorm <- clipnorm
args$clipvalue <- clipvalue
args$decay <- decay
args$lr <- lr
do.call(tfa$optimizers$LAMB, args)
}
attr(optimizer_lamb, "py_function_name") <- "lamb"
#' @title NovoGrad
#'
#' @param learning_rate A `Tensor` or a floating point value. or a schedule that is a `tf$keras$optimizers$schedules$LearningRateSchedule` The learning rate.
#' @param beta_1 A float value or a constant float tensor. The exponential decay rate for the 1st moment estimates.
#' @param beta_2 A float value or a constant float tensor. The exponential decay rate for the 2nd moment estimates.
#' @param epsilon A small constant for numerical stability.
#' @param weight_decay A floating point value. Weight decay for each param.
#' @param amsgrad boolean. Whether to apply AMSGrad variant of this algorithm from the paper "On the Convergence of Adam and beyond"
#' @param grad_averaging determines whether to use Adam style exponential moving averaging for the first order moments.
#' @param name Optional name for the operations created when applying gradients. Defaults to "NovoGrad".
#' @param clipnorm is clip gradients by norm.
#' @param clipvalue is clip gradients by value.
#' @param decay is included for backward compatibility to allow time inverse decay of learning rate.
#' @param lr is included for backward compatibility, recommended to use learning_rate instead.
#' @return Optimizer for use with `keras::compile()`
#'
#' @examples
#'
#' \dontrun{
#' keras_model_sequential() %>%
#' layer_dense(32, input_shape = c(784)) %>%
#' compile(
#' optimizer = optimizer_novograd(),
#' loss='binary_crossentropy',
#' metrics='accuracy'
#' )
#' }
#'
#' @export
optimizer_novograd <- function(learning_rate = 0.001,
beta_1 = 0.9,
beta_2 = 0.999,
epsilon = 1e-7,
weight_decay = 0.0,
grad_averaging = FALSE,
amsgrad = FALSE,
name = "NovoGrad",
clipnorm = NULL, clipvalue = NULL,
decay = NULL, lr = NULL) {
args <- list(
learning_rate = learning_rate,
beta_1 = beta_1,
beta_2 = beta_2,
epsilon = epsilon,
weight_decay = weight_decay,
grad_averaging = grad_averaging,
amsgrad = amsgrad,
name = name,
clipnorm = clipnorm,
clipvalue = clipvalue,
decay = decay,
lr = lr
)
args$clipnorm <- clipnorm
args$clipvalue <- clipvalue
args$decay <- decay
args$lr <- lr
do.call(tfa$optimizers$NovoGrad, args)
}
attr(optimizer_novograd, "py_function_name") <- "novograd"
#' @title Rectified Adam (a.k.a. RAdam)
#'
#' @param learning_rate A `Tensor` or a floating point value. or a schedule that is
#' a `tf$keras$optimizers$schedules$LearningRateSchedule` The learning rate.
#' @param beta_1 A float value or a constant float tensor. The exponential decay rate for the 1st moment estimates.
#' @param beta_2 A float value or a constant float tensor. The exponential decay rate for the 2nd moment estimates.
#' @param epsilon A small constant for numerical stability.
#' @param weight_decay A floating point value. Weight decay for each param.
#' @param amsgrad boolean. Whether to apply AMSGrad variant of this algorithm from the paper
#' "On the Convergence of Adam and beyond".
#' @param sma_threshold A float value. The threshold for simple mean average.
#' @param total_steps An integer. Total number of training steps. Enable warmup by setting a positive value.
#' @param warmup_proportion A floating point value. The proportion of increasing steps.
#' @param min_lr A floating point value. Minimum learning rate after warmup.
#' @param name Optional name for the operations created when applying gradients. Defaults to "RectifiedAdam".
#'
#' @param clipnorm is clip gradients by norm.
#' @param clipvalue is clip gradients by value.
#' @param decay is included for backward compatibility to allow time inverse decay of learning rate.
#' @param lr is included for backward compatibility, recommended to use learning_rate instead.
#'
#' @return Optimizer for use with `keras::compile()`
#' @export
optimizer_radam <- function(learning_rate = 0.001,
beta_1 = 0.9,
beta_2 = 0.999,
epsilon = 1e-7,
weight_decay = 0.0,
amsgrad = FALSE,
sma_threshold = 5.0,
# float for total_steps is here to be able to load models created before
# https://github.com/tensorflow/addons/pull/1375 was merged. It should be
# removed for Addons 0.11.
total_steps = 0,
warmup_proportion = 0.1,
min_lr = 0.0,
name = "RectifiedAdam",
clipnorm = NULL, clipvalue = NULL,
decay = NULL, lr = NULL) {
args <- list(
learning_rate = learning_rate,
beta_1 = beta_1,
beta_2 = beta_2,
epsilon = epsilon,
weight_decay = weight_decay,
amsgrad = amsgrad,
sma_threshold = sma_threshold,
# float for total_steps is here to be able to load models created before
# https://github.com/tensorflow/addons/pull/1375 was merged. It should be
# removed for Addons 0.11.
total_steps = total_steps,
warmup_proportion = warmup_proportion,
min_lr = min_lr,
name = name,
clipnorm = clipnorm,
clipvalue = clipvalue,
decay = decay,
lr = lr
)
args$clipnorm <- clipnorm
args$clipvalue <- clipvalue
args$decay <- decay
args$lr <- lr
do.call(tfa$optimizers$RectifiedAdam, args)
}
attr(optimizer_radam, "py_function_name") <- "radam"
#' @title Yogi
#'
#' @param learning_rate A Tensor or a floating point value. The learning rate.
#' @param beta1 A float value or a constant float tensor. The exponential decay rate for the 1st moment estimates.
#' @param beta2 A float value or a constant float tensor. The exponential decay rate for the 2nd moment estimates.
#' @param epsilon A constant trading off adaptivity and noise.
#' @param l1_regularization_strength A float value, must be greater than or equal to zero.
#' @param l2_regularization_strength A float value, must be greater than or equal to zero.
#' @param initial_accumulator_value The starting value for accumulators. Only positive values are allowed.
#' @param activation Use hard sign or soft tanh to determin sign.
#' @param name Optional name for the operations created when applying gradients. Defaults to "Yogi".
#' @param clipnorm is clip gradients by norm.
#' @param clipvalue is clip gradients by value.
#' @param decay is included for backward compatibility to allow time inverse decay of learning rate.
#' @param lr is included for backward compatibility, recommended to use learning_rate instead.
#'
#' @return Optimizer for use with `keras::compile()`
#' @export
optimizer_yogi <- function(learning_rate = 0.01,
beta1 = 0.9,
beta2 = 0.999,
epsilon = 1e-3,
l1_regularization_strength = 0.0,
l2_regularization_strength = 0.0,
initial_accumulator_value = 1e-6,
activation = "sign",
name = "Yogi",
clipnorm = NULL, clipvalue = NULL,
decay = NULL, lr = NULL) {
args <- list(
learning_rate = learning_rate,
beta1 = beta1,
beta2 = beta2,
epsilon = epsilon,
l1_regularization_strength = l1_regularization_strength,
l2_regularization_strength = l2_regularization_strength,
initial_accumulator_value = initial_accumulator_value,
activation = activation,
name = name,
clipnorm = clipnorm,
clipvalue = clipvalue,
decay = decay,
lr = lr
)
args$clipnorm <- clipnorm
args$clipvalue <- clipvalue
args$decay <- decay
args$lr <- lr
do.call(tfa$optimizers$Yogi, args)
}
attr(optimizer_yogi, "py_function_name") <- "yogi"