rstudio / tfprobability

R interface to TensorFlow Probability
https://rstudio.github.io/tfprobability/
Other
54 stars 16 forks source link

python error with layer_kl_divergence_add_loss() #128

Closed ThierryO closed 4 years ago

ThierryO commented 4 years ago

The layer below was working a few day ago.

layer_kl_divergence_add_loss(
    distribution_b = tfd_independent(
      tfd_normal(loc = c(0, 0), scale = 1),
      reinterpreted_batch_ndims = 1
    )
  )

Now I'm getting this error.

 Error in py_call_impl(callable, dots$args, dots$keywords) : 
  ValueError: in converted code:
    relative to /home/thierry/anaconda3/envs/r-reticulate/lib/python3.6/site-packages:

    tensorflow_probability/python/layers/distribution_layer.py:1306 call
        self.add_loss(self._kl_divergence_fn(distribution_a),
    tensorflow_probability/python/layers/distribution_layer.py:1217 __call__
        return self._kl_divergence_fn(distribution_a)
    tensorflow_probability/python/layers/distribution_layer.py:1338 _fn
        kl = kl_divergence_fn(distribution_a, distribution_b_)
    tensorflow_probability/python/layers/distribution_layer.py:1326 kl_divergence_fn
        input_tensor=distribution_a.log_prob(z) - distribution_b.log_prob(z),
    tensorflow_probability/python/distributions/distribution.py:874 log_prob
        return self._call_log_prob(value, name, **kwargs)
    tensorflow_probability/python/distributions/distribution.py:856 _call_log_prob
        return self._log_prob(value, **kwargs)
    tensorflow_probability/python/distribu 

The only thing that changed was a started update of my Ubuntu system. I've tried to update conda, reinstall Tensorflow and upgrade the R packages.

─ Session info ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 setting  value                       
 version  R version 3.6.3 (2020-02-29)
 os       Ubuntu 18.04.4 LTS          
 system   x86_64, linux-gnu           
 ui       RStudio                     
 language nl_BE:nl                    
 collate  nl_BE.UTF-8                 
 ctype    nl_BE.UTF-8                 
 tz       Europe/Brussels             
 date     2020-04-11                  

─ Packages ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 package       * version date       lib source        
 assertthat      0.2.1   2019-03-21 [1] CRAN (R 3.6.1)
 base64enc       0.1-3   2015-07-28 [1] CRAN (R 3.6.1)
 cli             2.0.2   2020-02-28 [1] CRAN (R 3.6.3)
 crayon          1.3.4   2017-09-16 [1] CRAN (R 3.6.1)
 digest          0.6.25  2020-02-23 [1] CRAN (R 3.6.3)
 ellipsis        0.3.0   2019-09-20 [1] CRAN (R 3.6.1)
 evaluate        0.14    2019-05-28 [1] CRAN (R 3.6.1)
 fansi           0.4.1   2020-01-08 [1] CRAN (R 3.6.2)
 forge           0.2.0   2019-02-26 [1] CRAN (R 3.6.3)
 fortunes        1.5-4   2016-12-29 [1] CRAN (R 3.6.1)
 generics        0.0.2   2018-11-29 [1] CRAN (R 3.6.1)
 glue            1.4.0   2020-04-03 [1] CRAN (R 3.6.3)
 htmltools       0.4.0   2019-10-04 [1] CRAN (R 3.6.1)
 jsonlite        1.6.1   2020-02-02 [1] CRAN (R 3.6.3)
 keras         * 2.2.5.0 2019-10-08 [1] CRAN (R 3.6.1)
 knitr           1.28    2020-02-06 [1] CRAN (R 3.6.3)
 lattice         0.20-41 2020-04-02 [4] CRAN (R 3.6.3)
 lifecycle       0.2.0   2020-03-06 [1] CRAN (R 3.6.3)
 magrittr        1.5     2014-11-22 [1] CRAN (R 3.6.1)
 Matrix          1.2-18  2019-11-27 [4] CRAN (R 3.6.1)
 pillar          1.4.3   2019-12-20 [1] CRAN (R 3.6.2)
 pkgconfig       2.0.3   2019-09-22 [1] CRAN (R 3.6.1)
 purrr           0.3.3   2019-10-18 [1] CRAN (R 3.6.1)
 R6              2.4.1   2019-11-12 [1] CRAN (R 3.6.1)
 rappdirs        0.3.1   2016-03-28 [1] CRAN (R 3.6.1)
 Rcpp            1.0.4.6 2020-04-09 [1] CRAN (R 3.6.3)
 reticulate      1.15    2020-04-02 [1] CRAN (R 3.6.3)
 rlang           0.4.5   2020-03-01 [1] CRAN (R 3.6.3)
 rmarkdown       2.1     2020-01-20 [1] CRAN (R 3.6.3)
 rstudioapi      0.11    2020-02-07 [1] CRAN (R 3.6.3)
 sessioninfo     1.1.1   2018-11-05 [1] CRAN (R 3.6.1)
 tensorflow      2.0.0   2019-10-02 [1] CRAN (R 3.6.1)
 tfdatasets    * 2.0.0   2019-12-13 [1] CRAN (R 3.6.3)
 tfestimators    1.9.1   2018-11-07 [1] CRAN (R 3.6.3)
 tfprobability * 0.9.0.0 2020-01-27 [1] CRAN (R 3.6.3)
 tfruns          1.4     2018-08-25 [1] CRAN (R 3.6.1)
 tibble          3.0.0   2020-03-30 [1] CRAN (R 3.6.3)
 tidyselect      1.0.0   2020-01-27 [1] CRAN (R 3.6.3)
 vctrs           0.2.4   2020-03-10 [1] CRAN (R 3.6.3)
 whisker         0.4     2019-08-28 [1] CRAN (R 3.6.1)
 withr           2.1.2   2018-03-15 [1] CRAN (R 3.6.1)
 xfun            0.12    2020-01-13 [1] CRAN (R 3.6.2)
 yaml            2.2.1   2020-02-01 [1] CRAN (R 3.6.3)
 zeallot         0.1.0   2018-01-28 [1] CRAN (R 3.6.1)
skeydan commented 4 years ago

Can you provide the output of reticulate::py_config()) and tensorflow::tf_config()?

Also, is that the complete example necessary to produce that error? Otherwise, can you post the complete code?

ThierryO commented 4 years ago

Thanks for looking into this. This is the smallest model which reproduces the error on my machine. The actual model has more layers before the layer_dense() but their output shape is 10L.

keras_model_sequential() %>%
  layer_dense(
    input_shape = 10L, 
    units = params_size_multivariate_normal_tri_l(4L)
  ) %>%
  layer_multivariate_normal_tri_l(event_size = 4L) %>%
  layer_kl_divergence_add_loss(
    distribution_b = tfd_independent(
      tfd_normal(loc = c(0, 0), scale = 1),
      reinterpreted_batch_ndims = 1
    )
  )
> reticulate::py_config()
python:         /home/thierry/anaconda3/envs/r-reticulate/bin/python
libpython:      /home/thierry/anaconda3/envs/r-reticulate/lib/libpython3.6m.so
pythonhome:     /home/thierry/anaconda3/envs/r-reticulate:/home/thierry/anaconda3/envs/r-reticulate
version:        3.6.10 |Anaconda, Inc.| (default, Jan  7 2020, 21:14:29)  [GCC 7.3.0]
numpy:          /home/thierry/anaconda3/envs/r-reticulate/lib/python3.6/site-packages/numpy
numpy_version:  1.17.3
tensorflow_probability:/home/thierry/anaconda3/envs/r-reticulate/lib/python3.6/site-packages/tensorflow_probability

python versions found: 
 /home/thierry/.virtualenvs/r-reticulate/bin/python
 /home/thierry/anaconda3/envs/r-reticulate/bin/python
 /usr/bin/python3
 /usr/bin/python
 /home/thierry/anaconda3/bin/python
> tensorflow::tf_config()
TensorFlow v2.0.0 (~/anaconda3/envs/r-reticulate/lib/python3.6/site-packages/tensorflow_probability)
Python v3.6 (~/anaconda3/envs/r-reticulate/bin/python)
skeydan commented 4 years ago

hi, I don't know how this could have worked before, but I think it's a problem with the dimensions. Judging from the KL divergence, the multivariate normal probably should have event size 2, right? Such that both are comparable.

This would work:

keras_model_sequential() %>%
  layer_dense(
    input_shape = 10L, 
    units = params_size_multivariate_normal_tri_l(2L)
  ) %>%
  layer_multivariate_normal_tri_l(event_size = 2L) %>%
  layer_kl_divergence_add_loss(
    distribution_b = tfd_independent(
      tfd_normal(loc = c(0, 0), scale = 1),
      reinterpreted_batch_ndims = 1
    )
  )
ThierryO commented 4 years ago

Yes. That was the problem. Below is the more generic solution in case someone else has a similar problem.

encoded_size <- 4L
keras_model_sequential() %>%
  layer_dense(
    input_shape = 10L, 
    units = params_size_multivariate_normal_tri_l(encoded_size)
  ) %>%
  layer_multivariate_normal_tri_l(event_size = encoded_size) %>%
  layer_kl_divergence_add_loss(
    distribution_b = tfd_independent(
      tfd_normal(loc = rep(0, encoded_size), scale = 1),
      reinterpreted_batch_ndims = 1
    )
  )