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
