简体   繁体   English

当我没有真实值时,如何编写自己的自定义损失函数?

[英]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 所以

  1. Import tensorflow backend 导入tensorflow后端
  2. Do these calculations in the loss function directly with tensorflow operations 直接使用张量流操作在损失函数中进行这些计算
 delta <- model2 %>% predict(x_train) adjusted_input <- input_vector + delta adjusted_y <- model1 %>% predict(adjusted_input) y_pred <- adjusted_ 
  1. To input the result of the first model you can use the keras Input layer: 输入第一个模型的结果,可以使用keras输入层:

    layer_input() %>%

Here is a guide for it. 这是它的指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何编写自己的函数来在 R 中创建 z 分数 - How do I write my own function to create a z-score in R 我有列表作为列值。 如何将每个列表项放入自己的行中? - I have lists as column-values. How do I get each list item into its own row? 当初始值未直接包含在目标函数中时,如何在R中编写优化代码? - How do I write optimization code in R when the initial values are not directly contained within the objective function? 如何编写调用数据和列的函数? - How do I write my function calling data and column? 当我有多个函数来执行复合任务时,如何编写 R 包文档? - How Do I Write R Package Documentation When I Have More than One Function to Perform a Composite Task? 如何编写自己的 R cumsum() function? - How can I write my own R cumsum() function? 如何在R中随机生成根据自己的密度function分布的数据? - How do I randomly generate data that is distributed according to my own density function in R? 如何编写中缀 function? - How do I write an infix function? 如何在R中编写摘要功能? - How do I write a summarize function in R? 如何获得自定义颜色并使我的图例具有正确的标签(ggplot)? - How do I get custom colors AND have my legend have the correct labels (ggplot)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM