【发布时间】:2018-11-20 08:10:25
【问题描述】:
我正在尝试找出一种方法来创建我自己的损失函数。我在 R 上使用 keras_model_sequential() 模型。
custom_loss <- function(x){
post <- second_model(x) #the current model
pri <- first_model() #another already defined model
LOSS <- sum((pri-post)^2)
return(LOSS)
}
问题是我没有基本的 y_pred 和 y_true 变量(keras 的默认损失函数需要),因为我没有标记示例。我只有一个带有定义输入的随机模型。我的目标是通过最小化损失函数的成本来塑造模型。换句话说,我希望我的网络能够自行学习输出 (y) 的良好值。
编辑:
x_train <- model1 %>% predict(input_vector)
--code defining the model2 --
y_true <- matrix(c(1),100,16) #dummy, because no target values
delta <- model2 %>% predict(x_train)
adjusted_input <- input_vector + delta
adjusted_y <- model1 %>% predict(adjusted_input)
y_pred <- adjusted_y #(just to have the same variable names as argument)
custom_loss <- function(y_pred, y_true){
LOSS <- sum((10-y_pred)^2)
return(LOSS)
}
现在问题来了……
model2 %>% compile(
loss = custom_loss(y_pred, y_true),
optimizer = optimizer_nadam(),
metrics = c("mae")
)
【问题讨论】:
标签: r tensorflow keras