python - 如何使用分类焦点损失进行 keras 的一种热编码?
问题描述
我正在研究癫痫发作预测。我有不平衡的数据集,我想通过使用焦点损失使其平衡。我有 2 类 one-hot 编码向量。我找到了以下焦点损失代码,但我不知道如何才能y_pred
在焦点损失代码中使用之前model.fit_generator
。
y_pred
是模型的输出。那么在拟合我的模型之前,我如何在焦点损失代码中使用它?
焦点损失代码:
def categorical_focal_loss(gamma=2.0, alpha=0.25):
"""
Implementation of Focal Loss from the paper in multiclass classification
Formula:
loss = -alpha*((1-p)^gamma)*log(p)
Parameters:
alpha -- the same as wighting factor in balanced cross entropy
gamma -- focusing parameter for modulating factor (1-p)
Default value:
gamma -- 2.0 as mentioned in the paper
alpha -- 0.25 as mentioned in the paper
"""
def focal_loss(y_true, y_pred):
# Define epsilon so that the backpropagation will not result in NaN
# for 0 divisor case
epsilon = K.epsilon()
# Add the epsilon to prediction value
#y_pred = y_pred + epsilon
# Clip the prediction value
y_pred = K.clip(y_pred, epsilon, 1.0-epsilon)
# Calculate cross entropy
cross_entropy = -y_true*K.log(y_pred)
# Calculate weight that consists of modulating factor and weighting factor
weight = alpha * y_true * K.pow((1-y_pred), gamma)
# Calculate focal loss
loss = weight * cross_entropy
# Sum the losses in mini_batch
loss = K.sum(loss, axis=1)
return loss
return focal_loss
我的代码:
history=model.fit_generator(generate_arrays_for_training(indexPat, train_data, start=0,end=100)
validation_data=generate_arrays_for_training(indexPat, test_data, start=0,end=100)
steps_per_epoch=int((len(train_data)/2)),
validation_steps=int((len(test_data)/2)),
verbose=2,epochs=65, max_queue_size=2, shuffle=True)
preictPrediction=model.predict_generator(generate_arrays_for_predict(indexPat, filesPath_data), max_queue_size=4, steps=len(filesPath_data))
y_pred1=np.argmax(preictPrediction,axis=1)
y_pred=list(y_pred1)
解决方案
为了社区的利益,来自评论部分。
这不是特定于
focal loss
,所有 keras 损失函数都采用y_true
和y_pred
,您不必担心这些参数来自哪里,它们是keras
自动提供的。
推荐阅读
- reactjs - MirageJs:如果我通过直通调用 api,则无法使用 axios
- python - Selenium Python 找不到元素
- kubernetes - 不同地域的客户端连接不上Filestore
- reactjs - TypeError:AddTag 不是函数
- php - 编译 ImageMagick:找不到“libwebp”,从 Amazon Linux 2 上的源代码编译
- react-bootstrap-table2 - 使用 React Bootstrap Table2 在列表中的每个项目后添加“,”
- javascript - Webpack 生成器 https://createapp.dev/ 启动项目失败
- javascript - 单击外部输入后,Vue.js inputmask隐藏占位符
- python - 如何在 python 中插入 youtube 视频
- sql - 如何从 PostgreSQL 返回“稀疏”json(选择多个属性)