【python】错误解决经历

持续更新ING

 

python

1、【RuntimeWarning: invalid value encountered in multiply】

{乘法中遇到无效值,比如 inf 或者 nan 等} {其他invalid问题类似}

2、【non-default argument follows default argument】

{原因是将没有默认值的参数在定义时放在了有默认值的参数的后面} →→解决→→{将没有default值的参数放在前面}

{python中规定:函数调用的时候,如果第一个参数使用了关键字绑定,后面的参数也必须使用关键字绑定}

3、【list index out of range

{ list[index] index超出范围 ////////  list是一个空的 没有一个元素 进行list[0]就会出现该错误 }

4、【ValueError: there aren't any elements to reflect in axis 0 of 'array'

{numpy中数组padding操作处报错,当输入数据(list or ndarray)长度为0时触发,详见 \Lib\site-packages\numpy\lib\arraypad.py}

{详细讨论参见GitHub上numpy中的issue}

{解决:已知是input data为空导致的,追溯到数据处理阶段debug即可,可以使用ipdb工具追踪}

5、import torchvision时报错【ImportError: cannot import name 'PILLOW_VERSION' from 'PIL'

参考CSDN博客,torchvision在运行时要调用PIL模块,调用PIL模块的PILLOW_VERSION函数。但是PILLOW_VERSION在Pillow 7.0.0之后的版本被移除了,Pillow 7.0.0之后的版本使用__version__函数代替PILLOW_VERSION函数。

{解决:根据报错的最后一行提示,打开function.py文件,使用from PIL import Image, ImageOps, ImageEnhance, __version__ 替换文件中from PIL import Image, ImageOps, ImageEnhance,PILLOW_VERSION这句,保存。}

6、pip3/pip list之后显示【WARNING: There was an error checking the latest version of pip.

{python3/python -m pip install --upgrade pip} 升级pip即可。貌似不影响使用,强迫症可以消除一下warning。 

7、【RuntimeWarning: Couldn't find ffprobe or avprobe - defaulting to ffprobe, but may not work///FileNotFoundError: [Errno 2] No such file or directory: 'ffprobe'】

{pydub AudioSegment处理音频的坑}{安装ffmpeg, 配置环境变量,解决}  

8、启动notebook时(jupyter notebook)报错【ModuleNotFoundError: No module named 'jupyter_server.contents'】【TypeError: warn() missing 1 required keyword-only argument: 'stacklevel'】  【解决】

参考:ModuleNotFoundError: No module named 'jupyter_server.contents' switching to Python kernel · Issue #24436 · microsoft/azuredatastudio (github.com)

pip uninstall traitlets
pip install traitlets==5.9.0

注:在此之前,重新安装jupyter_server等等都试过没有用。

 9、【TypeError: Object of type float32 is not JSON serializable】

TypeError: Object of type 'float32' is not JSON serializable-腾讯云开发者社区-腾讯云 (tencent.com)

 

pytorch

1、【invalid argument 0: Sizes of tensors must match except in dimension 0.】

{出现在 torch.utils.data.DataLoader 输出的 batch data 读取处}  {DataLoader里面数据读取有误,准确来说,是image类型数据读取,要注意通道数和尺寸的统一性} {将输入的图片transform为统一尺寸和通道}

2、【THCudaCheck FAIL file=/pytorch/aten/src/THC/generic/THCTensorMathPointwise.cu line=207 error=710 : device-side assert triggered

【RuntimeError: CUDA error: device-side assert triggered】

当模型在GPU上运行的时候其实是没办法显示出真正导致错误的地方的(按照PyTorch Dev的说法:“Because of the asynchronous nature of cuda, the assert might not point to a full correct stack trace pointing to where the assert was triggered from.”即这是CUDA的特性,他们也没办法),所以可以通过将模型改成在CPU上运行来检查出到底是哪里出错(因为CPU模式下会有更加细致的语法/程序检查)。但是当训练网络特别大的时候,这个方法通常是不可行的,转到CPU上训练的话可能会花费很长时间[1]。

{连续训练若干个task,每个task的类别数目不一致,训练第二个task的时候报错} {即网络输出的类比和实际类别数目不符合}

【有人说可以在命令前加上CUDA_LAUNCH_BLOCKING=1】【之后运行】

【跑完第一个task的所有epoch后UserWarning】【task2的epoch1仍旧报错,THCudaCheck FAIL file=/pytorch/aten/src/THCUNN/generic/ClassNLLCriterion.cu line=110 error=710 : device-side assert triggered

/pytorch/aten/src/THCUNN/ClassNLLCriterion.cu:106: void cunn_ClassNLLCriterion_updateOutput_kernel(Dtype *, Dtype *, Dtype*, long *, Dtype *, int, int, int, int, long) [with Dtype = float, Acctype = float]: block: [0,0,0], thread: [31,0,0] Assertion `t >= 0 && t < n_classes` failed.
THCudaCheck FAIL file=/pytorch/aten/src/THCUNN/generic/ClassNLLCriterion.cu line=110 error=710 : device-side assert triggered

 [2]中提出:基本上来说,device-side assert triggered意味着有数组的越界问题了。

 另,发现出现这个报错的问题挺多的,但是具体原因不一定是相同的,要仔细看报错的细节信息。

3、macOS系统,6核,Intel芯片,跑模型测试代码,torch.utils.data.DataLoader中num_workers仅能设置为0,一旦大于0,会出现下述报错:

RuntimeError: 

        An attempt has been made to start a new process before the

        current process has finished its bootstrapping phase.

        This probably means that you are not using fork to start your

        child processes and you have forgotten to use the proper idiom

        in the main module: 

            if __name__ == '__main__':

                freeze_support()

                ...

        The "freeze_support()" line can be omitted if the program

        is not going to be frozen to produce an executable.

查资料显示【多进程需要在main函数中运行】,添加【if __name__=='__main__': 】。报错并卡死,无法在并行任务启动后设置线程数量,也无法在使用原生的并行后端时调用set_num_threds,显示:

[W ParallelNative.cpp:229] Warning: Cannot set number of intraop threads after parallel work has started or after set_num_threads call when using native parallel backend (function set_num_threads)

参考:https://blog.csdn.net/weixin_38411989/article/details/115540746 ,代码添加os.environ["OMP_NUM_THREADS"] = “1”,没有报错,但是程序依然卡死,实际没用。WHY???

4. 【ModuleNotFoundError: No module named 'pytorch_lightning'】

在安装pytorch-lightning时一定注意自己的torch是pip安装还是conda安装(要想安装新的版本推荐pip安装),两者要保持一致,否则也会导致已安装的torch版本被替换。正确安装方式:pip install pytorch-lightning==版本名,版本名同torch。

5、发生在安装pytorch 2.X及以上时,import torchaudio 时报错【AttributeError: module 'torio' has no attribute '_extension' 】

conda uninstall torchaudio 之后,再 conda install torchaudio。解决。

 

 

GPU

1、【RuntimeError: CUDA out of memory.】  

{训练时报错,之前1.2G数据可训练,现在7.8G数据报错}{训练时,使用CUDA_VISIBLE_DEVICES分配给一块16G的显卡}

{最简单粗暴方法就是减少batch_size,解决}{也可以查看代码有无bug}

{batchNorm简单来说就是批规范化,这个层类似于网络输入进行零均值化和方差归一化的操作,BN层的统计数据更新是在每一次训练阶段model.train()后的forward()方法中自动实现的。}

StackOverFlow中相关问题:How to fix this strange error: “RuntimeError: CUDA error: out of memory”

关于该问题的更详细解释:pytorch坑之Runtime error:CUDA RUN OUT OF MEMORY

2、【terminate called after throwing an instance of 'c10::CUDAError'】【torch.multiprocessing.spawn.ProcessExitedException: process 1 terminated with signal SIGABRT】【解决?】

代码没有查到明显问题,单机多卡/单机单卡都遇到该问题。在V100上一直跑不动并报错(尝试过减少batch size/num_workers/学习率等,单机1、2、4卡,都不行),折腾了很久。查起来原因众说纷纭,没有找到统一/权威的错误原因和解决方式。

我的解决方式:突发奇想在A100上跑,就成功了!?

PyTorch: test/test_multiprocessing_spawn.py | Fossies)源码。
重新配置语义分割实验环境遇到的坑 - Oliver-cs - 博客园 (cnblogs.com)l)内存不足/目前还无法解决/只能把分布式训练给关了,暂时可以运行但很慢。
Error during training (Assertion `input_val >= zero && input_val <= one` failed.) · Issue #813 · Megvii-BaseDetection/YOLOX (github.com))提出降低学习率,对我没有用。
c10::CUDAError · Issue #67978 · pytorch/pytorch (github.com))修改GPU数目 / 升级 CUDA 和 pytorch / 删除了 init 函数之外的self.xxx = xxx.to(self.device)/ 设置torch.backends.cudnn等参数 / 增大num_workers/指定类似CUDA_VISIBLE_DEVICES=0,1 / 该错误似乎是由权重和输入的特定组合触发的,因为通常,从最后一个检查点重新开始训练会有所帮助几个时期。
c10::CUDAError happens Occasionally · Issue #3 · HPDL-Group/Merak (github.com))提出看起来与NCCL有关 / 通过在终止训练前睡三秒钟来消除此错误输出。我猜这些错误来自 CUDA/NCCL 资源,这些资源在代码的某个层没有得到完美清理。
torch1.8版本跑8p,使用amp,设置O1为动态,第2个epoch出错 · Issue #I595AD · Ascend/modelzoo - Gitee.com)中提出环境变量设置HCCL_EXEC_TIMEOUT=1000即可解决,仅mark。

 

Linux

1、【lsof: symbol lookup error: /lib64/libk5crypto.so.3: undefined symbol: EVP_KDF_ctrl, version OPENSSL_1_1_1b】【解决】
安装anaconda引起的ssh错误:undefined symbol: EVP_KDF_ctrl, version OPENSSL_1_1_1b_ssh: symbol lookup error: ssh: undefined symbol: e-CSDN博客
libk5crypto.so.3: undefined symbol: EVP_KDF_ctrl, version OPENSSL_1_1_1b_symbol lookup error: /lib64/libk5crypto.so.3: unde-CSDN博客
参考上述博客,查找libk5crypto.so.3的链接(ldd /lib64/libk5crypto.so.3),并在.bashrc的最后将正确的libcrypto.so.1.1指向的文件所在的文件夹加到LD_LIBRARY_PATH的最前面。 

2、【continuing with "central" filename version】
【unzip -qO UTF-8 temp.zip】 使用 -q(quiet)选项在解压过程中减少输出信息,并使用 -O 选项指定文件名编码为 UTF-8。这在解压包含非 ASCII 文件名的 ZIP 文件时有用。   

 

win10

1、【UnicodeDecodeError: 'gbk' codec can't decode byte 0xad in position 86: illegal multibyte sequence】【可以正常使用,会报错,强迫症need to fix it】

{打开报错的倒数第三行的history.py文件,定位到82行,添加代码[,encoding='utf-8']即可}

参考: 解决Python报错UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 658: illegal multibyte_个人博客-CSDN博客_python报错gbk

 

参考

[1] 关于贫僧在使用PyTorch时遇到了runtime error(59):device-side assert triggered at XXX这样神奇的BUG的事_Geek_of_CSDN的博客-CSDN博客

[2] ERROR device side assert triggered | Horseee

posted @ 2019-12-09 16:55  Skye_Zhao  阅读(23468)  评论(0编辑  收藏  举报