Implementing custom loss function in keras with different sizes for y_true and y_pred

后端 未结 3 689
执念已碎
执念已碎 2020-12-29 00:19

I am new to Keras. I need some help in writing a custom loss function in keras with TensorFlow backend for the following loss equation.

3条回答
  •  暖寄归人
    2020-12-29 01:07

    Yu's answer is correct still I want to share my experience. Whenever you want to write custom loss function, beware of certain things:

    1. At compile time, Keras will not complain about size mismatch. For example, if y_pred which comes from the output layer of your model has 3-D shape, y_true will default to 3-D shape. BUT, at run time i.e. during fit, if you pass target data as 2-D which many people do, you might end up getting the error in your loss function. E.g., if you are calculating sigmoid_crossentropy_with_logits, it will complain. Hence, do pass targets as 3-D via np.expand_dims.
    2. Also, in custom loss make sure you use y_true and y_pred as arguments (if somebody knows that we can use any other names as arguments, pl shout).

提交回复
热议问题