我最近开始使用python,更具体地说,是使用Keras开发机器学习应用程序。
我想用以下自定义损失函数训练一个人工神经网络,该函数使用pygmo库中给出的hypervolume:
def hypervolume_difference_loss(y_true, y_pred):
import numpy as np
from pygmo import hypervolume
ref_point = [1.0, 1.0]
ref_PF = np.array(y_true)
out_PF = np.array(y_pred)
hv_d = np.empty(shape=y_true.shape[0], dtype=float)
for i, (ref_point _i, out_PF _i) in enumerate(zip(ref_point , out_PF )):
ref_hv_obj = hypervolume(points=ref_point .reshape(10, -1,order='F'))
out_hv_obj = hypervolume(points=ref_point .reshape(10, -1,order='F'))
ref_hv = ref_hv_obj.compute(ref_point )
out_hv = out_hv_obj.compute(ref_point )
hv_d[i] = abs(ref_hv - out_hv)
return hv_d根据Keras文档,自定义损失函数必须是tensorflow符号函数。我已经找到了几个例子(例如link),展示了如何使用Keras后端函数来编写这样的符号函数,但我无法找到一种方法来编写可以使用外部库结果的符号函数。
我最近在这个link中发现了类似的问题,但没有提供任何解决方案。我能想到的唯一解决办法是用Keras后端表示重写超级卷函数。你能告诉我有没有更方便的方法吗?
我将非常感谢任何形式的帮助或建议。
提前谢谢你。
https://stackoverflow.com/questions/51307319
复制相似问题