我在R中为Keras和Tensorflow创建了一个自定义损失函数。为了使我的编码尝试看起来合理,我尝试重新创建MSE,并将其与Keras的内置功能进行比较。在这个完全可重现的示例中,我开发了函数my_mse()和metric_mse():
library(keras)
library(tensorflow)
epochs <- 3
dataset <- dataset_boston_housing()
c(c(train_data, train_targets), c(test_data, test_targets)) %<-% dataset
X_mean <- apply(train_data, 2, mean)
X_std <- apply(train_data, 2, sd)
train_data <- scale(train_data, center = X_mean, scale = X_std)
test_data <- scale(test_data, center = X_mean, scale = X_std)
set_random_seed(1234)
model <- keras_model_sequential() %>%
layer_dense(units = 64, activation = "relu", kernel_initializer = initializer_he_uniform(),input_shape = dim(train_data)[[2]]) %>%
layer_dense(units = 64, activation = "relu", kernel_initializer = initializer_he_uniform()) %>%
layer_dense(units = 1)
my_mse <- function(y_true, y_pred){
K <- backend()
loss <- K$mean(K$square(y_true-y_pred))
loss
}
metric_mse <- custom_metric("my_mse", function(y_true, y_pred) {
my_mse(y_true, y_pred)
})
model <- model %>% compile(
loss = "mse",
optimizer = optimizer_adam(lr=0.001),
metrics = metric_mse)
history <- model %>% fit(
train_data, train_targets,
epochs = epochs, batch_size = 2^4,
validation_split=0.2
)
但是,每个时期的度量和损耗结果总是略有不同,例如"1s 33ms/步长-损耗:490.4459-我的最小错误:484.7218-价值_损失:441.9290-我的最小值:446.8440 ".有没有什么明显的东西我遗漏了?
编辑:如果我把metric_mse()作为loss,把"mse"作为度量,那么这两个值实际上是一致的!这怎么可能呢?
提前感谢!
1条答案
按热度按时间pbgvytdp1#
loss
不仅仅是损失函数,可能在层上应用了正则化器,它们也被添加到损失函数中。你必须查看模型。这就是为什么mse作为度量可能与损失不同。如果你的损失函数与mse作为度量匹配,这意味着你的损失函数工作正常。