Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set class_weight in keras package of R?

I am using keras package in R to train a deep learning model. My data set is highly imbalanced. Therefore, I want to set class_weight argument in the fit function. Here is the fit function and its arguments that I used for my model

history <- model %>% fit(
  trainData, trainClass, 
  epochs = 5, batch_size = 1000, 
  class_weight = ????,
  validation_split = 0.2
)

In python I can set class_weight as follow:

class_weight={0:1, 1:30}

But I am not sure how to do it in R. In the help menu of R it describes class_weight as follow:

Optional named list mapping indices (integers) to a weight (float) to apply to the model's loss for the samples from this class during training. This can be useful to tell the model to "pay more attention" to samples from an under-represented class.

Any idea or suggestions?

like image 673
skorkmaz Avatar asked Oct 24 '17 10:10

skorkmaz


People also ask

How does Class_weight work in keras?

As mentioned in the Keras Official Docs, class_weight : Optional dictionary mapping class indices (integers) to a weight (float) value, used for weighting the loss function (during training only). This can be useful to tell the model to "pay more attention" to samples from an under-represented class.

How do I determine my class weight?

Generating class weights In binary classification, class weights could be represented just by calculating the frequency of the positive and negative class and then inverting it so that when multiplied to the class loss, the underrepresented class has a much higher error than the majority class.

What is Class_weight?

The LogisticRegression class provides the class_weight argument that can be specified as a model hyperparameter. The class_weight is a dictionary that defines each class label (e.g. 0 and 1) and the weighting to apply in the calculation of the negative log likelihood when fitting the model.


1 Answers

Class_weight needs to be a list, so

    history <- model %>% fit(
        trainData, trainClass, 
        epochs = 5, batch_size = 1000, 
        class_weight = list("0"=1,"1"=30),
        validation_split = 0.2
    )

seems to work. Keras internally uses a function called as_class_weights to change the list to a python-dictionary (see https://rdrr.io/cran/keras/src/R/model.R).

     class_weight <- dict(list('0'=1,'1'=10))
     class_weight
     >>> {0: 1.0, 1: 10.0}

Looks just like the python dictionary that you mentioned above.

like image 194
Brigitte Avatar answered Sep 22 '22 01:09

Brigitte