tf_case: tf.case

Description Usage Arguments Value Examples

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

Description

This is a minimal wrapper around tf.case() that allows you to supply the pred_fn_pairs using the ~.

Usage

1
2
3
4
5
6
7
tf_case(
  ...,
  pred_fn_pairs = list(...),
  default = NULL,
  exclusive = FALSE,
  name = "case"
)

Arguments

..., pred_fn_pairs

a list pred_fn_pairs supplied with the ~ like so: pred ~ fn_body

default

a function, optionally specified with the ~, (or something coercible to a function via as.function())

exclusive

bool, whether to evaluate all preds and ensure only one is true. If FALSE (the default), then the preds are evaluated in the order supplied until the first TRUE value is encountered (effectively, acting as an if()... else if() ... else if() ... chain)

name

a string, passed on to tf.case()

Value

The result from tf$case()

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
## Not run: 
fizz_buzz_one <- function(x) {
  tf_case(
    x %% 15 == 0 ~ "FizzBuzz",
    x %%  5 == 0 ~ "Buzz",
    x %%  3 == 0 ~ "Fizz",
    default = ~ tf$as_string(x, precision = 0L)
  )
}

fn <- tf_function(autograph(function(n) {
  for(e in tf$range(n))
    tf$print(fizz_buzz_one(e))
}))

x <- tf$constant(16)
fn(x)

## End(Not run)

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