How to set class weights in keras model for multiclass classification problem?

Question:

I am building an emotion recogniton model that receives both text and audio features. The dataset that I am using has 3 classes (Neutral, negative and positive). Due to the dataset being higly imbalanced I want to use class weight argument in fit function to give to each class a certain weight:

hist = text_model.fit(train_vectorsCNN, y_train_cat, validation_data=(val_vectorsCNN, y_val_cat), 
         callbacks=callbacks_list, epochs=100, batch_size=24, verbose=1, class_weight=class_weights_tobalance)

Variable class_weights_tobalance is a dictionary in which keys are labels and values are the weights computed in the following way:

class_weights_tobalance = class_weight.compute_class_weight(class_weight = 'balanced', classes = np.unique(y_train) , y = y_train)

Function compute_class_weight is imported from sklearn library. The problem I am facing is that my neural network uses categorical_crossentropy therefore it needs labels as an one-hot encoded vector but I can’t represent my dictionary keys in the same way. Any way to do so?

Asked By: Rodrigo Lemos

||

Answers:

How about just creating that new model but specify as follows:

new_model = Model(inputs=..., outputs=loaded_audio_model.layers[-2].output)
Answered By: Minh-Long Luu