Search code examples
python-3.xtensorflowkerasloss-function

How to define a keras custom loss function in simple mathematical operation


I define a custom function my_sigmoid as following:

import math
def my_sigmoid(x):
    a =  1/  ( 1+math.exp( -(x-300)/30 ) )
    return a

And then define a custom loss function called my_cross_entropy:

import keras.backend as K

def my_cross_entropy(y_true, y_pred):
    diff = abs(y_true-y_pred)
    y_pred_transform = my_sigmoid(diff)
    return K.categorical_crossentropy(0, y_pred_transform)

My keras backend is using tensorflow. And the error shows

TypeError: must be real number, not Tensor

I'm not familiar with tensorflow and don't know how to use custom loss.

The following are my model structure and error message:

import keras.backend as K
from keras.models import Sequential
from keras.layers import Conv2D, Dropout, Flatten, Dense

model=Sequential()
model.add(Conv2D(512,(5,X_train.shape[2]),input_shape=X_train.shape[1:4],activation="relu"))
model.add(Flatten())
model.add(Dropout(0.1))
model.add(Dense(100,activation="relu"))
model.add(Dense(100,activation="relu"))
model.add(Dense(50,activation="relu"))
model.add(Dense(10,activation="relu"))
model.add(Dense(1,activation="relu"))
model.compile(optimizer='adam', loss=my_cross_entropy)
model.fit(X_train,Y_train,batch_size = 10,epochs=200,validation_data=(X_test,Y_test))

enter image description here

And the shape of X_train and Y_train is : (120, 30, 80, 1) and (120,)


Solution

  • Change

    diff = abs(y_true-y_pred)
    

    into

    diff = K.abs(y_true-y_pred)
    

    same for

    math.exp()
    

    change that into

    K.exp()
    

    abs and Math.exp are functions that cannot handle Tensors. If you still have problems refer to : Custom Loss function Keras Tensorflow