Skip to content

Commit e3791c4

Browse files
authored
change default: tf_function(autograph=TRUE) (#471)
* change default: tf_function(autograph=TRUE) * add tfautograph to Suggests
1 parent 3d535d2 commit e3791c4

File tree

4 files changed

+38
-17
lines changed

4 files changed

+38
-17
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Roxygen: list(markdown = TRUE)
4949
Suggests:
5050
testthat (>= 2.1.0),
5151
keras,
52+
tfautograph,
5253
tfestimators,
5354
callr
5455
RoxygenNote: 7.1.1

NEWS.md

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

3+
- Changed default in `tf_function()` to `autograph=TRUE`.
34
- Added S3 generic `as_tensor()`.
45

56
# tensorflow 2.5.0

R/eager.R

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,31 +102,44 @@ as.logical.tensorflow.python.ops.variables.Variable <- as.logical.python.builtin
102102
#' runtime to apply optimizations and exploit parallelism in the computation
103103
#' defined by `f`.
104104
#'
105+
#' A guide to getting started with
106+
#' [`tf.function`](https://www.tensorflow.org/api_docs/python/tf/function) can
107+
#' be found [here](https://www.tensorflow.org/guide/function).
108+
#'
105109
#' @param f the function to be compiled
106110
#' @param input_signature A possibly nested sequence of `tf$TensorSpec` objects
107111
#' specifying the shapes and dtypes of the tensors that will be supplied to
108112
#' this function. If `NULL`, a separate function is instantiated for each
109113
#' inferred input signature. If `input_signature` is specified, every input to
110114
#' `f` must be a tensor.
111-
#' @param autograph Whether autograph should be applied on `f` before tracing a
112-
#' graph. This allows for dynamic control flow (if's, loops etc.) in the
113-
#' traced graph. See https://www.tensorflow.org/guide/autograph for more
114-
#' information. Note: We set the default to `FALSE` until this functionality
115-
#' is available from R.
115+
#' @param autograph TRUE or FALSE. If TRUE (the default), you can use tensors in
116+
#' R control flow expressions `if`, `while`, `for` and `break` and they will
117+
#' be traced into the tensorflow graph. A guide to getting started and
118+
#' additional details can be found:
119+
#' [here](https://t-kalinowski.github.io/tfautograph/)
116120
#' @param ... additional arguments passed on to `tf.function` (vary based on
117121
#' Tensorflow version). See
118-
#' https://www.tensorflow.org/api_docs/python/tf/function for details.
122+
#' [here](https://www.tensorflow.org/api_docs/python/tf/function#args_1) for
123+
#' details.
119124
#'
120125
#' @export
121126
tf_function <- function(f,
122127
input_signature = NULL,
123-
autograph = FALSE, # default will change to TRUE in TF 2.6
128+
autograph = TRUE,
124129
...) {
130+
if (!is.function(f))
131+
stop("`f` must be an R function")
125132

126-
if(!isFALSE(autograph)) stop("Autograph functionality is not (yet) supported from R.")
133+
if (!(isTRUE(autograph) || isFALSE(autograph)))
134+
stop("`autograph` must be TRUE or FALSE")
127135

128-
args <- list(py_func(f), input_signature, autograph, ...)
136+
if (autograph) {
137+
# Can't register tfautograph in Imports yet due to circular dependency
138+
if(!requireNamespace("tfautograph", quietly=TRUE))
139+
stop('"tfautograph" package required if autograph=TRUE. Please run install.packages("tfautograph")')
140+
f <- tfautograph::autograph(f)
141+
}
129142

143+
args <- list(py_func(f), input_signature, FALSE, ...)
130144
do.call(tf$`function`, args)
131145
}
132-

man/tf_function.Rd

Lines changed: 13 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)