tensorflow (version 1.3.1)

tf_imperative: Imperative style TensorFlow

Description

The results of the computation are available right after the execution of a line of code. If the return value is Tensor, it can be converted to base R object automatically if convert = TRUE is specified. Users can also call tensor$eval() to convert any Tensor objects inside the expr scope to base R objects.

Usage

tf_imperative(expr, new_step = FALSE, convert = TRUE)

Arguments

expr

A block of code expession

new_step

A boolean indicating whether the expression is evaluated as a new step. A graph is constructed and kept around in the background, both for just executing using the standard TensorFlow runtime, and also for allowing automatic differentiation via tf$gradients. If this is set to TRUE, the graph as well as the cached tensors that have been kept around for gradient computation will be cleared after the expression is evaluated.

convert

A boolean indicating whether to convert the returned Tensor object to base R object.

Details

Note that this function is currently only experimental, meaning that the interface is subject to change or remove at later releases.

Examples

Run this code
# NOT RUN {
tf_imperative({
  a <- tf$constant(list(list(7), list(6)))
  b <- tf$constant(list(list(6, 7)))
  list(
   tf$matmul(a, b),
   a * 4
  )
})

# This is equivalent to the following:

tf <- tf$contrib$imperative
a <- tf$constant(list(list(7), list(6)))
b <- tf$constant(list(list(6, 7)))
res1 <- tf$matmul(a, b)
res2 <- a * 4
list(res1$eval(), res2$eval())

# }

Run the code above in your browser using DataCamp Workspace