Each Answer to this Q is separated by one/two green lines.
I am running the following code snippet in google colab in a single cell:
%debug # Create tensors of shape (10, 3) and (10, 2). x = torch.randn(10, 3) y = torch.randn(10, 2) # Build a fully connected layer. linear = nn.Linear(3, 2) print ('w: ', linear.weight) print ('b: ', linear.bias)
I wish to debug a piece of code (step through it line by line) to understand what is going on. I wish to step inside the function nn.Linear.
However, when I step through, it does not enter the function at all. Is there a way to step through nn.Linear line by line? Also, how exactly do I set a breakpoint in nn.Linear? Besides, I wish to step though the snippet line by line as well. However, as the picture shows, the step command automatically steps through and executes the print statement as well.
Since Python 3.7 you can use a built-in breakpoint function. If this is not available, you can use
import pdb pdb.set_trace()
If you want to execute the next line you can try
n (next) instead of
Use pdb built-in breakpoint function according to below commands :
import pdb; pdb.set_trace()
- list Show the current location in the file
- h(elp) Show a list of commands, or find help on a specific command
- q(uit) Quit the debugger and the program
- c(ontinue) Quit the debugger, continue in the program
- n(ext) Go to the next step of the program
- Repeat the previous command
- p(rint) Print variables
- s(tep) Step into a subroutine
- r(eturn) Return out of a subroutine
You can use inbuilt Breakpoint functions to set breakpoint in nn.Linear.
import sys; sys.breakpoint()
There are many more available commands for interactive debugging,
Command Description list Show the current location in the file h(elp) Show a list of commands, or find help on a specific command q(uit) Quit the debugger and the program c(ontinue) Quit the debugger, continue in the program n(ext) Go to the next step of the program <enter> Repeat the previous command p(rint) Print variables s(tep) Step into a subroutine r(eturn) Return out of a subroutine