Skip to content

variational autoencoder#49

Merged
jjallaire merged 2 commits into
rstudio:masterfrom
dfalbel:example-variational_autoencoder
Jun 24, 2017
Merged

variational autoencoder#49
jjallaire merged 2 commits into
rstudio:masterfrom
dfalbel:example-variational_autoencoder

Conversation

@dfalbel

@dfalbel dfalbel commented Jun 23, 2017

Copy link
Copy Markdown
Contributor

setting up variational autoencoder
not ready to merge

@dfalbel

dfalbel commented Jun 23, 2017

Copy link
Copy Markdown
Contributor Author

@jjallaire this is now ready to merge

}

# note that "output_shape" isn't necessary with the TensorFlow backend
z <- layer_concatenate(list(z_mean, z_log_var)) %>%

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jjallaire here I would like to pass a list of tensors to the lambda layer, but

layer_lambda(list(z_mean, z_log_var)) was giving an error Error: Invalid input to layer function (must be a model or a tensor)

So I concatenated the values and separated inside the function. Do you see a better approach?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the code which receives the layer function input:

https://github.com/rstudio/keras/blob/master/R/layers-core.R#L420-L440

All I can think to do is to check for a list and just call layer_concatenate under the hood. However, I'm not sure if that results in an improvement in the code (as I think it's somewhat nice to see the explicit concatenation operation in the code).

@dfalbel dfalbel Jun 24, 2017

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does layer_concatenate works? It´s very similar to passing a list to layer_lambda. The python equivalent code uses a list in this example:

https://github.com/rstudio/keras/blob/master/vignettes/examples/variational_autoencoder.py#L29-L36

We could add a compose_layer.default checking for a listof tensors too. What do you think?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After reviewing the equivalent Python code, I think this should be able to work exactly as you are hoping. Could you provide a version of the code which exhibits the Error: Invalid input and I'll debug it from there?

@dfalbel dfalbel Jun 25, 2017

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's a minimal example:

x <- layer_input(c(100,2))
y <- layer_input(c(100,2))

my_fun <- function(args){
  a <- args[[1]]
  b <- args[[2]]
  a + b
}

layer_lambda(list(x, y), my_fun)

In the context of the example I would like to do:


sampling <- function(arg){
  z_mean <- arg[[1]]
  z_log_var <- arg[[2]]
  
  epsilon <- K$random_normal(
    shape = c(batch_size, latent_dim), 
    mean=0.,
    stddev=epsilon_std
  )
  
  z_mean + K$exp(z_log_var/2)*epsilon
}

# note that "output_shape" isn't necessary with the TensorFlow backend
z <- layer_lambda(list(z_mean, z_log_var), sampling)

@jjallaire jjallaire merged commit 43a56bd into rstudio:master Jun 24, 2017
@jjallaire

Copy link
Copy Markdown
Member

I think what you want is this then:

library(keras)

x <- layer_input(c(100,2))
y <- layer_input(c(100,2))

my_fun <- function(args){
  a <- args[[1]]
  b <- args[[2]]
  a + b
}

z <- layer_lambda(f = my_fun, arguments = list(x, y))

The reason you need the explicit f argument is that R keras layers always take a model (or tensor) as their first argument (for magrittr chaining). This isn't required though, but if you want evade this requirement you need to explicitly name your other arguments (that's why in our docs/examples we always name the first argument e.g. units in layer_dense) so that users become accustomed to always naming layer arguments.

@dfalbel

dfalbel commented Jun 25, 2017

Copy link
Copy Markdown
Contributor Author

That's exactly what I needed. I will submit a PR to fix this in the example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants