Skip to content

Commit 4681a67

Browse files
author
Sigrid Keydana
authored
Merge pull request #1026 from henry090/master
Update examples
2 parents 44d19ef + f42c7b7 commit 4681a67

File tree

4 files changed

+75
-65
lines changed

4 files changed

+75
-65
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): ",

vignettes/examples/nmt_attention.R

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ library(tibble)
2121
# Assumes you've downloaded and unzipped one of the bilingual datasets offered at
2222
# http://www.manythings.org/anki/ and put it into a directory "data"
2323
# This example translates English to Dutch.
24+
download_data = function(){
25+
if(!dir.exists('data')) {
26+
dir.create('data')
27+
}
28+
if(!file.exists('data/nld-eng.zip')) {
29+
download.file('http://www.manythings.org/anki/nld-eng.zip',
30+
destfile = file.path("data", basename('nld-eng.zip')))
31+
unzip('data/nld-eng.zip', exdir = 'data')
32+
}
33+
}
34+
download_data()
2435

2536
filepath <- file.path("data", "nld.txt")
2637

@@ -290,7 +301,7 @@ evaluate <-
290301
attention_matrix[t,] <- attention_weights %>% as.double()
291302

292303
pred_idx <-
293-
tf$compat$v1$multinomial(k_exp(preds), num_samples = 1L)[1, 1] %>% as.double()
304+
tf$random$categorical(k_exp(preds), num_samples = 1L)[1, 1] %>% as.double()
294305
pred_word <- index2word(pred_idx, target_index)
295306

296307
if (pred_word == '<stop>') {
@@ -387,7 +398,7 @@ for (epoch in seq_len(n_epochs)) {
387398
": ",
388399
(loss / k_cast_to_floatx(dim(y)[2])) %>% as.double() %>% round(4),
389400
"\n"
390-
) %>% print()
401+
) %>% cat()
391402

392403
variables <- c(encoder$variables, decoder$variables)
393404
gradients <- tape$gradient(loss, variables)
@@ -402,7 +413,7 @@ for (epoch in seq_len(n_epochs)) {
402413
": ",
403414
(total_loss / k_cast_to_floatx(buffer_size)) %>% as.double() %>% round(4),
404415
"\n"
405-
) %>% print()
416+
) %>% cat()
406417

407418
walk(train_sentences[1:5], function(pair)
408419
translate(pair[1]))

vignettes/examples/tfprob_vae.R

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,8 @@
55
#' https://blogs.rstudio.com/tensorflow/posts/2019-01-08-getting-started-with-tf-probability/
66

77
library(keras)
8-
use_implementation("tensorflow")
98
library(tensorflow)
10-
tfe_enable_eager_execution(device_policy = "silent")
11-
12-
tfp <- import("tensorflow_probability")
13-
tfd <- tfp$distributions
14-
9+
library(tfprobability)
1510
library(tfdatasets)
1611
library(dplyr)
1712
library(glue)
@@ -79,6 +74,17 @@ np <- import("numpy")
7974

8075
# assume data have been downloaded from https://github.com/rois-codh/kmnist
8176
# and stored in /tmp
77+
download_data = function(){
78+
if(!dir.exists('tmp')) {
79+
dir.create('tmp')
80+
}
81+
if(!file.exists('tmp/kmnist-train-imgs.npz')) {
82+
download.file('http://codh.rois.ac.jp/kmnist/dataset/kmnist/kmnist-train-imgs.npz',
83+
destfile = file.path("tmp", basename('kmnist-train-imgs.npz')))
84+
}
85+
}
86+
download_data()
87+
8288
kuzushiji <- np$load("/tmp/kmnist-train-imgs.npz")
8389
kuzushiji <- kuzushiji$get("arr_0")
8490

@@ -98,8 +104,8 @@ train_dataset <- tensor_slices_dataset(train_images) %>%
98104

99105
# Params ------------------------------------------------------------------
100106

101-
latent_dim <- 2
102-
mixture_components <- 16
107+
latent_dim <- 2L
108+
mixture_components <- 16L
103109

104110

105111
# Model -------------------------------------------------------------------
@@ -132,8 +138,8 @@ encoder_model <- function(name = NULL) {
132138
self$conv2() %>%
133139
self$flatten() %>%
134140
self$dense()
135-
tfd$MultivariateNormalDiag(loc = x[, 1:latent_dim],
136-
scale_diag = tf$nn$softplus(x[, (latent_dim + 1):(2 * latent_dim)] + 1e-5))
141+
tfd_multivariate_normal_diag(loc = x[, 1:latent_dim],
142+
scale_diag = tf$nn$softplus(x[, (latent_dim + 1):(2 * latent_dim)] + 1e-5))
137143
}
138144
})
139145
}
@@ -178,7 +184,7 @@ decoder_model <- function(name = NULL) {
178184
self$deconv2() %>%
179185
self$deconv3()
180186

181-
tfd$Independent(tfd$Bernoulli(logits = x),
187+
tfd_independent(tfd_bernoulli(logits = x),
182188
reinterpreted_batch_ndims = 3L)
183189

184190
}
@@ -192,30 +198,30 @@ learnable_prior_model <-
192198

193199
keras_model_custom(name = name, function(self) {
194200
self$loc <-
195-
tf$get_variable(
201+
tf$compat$v1$get_variable(
196202
name = "loc",
197203
shape = list(mixture_components, latent_dim),
198204
dtype = tf$float32
199205
)
200-
self$raw_scale_diag <- tf$get_variable(
206+
self$raw_scale_diag <- tf$compat$v1$get_variable(
201207
name = "raw_scale_diag",
202208
shape = c(mixture_components, latent_dim),
203209
dtype = tf$float32
204210
)
205211
self$mixture_logits <-
206-
tf$get_variable(
212+
tf$compat$v1$get_variable(
207213
name = "mixture_logits",
208214
shape = c(mixture_components),
209215
dtype = tf$float32
210216
)
211217

212218
function (x, mask = NULL) {
213-
tfd$MixtureSameFamily(
214-
components_distribution = tfd$MultivariateNormalDiag(
219+
tfd_mixture_same_family(
220+
components_distribution = tfd_multivariate_normal_diag(
215221
loc = self$loc,
216222
scale_diag = tf$nn$softplus(self$raw_scale_diag)
217223
),
218-
mixture_distribution = tfd$Categorical(logits = self$mixture_logits)
224+
mixture_distribution = tfd_categorical(logits = self$mixture_logits)
219225
)
220226
}
221227
})
@@ -234,8 +240,7 @@ compute_kl_loss <-
234240
}
235241

236242

237-
global_step <- tf$train$get_or_create_global_step()
238-
optimizer <- tf$train$AdamOptimizer(1e-4)
243+
optimizer <- tf$optimizers$Adam(1e-4)
239244

240245

241246
# Training loop -----------------------------------------------------------
@@ -253,7 +258,6 @@ checkpoint_prefix <- file.path(checkpoint_dir, "ckpt")
253258
checkpoint <-
254259
tf$train$Checkpoint(
255260
optimizer = optimizer,
256-
global_step = global_step,
257261
encoder = encoder,
258262
decoder = decoder,
259263
latent_prior_model = latent_prior_model
@@ -284,7 +288,7 @@ for (epoch in seq_len(num_epochs)) {
284288
compute_kl_loss(latent_prior,
285289
approx_posterior,
286290
approx_posterior_sample)
287-
291+
288292
loss <- kl_loss + avg_nll
289293
})
290294

@@ -299,18 +303,15 @@ for (epoch in seq_len(num_epochs)) {
299303

300304
optimizer$apply_gradients(purrr::transpose(list(
301305
encoder_gradients, encoder$variables
302-
)),
303-
global_step = tf$train$get_or_create_global_step())
306+
)))
304307
optimizer$apply_gradients(purrr::transpose(list(
305308
decoder_gradients, decoder$variables
306-
)),
307-
global_step = tf$train$get_or_create_global_step())
309+
)))
308310
optimizer$apply_gradients(purrr::transpose(list(
309311
prior_gradients, latent_prior_model$variables
310-
)),
311-
global_step = tf$train$get_or_create_global_step())
312+
)))
312313

313-
})
314+
})
314315

315316
checkpoint$save(file_prefix = checkpoint_prefix)
316317

@@ -329,3 +330,4 @@ for (epoch in seq_len(num_epochs)) {
329330
show_grid(epoch)
330331
}
331332
}
333+

vignettes/examples/vq_vae.R

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,19 @@
55
#' https://blogs.rstudio.com/tensorflow/posts/2019-01-24-vq-vae/
66

77
library(keras)
8-
use_implementation("tensorflow")
98
library(tensorflow)
10-
tfe_enable_eager_execution(device_policy = "silent")
11-
12-
use_session_with_seed(7778,
13-
disable_gpu = FALSE,
14-
disable_parallel_cpu = FALSE)
15-
16-
tfp <- import("tensorflow_probability")
17-
tfd <- tfp$distributions
18-
9+
library(tfprobability)
1910
library(tfdatasets)
11+
2012
library(dplyr)
2113
library(glue)
14+
15+
# curry has to be installed from github because CRAN version has no "set_defaults" function
16+
if(!('devtools' %in% rownames(installed.packages()) )) {
17+
install.packages('devtools')
18+
}
19+
devtools::install_github('thomasp85/curry')
20+
2221
library(curry)
2322

2423
moving_averages <- tf$python$training$moving_averages
@@ -63,7 +62,14 @@ write_png <- function(dataset, epoch, desc, images) {
6362

6463
np <- import("numpy")
6564

66-
# download from: https://github.com/rois-codh/kmnist
65+
# download from: https://github.com/rois-codh/kmnist via "download_data()" function
66+
download_data = function(){
67+
if(!file.exists('kmnist-train-imgs.npz')) {
68+
download.file('http://codh.rois.ac.jp/kmnist/dataset/kmnist/kmnist-train-imgs.npz',
69+
destfile = 'kmnist-train-imgs.npz')
70+
}
71+
}
72+
download_data()
6773
kuzushiji <- np$load("kmnist-train-imgs.npz")
6874
kuzushiji <- kuzushiji$get("arr_0")
6975

@@ -90,7 +96,7 @@ batch %>% dim()
9096
# Params ------------------------------------------------------------------
9197

9298
learning_rate <- 0.001
93-
latent_size <- 1
99+
latent_size <- 1L
94100
num_codes <- 64L
95101
code_size <- 16L
96102
base_depth <- 32
@@ -214,7 +220,7 @@ decoder_model <- function(name = NULL,
214220
self$deconv6() %>%
215221
# output shape: 7 28 28 1
216222
self$conv1()
217-
tfd$Independent(tfd$Bernoulli(logits = x),
223+
tfd_independent(tfd_bernoulli(logits = x),
218224
reinterpreted_batch_ndims = length(output_shape))
219225
}
220226
})
@@ -228,16 +234,16 @@ vector_quantizer_model <-
228234
keras_model_custom(name = name, function(self) {
229235
self$num_codes <- num_codes
230236
self$code_size <- code_size
231-
self$codebook <- tf$get_variable("codebook",
237+
self$codebook <- tf$compat$v1$get_variable("codebook",
232238
shape = c(num_codes, code_size),
233239
dtype = tf$float32)
234-
self$ema_count <- tf$get_variable(
240+
self$ema_count <- tf$compat$v1$get_variable(
235241
name = "ema_count",
236242
shape = c(num_codes),
237243
initializer = tf$constant_initializer(0),
238244
trainable = FALSE
239245
)
240-
self$ema_means = tf$get_variable(
246+
self$ema_means = tf$compat$v1$get_variable(
241247
name = "ema_means",
242248
initializer = self$codebook$initialized_value(),
243249
trainable = FALSE
@@ -308,7 +314,7 @@ update_ema <- function(vector_quantizer,
308314
updated_ema_means <-
309315
updated_ema_means / tf$expand_dims(updated_ema_count, axis = -1L)
310316

311-
tf$assign(vector_quantizer$codebook, updated_ema_means)
317+
tf$compat$v1$assign(vector_quantizer$codebook, updated_ema_means)
312318
}
313319

314320

@@ -321,7 +327,7 @@ decoder <- decoder_model(input_size = latent_size * code_size,
321327
vector_quantizer <-
322328
vector_quantizer_model(num_codes = num_codes, code_size = code_size)
323329

324-
optimizer <- tf$train$AdamOptimizer(learning_rate = learning_rate)
330+
optimizer <- tf$optimizers$Adam(learning_rate = learning_rate)
325331

326332
checkpoint_dir <- "./vq_vae_checkpoints"
327333

@@ -365,7 +371,7 @@ for (epoch in seq_len(num_epochs)) {
365371

366372
commitment_loss <- tf$reduce_mean(tf$square(codes - tf$stop_gradient(nearest_codebook_entries)))
367373

368-
prior_dist <- tfd$Multinomial(total_count = 1,
374+
prior_dist <- tfd_multinomial(total_count = 1,
369375
logits = tf$zeros(c(latent_size, num_codes)))
370376
prior_loss <- -tf$reduce_mean(tf$reduce_sum(prior_dist$log_prob(one_hot_assignments), 1L))
371377

@@ -379,12 +385,10 @@ for (epoch in seq_len(num_epochs)) {
379385

380386
optimizer$apply_gradients(purrr::transpose(list(
381387
encoder_gradients, encoder$variables
382-
)),
383-
global_step = tf$train$get_or_create_global_step())
388+
)))
384389
optimizer$apply_gradients(purrr::transpose(list(
385390
decoder_gradients, decoder$variables
386-
)),
387-
global_step = tf$train$get_or_create_global_step())
391+
)))
388392

389393
update_ema(vector_quantizer,
390394
one_hot_assignments,

0 commit comments

Comments
 (0)