首页 > 解决方案 > Keras:如何将 y_pred 和 y_true 一分为三

问题描述

我正在使用 Keras,并且想使用自定义损失函数

我正在尝试将 y_pred 和 y_true 分成三个部分,每个部分的长度是原始长度的 1/3

这是我的代码,但返回错误(“nan”丢失):

def create_loss_function_custom(len_train):
            tier_1 = int(len_train/3)
            tier_2 = int(len_train*2/3)
            def loss_function_custom(y_true, y_pred):
                y_pred_1 = y_pred[:, :tier_1]
                y_pred_2 = y_pred[:, tier_1:tier_2]
                y_pred_3 = y_pred[:, tier_2:]

                y_true_1 = y_true[:, :tier_1]
                y_true_2 = y_true[:, tier_1:tier_2]
                y_true_3 = y_true[:, tier_2:]

                loss_1= K.mean(K.binary_crossentropy(y_true_1,y_pred_1))
                loss_2 = K.mean(K.binary_crossentropy(y_true_2,y_pred_2))
                loss_3 = K.mean(K.binary_crossentropy(y_true_3, y_pred_3))


                return K.max([loss_1,loss_2,loss_3])
            return loss_function_custom

这个怎么做 ?

我正在使用 theano 后端

标签: pythonkerasdeep-learning

解决方案


推荐阅读