Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@ jobs:

- {os: 'ubuntu-latest' , tf: 'default', r: 'oldrel-1'}
- {os: 'ubuntu-latest' , tf: 'default', r: 'oldrel-2'}
- {os: 'ubuntu-20.04' , tf: 'default', r: '3.6'} # default R in ubuntu-20.04
# - {os: 'ubuntu-20.04' , tf: 'default', r: '3.6'} # default R in ubuntu-20.04

## release keras/tf version (if different from 'default')
# - {os: 'ubuntu-latest' , tf: 'release', r: 'release'}
# - {os: 'windows-latest', tf: 'release', r: 'release'}
# - {os: 'macOS-latest' , tf: 'release', r: 'release'}

## old keras/tf versions
- {os: 'ubuntu-latest', tf: '2.14', r: 'release'}
- {os: 'ubuntu-latest', tf: '2.13', r: 'release'}
- {os: 'ubuntu-latest', tf: '2.12', r: 'release'}
- {os: 'ubuntu-latest', tf: '2.11', r: 'release'}
- {os: 'ubuntu-latest', tf: '2.9' , r: 'release'}
# - {os: 'ubuntu-latest', tf: '2.14', r: 'release'}
# - {os: 'ubuntu-latest', tf: '2.13', r: 'release'}
# - {os: 'ubuntu-latest', tf: '2.12', r: 'release'}
# - {os: 'ubuntu-latest', tf: '2.11', r: 'release'}
# - {os: 'ubuntu-latest', tf: '2.9' , r: 'release'}

# these are allowed to fail
# - {os: 'ubuntu-latest', tf: '2.14.0rc1', r: 'release'}
Expand All @@ -58,6 +58,7 @@ jobs:
continue-on-error: ${{ matrix.tf == 'nightly' || contains(matrix.tf, 'rc') || matrix.r == 'devel' }}
env:
R_KEEP_PKG_SOURCE: yes
PY_REQUIRE_LEGACY_KERAS: true
# R_REMOTES_NO_ERRORS_FROM_WARNINGS: 'true'
# R_COMPILE_AND_INSTALL_PACKAGES: 'never' # commented out until CRAN builds TF 2.8
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
Expand Down Expand Up @@ -98,8 +99,9 @@ jobs:
cache-version: 1
upgrade: 'TRUE'

- name: Install Tensorflow + Keras deps
run: keras::install_keras(tensorflow = '${{ matrix.tf }}-cpu')
## Package uses py_require() instead
# - name: Install Tensorflow + Keras deps
# run: keras::install_keras(tensorflow = '${{ matrix.tf }}')

- uses: r-lib/actions/check-r-package@v2
with:
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ inst/doc
.Rprofile
.Renviron
revdep
keras.Rproj
keras2.Rproj
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: keras
Type: Package
Title: R Interface to 'Keras'
Version: 2.15.0.9000
Version: 2.16.0
Authors@R: c(
person("Tomasz", "Kalinowski", role = c("ctb", "cph", "cre"),
email = "[email protected]"),
Expand All @@ -23,8 +23,8 @@ Description: Interface to 'Keras' <https://keras.io>, a high-level neural
combinations of the two), and runs seamlessly on both 'CPU' and 'GPU' devices.
Encoding: UTF-8
License: MIT + file LICENSE
URL: https://tensorflow.rstudio.com/, https://github.com/rstudio/keras/tree/r2
BugReports: https://github.com/rstudio/keras/issues
URL: https://tensorflow.rstudio.com/, https://github.com/rstudio/keras3/tree/r2
BugReports: https://github.com/rstudio/keras3/issues
Depends:
R (>= 3.6)
Imports:
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ S3method(as_generator,tensorflow.python.data.ops.dataset_ops.Dataset)
S3method(as_generator,tensorflow.python.data.ops.dataset_ops.DatasetV2)
S3method(compile,keras.engine.training.Model)
S3method(compose_layer,default)
S3method(compose_layer,keras.engine.sequential.Sequential)
S3method(compose_layer,keras.models.Sequential)
S3method(evaluate,keras.engine.training.Model)
S3method(export_savedmodel,keras.engine.training.Model)
Expand Down Expand Up @@ -552,6 +553,7 @@ export(predict_classes)
export(predict_generator)
export(predict_on_batch)
export(predict_proba)
export(py_require_legacy_keras)
export(regularizer_l1)
export(regularizer_l1_l2)
export(regularizer_l2)
Expand Down
11 changes: 10 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
# keras (development version)
# keras 2.16.0

- Updates for usage with Legacy Keras (#1515).

- A package startup message is now displayed encouraging users to migrate to keras3.
- New function `py_require_legacy_keras()`.
- `install_keras()` now installs legacy keras `tf-keras`.
- `TF_USE_LEGACY_KERAS=1` envvar is now set on package startup.

- Documentation updates for CRAN (#1514)

# keras 2.15.0

Expand Down
34 changes: 32 additions & 2 deletions R/install.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,34 @@
#' Install TensorFlow and Keras, including all Python dependencies
#' Install TensorFlow and Legacy Keras, including all Python dependencies
#'
#' This function can be used to create a persistent virtual environment for usage
#' with Legacy Keras. New code is recommended to use the `keras3` package which
#' works automatically and does any special configuration like this.
#'
#' Note that recent versions of reticulate will automatically manage
#' environments if dependencies are declared with `reticulate::py_require()`.
#' Instead of using this `install_keras()` function, new users are first
#' encouraged to use `keras3`. If that's not an option, instead of creating a
#' persistent venv, you can this code at the start of the R session, before
#' loading keras:
#'
#' ```r
#' # declare requirements for legacy keras (tf-keras)
#' reticulate::py_require(c("tensorflow", "tf-keras", "numpy<2"))
#'
#' # also declare optional dependencies
#' reticulate::py_require(c(
#' "tensorflow-hub", "tensorflow-datasets", "scipy",
#' "requests", "Pillow", "h5py", "pandas", "pydot"
#' ))
#' library(keras)
#' ```
#'
#' Or more simply, call `py_require_legacy_keras()`
#'
#' ```r
#' library(keras)
#' py_require_legacy_keras()
#' ```
#'
#' This function will install Tensorflow and all Keras dependencies. This is a
#' thin wrapper around [`tensorflow::install_tensorflow()`], with the only
Expand Down Expand Up @@ -44,7 +74,7 @@ install_keras <- function(method = c("auto", "virtualenv", "conda"),
method = method,
conda = conda,
version = tensorflow,
extra_packages = pkgs,
extra_packages = c(pkgs, "tf-keras"),
# envname = envname,
# new_env = new_env,
...
Expand Down
1 change: 1 addition & 0 deletions R/layers-core.R
Original file line number Diff line number Diff line change
Expand Up @@ -563,5 +563,6 @@ compose_layer.keras.models.Sequential <- function(object, layer, ...) {
object
}

#' @export
compose_layer.keras.engine.sequential.Sequential <- compose_layer.keras.models.Sequential
# compose_layer.keras.src.engine.sequential.Sequential <- compose_layer.keras.models.Sequential
44 changes: 42 additions & 2 deletions R/package.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,44 @@ use_backend <- function(backend = c("tensorflow", "cntk", "theano", "plaidml"))
keras <- NULL


.onLoad <- function(libname, pkgname) {

.onAttach <- function(libname, pkgname) {
packageStartupMessage(
"The keras package is deprecated. Use the keras3 package instead."
)
}

#' @export
#' @rdname install_keras
py_require_legacy_keras <- function(extra_packages = TRUE) {
Sys.setenv(TF_USE_LEGACY_KERAS = 1L)
reticulate::py_require(
c(
"tf-keras", "tensorflow", "numpy<2",
if (extra_packages) {
c(
"tensorflow-hub",
"tensorflow-datasets",
"scipy",
"requests",
"Pillow",
"h5py",
"pandas",
"pydot"
)
}
)
)
}

.onLoad <- function(libname, pkgname) {

Sys.setenv(TF_USE_LEGACY_KERAS = 1L)

## In a future update, we will declare these dependencies by default.
## We're making this opt-in for now to minimize breaking user code
## and give package maintainers an opportunity to make updates
if (tolower(Sys.getenv("PY_REQUIRE_LEGACY_KERAS")) %in% c("1", "true", "yes"))
py_require_legacy_keras()

# resolve the implementation module (might be keras proper or might be tensorflow)
implementation_module <- resolve_implementation_module()
Expand Down Expand Up @@ -133,6 +166,10 @@ keras <- NULL
},

on_error = function(e) {
emit <- base::message
emit(
"To use legacy Keras via py_require(), call py_require_legacy_keras() at the start of the R session."
)
if (is_tensorflow_implementation())
stop(tf_config()$error_message, call. = FALSE)
else {
Expand All @@ -156,6 +193,9 @@ keras <- NULL
# replace "tensorflow.python.keras.*" with "keras.*"
classes <- sub(paste0("^", module), "keras", classes)

# replace legacy keras module name: tf_keras -> keras
classes <- sub("^tf_keras\\.", "keras.", classes)

# All python symbols moved in v2.13 under .src
# Preserve the original symbols for compatability with keras3,
# interleaving the back-compat class names after the originals.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

![](https://s3.amazonaws.com/keras.io/img/keras-logo-2018-large-1200.png)

[![R build status](https://github.com/rstudio/keras/workflows/R-CMD-check/badge.svg)](https://github.com/rstudio/keras/actions?workflow=R-CMD-check)
[![R build status](https://github.com/rstudio/keras3/workflows/R-CMD-check/badge.svg)](https://github.com/rstudio/keras3/actions?workflow=R-CMD-check)
[![CRAN_Status_Badge](https://www.r-pkg.org/badges/version/keras)](https://cran.r-project.org/package=keras)
[![license](https://img.shields.io/github/license/mashape/apistatus.svg?maxAge=2592000)](https://github.com/keras-team/keras/blob/master/LICENSE)

Expand Down
6 changes: 3 additions & 3 deletions index.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ This will provide you with default CPU-based installations of Keras and TensorFl

Below we walk through a simple example of using Keras to recognize handwritten digits from the [MNIST](https://en.wikipedia.org/wiki/MNIST_database) dataset. After getting familiar with the basics, check out the [tutorials](#tutorials) and additional [learning resources](#learning-more) available on this website.

The [Deep Learning with R](https://www.amazon.com/Deep-Learning-R-Francois-Chollet/dp/161729554X) book by François Chollet (the creator of Keras) provides a more comprehensive introduction to both Keras and the concepts and practice of deep learning.
The [Deep Learning with R](https://www.manning.com/books/deep-learning-with-r-second-edition) book by François Chollet (the creator of Keras) provides a more comprehensive introduction to both Keras and the concepts and practice of deep learning.

You may also find it convenient to download the [Deep Learning with Keras](https://github.com/rstudio/cheatsheets/raw/main/keras.pdf){target="_self"} cheat sheet, a quick high-level reference to all of the capabilities of Keras.

Expand Down Expand Up @@ -248,12 +248,12 @@ The [examples](articles/examples/index.html) demonstrate more advanced models in

The [function reference](reference/index.html) includes detailed information on all of the functions available in the package.

[![](https://images.manning.com/720/960/resize/book/a/4e5e97f-4e8d-4d97-a715-f6c2b0eb95f5/Allaire-DLwithR-HI.png){width=125 align=right style="margin-left:10px; margin-right: 20px; margin-top: 15px; border: solid 1px #cccccc;"}](https://www.amazon.com/Deep-Learning-R-Francois-Chollet/dp/161729554X)
[![](https://images.manning.com/264/352/resize/book/a/8f961a8-9cf7-47e8-a69b-949186e59a81/Chollet2-HI.png){width=125 align=right style="margin-left:10px; margin-right: 20px; margin-top: 15px; border: solid 1px #cccccc;"}](https://www.manning.com/books/deep-learning-with-r-second-edition)


### Deep Learning with R Book

If you want a more comprehensive introduction to both Keras and the concepts and practice of deep learning, we recommend the [Deep Learning with R](https://www.amazon.com/Deep-Learning-R-Francois-Chollet/dp/161729554X) book from Manning. This book is a collaboration between François Chollet, the creator of Keras, and J.J. Allaire, who wrote the R interface to Keras.
If you want a more comprehensive introduction to both Keras and the concepts and practice of deep learning, we recommend the [Deep Learning with R](https://www.manning.com/books/deep-learning-with-r-second-edition) book from Manning. This book is a collaboration between François Chollet, the creator of Keras, J.J. Allaire, who wrote the original R interface to Keras, and Tomasz Kalinowski, who maintains the R interface today.

The book presumes no significant knowledge of machine learning and deep learning, and goes all the way from basic theory to advanced practical applications, all using the R interface to Keras.

Expand Down
686 changes: 686 additions & 0 deletions index.html

Large diffs are not rendered by default.

41 changes: 36 additions & 5 deletions man/install_keras.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/keras-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vignettes/index.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ Keras provides a vocabulary for building deep learning models that is simple, el

### Deep Learning with R Book

If you want a more comprehensive introduction to both Keras and the concepts and practice of deep learning, we recommend the [Deep Learning with R](https://www.amazon.com/Deep-Learning-R-Francois-Chollet/dp/161729554X) book from Manning. This book is a collaboration between François Chollet, the creator of Keras, and J.J. Allaire, who wrote the R interface to Keras.
If you want a more comprehensive introduction to both Keras and the concepts and practice of deep learning, we recommend the [Deep Learning with R](https://www.manning.com/books/deep-learning-with-r-second-edition) book from Manning. This book is a collaboration between François Chollet, the creator of Keras, J.J. Allaire, who wrote the original R interface to Keras, and Tomasz Kalinowski, who maintains the R interface today.

The book presumes no significant knowledge of machine learning and deep learning, and goes all the way from basic theory to advanced practical applications, all using the R interface to Keras.

Expand Down
Loading