superstator
superstator

Reputation: 3228

Error using Keras callback_tensorboard

I am using R 3.2.3, Keras 2.1.6, and TensorFlow 1.10 for a text classification problem. I am trying to set up instrumentation via TensorBoard, but I'm stuck on this error:

Error in py_call_impl(callable, dots$args, dots$keywords) : ValueError: To visualize embeddings, embeddings_data must be provided.

Here's my minimal model/training setup:

# x, y, and tokens loaded from tab files
num_samples <- 30000L
train_sample <- sample(1:dim(x)[1], num_samples)

tb_log <- "tb_log"
tensorboard(tb_log)

model <- keras_model_sequential() %>% 
  layer_embedding(input_dim = dim(tokens)[1], output_dim = 128, input_length = 1000) %>% 
  layer_conv_1d(filters = 32, kernel_size = 7, activation = "relu") %>% 
  layer_max_pooling_1d(pool_size = 5) %>% 
  layer_conv_1d(filters = 32, kernel_size = 7, activation = "relu") %>% 
  layer_global_max_pooling_1d() %>%
  layer_dense(units = 1)

summary(model)

model %>% compile(
  optimizer = "rmsprop",
  loss = "binary_crossentropy",
  metrics = c("acc")
)

history <- model %>% fit(
  x[train_sample,], y[train_sample],
  epochs = 3,
  batch_size = 128,
  validation_split = 0.5,
  callbacks = c(callback_tensorboard(
    log_dir = tb_log,
    embeddings_freq = 1,
    histogram_freq = 1
  ))
)

The model trains for the first epoch, then the process terminates with the above error. If I remove the callbacks option from the fit call, the model trains and works as expected. There is no embeddings_data argument for the callback that I can see. I've tried passing in embeddings_metadata as described here, but I still get the same error. If I just remove the embedding_freq option from the callback, I get this error:

Error in py_call_impl(callable, dots$args, dots$keywords) : 
  InvalidArgumentError: You must feed a value for placeholder tensor 'embedding_10_input' with dtype float and shape [?,1000]

Am I missing something obvious?

update

The second error (InvalidArgumentError) is apparently caused by some corruption of the environment after trying to use the callback with embeddings_freq set. If I remove that option, delete the logs folder, and restart my R session from scratch, I can get it to train and produce histograms, etc, but still no dice on visualizing actual embeddings.

Upvotes: 2

Views: 521

Answers (1)

superstator
superstator

Reputation: 3228

This seems to have been down to a version mismatch between TensorFlow, the python Keras module, and the R Keras module. For someone trying to troubleshoot this in the future, you can check all three versions like so:

python -c "import tensorflow; print(tensorflow.__version__)"
python -c "import keras; print(keras.__version__)"
Rscript -e "library(keras); sessionInfo()"

Or python3 -c ... as the case may be. It's also important that your R environment is using the right Python environment, which you can check with:

Rscript -e "reticulate::py_config()"

Beyond that it's a little bit of trial and error; I haven't found anything that consistently documents which Keras versions support which TensorFlow versions, etc. For my situation the magic nexus ended up being TensorFlow 1.10 built for Python 2.7.2, and Keras 2.2.

Upvotes: 1

Related Questions