Skip to content

Commit 647391b

Browse files
authored
Merge pull request #1274 from rstudio/preprocessing-layers-guide
Preprocessing layers vignette
2 parents 9201439 + 53b77f6 commit 647391b

24 files changed

+749
-55
lines changed

.Rbuildignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
^issues$
1313
^external$
1414
^vignettes/examples$
15+
^vignettes/new-guides$
1516
^vignettes/learn\.Rmd$
1617
^vignettes/tools\.Rmd$
1718
^vignettes/images/resources-.*$
@@ -40,4 +41,5 @@
4041
^cran-comments\.md$
4142
^\.github$
4243
^\.vscode$
44+
^scratch$
4345

.github/workflows/R-CMD-check.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
- {os: 'ubuntu-20.04', tf: 'default', r: '3.6'} # default R in ubuntu-20.04
3232
- {os: 'ubuntu-20.04', tf: 'default', r: '3.5'} #
3333
- {os: 'ubuntu-20.04', tf: 'default', r: '3.4'} #
34-
- {os: 'ubuntu-20.04', tf: 'default', r: '3.3'} # Oldest R available in Rstudio Marketplace offerings
34+
# - {os: 'ubuntu-20.04', tf: 'default', r: '3.3'} # Oldest R available in RStudio Marketplace offerings; rcmdcheck not available
3535
# - {os: 'ubuntu-20.04', tf: 'tensorflow-cpu', r: '3.2'} # rcmdcheck not available
3636
# - {os: 'ubuntu-20.04', tf: 'tensorflow-cpu', r: '3.1'} # remotes not available
3737

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ doc
1818
Meta
1919
.vscode
2020
scratch
21+
inst/doc

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ License: MIT + file LICENSE
2626
URL: https://keras.rstudio.com
2727
BugReports: https://github.com/rstudio/keras/issues
2828
Depends:
29-
R (>= 3.2)
29+
R (>= 3.4)
3030
Imports:
3131
generics (>= 0.0.1),
3232
reticulate (>= 1.10),

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ export(application_vgg16)
104104
export(application_vgg19)
105105
export(application_xception)
106106
export(array_reshape)
107+
export(as_tensor)
107108
export(backend)
108109
export(bidirectional)
109110
export(callback_csv_logger)
@@ -611,6 +612,7 @@ importFrom(reticulate,use_python)
611612
importFrom(reticulate,use_virtualenv)
612613
importFrom(rlang,"%||%")
613614
importFrom(stats,predict)
615+
importFrom(tensorflow,as_tensor)
614616
importFrom(tensorflow,evaluate)
615617
importFrom(tensorflow,export_savedmodel)
616618
importFrom(tensorflow,install_tensorflow)

NEWS.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# keras (development version)
22

3-
- New family of *preprocessing* layers. These are the spiritual successor to the `tfdatasets::step_*` family of data transformers (to be deprecated in a future release). New function:
3+
- New family of *preprocessing* layers. These are the spiritual successor to the `tfdatasets::step_*` family of data transformers (to be deprecated in a future release).
4+
Added a new vignette: "Working with preprocessing layers".
5+
New functions:
46

57
Image preprocessing:
68
- `layer_resizing()`
@@ -53,9 +55,9 @@
5355

5456
- Fixed an issue in `layer_input()` where passing a tensorflow `DType` objects to argument `dtype` would throw an error.
5557

56-
- Fixed an issue in `compile()` where passing an R function via an in-line
58+
- Fixed an issue in `compile()` where passing an R function via an in-line
5759
call would result in an error from subsequent `fit()` calls.
58-
(e.g., `compile(loss = function(y_true, y_pred) my_loss(y_true, y_pred))`
60+
(e.g., `compile(loss = function(y_true, y_pred) my_loss(y_true, y_pred))`
5961
now succeeds)
6062

6163
- `clone_model()` gains a `clone_function` argument that allows you to customize each layer as it is cloned.
@@ -72,9 +74,10 @@
7274
`layer_subtract()`, `layer_multiply()`, `layer_average()`, `layer_maximum()`,
7375
`layer_minimum()` , `layer_dot()`)
7476

75-
- `%py_class%` gains the ability to delay initializing the python session until first use.
77+
- `%py_class%` gains the ability to delay initializing the Python session until first use.
7678
It is now safe to implement and export `%py_class%` objects in an R package.
7779

80+
- Bumped minimum R version to 3.4. Expanded CI to test on all supported R version. Fixed regression that prevented package installation on R <= 3.4
7881
# keras 2.6.0
7982

8083
Breaking changes (Tensorflow 2.6):
@@ -600,7 +603,7 @@ SavedModel format.
600603
- Don't re-export `install_tensorflow()` and `tf_config()` from tensorflow
601604
package.
602605

603-
- `is_keras_available()` function to probe whether the Keras python
606+
- `is_keras_available()` function to probe whether the Keras Python
604607
package is available in the current environment.
605608

606609
- `as.data.frame()` S3 method for Keras training history

R/backend.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2160,7 +2160,10 @@ k_random_normal_variable <- function(shape, mean, scale, dtype = NULL, name = NU
21602160
#'
21612161
#' @export
21622162
k_random_uniform <- function(shape, minval = 0.0, maxval = 1.0, dtype = NULL, seed = NULL) {
2163-
# TODO: if dtype is int, then minval/maxval should be cast to int
2163+
if(!is.null(dtype)) {
2164+
minval <- as_tensor(minval, dtype = dtype)
2165+
maxval <- as_tensor(maxval, dtype = dtype)
2166+
}
21642167
keras$backend$random_uniform(
21652168
shape = backend_normalize_shape(shape),
21662169
minval = minval,

R/install.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#' Install TensorFlow and Keras, including all python dependencies
1+
#' Install TensorFlow and Keras, including all Python dependencies
22
#'
33
#' This function will install Tensorflow and all Keras dependencies. This is a
44
#' thin wrapper around [`tensorflow::install_tensorflow()`], with the only
@@ -27,7 +27,7 @@ install_keras <- function(method = c("auto", "virtualenv", "conda"),
2727
...) {
2828

2929
pkgs <- default_extra_packages(tensorflow)
30-
if(!is.null(extra_packages))
30+
if(!is.null(extra_packages)) # user supplied package version constraints take precedence
3131
pkgs[gsub("[=<>~]{1,2}[0-9.]+$", "", extra_packages)] <- extra_packages
3232

3333
if(tensorflow == "default") # may be different from tensorflow

R/layers-core.R

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -423,32 +423,45 @@ normalize_path <- function(path) {
423423
}
424424

425425

426-
normalize_shape <- function (x) {
427-
# reflect NULL back
428-
if (is.null(x))
429-
return(x)
430-
else
431-
as_shape(x)
432-
}
433426

434-
as_shape <- function (x) {
427+
# Helper function to coerce shape arguments to tuple
428+
# tf$reshape()/k_reshape() doesn't accept a tf.TensorShape object
429+
normalize_shape <- function(shape) {
435430

436-
if (inherits(x, "tensorflow.python.framework.tensor_shape.TensorShape"))
437-
return(x)
438-
439-
if (is.null(x))
440-
dims <- NULL
441-
else
442-
dims <- lapply(x, function(d) {
443-
if (is.null(d) || isTRUE(is.na(d)))
444-
NULL
431+
# reflect NULL back
432+
if (is.null(shape))
433+
return(shape)
434+
435+
# if it's a list or a numeric vector then convert to integer
436+
if (is.list(shape) || is.numeric(shape)) {
437+
shape <- lapply(shape, function(value) {
438+
if (!is.null(value))
439+
as.integer(value)
445440
else
446-
as.integer(d)
441+
NULL
447442
})
443+
}
444+
445+
if(inherits(shape, "tensorflow.python.framework.tensor_shape.TensorShape"))
446+
shape <- as.list(shape$as_list()) # unpack for tuple()
448447

449-
tensorflow::tf$TensorShape(dims)
448+
# coerce to tuple so it's iterable
449+
tuple(shape)
450450
}
451451

452+
# @export
453+
# format.python.builtin.object <- function(x, ...) {
454+
# capture.output(print(x, ...))
455+
# }
456+
457+
as_shape <- function(x) {
458+
lapply(x, function(d) {
459+
if (is.null(d))
460+
NULL
461+
else
462+
as.integer(d)
463+
})
464+
}
452465

453466
#' Create a Keras Layer
454467
#'

R/layers-merge.R

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
#' layer instance is returned.
1313
#'
1414
#' @family merging_layers
15-
#' @seealso
16-
#' @seealso
17-
#'
1815
#'
1916
#' @seealso
2017
#' + <https://www.tensorflow.org/api_docs/python/tf/keras/layers/add>

0 commit comments

Comments
 (0)