Introduction Drawbacks
Imperative DL Code With Python Side-effects
[email protected]
2def(x):
3print("Input:, x)
4f(1)
5f(1)
6f(2)
Output (expecting1,1,2):
Input: 1
Input: 2
Side-effect producing, native Python statements, e.g., printing, list
appending, global variable mutation, are problematic for
tf.function-decorated functions (i.e., “tf.functions”).
Because they are traced, a function’s behavior is “etched” into its
corresponding graph.
Can have multipletimes
ornot at all.
Side-effects occur whentf.functions are called the first time.
Subsequent calls with similar arguments execute the graph instead.
Khatchadourian, Castro Vélez, Bagherzadeh, Jia, Raja Hybridize Functions Imperative DL Refactoring 8 / 18