Skip to content

Commit e1728fa

Browse files
committed
v0.3.6
给RNN中加入了dropout, L1-L2正则化
1 parent a8b14ac commit e1728fa

12 files changed

Lines changed: 184 additions & 61 deletions

File tree

R/document_control.R

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,39 @@
301301
#' \item \code{units [int]}
302302
#'
303303
#' The number of neurons (or units) in the Recurrent Layer
304-
#' (GRU or LSTM). Conceptually, this parameter represents the memory
305-
#' capacity and complexity of the network; it dictates how much
304+
#' (RNN, GRU or LSTM). Conceptually, this parameter represents the
305+
#' memory capacity and complexity of the network; it dictates how much
306306
#' information about the sequential trials the model can store and
307307
#' process.
308+
#'
309+
#' \item \code{dropout [double]}
310+
#'
311+
#' Dropout is a powerful regularization technique used to prevent
312+
#' overfitting in RNNs. During each training iteration, a predefined
313+
#' percentage of neurons (units) are randomly "dropped" or deactivated
314+
#' by setting their activations to zero.
315+
#'
316+
#' \item \code{L [int]}
317+
#'
318+
#' This parameter determines the type of regularization applied to the
319+
#' log-likelihood to penalize model complexity, which helps prevent
320+
#' overfitting. The default is \code{0}.
321+
#' \itemize{
322+
#' \item \code{L = 0}: No regularization.
323+
#' \item \code{L = 1}: L1 regularization (Lasso), which adds a
324+
#' penalty proportional to the sum of the absolute values of
325+
#' the free parameters.
326+
#' \item \code{L = 2}: L2 regularization (Ridge), which adds a
327+
#' penalty proportional to the sum of the squared values of
328+
#' the free parameters.
329+
#' }
330+
#'
331+
#' \item \code{penalty [double]}
332+
#'
333+
#' This parameter specifies the strength of the regularization, acting
334+
#' as a multiplier for the penalty term defined by \code{L}. A larger
335+
#' value imposes a stronger penalty on the free parameters. The
336+
#' default value is \code{1e-5}.
308337
#'
309338
#' \item \code{batch_size [int]}
310339
#'
@@ -363,6 +392,9 @@
363392
#' loss = "MSE",
364393
#' info = c(colnames$object, colnames$action),
365394
#' units = 128,
395+
#' dropout = 0
396+
#' L = 0,
397+
#' penalty = 1e-5,
366398
#' batch_size = 10,
367399
#' epochs = 100,
368400
#' check = TRUE

R/engine_RNN.R

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ engine_RNN <- function(
197197
} else if (loss == "MAE") {
198198
units_out <- n_params
199199
loss_func <- "mean_absolute_error"
200-
} else if (loss == "Huber") {
200+
} else if (loss == "HBR") {
201201
units_out <- n_params
202202
loss_func <- "huber_loss"
203203
} else {
@@ -216,55 +216,79 @@ engine_RNN <- function(
216216
object = RNN,
217217
units = units,
218218
input_shape = c(n_trials, n_info),
219-
return_sequences = FALSE
220219
)
221220
},
222221
"GRU" = {
223222
RNN <- keras::layer_gru(
224223
object = RNN,
225224
units = units,
226225
input_shape = c(n_trials, n_info),
227-
return_sequences = FALSE,
228226
)
229227
},
230228
"LSTM" = {
231229
RNN <- keras::layer_lstm(
232230
object = RNN,
233231
units = units,
234232
input_shape = c(n_trials, n_info),
235-
return_sequences = FALSE,
236233
)
237234
},
238235
"BiRNN" = {
239236
RNN <- keras::bidirectional(
240237
object = RNN,
241-
layer = keras::layer_simple_rnn(
242-
units = units,
243-
return_sequences = FALSE
244-
),
238+
layer = keras::layer_simple_rnn(units = units),
245239
input_shape = c(n_trials, n_info)
246240
)
247241
},
248242
"BiGRU" = {
249243
RNN <- keras::bidirectional(
250244
object = RNN,
251-
layer = keras::layer_gru(units = units, return_sequences = FALSE),
245+
layer = keras::layer_gru(units = units),
252246
input_shape = c(n_trials, n_info)
253247
)
254248
},
255249
"BiLSTM" = {
256250
RNN <- keras::bidirectional(
257251
object = RNN,
258-
layer = keras::layer_lstm(units = units, return_sequences = FALSE),
252+
layer = keras::layer_lstm(units = units),
259253
input_shape = c(n_trials, n_info)
260254
)
261255
},
262-
) |>
263-
# Hidden Layer
264-
keras::layer_dense(
265-
units = units / 2,
266-
activation = "relu"
267-
) |>
256+
)
257+
258+
# Hidden Layer
259+
switch(
260+
EXPR = as.character(L),
261+
"0" = {
262+
RNN <- keras::layer_dense(
263+
object = RNN,
264+
units = units / 2,
265+
activation = "relu",
266+
kernel_initializer = keras::initializer_he_normal()
267+
)
268+
},
269+
"1" = {
270+
RNN <- keras::layer_dense(
271+
object = RNN,
272+
units = units / 2,
273+
activation = "relu",
274+
kernel_initializer = keras::initializer_he_normal(),
275+
kernel_regularizer = keras::regularizer_l1(penalty)
276+
)
277+
},
278+
"2" = {
279+
RNN <- keras::layer_dense(
280+
object = RNN,
281+
units = units / 2,
282+
activation = "relu",
283+
kernel_initializer = keras::initializer_he_normal(),
284+
kernel_regularizer = keras::regularizer_l2(penalty)
285+
)
286+
}
287+
)
288+
289+
RNN <- RNN |>
290+
# Dropout Layer
291+
keras::layer_dropout(rate = dropout) |>
268292
# Output Layer
269293
keras::layer_dense(
270294
units = units_out,

R/estimate_2_RNN.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ estimate_2_RNN <- function(
113113
loss = "MSE",
114114
info = c(colnames$object, colnames$action),
115115
units = 128,
116+
dropout = 0,
117+
L = 0,
118+
penalty = 1e-5,
116119
batch_size = 10,
117120
epochs = 100,
118121
check = TRUE

R/step_2_rcv_d.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ rcv_d <- function(
204204
loss = "MSE",
205205
info = c(colnames$object, colnames$action),
206206
units = 128,
207+
dropout = 0,
208+
L = 0,
209+
penalty = 1e-5,
207210
batch_size = 10,
208211
epochs = 100,
209212
check = TRUE

R/step_3_fit_p.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ fit_p <- function(
167167
loss = "MSE",
168168
info = c(colnames$object, colnames$action),
169169
units = 128,
170+
dropout = 0,
171+
L = 0,
172+
penalty = 1e-5,
170173
batch_size = 10,
171174
epochs = 100,
172175
check = TRUE

R/zzz.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ utils::globalVariables(c(
1515
# ABC
1616
"tol", "reduction", "ncomp", "metric",
1717
# RNN,
18-
"info", "layer", "loss", "units", "batch_size", "epochs", "check"
18+
"layer", "loss", "info", "units", "dropout", "L", "penalty",
19+
"batch_size", "epochs", "check"
1920
))

dev/CODE/3_Estimate_SBI.Rmd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,9 @@ recovery.RNN <- multiRL::rcv_d(
245245
lowers = list(c(0, 0)),
246246
uppers = list(c(1, 10)),
247247
control = list(
248-
core = 10, sample = 100, train = 100,
248+
core = 10, sample = 100, train = 1000,
249249
#tol = 0.5, reduction = "PCA",
250-
layer = "BiLSTM", loss = "MDN", check = FALSE
250+
layer = "GRU", loss = "MSE", check = FALSE, dropout = 0, L = 2
251251
)
252252
)
253253
```

docs/pkgdown.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ articles:
88
Step_2_rcv_d: Step_2_rcv_d.html
99
Step_3_fit_p: Step_3_fit_p.html
1010
Step_4_rpl_e: Step_4_rpl_e.html
11-
last_built: 2026-03-28T09:04Z
11+
last_built: 2026-03-28T16:19Z
1212
urls:
1313
reference: https://yuki-961004.github.io/multiRL/reference
1414
article: https://yuki-961004.github.io/multiRL/articles

docs/reference/control.html

Lines changed: 61 additions & 36 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/search.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)