【发布时间】:2020-03-09 01:56:41
【问题描述】:
我正在学习 R 中的 Keras,并希望构建和优化具有最小 Mean_absolute_percentage_error (MAPE) 的 NN 模型。
我在官方文档page 上找到了这个例子,但它报告了Mean_absolute_error
如何调整该代码以优化 MAPE?
【问题讨论】:
标签: r keras neural-network
我正在学习 R 中的 Keras,并希望构建和优化具有最小 Mean_absolute_percentage_error (MAPE) 的 NN 模型。
我在官方文档page 上找到了这个例子,但它报告了Mean_absolute_error
如何调整该代码以优化 MAPE?
【问题讨论】:
标签: r keras neural-network
这一切都可以通过查看keras的官方文档来解决。 metrics 函数正是您要寻找的。在 keras 中,模型的性能是通过度量函数来判断的。
指标状态的documentation:
"度量函数类似于损失函数,不同之处在于 训练模型时不使用评估指标的结果。 您可以将任何损失函数用作度量函数。”
如果您想使用 mean_absolute_percentage_error 优化模型,您应该寻找损失函数而不是指标。但实际上改变它是一样的。既然您提出了有关 mean_absolute_error 的问题,我假设您想更改指标。
因此,在示例中,您可以使用任何 loss 函数轻松更改 metrics 参数。当然还有 mean_absolute_percentage_error。
build_model <- function() {
model <- keras_model_sequential() %>%
layer_dense(units = 64, activation = "relu",
input_shape = dim(train_data)[2]) %>%
layer_dense(units = 64, activation = "relu") %>%
layer_dense(units = 1)
model %>% compile(
loss = "mse",
optimizer = optimizer_rmsprop(),
metrics = list("mean_absolute_percentage_error")
)
model
}
model <- build_model()
model %>% summary()
只需在图中执行相同操作即可。
library(ggplot2)
plot(history, metrics = "mean_absolute_percentage_error", smooth = FALSE) +
coord_cartesian(ylim = c(0, 5)) #you should change lims accordingly
如果您想更改损失函数,请在您的模型构建中使用它。
loss = "mean_absolute_percentage_error",
编辑:我不小心在这个答案中使用了 python 文档,因为 r documentation 使用了另一种语法。但这在这里并没有什么不同,因为我们只使用了损失函数名称。您也可以像这样使用它:metrics = metric_mean_absolute_percentage_error。 python 文档有时会更详细地解释函数的作用。
【讨论】: