55# ' https://blogs.rstudio.com/tensorflow/posts/2018-09-17-eager-captioning
66
77library(keras )
8- use_implementation(" tensorflow" )
98library(tensorflow )
10- tfe_enable_eager_execution(device_policy = " silent" )
119
1210np <- import(" numpy" )
1311
@@ -32,10 +30,6 @@ debugshapes <- FALSE
3230restore_checkpoint <- FALSE
3331saved_features_exist <- FALSE
3432
35- use_session_with_seed(7777 ,
36- disable_gpu = FALSE ,
37- disable_parallel_cpu = FALSE )
38-
3933annotation_file <- " train2014/annotations/captions_train2014.json"
4034image_path <- " train2014/train2014"
4135
@@ -138,7 +132,7 @@ top_k <- 5000
138132tokenizer <- 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 )
142136train_captions_tokenized <-
143137 tokenizer %> % texts_to_sequences(train_captions )
144138validation_captions_tokenized <-
@@ -216,7 +210,7 @@ map_func <- function(img_name, cap) {
216210train_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 <-
360354encoder <- cnn_encoder(embedding_dim )
361355decoder <- rnn_decoder(embedding_dim , gru_units , vocab_size )
362356
363- optimizer = tf $ train $ AdamOptimizer ()
357+ optimizer = tf $ optimizers $ Adam ()
364358
365359cx_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\n Total loss (epoch): " ,
0 commit comments