Python 多输出模型的Keras自定义损失函数?

Python 多输出模型的Keras自定义损失函数?,python,tensorflow,machine-learning,keras,deep-learning,Python,Tensorflow,Machine Learning,Keras,Deep Learning,(您可以在下面的部分中找到最小代码和colab链接) 我将keras模型定义为: model_in=Input((无,无,3)) atts_out、clss_out、regs_out=[]、[]、[] 对于范围(5)中的i: ... # 做我想做的,产生att_out,cls_out,reg_out张量 atts_out.append(att_out) cls_out.append(cls_out) regs_out.append(reg_out) model=keras.model(mode

(您可以在下面的部分中找到最小代码和colab链接)
我将keras模型定义为:

model_in=Input((无,无,3))
atts_out、clss_out、regs_out=[]、[]、[]
对于范围(5)中的i:
...  # 做我想做的,产生att_out,cls_out,reg_out张量
atts_out.append(att_out)
cls_out.append(cls_out)
regs_out.append(reg_out)
model=keras.model(model_in,[atts_out,cls_out,regs_out])
现在我想在
model.compile()
中使用一个自定义的loss函数,但我对它的参数(y\u true和y\u pred)形状感到困惑。假设我想在模型输出的
regs\u out
上应用MSE,我应该如何完成下面的代码

def混合动力系统损耗(y_真,y_pred):
true_regs_out=…#如何从y_true中提取regs_?
pred_regs_out=…#如何从y_pred中提取regs_?
regs_损失=0
对于true_reg_out,pred_reg_out in zip(true_regs_out,pred_regs_out):
#true_reg_out和true_reg_out的形状是什么?是(批次大小、附件形状)?
注册损失+=(真实注册输出-预测注册输出)**2
退货记录损失
完全极小示例 这是另一个完整的例子:

将numpy导入为np
导入tensorflow作为tf
从tensorflow进口keras
从tensorflow.keras.layers导入*
def f1(a、b):
返回tf.reduce_sum((a-b)**1)
def f2(a、b):
返回tf.reduce_sum((a-b)**2)
def f3(a、b):
返回tf.reduce_sum((a-b)**3)
def混合动力系统损耗(y_真,y_pred):
#我想在x1上应用f1,在x2上应用f2,在x3上应用f3
x1_真=…#如何提取x1_真
x1_pred=…#如何提取x1_pred
x2_真=…#如何提取x2_真
x2_pred=…#如何提取x2_pred
x3_真=…#如何提取x3_真
x3_pred=…#如何提取x3_pred
返回f1(x1_真,x1_pred)+f1(x2_真,x2_pred)+f1(x2_真,x2_pred)
模型_in=输入((3,))
x1=密度(10)(模型_-in)
x2=密度(20)(模型_-in)
x3=密度(30)(模型_-in)
model=keras.model(model_in,[[x1,x2],x3])
模型编译('adam',混合损耗)
t0=np.rand.rand(5,3)
t1=np.rand.rand(5,10)+10
t2=np.rand.rand(5,20)+20
t3=np.rand.rand(5,30)+30
模型拟合(t0,[[t1,t2],t3],批次尺寸=2)


完成上面的代码可以解决我的问题,但是,如果您能给我提供有关原生keras中的
y_true
y_pred
类型和形状的详细信息,我将非常感激。您可以为每个输出设置不同的损耗,并以不同的方式对其进行称重。在原生keras中,您可以为每个输出设置不同的损耗,并以不同的方式对其进行称重