liveBook · Manning

Graph mode

As explained in Deep Learning with R, Third Edition

Graph mode is a TensorFlow execution mode in which decorated functions are represented and optimized as computation graphs.

Graph mode is TensorFlow’s default compilation mode. Wrapping a function with tf_function() converts its operations into a compiled program that can run faster on later calls. section 3.3.1

Compile a dense layer

dense <- tf_function(\(inputs, W, b) { tf$nn$relu(tf$matmul(inputs, W) + b) }) The first call takes longer while TensorFlow compiles the function; subsequent calls reuse the compiled program. section 3.3.1

Use graph mode with these practical considerations:

  • Debug the function in the R runtime before enabling compilation, because executed code differs from the code you write and debugging can become painful. section 3.3.1
  • Choose XLA compilation with jit_compile = TRUE when additional optimization is useful; its first execution takes longer than graph mode. section 3.3.1

How Deep Learning with R, Third Edition covers it

How deep it goes
Defines the default compilation mode and explains startup cost, reuse, debugging, and its distinction from XLA.
Examples
Uses an R dense-layer function containing matrix multiplication and ReLU as the compilation example.
What you can do after
Apply it via tf_function() after debugging TensorFlow code in the R runtime.
How it connects
Connects TensorFlow execution with tf_function(), XLA compilation, and performance optimization.

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.