I am trying to build a simpleRNN network with custom loss function. I am predicting bmi based on 25 different features. My dataset is unbalanced and have outliers and want to predict better on the outliers. Rather it is more important to predict better on outliers. For my custom loss function I have added condition that if the loss is greater than 2 units then I want to penalize those observations more.
import keras.backend as K
def custom_loss(y_true, y_pred):
loss = K.abs(y_pred - y_true)
wt = loss * 5
loss_mae = K.switch((loss > 2),wt,loss)
return loss_mae
model = Sequential()
model.add(SimpleRNN(units=64, input_shape=(25, 1), activation="relu"))
model.add(Dense(32, activation="linear"))
model.add(Dropout(0.2))
model.add(Dense(1, activation="linear"))
model.compile(loss=custom_loss, optimizer='adam')
model.add(Dropout(0.1))
model.summary()
model.fit(train_x, train_y)
sample predictions after running this code
preds=[[16.015867], [16.022823], [15.986835], [16.69895 ], [17.537468]]
actual=[[18.68], [24.35], [18.07], [15.2 ], [13.78]]
As you can see, the prediction for 2nd and 5th obs, is still way off. Am I doing anything wrong in the code?