Strange error when calling fit() in R for keras

Viewed 54

I am trying t implement a Bi LSTM in R using Keras. The problem is a text classification that detects the severity of different tweets. I am using some code I found online and this is my model:

model <- keras_model_sequential() %>%
 layer_embedding(input_dim = max_features, output_dim = 32, input_length = max.length) %>% 
  layer_lstm(units = 32, return_sequences = TRUE) %>% 
  layer_lstm(units = 32, return_sequences = TRUE) %>%
  bidirectional(layer_lstm(units = 32)) %>% 
  layer_dense(units = 1, activation = 'softmax')


summary(model)

I then tried to train the model:

model %>%
  compile(loss = 'categorical_crossentropy',
          optimizer = 'adam',
          metrics = c('accuracy'))

history2 <- model %>%
  fit(x.train,
      train.Labels,
      epochs = 20,
      batch_size = 32,
      validation_split = 0.2,
      verbose = 2,
      class_weight = list("0" = 1, "1" = 22.9, "2" = 38.4, "3" = 33.4, "4" = 83.3, "5" = 382.2, "6" = 4280.4))

plot(history)

and everytime I try to run fit(), I am hit with this error:

Error in py_call_impl(callable, dots$args, dots$keywords) : ValueError: in user code: C:\Users\farah\AppData\Local\R-MINI~1\envs\R-RETI~1\lib\site-packages\keras\engine\training.py:853 train_function * return step_function(self, iterator) C:\Users\farah\AppData\Local\R-MINI~1\envs\R-RETI~1\lib\site-packages\keras\engine\training.py:842 step_function ** outputs = model.distribute_strategy.run(run_step, args=(data,)) C:\Users\farah\AppData\Local\R-MINI~1\envs\R-RETI~1\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:1286 run return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs) C:\Users\farah\AppData\Local\R-MINI~1\envs\R-RETI~1\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2849 call_for_each_replica return self._call_for_each_replica(fn, args, kwargs) C:\Users\farah\AppData\Local\R-MINI~1\envs\R-RETI~1\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:3632 _call_for_each_replica return fn(*args, **kwargs)

I am not entirely sure what it means and I would appreciate any help!

If you do require any additional information, please let me know!

1 Answers

The error message indicates a "ValueError" exception, but it's not clear what the value error is since only the stack trace is shown. Sometimes running this gives additional information:

for(x in reticulate::py_last_error()) cat(x, "\n")
Related