tf_cond: tf.cond

Description Usage Arguments Value Note Examples

View source: R/tf-ctrl-flow-wrappers.R

Description

This is a minimal wrapper around tf$cond() that allows you to supply true_fn and false_fn as lambda functions defined using the tilde ~.

Usage

1
tf_cond(pred, true_fn, false_fn, name = NULL)

Arguments

pred

R logical or a tensor.

true_fn, false_fn

a ~ function, a function, or something coercible to a function via as.function

name

a string, passed on to tf.cond()

Value

if cond is a tensor, then the result of tf.cond(). Otherwise, if pred is an EagerTensor or an R logical, then the result of either true_fn() or false_fn()

Note

in Tensorflow version 1, the strict keyword argument is supplied with a value of TRUE (different from the default)

Examples

1
2
3
4
5
6
7
8
9
## Not run: 
## square if positive
# using tf$cond directly:
raw <- function(x) tf$cond(x > 0, function() x * x, function() x)

# using tf_cond() wrapper
tilde <- function(x) tf_cond(x > 0, ~ x * x, ~ x)

## End(Not run)

tfautograph documentation built on Sept. 18, 2021, 1:07 a.m.