【发布时间】:2018-12-21 07:32:54
【问题描述】:
我想计算加权均方误差,其中权重是数据中的一个向量。我根据堆栈溢出可用的建议编写了自定义代码。
功能如下:
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')
问候
【问题讨论】:
标签: python r keras loss-function