1

I'm using the keras package in R to fit a neural network model. The model I'm working on has two outputs: output1 is continuous(for regression), output2 is binary(for classification).

Since we have a very imbalanced dataset for the classification problem(output2), I want to assign different class weights to deal with the imbalance, but apparently we don't need to do that for output1(the regression).

Here is the sample code for the NN model that I'm working on:

input <- layer_input(shape = c(32,24))
output <- input %>%
  layer_lstm(units = 64, dropout = 0.2, recurrent_dropout = 0.2) 
pred1 <- output %>%
  layer_dense(units = 1, name = "output1")
pred2 <- output %>%
  layer_dense(units = 1, activation = "sigmoid", name = "output2")
model <- keras_model(
  input,
  list(pred1, pred2)
)  
summary(model)

model %>% compile(
  optimizer = "rmsprop",
  loss = list(
    output1 = "mse",
    output2 = "binary_crossentropy"
  ),
  loss_weights = list(
    output1 = 0.25,
    output2 = 10
  )
)

history <- model %>% fit(
  train_x, list(output1 = train_y1,output2 = train_y2),
  epochs = 10, 
  batch_size = 5000,
  class_weight = ???,
  validation_data = list(valid_x, list(output1 = valid_y1,output2 = valid_y2))
)

If we just have one binary output, I know that the class weights can be assigned by:

class_weight = list("0"=1,"1"=100),

but it doesn't work anymore when we have two outputs and just want to assign the weights to one of them. I guess I may need to somehow specify the name of the binary output in "class_weight" so that it knows the weights only apply to output2, but I don't know how to do it in R.

Does anyone know how to assign class weights to the binary output only when we have two outputs(one is regression, one is classification)? Thank you very much for the help!

greatpj
  • 11
  • 4
  • Welcome to StackOverflow. See [how to make a great reproducible example](https://stackoverflow.com/a/5963610/2359523) to aid others in answering your question. – Anonymous coward Nov 13 '18 at 15:19
  • isn't this relevant https://datascience.stackexchange.com/questions/13490/how-to-set-class-weights-for-imbalanced-classes-in-keras ? – Areza Nov 13 '18 at 15:21
  • @user702846, thank you for the quick response. I think the case they discussed was for one categorical output, not for multiple outputs. Also it seems that they used python, but I'm using R. – greatpj Nov 13 '18 at 16:13
  • @greatpj - it doesn't matter if it is python or R or ... they are all wrapper, meaning the argument to cal keras exist in all platform. Dictionary in python is equivalent of a list in R. So give it a try like https://stackoverflow.com/questions/46907881/how-to-set-class-weight-in-keras-package-of-r . Regarding two output - what is your response variable, are you trying a multiple regression using Keras ? why don't you make two NN ? may be you can elaborate on this with example in your question. – Areza Nov 13 '18 at 22:13
  • @user702846, Thank you for the suggestion. I included the sample code in the updated question. Hope it is clear now about the two outputs. If we just have one output, I know in R we can use class_weight = list("0"=1,"1"=100), but now I have two outputs, still using class_weight = list("0"=1,"1"=100) doesn't work. – greatpj Nov 14 '18 at 00:01

0 Answers0