How to do point-wise categorical crossentropy loss in Keras?

前端 未结 4 1231
栀梦
栀梦 2021-01-18 02:26

I have a network that produces a 4D output tensor where the value at each position in spatial dimensions (~pixel) is to be interpreted as the class probabilities for that po

4条回答
  •  Happy的楠姐
    2021-01-18 03:06

    You could also not reshape anything and define both softmax and loss on your own. Here is softmax which is applied to the last input dimension (like in tf backend):

    def image_softmax(input):
        label_dim = -1
        d = K.exp(input - K.max(input, axis=label_dim, keepdims=True))
        return d / K.sum(d, axis=label_dim, keepdims=True)
    

    and here you have loss (there is no need to reshape anything):

    __EPS = 1e-5
    def image_categorical_crossentropy(y_true, y_pred):
        y_pred = K.clip(y_pred, __EPS, 1 - __EPS)
        return -K.mean(y_true * K.log(y_pred) + (1 - y_true) * K.log(1 - y_pred))
    

    No further reshapes need.

提交回复
热议问题