-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.R
77 lines (62 loc) · 2.34 KB
/
main.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# Load libraries
library(keras)
library(tensorflow)
library(tfdatasets)
# Use tensorflow for keras
use_implementation("tensorflow")
# enable eager execuction
tfe_enable_eager_execution(device_policy = "silent")
# load mnist dataset
mnist <- dataset_mnist()
# use %<-% to assign multiple variables from list
c(train_images, train_labels) %<-% mnist$train
# Expand the training set and cast all the values to float 32
train_images <- train_images %>%
k_expand_dims() %>%
k_cast(dtype = "float32")
# normalize the values to between -1 and 1
# because generator uses tanh activation (looks like sigmoid but between -1 and 1)
# 127.5 because max value is 256 and min value is 0
train_images <- (train_images - 127.5)/127.5
# Get the number of training observations to be used in each train step
# total number of training obs
buffer_size <- 60000
# number we want in each batch
batch_size <- 256
# number of batches in each epoch (train step)
batches_per_epoch <- (buffer_size / batch_size) %>% round()
# shuffle the dataset
# slice into batches based on the desired batch size set above
# used for the discriminator only
train_dataset <- tensor_slices_dataset(train_images) %>%
dataset_shuffle(buffer_size) %>%
dataset_batch(batch_size)
# Source generator and discriminator
source("generator.R")
source("discriminator.R")
generator <- generator()
discriminator <- discriminator()
# Use eager execution in the generator and discriminator calls
generator$call = tf$contrib$eager$defun(generator$call)
discriminator$call = tf$contrib$eager$defun(discriminator$call)
source("loss_fns.R")
# Optimizers
#discriminator_optimizer <- tf$train$AdamOptimizer(1e-4)
discriminator_optimizer <- tf$compat$v1$train$AdamOptimizer(1e-4)
#generator_optimizer <- tf$train$AdamOptimizer(1e-4)
generator_optimizer <- tf$compat$v1$train$AdamOptimizer(1e-4)
# Number of epochs to train
num_epochs <- 20
# Number of examples to generate
num_examples_to_generate <- 25L
# noise to give generator
noise_dim <- 100
random_vector_for_generation <-
k_random_normal(c(num_examples_to_generate,
noise_dim))
# Source the function to save generated images
source("generate_and_save_imgs.R")
# Source the main training function
source("training.R")
# Start training
train(train_dataset, num_epochs, noise_dim)