Debugging in Google Colab

thegreatcoder picture thegreatcoder · Oct 5, 2018 · Viewed 12.7k times · Source

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.

Step_though_collab

Answer

Querenker picture Querenker · Mar 15, 2019

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).