How to remove the warning prompt when executing Python code in the terminal

Problem Description

When executing Python code in the terminal, the terminal will output a warning warning. like:

UserWarning: Implicit dimension choice for softmax has been deprecated. Change the call to include dim=X as an argument.
  output = self.softmax(output)

Cause Analysis:

The main reason for this warning is that the Softmax() function in Pytorch requires the specified dim parameter. There was no warning in this way of writing in earlier Pytorch versions. Now, although the program can still run successfully, there will be a warning prompt.

can be nn.Softmax()changed tonn.Softmax(dim=1)

This is not the focus of this article. The focus of this article is to prevent warning prompts from appearing in the terminal.


solution:

If you don't want the terminal to output warnings, you can remove the warnings when the terminal executes the code or in the program.

You can add parameters when executing Python code in the terminal -W ignore:

python -W ignore main.py

The way to remove warnings in the program is:

import warnings
warnings.filterwarnings("ignore")

However, the warning still needs to be taken seriously. If you ignore it for a short time, there may be other problems that are difficult to troubleshoot later~

Guess you like

Origin blog.csdn.net/qq_39691492/article/details/125787303