如何在Keras培训期间在损失函数中打印?

2024-06-01 23:03:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图在Keras(Tensorflow后端)中创建一个loss函数,但是我有点困于检查自定义loss函数的内部。实际上,只有当我编译模型时,打印才会出现在控制台上,之后就没有打印了。(我只是测试非常简单的自定义函数,解决这个问题后我将创建真正的函数)。我使用train_on_batch函数训练模型。我怎样才能解决这个问题?

def loss_yolo(self, y_true, y_pred):  
    print('inside loss function')
    loss = K.mean(y_true - y_pred)
    return loss

model.compile(optimizer='sgd', loss=loss_yolo)

print('train on batch')
print(model.train_on_batch(x, y))

结果是

inside loss function

train on batch

-0.481604


Tags: 函数模型truemodelonbatchyolofunction