(wind swiping sound)
- [Instructor] Using the Python debugger in a terminal.
In this video, we'll learn how to use a Python debugger
in a terminal.
For this example, we're running code
from the token classification section,
downloading the Conll dataset
before loading a tokenizer to pre-process it.
Check out the section of the course link below
for more information.
Once this is done,
we try to batch together some features
of the training dataset by padding them
and returning a tensor.
If we try to execute our scripts in a terminal
we get an error.
Note that we use PyTorch here
we return tensors equal pity.
But you would get the same error with TensorFlow.
As we have seen in the, 'How to debug an error?' video,
The raw message is at the end and it indicates we
should use pairing, which we're actually trying to do.
So this is not useful and we need to go little deeper
to debug the problem.
Fortunately, you can use the Python debugger quite easily
in a terminal by launching your script with Python -m PDB
and then the name of the training script.
When executing that comment, you are sent
to the first instruction of your script.
You can run just the next instruction by typing N and enter.
Or you can continue directly to zero by typing C and enter.
Once there, you go to the very bottom
of the traceback and you can type commands.
The first two commands you should learn are U and D,
for up and down.
This allows you to get up and down in the traceback.
Going up twice, we get to the point the error was reached.
The first command to learn is P for print.
It allows you to print any value you want.
For instance, here we can see the value of return_tensors
or batch_outputs to try to understand what triggered zero.
The batch outputs dictionary is a bit hard to see
so let's dive into smaller pieces of it.
Inside the debugger, you can not only print any variable
but also evaluate any expression,
so we can look independently at the inputs.
Also labels.
Those labels are definitely weird.
They are various size, which we can confirm
by printing the sites using a release compression.
No wonder the tokenizer wasn't able to create
a tensor with them.
This is because the pad method only takes care
of the tokenizer outputs, the input IDs, the attention mask
and the token type IDs.
So we have to pad the level ourselves
before trying to create a new sensor with them.
Once you're ready to execute the Python debugger,
you can press Q for quit and enter.
Another way we can access the Python debugger,
is to put a breaking point in our script.
We can do this using the PDB that set_trace method.
As long as we import the PDB module
at the beginning of our script.
Saving and then relaunching our script, with just Python.
We'll stop the execution at the breaking point we set.
We can inspect all the variable
before the next instruction is executed again.
For instance, here, the features.
Typing N and enter execute the next instruction
which takes us back inside traceback.
When going to fix zero manually is to
pad all the labels to the longest.
Another way is to use the data creator suitable
for token classification.
If you want to learn how to use the Python
debugger in a notebook, check out the video in link below.
(wind swiping sound)