简体   繁体   中英

How can I address individual outputs of a neural network from a custom Tensorflow loss function?

I'd like to implement a custom loss function (specifically for this ) for training a NN in Tensorflow, in which NN has two outputs, and the loss function is a formula involving both outputs and the expected output. How can I individually adress those? Examples from tensorflow it looks eg like:

def mean_absolute_percentage_error(y_true, y_pred):
  y_pred = ops.convert_to_tensor(y_pred)
  y_true = math_ops.cast(y_true, y_pred.dtype)
  diff = math_ops.abs(
      (y_true - y_pred) / K.clip(math_ops.abs(y_true), K.epsilon(), None))
  return 100. * K.mean(diff, axis=-1)

What exactly are the tensors passed in here as y_true - what shape do they have? I haven't been able to find any documentation about that. All the examples I've seen for custom losses just calculate all outputs at once, whereas I need to pick out individual outputs.

Please note: I cannot define the network into two outputs and define a separate loss for each output, as the Model.compile documentation allows / as it is done here , since the loss function is a function of all three values.

我没有找到有关参数y_pred和y_true的形状的文档,但以下似乎有效:访问单个输出(假设输出是一维的),您可以在张量的最后一个轴上切片:例如y_pred[..., 0:1]是第一个输出, y_pred[..., 1::2]是每个奇数编号的输出, y_true[..., 0::2]每个偶数编号的标记,依此类推。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM