Skip to content

Commit dd57faa

Browse files
committed
tf2 compatibility
1 parent 44d19ef commit dd57faa

File tree

1 file changed

+4
-11
lines changed

1 file changed

+4
-11
lines changed

vignettes/examples/eager_image_captioning.R

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
#' https://blogs.rstudio.com/tensorflow/posts/2018-09-17-eager-captioning
66

77
library(keras)
8-
use_implementation("tensorflow")
98
library(tensorflow)
10-
tfe_enable_eager_execution(device_policy = "silent")
119

1210
np <- import("numpy")
1311

@@ -32,10 +30,6 @@ debugshapes <- FALSE
3230
restore_checkpoint <- FALSE
3331
saved_features_exist <- FALSE
3432

35-
use_session_with_seed(7777,
36-
disable_gpu = FALSE,
37-
disable_parallel_cpu = FALSE)
38-
3933
annotation_file <- "train2014/annotations/captions_train2014.json"
4034
image_path <- "train2014/train2014"
4135

@@ -138,7 +132,7 @@ top_k <- 5000
138132
tokenizer <- text_tokenizer(num_words = top_k,
139133
oov_token = "<unk>",
140134
filters = '!"#$%&()*+.,-/:;=?@[\\]^_`{|}~ ')
141-
tokenizer$fit_on_texts(sample_captions)
135+
fit_text_tokenizer(tokenizer, sample_captions)
142136
train_captions_tokenized <-
143137
tokenizer %>% texts_to_sequences(train_captions)
144138
validation_captions_tokenized <-
@@ -216,7 +210,7 @@ map_func <- function(img_name, cap) {
216210
train_dataset <-
217211
tensor_slices_dataset(list(train_images, train_captions_padded)) %>%
218212
dataset_map(function(item1, item2)
219-
tf$py_func(map_func, list(item1, item2), list(tf$float32, tf$int32))) %>%
213+
tf$py_function(map_func, list(item1, item2), list(tf$float32, tf$int32))) %>%
220214
# dataset_shuffle(buffer_size) %>%
221215
dataset_batch(batch_size)
222216

@@ -360,7 +354,7 @@ rnn_decoder <-
360354
encoder <- cnn_encoder(embedding_dim)
361355
decoder <- rnn_decoder(embedding_dim, gru_units, vocab_size)
362356

363-
optimizer = tf$train$AdamOptimizer()
357+
optimizer = tf$optimizers$Adam()
364358

365359
cx_loss <- function(y_true, y_pred) {
366360
mask <- 1 - k_cast(y_true == 0L, dtype = "float32")
@@ -530,8 +524,7 @@ if (!restore_checkpoint) {
530524
variables <- c(encoder$variables, decoder$variables)
531525
gradients <- tape$gradient(loss, variables)
532526

533-
optimizer$apply_gradients(purrr::transpose(list(gradients, variables)),
534-
global_step = tf$train$get_or_create_global_step())
527+
optimizer$apply_gradients(purrr::transpose(list(gradients, variables)))
535528
})
536529
cat(paste0(
537530
"\n\nTotal loss (epoch): ",

0 commit comments

Comments
 (0)