Skip to content

Commit 54610e4

Browse files
committed
support -cpu and -gpu suffix in KERAS_BACKEND environment variable
1 parent 251ef39 commit 54610e4

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

R/package.R

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,18 @@ keras <- NULL
7272
# the tensorflow R package calls `py_require()` to ensure GPU is usable on Linux
7373
# use_backend() includes py_require(action = "remove") calls to undo
7474
# what tensorflow:::.onLoad() did. Keep them in sync!
75-
use_backend(Sys.getenv("KERAS_BACKEND", "tensorflow"))
75+
backend <- Sys.getenv("KERAS_BACKEND", "tensorflow")
76+
gpu <- NA
77+
if (endsWith(backend, "-cpu")) {
78+
gpu <- FALSE
79+
backend <- sub("-cpu$", "", backend)
80+
Sys.setenv("KERAS_BACKEND" = backend)
81+
} else if (endsWith(backend, "-gpu")) {
82+
gpu <- TRUE
83+
backend <- sub("-gpu$", "", backend)
84+
Sys.setenv("KERAS_BACKEND" = backend)
85+
}
86+
use_backend(backend, gpu)
7687

7788
# delay load keras
7889
try(keras <<- import("keras", delay_load = list(

0 commit comments

Comments
 (0)