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.
The easiest way to debug a Jupyter notebook is to use the %debug magic command. Whenever you encounter an error or exception, just open a new notebook cell, type %debug and run the cell. This will open a command line where you can test your code and inspect all variables right up to the line that threw the error.
Colab now has a variable inspector to give you more insights into what your code is doing while executing.
A debugger is a program that can help you find out what is going on in a computer program. You can stop the execution at any prescribed line number, print out variables, continue execution, stop again, execute statements one by one, and repeat such actions until you have tracked down abnormal behavior and found bugs.
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()
instead.
If you want to execute the next line you can try n
(next) instead of s
(step).
Use pdb built-in breakpoint function according to below commands :
import pdb; pdb.set_trace()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With