我正在尝试从Keras中的TF实现加权交叉熵。 来自TF网站的文档:https://www.tensorflow.org/api_docs/python/tf/nn/weighted_cross_entropy_with_logits
这就是我要做的:
import tensorflow as tf
from keras import backend as K
# Create the custom loss function
def weighted_binary_crossentropy(weights):
def w_binary_crossentropy(y_true, y_pred):
return K.mean(tf.nn.weighted_cross_entropy_with_logits(
y_true,
y_pred,
weights,
name=None
), axis=-1)
return w_binary_crossentropy
# Optimizers, Loss and Compile
adam = Adam(lr=0.0001)
weighted_loss = weighted_binary_crossentropy(weights=1)
model.compile(optimizer=adam, loss=weighted_loss, metrics=['accuracy'])
开始翻译,但损失未更新/卡住。我的期望是,如果将权重设置为1,则结果将与标准交叉熵损失相同。 我错过了什么吗?