greta-dev / greta

simple and scalable statistical modelling in R
https://greta-stats.org
Other
527 stars 63 forks source link

Implement strategy to manage TF 2.16 using legacy keras 2 #675

Open njtierney opened 1 month ago

njtierney commented 1 month ago

From @ t-kalinowski at https://github.com/rstudio/tensorflow/issues/602

First, it's possible to use keras 2 (i.e., library(keras) with TF 2.16. To make that work presently, you can install legacy keras as tf_keras, and then make sure an env var is set when keras is first loaded.

tensorflow::install_tensorflow(
   version = "release" # or "2.16" or "2.17" 
   envname = "r-tensorflow", 
   extra_packages = "tf_keras", # legacy keras
   python_version = "3.10"
)
# New R session
Sys.setenv("TF_USE_LEGACY_KERAS"="1")
tensorflow::tf_config() # initialize python, load tensorflow+keras
keras:::keras_version() < "3.0" # confirm legacy Keras in use

This would mean that we would need to add some more things to startup, but overall it's great to know that this is all possible, and we can push back against using keras 3 for at least a little bit longer