liveBook · Manning

Eager evaluation

As explained in Deep Learning with R, Third Edition

Eager execution is an execution mode in which operations run immediately and produce results without first constructing a deferred computation graph.

Eager execution runs each operation immediately and returns its result, much like ordinary R or NumPy code. This direct flow makes programs easier to inspect and debug, but often sacrifices performance compared with compiled computation. section 3.3.1 section 3.4.3

Immediate tensor operations

TensorFlow can create tensors, apply operations, and display each result as the code runs: create a matrix of ones, square it, take its square root, add results, multiply matrices, and concatenate tensors. No separate graph-building step is needed for these operations. section 3.3.1

Use eager execution as the practical baseline, then consider compilation when performance requires it.

  • Debug operations in the R runtime before changing execution modes; the executed code remains visible and failures are easier to investigate. section 3.3.1
  • Expect slower execution because operations run one after another instead of being fused or parallelized. section 3.3.1 section 3.4.3
  • Compile TensorFlow functions with tf_function() only after debugging; the first call incurs compilation, while later calls can run faster. section 3.3.1
  • Treat PyTorch eager execution as the normal approach in practice, because torch$compile() may fail for some models or provide no speedup. section 3.4.2
a <- tf$ones(shape(2, 2))
b <- tf$square(a)  #1
c <- tf$sqrt(a)  #2
d <- b + c  #3
e <- tf$matmul(a, b)  #4
f <- tf$concat(list(a, b), axis = 0L) #5

Inspect a sequence of TensorFlow tensor operations that execute directly and produce values immediately. section 3.3.1

How Deep Learning with R, Third Edition covers it

How deep it goes
Explains immediate execution, debugging benefits, and performance costs, then contrasts eager operation with compilation.
Examples
Uses TensorFlow tensor operations and nested gradient tapes, plus PyTorch linear models and training steps, as concrete eager examples.
What you can do after
Diagnose/debug tensor code interactively, then choose whether TensorFlow or PyTorch compilation is worth its performance and reliability costs.
How it connects
Connects eager execution with compilation, graph mode, operation fusion, automatic differentiation, and gradient tapes.

Also explained in

Compare how 5 books explain eager evaluation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with R, Third Edition. The section links point to the parts of the book it draws on.