22# '
33# ' @description
44# '
5- # ' Beginning with reticulate version 1.41, in most circumstances, calling the
6- # ' `install_tensorflow()` function is no longer necessary, because reticulate
7- # ' automatically registers python requirements with `reticulate::py_require()`
8- # ' when tensorflow is loaded.
5+ # ' This function installs TensorFlow into a persistant virtual environment.
6+ # ' Beginning with reticulate version 1.41, in most circumstances, creating a
7+ # ' persistent virtual environment by calling the `install_tensorflow()` function
8+ # ' is no longer necessary, because reticulate automatically will resolve a
9+ # ' python environment that satisfies all python requirements declared with
10+ # ' `reticulate::py_require()`.
911# '
10- # ' The Python packages registered with `py_require()` by the tensorflow R
11- # ' package:
12+ # ' New code is recommended to call `py_require_tensorflow()` at the start of an
13+ # ' R session to declare tensorflow requirements via `py_requore()`. In a future
14+ # ' package update this will by default be done in tensorflow's `.onLoad` hook.
15+ # '
16+ # ' The `py_require_tensorflow()` function that can dynamically modify the python
17+ # ' requirements to enable usage of a GPU if one is available and usable by the R
18+ # ' session.
19+ # '
20+ # ' The Python packages registered with `py_require()` by
21+ # ' `py_require_tensorflow()`:
1222# '
1323# ' - On Linux: if a GPU is detected: `"tensorflow[and-cuda]"`, otherwise,
1424# ' `"tensorflow-cpu"`.
3141# ' virtual environment with more complete set packages that includes additional
3242# ' optional dependencies, use [`keras3::install_keras()`].
3343# '
34- # ' @details You may be prompted to download and install miniconda if reticulate
35- # ' did not find a non-system installation of python. Miniconda is the
36- # ' recommended installation method for most users, as it ensures that the R
37- # ' python installation is isolated from other python installations. All python
38- # ' packages will by default be installed into a self-contained conda or venv
39- # ' environment named "r-reticulate". Note that "conda" is the only supported
40- # ' method on M1 Mac.
41- # '
42- # ' If you initially declined the miniconda installation prompt, you can later
43- # ' manually install miniconda by running [`reticulate::install_miniconda()`].
44- # '
4544# ' @section Custom Installation: `install_tensorflow()` or
4645# ' `keras3::install_keras()` isn't required to use tensorflow with the
4746# ' package. If you manually configure a python environment with the required
131130# ' @param new_env If `TRUE`, any existing Python virtual environment and/or
132131# ' conda environment specified by `envname` is deleted first.
133132# '
133+ # ' @param use_gpu Only consulted if on Linux. It has no effect on macOS or
134+ # ' Windows. If `NA`, the R package will attempt to detect a GPU. If a GPU is
135+ # ' detected, then this is taken as `TRUE`, `FALSE` otherwise. If `TRUE`, then
136+ # ' `tensorflow[and-cuda]` is declared, otherwise, `tensorflow-cpu` is declared
137+ # ' via `py_require()`.
138+ # '
134139# ' @param ... other arguments passed to [`reticulate::conda_install()`] or
135140# ' [`reticulate::virtualenv_install()`], depending on the `method` used.
136141# '
@@ -372,13 +377,15 @@ has_gpu <- function() {
372377}
373378
374379
375- get_py_requirements <- function () {
380+ get_py_requirements <- function (use_gpu = NA ) {
376381 python_version <- NULL
377382 packages <- " tensorflow"
378383
379384 if (is_linux()) {
385+ if (is.na(use_gpu ))
386+ use_gpu <- has_gpu()
380387
381- if (has_gpu() ) {
388+ if (use_gpu ) {
382389 packages <- " tensorflow[and-cuda]"
383390 } else {
384391 packages <- " tensorflow-cpu"
@@ -402,7 +409,7 @@ get_py_requirements <- function() {
402409 list (packages = packages , python_version = python_version )
403410}
404411
405- default_version <- numeric_version(" 2.18 " )
412+ default_version <- numeric_version(" 2.20 " )
406413
407414parse_tensorflow_version <- function (version ) {
408415 # returns unquoted string directly passable to pip, e.g 'tensorflow==2.5.*'
0 commit comments