Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging in Google Colab

Tags:

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

like image 909
thegreatcoder Avatar asked Oct 05 '18 00:10

thegreatcoder


People also ask

How do I debug with Jupyter?

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.

Is there a variable Explorer in Google Colab?

Colab now has a variable inspector to give you more insights into what your code is doing while executing.

What is debugging in Python?

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.


Video Answer


2 Answers

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

like image 88
Querenker Avatar answered Oct 26 '22 21:10

Querenker


Use pdb built-in breakpoint function according to below commands :

import pdb;  pdb.set_trace() 

Command Description

  1. list Show the current location in the file
  2. h(elp) Show a list of commands, or find help on a specific command
  3. q(uit) Quit the debugger and the program
  4. c(ontinue) Quit the debugger, continue in the program
  5. n(ext) Go to the next step of the program
  6. Repeat the previous command
  7. p(rint) Print variables
  8. s(tep) Step into a subroutine
  9. r(eturn) Return out of a subroutine
like image 28
Javad Shirkhani Avatar answered Oct 26 '22 21:10

Javad Shirkhani