Search code examples
pythontensorflowmachine-learningkerasloss-function

Weighted custom loss keras


You can use weighted MSE in Keras like this

model.fit(sample_weight=weights, loss='mse', ...)

I want to use weighted RMSE but Keras library doesn't have rmse, I wrote it myself

def root_mean_squared_error(y_true, y_pred):
    return K.sqrt(K.mean(K.square(y_pred - y_true)))

but how then to use weights?


Solution

  • From the documentation, it appears that it's done automatically:

    Creating custom losses: Any callable with the signature loss_fn(y_true, y_pred) that returns an array of losses (one of sample in the input batch) can be passed to compile() as a loss. Note that sample weighting is automatically supported for any such loss.