R Keras中的自定义损失函数

时间:2018-07-13 00:51:06

标签: python r keras loss-function

我想计算加权均方误差,其中权重是数据中的一个向量。我根据堆栈溢出的建议编写了一个自定义代码。

下面提供了该功能:

weighted_mse <- function(y_true, y_pred,weights){
  # convert tensors to R objects
  K        <- backend()
  y_true   <- K$eval(y_true)
  y_pred   <- K$eval(y_pred)
  weights  <- K$eval(weights)

  # calculate the metric
  loss <- sum(weights*((y_true - y_pred)^2)) 

  # convert to tensor
  return(K$constant(loss))
  }

但是,我不确定如何将自定义函数传递给编译器。如果有人可以帮助我,那将是很棒的。谢谢。

model      <- model %>% compile(
                loss = 'mse', 
                optimizer = 'rmsprop',
                metrics = 'mse')

致谢

2 个答案:

答案 0 :(得分:3)

我还没有将Keras与R一起使用,但是按照documentation中的示例,可能应该可以:

weighted_mse <- function(y_true, y_pred, weights){
    K        <- backend()
    weights  <- K$variable(weights)
    # calculate the metric
    loss <- K$sum(weights * (K$pow(y_true - y_pred, 2))) 
    loss
}

metric_weighted_mse <- custom_metric("weighted_mse", function(y_true, y_pred) {
    weighted_mse(y_true, y_pred, weights)
})

model <- model %>% compile(
    loss = 'mse', 
    optimizer = 'rmsprop',
    metrics = metric_weighted_mse)

请注意,我为损失函数使用了包装器,因为它有一个额外的参数。另外,损失函数将输入作为张量处理,这就是为什么应该使用K$variable(weights)转换权重的原因。

答案 1 :(得分:3)

您无法eval遭受损失。这将破坏图表。

您应该只使用sample_weight方法的fit参数:https://keras.rstudio.com/reference/fit.html

##not sure if this is valid R, but 
##at some point you will call `fit` for training with `X_train` and `Y_train`, 
##so, just add the weights.
history <- model$fit(X_train, Y_train, ..., sample_weight = weights)

仅此而已(不要使用自定义损失)。


仅供参考-将损失函数传递给compile

仅适用于采用y_truey_pred的函数。 (如果您使用的是sample_weights,则不需要)

model      <- model %>% compile(
            loss = weighted_mse, 
            optimizer = 'rmsprop',
            metrics = 'mse')

但是这行不通,您需要类似于@spadarian创建的包装器。

此外,保持数据与权重之间的相关性将非常复杂,这不仅是因为Keras会成批地划分数据,也因为数据会被混洗。