How to set class_weight in keras package of R?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Keras for R, class_weight is used during training to make mistakes on some classes count more than mistakes on others. This is especially useful for imbalanced classification problems where a minority class would otherwise be under-emphasized during optimization.
Pass class_weight to fit()
The basic idea is to provide a named list mapping class labels to numeric weights.
In this example, errors on class 1 count four times as much as errors on class 0 during training.
Match the Weights to the Encoded Labels
The most important practical detail is that the keys in class_weight must correspond to the actual encoded class labels your model sees.
If your target values are 0 and 1, the mapping above is appropriate. If your labels are encoded differently, the weights must follow that encoding exactly.
That is why it helps to inspect the class distribution before assigning weights.
Why class_weight Helps
Without weighting, the optimizer may find it cheaper to predict the majority class too often. Weighting does not create new information, but it changes the training objective so minority-class mistakes are penalized more strongly.
That can improve recall or balanced performance, especially when rare classes matter more than overall raw accuracy.
class_weight vs Resampling
class_weight is not the only way to handle imbalance. Other common approaches are:
- oversampling minority classes
- undersampling majority classes
- generating synthetic samples
Weighting is attractive because it does not alter the dataset itself. It changes the loss calculation while leaving the original training rows in place.
Use It with the Right Loss Setup
The loss function and label format still matter. For example:
- integer labels with sparse categorical cross-entropy
- one-hot labels with categorical cross-entropy
- binary labels with binary cross-entropy
The weighting concept is the same, but the model output shape and label format must still match the chosen loss correctly.
Start with Moderate Weights
A good first pass is usually to rebalance the classes moderately rather than assigning extreme values immediately. Very aggressive weighting can make optimization unstable or push the model into predicting the minority class too often, which simply replaces one bias with another.
Measure the Right Outcome
Once class weights are enabled, look at metrics such as recall, precision, confusion matrices, or per-class performance instead of staring only at overall accuracy. The whole point of weighting is to change the training tradeoff, so evaluation should reflect that goal directly.
That keeps the weighting decision tied to the actual modeling goal instead of turning it into a blind parameter tweak.
A weighted model should be evaluated as intentionally different, not just compared by default accuracy alone.
Common Pitfalls
- Using
class_weightkeys that do not match the encoded label values actually seen by the model. - Expecting class weighting to fix a fundamentally bad dataset or feature set.
- Judging success only by raw accuracy when the real goal is minority-class performance.
- Mixing label encoding and loss setup incorrectly before even addressing weighting.
- Applying huge class weights without checking whether training becomes unstable or overcompensated.
Summary
- In Keras for R, pass
class_weightas a named list tofit(). - The weight keys must match the class labels used in training.
- '
class_weightchanges the loss contribution of each class, which helps with imbalance.' - It is one tool among several, not a universal fix.
- Evaluate the model with metrics that reflect class imbalance instead of relying only on overall accuracy.

