Error: Received a label value of 1 which is outside the valid range of [0, 1)-Python,Keras

由于keras在做二分类损失的时候,激活函数选择的是sigmod,所以此处不能超过1

当选择的损失函数对应的取值超出范围的时候就会报错。

如下合理的loss使用方式供参考:

# 由于此处为二分类问题,此处选择sigmoid作为激活函数
out1 = layers.Dense(units=1, activation="sigmoid", name='out1')(features)
# 为多分类,此处选择softmax作为激活函数
out2 = layers.Dense(units=6, activation="softmax", name='out2')(features)


# Received a label value of 1 which is outside the valid range of [0, 1) - Python, Keras
# In the last Dense layer you used model.add(Dense(1, activation='softmax')).

# Here 1 restricts its value from [0, 1) change its shape to the maximum output label.
# For eg your output is from label [0,7) then use model.add(Dense(7, activation='softmax'))
def focal_loss(y_true, y_pred):
    print('focal_loss', y_true, y_pred)
    return sparse_categorical_focal_loss(y_true, y_pred, gamma=2, from_logits=True)

model = tf.keras.Model(inputs=inputs, outputs=[out1, out2])
model = create_model(encoding_size)
model.compile(
    optimizer=tf.keras.optimizers.Adam(learning_rate=learning_rate),
    loss={'out1': tf.keras.losses.BinaryCrossentropy(), "out2": focal_loss},
    loss_weights={'out1': 0.5, "out2": 0.5},
    metrics=["accuracy"
        # tf.keras.metrics.SparseCategoricalAccuracy(name="accuracy"),
        # tf.keras.metrics.AUC(name="auc")
    ]
)

参考:https://stackoverflow.com/questions/44151760/received-a-label-value-of-1-which-is-outside-the-valid-range-of-0-1-python

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mtj66

看心情

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值