[英]How do I write my own custom loss function when I do not have the true values?
I'm trying to figure out a way to create my own loss function. 我试图找出一种创建自己的损失函数的方法。 I'm using keras_model_sequential() model on R.
我在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)
}
The problem is that I do not have the basic y_pred and y_true variables (which keras's default loss functions require), as I do not have labeled examples. 问题是我没有基本的y_pred和y_true变量(keras的默认损失函数需要这些变量),因为我没有带标签的示例。 I just have a random model with defined input.
我只是具有定义输入的随机模型。 And my goal is to shape the model by minimizing the cost of my loss function.
我的目标是通过最小化损失函数的成本来塑造模型。 In other words, I want my network to learn itself the good values for the output (y).
换句话说,我希望我的网络能够为输出(y)学习良好的价值。
Edit: 编辑:
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)
}
And now comes the problem... 现在出现了问题...
model2 %>% compile(
loss = custom_loss(y_pred, y_true),
optimizer = optimizer_nadam(),
metrics = c("mae")
)
Ok, now I get it. 好吧,现在我明白了。 Since you use tensorflow backend you have to pass tensor objects in the loss functions and import the backend of tensorflow for calculations.
由于您使用tensorflow后端,因此必须在损失函数中传递张量对象并导入tensorflow后端进行计算。
So 所以
delta <- model2 %>% predict(x_train) adjusted_input <- input_vector + delta adjusted_y <- model1 %>% predict(adjusted_input) y_pred <- adjusted_
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.