自定义Keras binary_crossentropy损失函数不起作用 [英] Custom Keras binary_crossentropy loss function not working

查看:78
本文介绍了自定义Keras binary_crossentropy损失函数不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图重新定义keras的binary_crossentropy损失函数,以便我可以对其进行自定义,但它给我的结果与现有的结果不同.

I’m trying to re-define keras’s binary_crossentropy loss function so that I can customize it but it’s not giving me the same results as the existing one.

我在Keras 2.2.4上使用TF 1.13.1.

I'm using TF 1.13.1 with Keras 2.2.4.

我经历了Keras的github代码.我的理解是,使用tensorflow_backend.py中定义的binary_crossentropy,在loss.py中定义了model.compile(optimizer ='adam',loss ='binary_crossentropy',metrics = ['accuracy'])中的损失.

I went through Keras’s github code. My understanding is that the loss in model.compile(optimizer='adam', loss='binary_crossentropy', metrics =['accuracy']), is defined in losses.py, using binary_crossentropy defined in tensorflow_backend.py.

我运行了一个虚拟数据并进行了模型测试.这是我的发现:

I ran a dummy data and model to test it. Here are my findings:

  • 自定义损失功能输出的结果与喀拉拉邦的结果相同
  • 在keras模型中使用自定义损失会得出不同的准确性结果
from numpy.random import seed
seed(1)
from tensorflow import set_random_seed
set_random_seed(2)

import tensorflow as tf
from keras import losses
import keras.backend as K
import keras.backend.tensorflow_backend as tfb
from keras.layers import Dense
from keras import Sequential

#Dummy check of loss output
def binary_crossentropy_custom(y_true, y_pred):
    return K.mean(binary_crossentropy_custom_tf(y_true, y_pred), axis=-1)

def binary_crossentropy_custom_tf(target, output, from_logits=False):
    """Binary crossentropy between an output tensor and a target tensor.

    # Arguments
        target: A tensor with the same shape as `output`.
        output: A tensor.
        from_logits: Whether `output` is expected to be a logits tensor.
            By default, we consider that `output`
            encodes a probability distribution.

    # Returns
        A tensor.
    """
    # Note: tf.nn.sigmoid_cross_entropy_with_logits
    # expects logits, Keras expects probabilities.
    if not from_logits:
        # transform back to logits
        _epsilon = tfb._to_tensor(tfb.epsilon(), output.dtype.base_dtype)
        output = tf.clip_by_value(output, _epsilon, 1 - _epsilon)
        output = tf.log(output / (1 - output))

    return tf.nn.sigmoid_cross_entropy_with_logits(labels=target,
                                                   logits=output)

logits = tf.constant([[-3., -2.11, -1.22],
                     [-0.33, 0.55, 1.44],
                     [2.33, 3.22, 4.11]])

labels = tf.constant([[1., 1., 1.], 
                      [1., 1., 0.], 
                      [0., 0., 0.]])

custom_sigmoid_cross_entropy_with_logits = binary_crossentropy_custom(labels, logits)
keras_binary_crossentropy = losses.binary_crossentropy(y_true=labels, y_pred=logits)

with tf.Session() as sess:
    print('CUSTOM sigmoid_cross_entropy_with_logits: ', sess.run(custom_sigmoid_cross_entropy_with_logits), '\n')
    print('KERAS keras_binary_crossentropy: ', sess.run(keras_binary_crossentropy), '\n')

#CUSTOM sigmoid_cross_entropy_with_logits:  [16.118095 10.886106 15.942386] 

#KERAS keras_binary_crossentropy:  [16.118095 10.886106 15.942386] 

#Dummy check of model accuracy

X_train = tf.random.uniform((3, 5), minval=0, maxval=1, dtype=tf.dtypes.float32)
labels = tf.constant([[1., 0., 0.], 
                      [0., 0., 1.], 
                      [1., 0., 0.]])

model = Sequential()
#First Hidden Layer
model.add(Dense(5, activation='relu', kernel_initializer='random_normal', input_dim=5))
#Output Layer
model.add(Dense(3, activation='sigmoid', kernel_initializer='random_normal'))

#I ran model.fit for each model.compile below 10 times using the same X_train and provide the range of accuracy measurement
# model.compile(optimizer='adam', loss='binary_crossentropy', metrics =['accuracy']) #0.748 < acc < 0.779
# model.compile(optimizer='adam', loss=losses.binary_crossentropy, metrics =['accuracy']) #0.761 < acc < 0.778
model.compile(optimizer='adam', loss=binary_crossentropy_custom, metrics =['accuracy']) #0.617 < acc < 0.663

history = model.fit(X_train, labels, steps_per_epoch=100, epochs=1)

我希望自定义损失函数能够提供相似的模型精度输出,但事实并非如此.任何想法?谢谢!

I'd expect the custom loss function to give similar model accuracy output but it does not. Any idea? Thanks!

推荐答案

Keras会根据损失自动选择要使用的 accuracy 实现,如果您使用自定义损失,则此方法将无效.但是在这种情况下,您可以明确使用正确的精度,即 binary_accuracy :

Keras automatically selects which accuracy implementation to use according to the loss, and this won't work if you use a custom loss. But in this case you can just explictly use the right accuracy, which is binary_accuracy:

model.compile(optimizer='adam', loss=binary_crossentropy_custom, metrics =['binary_accuracy'])

这篇关于自定义Keras binary_crossentropy损失函数不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆