我正试图想出一种创建自己的损失函数的方法。我在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")
)
答案 0 :(得分:1)
好的,现在我明白了。由于您使用tensorflow后端,您必须在损失函数中传递张量对象并导入tensorflow的后端以进行计算。
所以