简体   繁体   English

如何将 model 的输入张量传递给损失 function?

[英]How to pass the input tensor of a model to a loss function?

My goal is to create a custom loss function that calculates the loss based on y_true , y_pred and the tensor of the models input layer:我的目标是创建一个自定义损失 function 根据y_truey_pred和模型输入层的张量计算损失:

import numpy as np
from tensorflow import keras as K

input_shape = (16, 16, 1)

input = K.layers.Input(input_shape)
dense = K.layers.Dense(16)(input)
output = K.layers.Dense(1)(dense)

model = K.Model(inputs=input, outputs=output)


def CustomLoss(y_true, y_pred):
  return K.backend.sum(K.backend.abs(y_true - model.input * y_pred))


model.compile(loss=CustomLoss)
model.fit(np.ones(input_shape), np.zeros(input_shape))

However, this code fails with the following error message:但是,此代码失败并显示以下错误消息:

TypeError: Cannot convert a symbolic Keras input/output to a numpy array. This error may indicate that you're trying to pass a symbolic value to a NumPy call, which is not supported. Or, you may be trying to pass Keras symbolic inputs/outputs to a TF API that does not register dispatching, preventing Keras from automatically converting the API call to a lambda layer in the Functional Model.

How can I pass the input tensor of my model to the loss function?如何将我的 model 的输入张量传递给损失 function?

Tensorflow Version: 2.4.1 Tensorflow 版本:2.4.1
Python Version: 3.8.8 Python 版本:3.8.8

You can use add_loss to pass external layers to your loss .您可以使用add_loss将外部层传递给您的loss Here an example:这里有一个例子:

import numpy as np
from tensorflow import keras as K

def CustomLoss(y_true, y_pred, input_l):
    return K.backend.sum(K.backend.abs(y_true - input_l * y_pred))

input_shape = (16, 16, 1)
n_sample = 10

X = np.random.uniform(0,1, (n_sample,) + input_shape)
y = np.random.uniform(0,1, (n_sample,) + input_shape)

inp = K.layers.Input(input_shape)
dense = K.layers.Dense(16)(inp)
out = K.layers.Dense(1)(dense)

target = K.layers.Input(input_shape)
model = K.Model(inputs=[inp,target], outputs=out)

model.add_loss( CustomLoss( target, out, inp ) )
model.compile(loss=None, optimizer='adam')
model.fit(x=[X,y], y=None, epochs=3)

To use the model in inference mode (removing the target from inputs)在推理模式下使用 model(从输入中删除目标)

final_model = K.Model(model.input[0], model.output)
final_model.predict(X)

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

相关问题 如何使用 model 输入损耗 function? - How to use model input in loss function? 如何在 Keras 中编译自定义损失 function ,将预测与来自输入张量的信息连接起来? - How do you compile a custom loss function in Keras that concatenates the prediction with information from the input tensor? 创建 keras 张量,其形状与 model output 用于自定义损失 ZC1C425268E68385D1ABZA77 - Create keras tensor with shape as same as model output for custom loss function 如何在 Keras model 的自定义损失中访问张量的内容 - How can I access to content of Tensor in custom loss of Keras model 如何将TensorFlow张量传递给TFLearn模型 - How to pass a TensorFlow tensor to a TFLearn model 在自定义损失函数中重塑张量 - Reshape tensor in custom loss function 如何在 TensorFlow 中的自定义损失函数中归一化张量? - How to normalize tensor inside custom loss function in TensorFlow? 如何有条件地将值分配给张量[蒙版损失函数]? - How to conditionally assign values to tensor [masking for loss function]? 如何在我的自定义损失 function 中将张量保存到 Numpy 数组? - How to save tensor to Numpy array in my customized loss function? 损失函数中如何在角膜张量上使用numpy函数? - How to use numpy functions on a keras tensor in the loss function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM