segmentation_models icon indicating copy to clipboard operation
segmentation_models copied to clipboard

loss function with mean or sum,which is better?

Open lytk01 opened this issue 7 years ago • 2 comments

Hello, Brother! In your code categorical_crossentropy compute with mean, https://github.com/qubvel/segmentation_models/blob/e9cac0d5bc2a7c0e1c6fa9050d988a6f9854313c/segmentation_models/base/functional.py#L251 but in keras code ,the return is

return - tf.reduce_sum(target * tf.log(output), axis)

https://github.com/keras-team/keras/blob/c10d24959b0ad615a21e671b180a1b2466d77a2b/keras/backend/tensorflow_backend.py#L3564

so which is better?

lytk01 avatar Aug 19 '19 09:08 lytk01

There are no difference, keras average them anyway (because K.categorical_crossentropy does not return scalar)

qubvel avatar Aug 20 '19 11:08 qubvel

@qubvel This is a great library and we are using it extensively these days. Thanks. I also stumbled upon this exact same question asked here. Why do you think there are no difference between your implementation and keras? If I understand correctly in keras they do not count the number of classes in their mean calculation but in our case we do it. Though it should not affect the training, still what we calculate here is a scaled version of cross entrophy and the scaling depends on the number of classes. isn't it?

s-udhaya avatar May 08 '20 16:05 s-udhaya