rstudio / tfprobability

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

vignettes run-time-error Dynamic Linear Model #149

Closed goephs closed 2 years ago

goephs commented 2 years ago

A run-time error when executing trying to replicate the vignettes dynamic linear models R code: After pip3 install tensorflow and pip3 install tensorflow_probability

In function fit_vi--

loss_and_dists <- ts_train %>% sts_build_factored_variational_loss(model = model)

...Produces the following run-time error

Error in py_get_attr_impl(x, name, silent) : AttributeError: module 'tensorflow_probability.python.sts' has no attribute 'build_factored_variational_loss' Detailed traceback: File "C:\Python39\lib\site-packages\tensorflow_probability\python\internal\lazy_loader.py", line 54, in __getattr__ return getattr(module, item)

anirban-mukherjee commented 2 years ago

tfp.sts.build_factored_variational_loss has been deprecated. See: https://newreleases.io/project/github/tensorflow/probability/release/v0.10.0.

t-kalinowski commented 2 years ago

@anirban-mukherjee Would you be interested in filing a PR to update the vignette or R functions?

goephs commented 2 years ago

Hi Tomasz, I'm still fairly new to github so a pull request is something I've never done.

I'm still attempting to replicate the vignette and at this point in the R code, I get another error (it could be my setup). Not sure what to do at this point. Regards

# call fit_vi defined above
c(
param_distributions,
param_samples,
fc_means,
fc_sds,
smoothed_means,
smoothed_covs,
filtered_means,
filtered_covs
) %<-% fit_vi(
ts,
ts_train,
model,
n_iterations,
n_param_samples,
n_forecast_steps,
n_forecast_samples
)

Error Message from R

Loaded Tensorflow version 2.7.0 Error in on_load() : TensorFlow Probability has to be used with the TensorFlow Keras backend.

t-kalinowski commented 2 years ago

Can you please install the development version of tfprobability?

remotes::install_github("rstudio/reticulate")
remotes::install_github("rstudio/tensorflow")
remotes::install_github("rstudio/keras")
remotes::install_github("rstudio/tfprobability")
goephs commented 2 years ago
loss_and_dists <-
      ts_train %>% sts_build_factored_variational_loss(model = model)

Error in py_get_attr_impl(x, name, silent) : AttributeError: module 'tensorflow_probability.python.sts' has no attribute 'build_factored_variational_loss' Detailed traceback: File "C:\Python39\lib\site-packages\tensorflow_probability\python\internal\lazy_loader.py", line 54, in getattr return getattr(module, item)

goephs commented 2 years ago

And with this R code:

loss_and_dists <-
      ts_train %>% build_factored_variational_loss(model = model)

I get this R message

Error in build_factored_variational_loss(., model = model) : could not find function "build_factored_variational_loss"

goephs commented 2 years ago

Context of usage

https://cran.r-project.org/web/packages/tfprobability/vignettes/dynamic_linear_models.html

anirban-mukherjee commented 2 years ago

@goephs tfprobability is based on TensorFlow Probability (https://github.com/tensorflow/probability). build_factored_variational_loss in R calls tfp.sts.build_factored_variational_loss in TensorFlow Probability. This function has been deprecated (i.e., removed). The code in the vignette should work with TensorFlow 1.14 and TensorFlow Probability 0.7 but will not work with newer versions and needs to be updated. These changes may be minor or major, depending on the upstream changes.

I will take a look to see why the TensorFlow Probability team deprecated the function and what is the new recommended path to specifying a model. So far I am not finding much information on this issue.

@skeydan Is the vignette @ https://cran.r-project.org/web/packages/tfprobability/vignettes/dynamic_linear_models.html based on an upstream Python vignette or blog post? If so, perhaps they have updated the upstream vignette and we can piggyback?

anirban-mukherjee commented 2 years ago

I took a quick look. I think the function that is now recommended for vi is tfp.vi.build_factored_surrogate_posterior. With eager/TF2 semantics, I think the vignette will need to change considerably as I think fit_vi is no longer needed and there is no replacement for the variational loss function.

Instead the function to find the posterior is directly called on the model. For instance:

surrogate_posterior = tfp.sts.build_factored_surrogate_posterior(
  model=model)
loss_curve = tfp.vi.fit_surrogate_posterior(
  target_log_prob_fn=model.joint_log_prob(observed_time_series),
  surrogate_posterior=surrogate_posterior,
  optimizer=tf.optimizers.Adam(learning_rate=0.1),
  num_steps=200)
posterior_samples = surrogate_posterior.sample(50)

where model is the STS model of interest.

Unfortunately, I am not familiar enough with STS models and the library semantics to take the task on.

t-kalinowski commented 2 years ago

@anirban-mukherjee Thank you for taking a look! I'm going to archive the vignette since it's no longer recommended.