diff --git a/NEWS.md b/NEWS.md index 5dbb30216f..6f25a73739 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # keras (development version) +- Updates to allow both R packages {keras} and {keras3} to be loaded. + # keras 2.13.0 - Default TF version installed by `install_keras()` is now 2.13. diff --git a/R/package.R b/R/package.R index 836da83e56..90a9d506d6 100644 --- a/R/package.R +++ b/R/package.R @@ -153,7 +153,23 @@ keras <- NULL classes <- sub(paste0("^", module), "keras", classes) # All python symbols moved in v2.13 under .src - classes <- sub("^keras\\.src\\.", "keras.", classes) + # Preserve the original symbols for compatability with keras3, + # interleaving the back-compat class names after the originals. + # E.g., this: + # "keras.src.models.sequential.Sequential" + # "keras.src.models.model.Model" + # ... "python.builtin.object" + # + # becomes this: + # "keras.src.models.sequential.Sequential" "keras.models.sequential.Sequential" + # "keras.src.models.model.Model" "keras.models.model.Model" + # ... "python.builtin.object" + + classes <- unique(as.vector(rbind( + classes, + sub("^keras\\.src\\.", "keras.", classes), + deparse.level = 0 + ))) # let KerasTensor inherit all the S3 methods of tf.Tensor, but # KerasTensor methods take precedence.