variational autoencoder#49
Conversation
|
@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)) %>% |
There was a problem hiding this comment.
@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?
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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)
|
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 |
|
That's exactly what I needed. I will submit a PR to fix this in the example. |
setting up variational autoencoder
not ready to merge