python - 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 后端
解决方案
推荐阅读
- azure - 获取敏感度标签错误 - 未配置合规性策略:缺失
标签 - react-native - (Android) 无法使用 react-native-image-marker 从路径中读取文件
- autotools - 如何使用 autotools 构建系统完全包含另一个项目?
- html - 在 html 中打开图片 url
- visual-studio - Visual Studio 无法打开包含文件,从路径中删除字符
- python - Pygame - 及时随机掉落敌人
- swift - Cocoa 绑定仅以一种方式工作(NSMutableDictionary)
- wso2 - WSO2 EI REST API 管理器资源
- c# - 用 C# 在 Excel 单元格中引入公式
- javascript - 将 JS 应用从同步调用重写为异步