Accessing "tensorflow as tf" with Keras3 in RStudio

The documentation for the R package keras3 indicates that attaching it also attaches tensorflow. I can see in the initiation code resulting from:
keras3::install_keras()
that this does, indeed, attach the tensorflow library, but it evidently doesn't attach the tensorflow R package, as the following code fails:
tf$constant("Hello TensorFlow!')
If I specifically attach the tensorflow R package:
library(tensorflow)
Then the above code works.
So attaching the R package tensorflow evidently does something that achieves "import tensorflow as tf" that is not done by simply attaching keras3.
In summary, while attaching keras3 attaches the tensorflow library, it doesn't create a way to call tensorflow functions directly from R.
Of course, I can simply attach the R tensorflow package myself. But when I do that, I am told that the tensorflow package masks two tensorflow functions from keras3: set_random_seed and shape. In general, it would seem preferable not to load the tensorflow library twice.
So, finally, my question is whether it is possible to run the magic that library(tensorflow) activates to permit tensorflow calls directly from R without attaching the rest of the R tensorflow package.
Thanks in advance for any help with the above.
Larry Hunscker

Running R 4.4.1/RStudio 2024.04.2 Build 764 on a 64-bit Windows 11 laptop.

To avoid that warning you can do:

library(tensorflow, exclude = c("shape", "set_random_seed"))
library(keras3)

Or you can do:

library(keras3)
tf <- reticulate::import("tensorflow")
1 Like

Many thanks, t-kalinowski, for the prompt reply.

  1. Thanks for pointing out the "exclude" parameter for the library() function. This was new to me and may be convenient for this and other purposes.
  2. More to the poiont, the reference to reticulate:: import was exactly what I was looking for.
    Again, thanks. Larry Hunsicker

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.