Difference between model.compile's parameter 'weighted_metrics' and model.fit_generator's parameter 'class_weight' in keras?

Viewed 2513

While training a keras model for image classification (120 classes from DOG BREED IDENTIFICATION dataset, KAGGLE), I need to balance the classes using class weights which I read somewhere and in examples I have seen people using fit_generator's parameter, class_weight. But I found another parameter in model.compile, weighted_metrics whose description in docs is: 'List of metrics to be evaluated and weighted by sample_weight or class_weight during training and testing'. Shall I be using this? Please explain the purpose of this parameter with any example.

#Calculating Class weights
counter = Counter(train_generator.classes)
max_value = float(max(counter.values()))

CLASS_WEIGHTS = {classid: max_value / num_occurences
                 for classid, num_occurences in counter.items()}
# Model Compile
model.compile(optimizer=Adam(lr=LR),
              loss=categorical_crossentropy,
              metrics=[categorical_accuracy],
              weighted_metrics=None) # <--------------- This parameter

STEPS_PER_EPOCH = train_generator.n//train_generator.batch_size
VAL_STEPS = val_generator.n//val_generator.batch_size

model.fit_generator(train_generator,
                    steps_per_epoch=STEPS_PER_EPOCH,
                    epochs=EPOCHS,
                    callbacks=callback_list,
                    verbose=1,
                    class_weight=CLASS_WEIGHTS,
                    validation_data=val_generator,
                    validation_steps=VAL_STEPS) # USED CLASS_WEIGHTS HERE
1 Answers

Yes, you can use them for your unbalanced dataset.

weighted_metrics

is a list of metrics that take into account the

class_weights

that you pass in fit_generator.

So in your example, you can set

weighted_metrics=['accuracy']

and

class_weight = {0 : 3, 1: 4}

The purpose of weighted_metrics parameter is to give a list of metrics that will take into account the class_weights that you pass in fit_generator.

Related