keras ensure equal class representation during traingin

from sklearn.utils import class_weight
class_weights = class_weight.compute_class_weight('balanced',
                                                 y_train), y_train, class_weight=class_weights)

Here is what the above code is Doing:
1. We’re using the class_weight.compute_class_weight function to compute the class weights.
2. We’re passing in the string ‘balanced’ to the compute_class_weight function. This uses the values of y to automatically adjust weights inversely proportional to class frequencies in the input data.
3. We’re passing in the unique values of y_train (0 and 1) to the compute_class_weight function.
4. We’re passing in the labels from our training data (y_train) to the compute_class_weight function.
5. We’re storing the resulting class weights in a variable called class_weights.
6. We’re passing the class_weights variable to the fit function when we train our model.

Note that we’re only using the class_weight parameter when we train our model. We’re not using it when we evaluate our model.

Let’s see if using class weights helps our model.