在Tensorflow中,如何将多个损失与所需公式相结合
我有一个由单个输出神经元组成的CNN模型,其值在0到1之间。我想计算该特定输出神经元的损失组合。
我使用的是平均绝对误差和平均平方误差,并创建这样的损失:
loss = tf.keras.losses.MeanAbsoluteError() + tf.keras.losses.MeanSquaredError()
现在,由于某些问题,TensorFlow框架不支持这样的损失函数。这是错误:
Traceback (most recent call last):
File "run_kfold.py", line 189, in <module>
loss = tf.keras.losses.MeanAbsoluteError() + tf.keras.losses.MeanSquaredError()
TypeError: unsupported operand type(s) for +: 'MeanAbsoluteError' and 'MeanSquaredError'
任何人都可以建议如何计算特定输出层的组合损失。这将有助于创建多个加权损失,例如:
l_1 = 0.6
l_2 = 0.4
loss = l_1 * tf.keras.losses.MeanAbsoluteError() + l_2 *tf.keras.losses.MeanSquaredError()
然后,我可以将此损失变量传递给 model.compile()函数
model.compile(optimizer=opt,
loss=loss,
metrics = ['accuracy', sensitivity, specificity, tf.keras.metrics.RootMeanSquaredError(name='rmse')]
)
I have a CNN model with a single output neuron consisting of sigmoid activation, hence its value is in between 0 and 1. I wanted to calculate a combination of loss for this particular output neuron.
I was using Mean Absolute Error and Mean Squared Error for the same, and creating a loss like this:
loss = tf.keras.losses.MeanAbsoluteError() + tf.keras.losses.MeanSquaredError()
Now, due to some issue, the tensorflow framework is not supporting loss function like this. Here is the error:
Traceback (most recent call last):
File "run_kfold.py", line 189, in <module>
loss = tf.keras.losses.MeanAbsoluteError() + tf.keras.losses.MeanSquaredError()
TypeError: unsupported operand type(s) for +: 'MeanAbsoluteError' and 'MeanSquaredError'
Can anyone suggest how to calculate combo loss for a certain output layer. This will help to create multiple weighted losses in combination, like this:
l_1 = 0.6
l_2 = 0.4
loss = l_1 * tf.keras.losses.MeanAbsoluteError() + l_2 *tf.keras.losses.MeanSquaredError()
I can then pass this loss variable to the model.compile() function
model.compile(optimizer=opt,
loss=loss,
metrics = ['accuracy', sensitivity, specificity, tf.keras.metrics.RootMeanSquaredError(name='rmse')]
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以编写一个函数并使用
MeanabSoluteError()
andMeansQuaredError()
并计算Custom_loss并返回:You can write a function and use
MeanAbsoluteError()
andMeanSquaredError()
and compute custom_loss and return it: