rstudio / keras3

R Interface to Keras
https://keras3.posit.co/
Other
839 stars 282 forks source link

Can't run text classification tutorial on Tensorflow for R website #1005

Open djduncan585 opened 4 years ago

djduncan585 commented 4 years ago

I'm attempting to run the text classification tutorial located at https://tensorflow.rstudio.com/tutorials/beginners/basic-ml/tutorial_basic_text_classification/ inside of R Studio. Version of TensorFlow for R Studio installed is 2.2.

I attempt to run the section of the code that says history <- model %>% fit( training$text, as.numeric(training$tag == "pos"), epochs = 10, batch_size = 512, validation_split = 0.2, verbose=2 ) and get back the error Error in py_call_impl(callable, dots$args, dots$keywords) : RuntimeError: Evaluation error: invalid argument type.

I've reinstalled Keras and updated TensorFlow. I've reinstalled Reticulate. Any ideas?

turgut090 commented 4 years ago

Hi, I think this is due to keras version. Because I tried to install tf-nightly and keras from R side and as a result the Keras version was 2.3.0-tf and tf version was 2.2-nightly. But if you import packages via reticulate the following will work fine:

keras = reticulate::import('keras')
tensorflow = reticulate::import('tensorflow')

> keras = reticulate::import('keras')
> keras$`__version__`
[1] "2.3.1"
> tensorflow = reticulate::import('tensorflow')
> tensorflow$`__version__`
[1] "2.2.0-dev20200329"

Later, this will work fine but this is pythonic:

model = keras$Sequential()
model$add(keras$layers$Dense(units=1L))

model$compile(loss=keras$losses$binary_crossentropy,
              optimizer=keras$optimizers$SGD(lr=0.01, momentum=0.9, nesterov=T))

model$fit(x_data, y_data, epochs=5L, batch_size=32L)
skeydan commented 4 years ago

@henry090 thanks!

We will have to investigate this for the upcoming 2.2 release.

@djduncan585 please run the website code with the current TensorFlow release 2.1, not the nightly build, for now.

tensorflow::install_tensorflow(version = "2.1")

Code to reproduce:

library(keras)
library(dplyr)
library(ggplot2)
library(purrr)

paths <- pins::pin_get("nltkdata/movie-review", "kaggle")
# we only need the movie_review.csv file
path <- paths[1]

df <- readr::read_csv(path)
training_id <- sample.int(nrow(df), size = nrow(df)*0.8)
training <- df[training_id,]
testing <- df[-training_id,]

num_words <- 10000
max_length <- 50
text_vectorization <- layer_text_vectorization(
  max_tokens = num_words, 
  output_sequence_length = max_length, 
)

text_vectorization %>% 
  adapt(df$text)

input <- layer_input(shape = c(1), dtype = "string")

output <- input %>% 
  text_vectorization() %>% 
  layer_embedding(input_dim = num_words + 1, output_dim = 16) %>%
  layer_global_average_pooling_1d() %>%
  layer_dense(units = 16, activation = "relu") %>%
  layer_dropout(0.5) %>% 
  layer_dense(units = 1, activation = "sigmoid")

model <- keras_model(input, output)

model %>% compile(
  optimizer = 'adam',
  loss = 'binary_crossentropy',
  metrics = list('accuracy')
)

history <- model %>% fit(
  training$text,
  as.numeric(training$tag == "pos"),
  epochs = 10,
  batch_size = 512,
  validation_split = 0.2,
  verbose=1
)
Error in py_call_impl(callable, dots$args, dots$keywords) :    RuntimeError: Evaluation error: invalid argument type. > traceback() 14: stop(list(message = "RuntimeError: Evaluation error: invalid argument type.",          call = py_call_impl(callable, dots$args, dots$keywords),          cppstack = list(file = "", line = -1L, stack = c("/home/key/R/x86_64-redhat-linux-gnu-library/3.6/reticulate/libs/reticulate.so(Rcpp::exception::exception(char const*, bool)+0x74) [0x7f6981530984]",          "/home/key/R/x86_64-redhat-linux-gnu-library/3.6/reticulate/libs/reticulate.so(Rcpp::stop(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)+0x29) [0x7f69815309ed]",          "/home/key/R/x86_64-redhat-linux-gnu-library/3.6/reticulate/libs/reticulate.so(+0x1ef71) [0x7f6981522f71]",          "/home/key/R/x86_64-redhat-linux-gnu-library/3.6/reticulate/libs/reticulate.so(_reticulate_py_call_impl+0xa9) [0x7f698152ba59]",          "/usr/lib64/R/lib/libR.so(+0x13a026) [0x7f69a585a026]", "/usr/lib64/R/lib/libR.so(Rf_eval+0x1a0) [0x7f69a5869d70]",          "/usr/lib64/R/lib/libR.so(+0x14bb0f) [0x7f69a586bb0f]", "/usr/lib64/R/lib/libR.so(Rf_applyClosure+0x163) [0x7f69a586c993]",          "/usr/lib64/R/lib/libR.so(+0x13e2f5) [0x7f69a585e2f5]", "/usr/lib64/R/lib/libR.so(Rf_eval+0x1a0) [0x7f69a5869d70]",          "/usr/lib64/R/lib/libR.so(+0x14bb0f) [0x7f69a586bb0f]", "/usr/lib64/R/lib/libR.so(Rf_applyClosure+0x163) [0x7f69a586c993]",          "/usr/lib64/R/lib/libR.so(Rf_eval+0x2ba) [0x7f69a5869e8a]",          "/usr/lib64/R/lib/libR.so(+0xc85bd) [0x7f69a57e85bd]", "/usr/lib64/R/lib/libR.so(+0x134c15) [0x7f69a5854c15]",          "/usr/lib64/R/lib/libR.so(Rf_eval+0x1a0) [0x7f69a5869d70]",          "/usr/lib64/R/lib/libR.so(+0x14bb0f) [0x7f69a586bb0f]", "/usr/lib64/R/lib/libR.so(Rf_applyClosure+0x163) [0x7f69a586c993]",          "/usr/lib64/R/lib/libR.so(+0x13e2f5) [0x7f69a585e2f5]", "/usr/lib64/R/lib/libR.so(Rf_eval+0x1a0) [0x7f69a5869d70]",          "/usr/lib64/R/lib/libR.so(+0x14bb0f) [0x7f69a586bb0f]", "/usr/lib64/R/lib/libR.so(Rf_applyClosure+0x163) [0x7f69a586c993]",          "/usr/lib64/R/lib/libR.so(+0x18bde3) [0x7f69a58abde3]", "/usr/lib64/R/lib/libR.so(+0x18c210) [0x7f69a58ac210]",          "/usr/lib64/R/lib/libR.so(+0x18c5c6) [0x7f69a58ac5c6]", "/usr/lib64/R/lib/libR.so(+0x13406f) [0x7f69a585406f]",          "/usr/lib64/R/lib/libR.so(Rf_eval+0x1a0) [0x7f69a5869d70]",          "/usr/lib64/R/lib/libR.so(+0x14bb0f) [0x7f69a586bb0f]", "/usr/lib64/R/lib/libR.so(Rf_applyClosure+0x163) [0x7f69a586c993]",          "/usr/lib64/R/lib/libR.so(Rf_eval+0x2ba) [0x7f69a5869e8a]",          "/usr/lib64/R/lib/libR.so(+0x14bb0f) [0x7f69a586bb0f]", "/usr/lib64/R/lib/libR.so(Rf_applyClosure+0x163) [0x7f69a586c993]",          "/usr/lib64/R/lib/libR.so(+0x13e2f5) [0x7f69a585e2f5]", "/usr/lib64/R/lib/libR.so(Rf_eval+0x1a0) [0x7f69a5869d70]",          "/usr/lib64/R/lib/libR.so(+0x14a69c) [0x7f69a586a69c]", "/usr/lib64/R/lib/libR.so(Rf_eval+0x83e) [0x7f69a586a40e]",          "/usr/lib64/R/lib/libR.so(+0x14ff65) [0x7f69a586ff65]", "/usr/lib64/R/lib/libR.so(+0x189be9) [0x7f69a58a9be9]",          "/usr/lib64/R/lib/libR.so(+0x13406f) [0x7f69a585406f]", "/usr/lib64/R/lib/libR.so(Rf_eval+0x1a0) [0x7f69a5869d70]",          "/usr/lib64/R/lib/libR.so(+0x14bb0f) [0x7f69a586bb0f]", "/usr/lib64/R/lib/libR.so(Rf_applyClosure+0x163) [0x7f69a586c993]",          "/usr/lib64/R/lib/libR.so(+0x13e2f5) [0x7f69a585e2f5]", "/usr/lib64/R/lib/libR.so(Rf_eval+0x1a0) [0x7f69a5869d70]",          "/usr/lib64/R/lib/libR.so(+0x14bb0f) [0x7f69a586bb0f]", "/usr/lib64/R/lib/libR.so(Rf_applyClosure+0x163) [0x7f69a586c993]",          "/usr/lib64/R/lib/libR.so(Rf_eval+0x2ba) [0x7f69a5869e8a]",          "/usr/lib64/R/lib/libR.so(+0x14bb0f) [0x7f69a586bb0f]", "/usr/lib64/R/lib/libR.so(Rf_applyClosure+0x163) [0x7f69a586c993]",          "/usr/lib64/R/lib/libR.so(Rf_eval+0x2ba) [0x7f69a5869e8a]",          "/usr/lib64/R/lib/libR.so(+0x14f472) [0x7f69a586f472]", "/usr/lib64/R/lib/libR.so(+0x134c15) [0x7f69a5854c15]",          "/usr/lib64/R/lib/libR.so(Rf_eval+0x1a0) [0x7f69a5869d70]",          "/usr/lib64/R/lib/libR.so(+0x14bb0f) [0x7f69a586bb0f]", "/usr/lib64/R/lib/libR.so(Rf_applyClosure+0x163) [0x7f69a586c993]",          "/usr/lib64/R/lib/libR.so(+0x13e2f5) [0x7f69a585e2f5]", "/usr/lib64/R/lib/libR.so(Rf_eval+0x1a0) [0x7f69a5869d70]",          "/usr/lib64/R/lib/libR.so(+0x14a69c) [0x7f69a586a69c]", "/usr/lib64/R/lib/libR.so(Rf_eval+0x83e) [0x7f69a586a40e]",          "/usr/lib64/R/lib/libR.so(+0x14ff65) [0x7f69a586ff65]", "/usr/lib64/R/lib/libR.so(+0x189be9) [0x7f69a58a9be9]",          "/usr/lib64/R/lib/libR.so(+0x13406f) [0x7f69a585406f]", "/usr/lib64/R/lib/libR.so(Rf_eval+0x1a0) [0x7f69a5869d70]",          "/usr/lib64/R/lib/libR.so(+0x14bb0f) [0x7f69a586bb0f]", "/usr/lib64/R/lib/libR.so(Rf_applyClosure+0x163) [0x7f69a586c993]",          "/usr/lib64/R/lib/libR.so(+0x13e2f5) [0x7f69a585e2f5]", "/usr/lib64/R/lib/libR.so(Rf_eval+0x1a0) [0x7f69a5869d70]",          "/usr/lib64/R/lib/libR.so(+0x14bb0f) [0x7f69a586bb0f]", "/usr/lib64/R/lib/libR.so(Rf_applyClosure+0x163) [0x7f69a586c993]",          "/usr/lib64/R/lib/libR.so(Rf_eval+0x2ba) [0x7f69a5869e8a]",          "/usr/lib64/R/lib/libR.so(+0x14e537) [0x7f69a586e537]", "/usr/lib64/R/lib/libR.so(Rf_eval+0x58d) [0x7f69a586a15d]",          "/usr/lib64/R/lib/libR.so(Rf_ReplIteration+0x1f2) [0x7f69a589b8e2]",          "/usr/lib64/R/lib/libR.so(+0x17bc70) [0x7f69a589bc70]", "/usr/lib64/R/lib/libR.so(run_Rmainloop+0x50) [0x7f69a589bd30]",          "/usr/lib/rstudio/bin/rsession(+0x8732e6) [0x55cd2a1432e6]",          "/usr/lib/rstudio/bin/rsession(+0x851c7d) [0x55cd2a121c7d]",          "/usr/lib/rstudio/bin/rsession(+0x8b57b) [0x55cd2995b57b]",          "/lib64/libc.so.6(__libc_start_main+0xf3) [0x7f69a47291a3]",          "/usr/lib/rstudio/bin/rsession(+0xb20e1) [0x55cd299820e1]"         )))) 13: py_call_impl(callable, dots$args, dots$keywords) 12: (function (...)      {         dots <- py_resolve_dots(list(...))         result <- py_call_impl(callable, dots$args, dots$keywords)         if (convert)              result <- py_to_r(result)         if (is.null(result))              invisible(result)         else result     })(batch_size = 512L, epochs = 10L, verbose = 1L, callbacks = list(         <environment>), validation_split = 0.2, shuffle = TRUE, class_weight = NULL,          sample_weight = NULL, initial_epoch = 0L, x = <environment>,          y = <environment>) 11: do.call(object$fit, args) 10: fit.keras.engine.training.Model(., training$text, as.numeric(training$tag ==          "pos"), epochs = 10, batch_size = 512, validation_split = 0.2,          verbose = 1) 9: fit(., training$text, as.numeric(training$tag == "pos"), epochs = 10,         batch_size = 512, validation_split = 0.2, verbose = 1) 8: function_list[[k]](value) 7: withVisible(function_list[[k]](value)) 6: freduce(value, `_function_list`) 5: `_fseq`(`_lhs`) 4: eval(quote(`_fseq`(`_lhs`)), env, env) 3: eval(quote(`_fseq`(`_lhs`)), env, env) 2: withVisible(eval(quote(`_fseq`(`_lhs`)), env, env)) 1: model %>% fit(training$text, as.numeric(training$tag == "pos"),         epochs = 10, batch_size = 512, validation_split = 0.2, verbose = 1)
djduncan585 commented 4 years ago

Tensorflow 2.1 appears to be running fine; thank you.