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?

Upvotes: 0

Views: 315

Answers (1)

Nicolas Gervais
Nicolas Gervais

Reputation: 36584

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.

Upvotes: 1

Related Questions