library(keras)
build_model <- function() {
model <- keras_model_sequential() %>%
layer_dense(units = 64, activation = "relu",
input_shape = dim(train_data)[[2]]) %>%
regularizer_l1_l2(l1 = 0.01, l2 = 0.01) %>%
layer_dense(units = 64, activation = "relu") %>%
regularizer_l1_l2(l1 = 0.01, l2 = 0.01) %>%
layer_dense(units = 1)
model %>% compile(
optimizer = "rmsprop",
loss = "mse",
metrics = c("mae")
)
}
model <- build_model()
我正在尝试在R中使用keras应用L1和L2正则化。但是,我遇到了错误:
Error in regularizer_l1_l2(., l1 = 0.01, l2 = 0.01) : unused argument (.)
我使用的正则化语法与链接中提到的相同。 https://keras.rstudio.com/reference/regularizer_l1.html 谁能告诉我我在做什么错?
答案 0 :(得分:1)
这将是正确的语法:
library(keras)
build_model <- function() {
model <- keras_model_sequential() %>%
layer_dense(units = 64,
activation = "relu",
kernel_regularizer = regularizer_l1_l2(l1 = 0.01, l2 = 0.01),
input_shape = dim(train_data)[[2]]) %>%
layer_dense(units = 64,
activation = "relu",
kernel_regularizer = regularizer_l1_l2(l1 = 0.01, l2 = 0.01)) %>%
layer_dense(units = 1)
model %>% compile(
optimizer = "rmsprop",
loss = "mse",
metrics = c("mae")
)
}
可复制的示例:
library(keras)
mnist <- dataset_mnist()
train_images <- mnist$train$x
train_labels <- mnist$train$y
test_images <- mnist$test$x
test_labels <- mnist$test$y
train_images <- array_reshape(train_images, c(60000, 28*28))
train_images <- train_images / 255
test_images <- array_reshape(test_images, c(10000, 28*28))
test_images <- test_images / 255
train_labels <- to_categorical(train_labels)
test_labels <- to_categorical(test_labels)
network <- keras_model_sequential() %>%
layer_dense(units = 512,
activation = "relu",
kernel_regularizer = regularizer_l1_l2(l1 = 0.001, l2 = 0.001),
input_shape = c(28 * 28)) %>%
layer_dense(units = 10, activation = "softmax")
network %>% compile(
optimizer = "rmsprop",
loss = "categorical_crossentropy",
metrics = c("accuracy")
)
network %>% fit(train_images,
train_labels,
epochs = 5,
batch_size = 128)
metrics <- network %>% evaluate(test_images, test_labels)
> metrics
#output
$`loss`
[1] 0.6863746
$acc
[1] 0.921