【发布时间】:2020-05-12 20:54:25
【问题描述】:
我在使用 Keras 自定义损失函数时遇到问题。我希望能够以 numpy 数组的形式访问 truth。 因为它是一个回调函数,我想我不是在急切执行,这意味着我无法使用 backend.get_value() 函数访问它。我也尝试了不同的方法,但它总是回到这个“张量”对象不存在的事实。
我需要在自定义损失函数中创建会话吗?
我使用的是最新的 Tensorflow 2.2。
def custom_loss(y_true, y_pred):
# 4D array that has the label (0) and a multiplier input dependant
truth = backend.get_value(y_true)
loss = backend.square((y_pred - truth[:,:,0]) * truth[:,:,1])
loss = backend.mean(loss, axis=-1)
return loss
model.compile(loss=custom_loss, optimizer='Adam')
model.fit(X, np.stack(labels, X[:, 0], axis=3), batch_size = 16)
我希望能够接触到真相。它有两个组件(标签、乘数,每个项目都不同。我看到了一个依赖于输入的解决方案,但我不确定如何访问该值。Custom loss function in Keras based on the input data
【问题讨论】:
-
你想对自定义损失函数里面的numpy数组做什么?你不会在那里对
truth做任何事情。weight_building和weight_space实际上在做什么?你能告诉我们你是如何编译模型的吗?如果删除truth=backend.get_value(y_true),是否会收到错误消息? -
我对其进行了简化和编辑。
标签: tensorflow keras