Example: Differentiating a Function with Dynamic Control Flow
To illustrate automatic differentiation with dynamic control flow, consider a contrived function where a while loop scales an input vector until its norm exceeds a threshold, followed by an if statement that returns different multiples of the vector based on the sum of its elements. Even though the number of loop iterations and the final scaling factor depend entirely on the specific values of the input, the deep learning framework constructs a concrete computational graph during the forward pass. Because the resulting function is linear with a piecewise defined scale, its gradient with respect to can be logically verified to match the element-wise division .
0
1
Tags
D2L
Dive into Deep Learning @ D2L