-
Notifications
You must be signed in to change notification settings - Fork 280
Expand file tree
/
Copy pathpackage.R
More file actions
513 lines (433 loc) · 16.5 KB
/
Copy pathpackage.R
File metadata and controls
513 lines (433 loc) · 16.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
#' R interface to Keras
#'
#' Keras is a high-level neural networks API, developed with a focus on enabling
#' fast experimentation. Keras has the following key features:
#'
#' - Allows the same code to run on CPU or on GPU, seamlessly.
#' - User-friendly API which makes it easy to quickly prototype deep learning models.
#' - Built-in support for convolutional networks (for computer vision), recurrent
#' networks (for sequence processing), and any combination of both.
#' - Supports arbitrary network architectures: multi-input or multi-output models,
#' layer sharing, model sharing, etc. This means that Keras is appropriate for
#' building essentially any deep learning model, from a memory network to a neural
#' Turing machine.
#' - Is capable of running on top of multiple back-ends including
#' [TensorFlow](https://github.com/tensorflow/tensorflow),
#' [Jax](https://github.com/jax-ml/jax),
#' or [PyTorch](https://github.com/pytorch/pytorch).
#'
#' See the package website at <https://keras3.posit.co> for complete documentation.
#'
#' @importFrom reticulate
#' import import_from_path py_install
#' dict tuple
#' iterate py_iterator iter_next
#' py_call py_eval
#' py_capture_output py_is_null_xptr
#' py_get_attr py_has_attr
#' py_to_r r_to_py
#' np_array
#' @importFrom graphics par plot points
#' @importFrom tensorflow tf_version tf_config install_tensorflow all_dims
#' @aliases keras3-package
"_PACKAGE"
# package level global state
.globals <- new.env(parent = emptyenv())
tf <- NULL
ops <- NULL
np <- NULL
#' Main Keras module
#'
#' The `keras` module object is the equivalent of
#' `reticulate::import("keras")` and provided mainly as a convenience.
#'
#' @returns the keras Python module
#' @export
#' @usage NULL
#' @format An object of class `python.builtin.module`
keras <- NULL
.onLoad <- function(libname, pkgname) {
if (is.na(Sys.getenv("TF_CPP_MIN_LOG_LEVEL", NA)))
Sys.setenv("TF_CPP_MIN_LOG_LEVEL" = "2")
# tensorflow:::.onLoad() registers some reticulate class filter hooks
# we need to identify tensorflow tensors reliably.
requireNamespace("tensorflow", quietly = TRUE)
maybe_register_S3_methods()
registerS3method("%*%", "tensorflow.tensor", op_matmul, baseenv())
# if KERAS_PYTHON is defined then forward it to RETICULATE_PYTHON
keras_python <- get_keras_python()
if (!is.null(keras_python))
Sys.setenv(RETICULATE_PYTHON = keras_python)
py_require(c(
"keras", "pydot", "scipy", "pandas", "Pillow", "ipython"
#, "tensorflow_datasets"
))
if (is.na(Sys.getenv("KERAS_HOME", NA))) {
if (!dir.exists("~/.keras/")) {
Sys.setenv("KERAS_HOME" = normalizePath(
tools::R_user_dir("keras3", "cache"),
mustWork = FALSE
))
}
}
# default backend is tensorflow for now
# the tensorflow R package calls `py_require()` to ensure GPU is usable on Linux
# use_backend() includes py_require(action = "remove") calls to undo
# what tensorflow:::.onLoad() did. Keep them in sync!
# backend <- Sys.getenv("KERAS_BACKEND", "jax")
# ~/.keras.keras.json also has an (undocumented) 'backend' field
backend <- Sys.getenv("KERAS_BACKEND", "tensorflow")
gpu <- NA
if (endsWith(backend, "-cpu")) {
gpu <- FALSE
backend <- sub("-cpu$", "", backend)
Sys.setenv("KERAS_BACKEND" = backend)
} else if (endsWith(backend, "-gpu")) {
gpu <- TRUE
backend <- sub("-gpu$", "", backend)
Sys.setenv("KERAS_BACKEND" = backend)
}
if(Sys.getenv("DEVTOOLS_LOAD") == "keras3") {
if (Sys.getenv("KERAS_BACKEND_CONFIGURED") != "yes") {
use_backend(backend, gpu)
Sys.setenv("KERAS_BACKEND_CONFIGURED" = "yes")
}
} else {
use_backend(backend, gpu)
}
# delay load keras
try(keras <<- import("keras", delay_load = list(
priority = 10, # tensorflow priority == 5
environment = "r-keras",
# get_module = function() {
# resolve_implementation_module()
# },
on_load = function() {
# check version
# check_implementation_version()
# disabled because of errors with keras-hub
# tryCatch(
# import("tensorflow")$experimental$numpy$experimental_enable_numpy_behavior(),
# error = function(e) {
# warning("failed setting experimental_enable_numpy_behavior")
# })
},
on_error = function(e) {
if (is_tensorflow_implementation())
stop(tf_config()$error_message, call. = FALSE)
else {
if (grepl("No module named keras", e$message)) {
keras_not_found_message(e$message)
} else {
stop(e$message, call. = FALSE)
}
}
}
)))
# register class filter to alias classes to 'keras'
# reticulate::register_class_filter(function(classes) {
#
# module <- resolve_implementation_module()
#
# if (identical(module, "tensorflow.keras"))
# module <- "tensorflow.python.keras"
#
# # replace "tensorflow.python.keras.*" with "keras.*"
# classes <- sub(paste0("^", module), "keras", classes)
#
# # All python symbols moved in v2.13 under .src
# classes <- sub("^keras\\.src\\.", "keras.", classes)
#
# # let KerasTensor inherit all the S3 methods of tf.Tensor, but
# # KerasTensor methods take precedence.
# if(any("keras.engine.keras_tensor.KerasTensor" %in% classes))
# classes <- unique(c("keras.engine.keras_tensor.KerasTensor",
# "tensorflow.tensor",
# classes))
# classes
# })
# tensorflow use_session hooks
setHook("tensorflow.on_before_use_session", tensorflow_on_before_use_session)
setHook("tensorflow.on_use_session", tensorflow_on_use_session)
## numpy is loaded in a non-standard code path by reticulate
## (it's loaded early in c++ and bypasses the importer that calls hooks)
## So we indiscriminately register the s3 methods for numpy arrays
## instead of using py_register_load_hook("numpy")
registerS3method("@", "numpy.ndarray", at.keras_backend_tensor, baseenv())
registerS3method("@<-", "numpy.ndarray", at_set.keras_backend_tensor, baseenv())
reticulate::py_register_load_hook("keras", function() {
keras <- import("keras")
convert_to_tensor <- import("keras.ops", convert = FALSE)$convert_to_tensor
with(keras$device("cpu:0"), {
all_backend_tensor_s3_classes <- class(convert_to_tensor(array(1L)))
backend_tensor_class <- all_backend_tensor_s3_classes[1L]
if ("jax.Array" %in% all_backend_tensor_s3_classes)
backend_tensor_class <- "jax.Array"
# message("setting methods on backend_tensor_class: ", backend_tensor_class,
# "\nother options: ", paste0(all_backend_tensor_s3_classes, collapse = " "))
})
symbolic_tensor_class <- nameOfClass__python.builtin.type(keras$KerasTensor)
registerS3method("@", symbolic_tensor_class, at.keras_backend_tensor, baseenv())
registerS3method("@", backend_tensor_class, at.keras_backend_tensor, baseenv())
py_subset <- utils::getS3method("[", "python.builtin.object", envir = asNamespace("reticulate"))
registerS3method("[", "keras_r_backend_tensor", op_subset, baseenv())
registerS3method("[", "keras_py_backend_tensor", py_subset, baseenv())
registerS3method("@<-", symbolic_tensor_class, at_set.keras_backend_tensor, baseenv())
registerS3method("@<-", backend_tensor_class, at_set.keras_backend_tensor, baseenv())
`py_subset<-` <- utils::getS3method("[<-", "python.builtin.object", envir = asNamespace("reticulate"))
registerS3method("[<-", "keras_r_backend_tensor", `op_subset<-`, baseenv())
registerS3method("[<-", "keras_py_backend_tensor", `py_subset<-`, baseenv())
registerS3method("as.array", backend_tensor_class, op_convert_to_array, baseenv())
registerS3method("^", backend_tensor_class, `^__keras.backend.tensor`, baseenv())
registerS3method("%*%", backend_tensor_class, op_matmul, baseenv())
registerS3method("t", backend_tensor_class, op_transpose, baseenv())
registerS3method("aperm", backend_tensor_class, op_transpose, baseenv())
registerS3method("all.equal", backend_tensor_class, all.equal.numpy.ndarray, baseenv())
# "jax._src.core.Tracer"
if (keras$config$backend() == "jax") {
local({
#
jax <- import("jax")
jax_types <- c(
jax$Array$`__subclasses__`(),
jax$core$Tracer
)
for (py_type in jax_types) {
s3_classname <- nameOfClass__python.builtin.type(py_type)
registerS3method("@" , s3_classname, at.keras_backend_tensor, baseenv())
registerS3method("@<-" , s3_classname, at_set.keras_backend_tensor, baseenv())
registerS3method("as.array", s3_classname, op_convert_to_array, baseenv())
registerS3method("^" , s3_classname, `^__keras.backend.tensor`, baseenv())
registerS3method("%*%" , s3_classname, op_matmul, baseenv())
}
})
}
})
reticulate::py_register_load_hook("torch", function() {
# force keras load hooks to run
keras$ops
})
reticulate::py_register_load_hook("jax", function() {
# force keras load hooks to run
keras$ops
})
reticulate::py_register_load_hook("tensorflow", function() {
# Globally enabling this is too disruptive - causes
# errors in tf internal calls like `tf.strings.split("foo\nbar", "\n")`
# also, internal keras calls in `fit()` that check for overflow.
# we only use numpy style slicing via an internal method in op_subset()
# tf <- import("tensorflow")
# if(Sys.getenv("TENSORFLOW_ENABLE_NUMPY_BEHAVIOR") != "false")
# py_capture_output({
# tf$experimental$numpy$experimental_enable_numpy_behavior(
# prefer_float32 = TRUE,
# dtype_conversion_mode = "legacy"
# # "all" or "safe" leads to error in keras
# # can optionally also do "off", but that's even more strict
# # dtype_conversion_mode = "off"
# )
# }, "stderr")
# we still need to register tensorflow `@` and `@<-` methods even if the
# backend is not tensorflow, because tf.data can be used with other backends
# and tensorflow.tensor might still be encountered.
registerS3method("@", "tensorflow.tensor", at.keras_backend_tensor, baseenv())
registerS3method("@<-", "tensorflow.tensor", at_set.keras_backend_tensor, baseenv())
})
# on_load_make_as_activation()
np <<- try(import("numpy", convert = FALSE, delay_load = TRUE))
tf <<- try(import("tensorflow", delay_load = TRUE))
ops <<- try(import("keras.ops", delay_load = list(
before_load = function() {
# force the load hooks on 'keras' to run
keras$ops
}
)))
}
## should this live in reticulate?? probably...
#' @export
all.equal.numpy.ndarray <- function(target, current, ...) {
# or use numpy.allequal?
all.equal(as.array(target), as.array(current), ...)
}
at.keras_backend_tensor <- function(object, name) {
out <- rlang::env_clone(object)
attrs <- attributes(object)
cls <- switch(
name,
"1" = , one = , R = , r = "keras_r_backend_tensor",
"0" = , zero =, Py =, py = "keras_py_backend_tensor",
stop("<subset-style> must be 'r' or 'py' in expression <tensor>@<subset-style>")
)
attrs$class <- unique(c(cls, attrs$class))
attributes(out) <- attrs
out
}
at_set.keras_backend_tensor <- function(object, name, value) {
value
}
keras_not_found_message <- function(error_message) {
message(error_message)
message("Use the install_keras() function to install the core Keras library")
}
maybe_register_S3_methods <- function() {
# Tensorflow 2.16 exports these methods, but we don't need to
# take a dep on TF>=2.16. So we conditionally export them if installed
# tensorflow package is older. This is to avoid a warning about
# overwritten S3 methods on package load.
.register_no_overwrite <- function(class) {
if (is.null(utils::getS3method("py_to_r", class, optional = TRUE,
envir = asNamespace("reticulate")))) {
# __ instead of . to avoid a roxygen warning about unexported S3 methods
method <- get(paste0("py_to_r__", class))
registerS3method("py_to_r", class, method,
envir = asNamespace("reticulate"))
}
}
.register_no_overwrite("keras.src.utils.tracking.TrackedDict")
.register_no_overwrite("keras.src.utils.tracking.TrackedList")
.register_no_overwrite("keras.src.utils.tracking.TrackedSet")
}
# not exported regular function since nameOfClass() requires R>4.3
# __ instead of . to avoid roxygen warning
nameOfClass__python.builtin.type <- function(x) {
paste(
as_r_value(py_get_attr(x, "__module__")),
as_r_value(py_get_attr(x, "__name__")),
sep = "."
)
}
resolve_implementation_module <- function() {
# determine implementation to use
module <- get_keras_implementation()
# set the implementation module
if (identical(module, "tensorflow"))
module <- "tensorflow.keras"
# return implementation_module
module
}
get_keras_implementation <- function(default = "keras") {
get_keras_option("KERAS_IMPLEMENTATION", default = default)
}
get_keras_python <- function(default = NULL) {
get_keras_option("KERAS_PYTHON", default = default, as_lower = FALSE)
}
get_keras_option <- function(name, default = NULL, as_lower = TRUE) {
# case helper
uncase <- function(x) {
if (as_lower && !is.null(x) && !is.na(x))
tolower(x)
else
x
}
value <- Sys.getenv(name, unset = NA)
if (!is.na(value))
uncase(value)
else
uncase(default)
}
is_tensorflow_implementation <- function(implementation = get_keras_implementation()) {
grepl("^tensorflow", implementation)
}
is_keras_implementation <- function(implementation = get_keras_implementation()) {
identical(implementation, "keras")
}
check_implementation_version <- function() {
# get current implementation
implementation <- get_keras_implementation()
# version variables
ver <- NULL
required_ver <- NULL
# define implemetation-specific version/required-version
if (is_tensorflow_implementation(implementation)) {
name <- "TensorFlow"
ver <- tf_version()
required_ver <- "1.9"
update_with <- "tensorflow::install_tensorflow()"
} else if (is_keras_implementation(implementation)) {
name <- "Keras"
ver <- keras_version()
required_ver <- "2.0.0"
update_with <- "keras3::install_keras()"
}
# check version if we can
if (!is.null(required_ver)) {
if (ver < required_ver) {
stop("Keras loaded from ", implementation, " v", ver, ", however version ",
required_ver, " is required. Please update with ", update_with, ".",
call. = FALSE)
}
}
}
# Current version of Keras
keras_version <- function() {
if(keras$`__name__` == "keras_core")
return(package_version("3.0.0"))
ver <-
as_r_value(py_get_attr(keras, "__version__", TRUE)) %||%
tensorflow::tf_config()$version_str
ver <- gsub("[^0-9.-]+", ".", as.character(ver), perl = TRUE)
ver <- gsub("[.-]+", ".", ver, perl = TRUE)
package_version(ver)
}
#' Check if Keras is Available
#'
#' Probe to see whether the Keras Python package is available in the current
#' system environment.
#'
#' @param version Minimum required version of Keras (defaults to `NULL`, no
#' required version).
#'
#' @returns Logical indicating whether Keras (or the specified minimum version of
#' Keras) is available.
#'
#' @examples
#' \dontrun{
#' # testthat utilty for skipping tests when Keras isn't available
#' skip_if_no_keras <- function(version = NULL) {
#' if (!is_keras_available(version))
#' skip("Required keras version not available for testing")
#' }
#'
#' # use the function within a test
#' test_that("keras function works correctly", {
#' skip_if_no_keras()
#' # test code here
#' })
#' }
#'
#' @noRd
# @export
is_keras_available <- function(version = NULL) {
implementation_module <- resolve_implementation_module()
if (reticulate::py_module_available(implementation_module)) {
if (!is.null(version))
keras_version() >= version
else
TRUE
} else {
FALSE
}
}
# TODO: add option in `is_keras_available()` to avoid initializing Python
# (maybe in a callr call?), reexport.
# TODO: add func `is_backend_available()`, usage `is_backend_available("tensorflow")`
#' New axis
#'
#' This is an alias for `NULL`. It is meant to be used in `[` on tensors,
#' to expand dimensions of a tensor
#'
#' ```r
#' x <- op_convert_to_tensor(1:10)
#'
#' op_shape(x)
#' op_shape(x[])
#' op_shape(x[newaxis])
#' op_shape(x@py[newaxis])
#' op_shape(x@r[newaxis])
#'
#' op_shape(x[newaxis, .., newaxis])
#' op_shape(x@py[newaxis, .., newaxis])
#' op_shape(x@r[newaxis, .., newaxis])
#' ````
#' @export
newaxis <- NULL