how to use weighted_categorical_crossentropy loss function

拥有回忆 提交于 2020-06-13 11:46:55

问题


I'm working in semantic segmentation been inspired from the article of Alex Kendall Segnet I found the Implementation here link, with : Input (rgb) shape=(batch_size, width, height, 3) Output (one-hot) shape=(batch_size, width, height, n_classes)

the number of output classes is 12.

I tried to use The weighted categorical crossentropy loss function since I have my optimal weights vector as below :

def weighted_categorical_crossentropy(weights):
    # weights = [0.9,0.05,0.04,0.01]
    def wcce(y_true, y_pred):
        Kweights = K.constant(weights)
        if not K.is_tensor(y_pred): y_pred = K.constant(y_pred)
        y_true = K.cast(y_true, y_pred.dtype)
        return K.categorical_crossentropy(y_true, y_pred) * K.sum(y_true * Kweights, axis=-1)
    return wcce

it worked for me I'm getting no error but the result of loss and accuracy is the same. is it the right way to use weighted_categorical_crossentopy.

来源:https://stackoverflow.com/questions/61309991/how-to-use-weighted-categorical-crossentropy-loss-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!