运行pytorch时遇到的一些报错
其中有些错误可能不是pytorch里的而是python或者其他包或者模块的。
1 torch.distributions.relaxed_categorical.RelaxedOneHotCategorical
temperature = torch.tensor([0.5])
alpha_nor = torch.tensor([[-1.2331e-03, 8.3893e-04, 1.1213e-03, -6.1222e-04, 6.4768e-04,
1.0734e-03, 6.4606e-04, 2.1227e-04],
[ 7.7460e-04, 7.1466e-04, 9.3858e-04, 8.7815e-04, -2.9023e-04,
-5.1935e-04, -6.0653e-04, 1.5704e-03],
[-8.6886e-04, -2.0650e-03, -1.7860e-04, -7.3414e-04, -3.9698e-04,
1.6050e-03, 4.6554e-05, 2.9096e-04],
[-3.7747e-04, 8.1001e-04, -5.3438e-04, -9.5279e-05, 4.9896e-04,
1.0379e-04, -1.0063e-03, 1.0876e-03],
[ 1.9967e-04, -1.5754e-03, -2.1347e-03, -2.3987e-04, 8.1780e-04,
-9.3016e-04, 4.3183e-04, -1.7035e-03],
[-1.5567e-03, 1.2958e-04, 2.3189e-03, -2.2861e-04, -1.1061e-03,
4.4689e-04, -3.8428e-04, 1.5116e-03],
[ 1.2204e-04, -2.0824e-04, -3.5004e-05, -7.2881e-05, 1.2077e-03,
-1.6365e-04, -8.9736e-04, -4.2642e-04],
[-3.1843e-04, 6.0838e-04, 1.8222e-03, -4.0902e-04, -7.6868e-04,
-6.7489e-04, -9.2356e-04, 9.7972e-04],
[-9.6215e-04, 6.1356e-04, -6.9233e-04, -6.7823e-04, -1.5504e-04,
-1.5189e-03, -6.9232e-04, -9.5939e-04],
[ 2.6768e-03, 1.9618e-04, -1.0318e-03, -1.9523e-04, -5.3733e-04,
2.8195e-04, 1.2514e-03, -1.5693e-04],
[ 1.9633e-03, -1.2755e-04, 9.8814e-06, -1.0365e-03, 4.3264e-04,
4.1342e-04, -1.0962e-05, 4.1204e-04],
[-4.0240e-04, -1.0943e-03, 3.9549e-04, -5.5797e-04, -4.5615e-04,
1.0067e-03, 1.8574e-04, 1.0612e-05],
[ 1.3362e-03, -9.0677e-04, -1.0050e-04, 1.5120e-03, -1.2855e-03,
4.3930e-04, -8.1739e-04, -4.7392e-04],
[ 1.4855e-04, 1.3363e-03, 6.1041e-05, 3.3915e-04, 1.8656e-03,
-2.8654e-04, 4.1044e-04, -1.0102e-03]])
Z= torch.distributions.relaxed_categorical.RelaxedOneHotCategorical(
temperature, alpha_nor)
报错:ValueError: The parameter probs has invalid values
分析:该类第二个参数和第三个参数二选一
x = torch.tensor([-0.9])
Z= torch.distributions.relaxed_categorical.RelaxedOneHotCategorical(temperature,x) # 不报错
Z= torch.distributions.relaxed_categorical.RelaxedOneHotCategorical(temperature,probs = x) # 不报错
Z= torch.distributions.relaxed_categorical.RelaxedOneHotCategorical(temperature,logits = x) # 不报错
x = torch.abs(torch.randn([4,3]))
Z= torch.distributions.relaxed_categorical.RelaxedOneHotCategorical(temperature,x) # 不报错
Z= torch.distributions.relaxed_categorical.RelaxedOneHotCategorical(temperature,probs = x) # 不报错
Z= torch.distributions.relaxed_categorical.RelaxedOneHotCategorical(temperature,logits = x) # 不报错
x = -torch.abs(torch.randn([4,3]))
Z= torch.distributions.relaxed_categorical.RelaxedOneHotCategorical(temperature,x) # 不报错
Z= torch.distributions.relaxed_categorical.RelaxedOneHotCategorical(temperature,probs = x) # 不报错
Z= torch.distributions.relaxed_categorical.RelaxedOneHotCategorical(temperature,logits = x) # 不报错
x = torch.tensor([0.1,0.4,-0.5])
# Z= torch.distributions.relaxed_categorical.RelaxedOneHotCategorical(temperature,x) # 报错:ValueError: The parameter probs has invalid values
# Z= torch.distributions.relaxed_categorical.RelaxedOneHotCategorical(temperature,probs = x) # 报错:ValueError: The parameter probs has invalid values
Z= torch.distributions.relaxed_categorical.RelaxedOneHotCategorical(temperature,logits = x) # 不报错
当传入的参数全是正或者全是负时不会报错,但是当传入的参数中有正有负时,不能作为probs,只能作为logits。
但是这两个参数具体含义还是不清楚,也不明白为什么会有上述区别。请大家多指教。
解决:指明传入的参数是类定义中的logits,即
Z= torch.distributions.relaxed_categorical.RelaxedOneHotCategorical(
temperature, logits = alpha_nor)
2 BrokenPipeError: [Errno 32] Broken pipe
把DataLoader中的num_workers的值改为0即可。
3 import自己写的py文件不存在
这里特指一种情况。当文件组织形式如下:
---conf
|---__init__.py
|---cfgs.py
---main.py
且___init__.py有:
from cfgs import *
报错:运行main.py文件时报错没有这个文件。
解决:将___init__.py中的那句改为:
from conf.cfgs import *
4 os.mkdir()报错找不到文件
报错:
当时气得我:
分析:不过冷静下来是我错怪你了——os.mkdir()只能创建单级目录。
解决:创建多级目录要用os.makedirs()。
5 Pygraphviz报错:ValueError: Program dot not found in path.
谷歌查到的解决方法是:同时安装graphviz(必要时安装pydot_ng)。但是我当时装pygraphviz时就用conda和pip安装过graphviz,而且用pip install还可以看到,但版本还是0.17。
因为是在Windows上操作,用pip或者conda安装可能达不到效果,因此最后到官网下载安装了(参考),回到命令行还是报错……后来重启了一下命令行就好了。
ps.我想在没有root权限的Linux系统上安装pygraphviz至今没有成功。
6 指定哪个GPU的方式
现在我主要用两种方式:
# 第一种方式
import torch
gpu = 2
torch.cuda.set_device(gpu)
# 第二种方式
import os
gpu = '2'
os.environ['CUDA_VISIBLE_DEVICES'] = gpu
指定编号位7的GPU,但是第一种方式可以顺利运行的代码,用第二种方式却报错:
RuntimeError: Attempting to deserialize object on CUDA device 4 but torch.cuda.device_count() is 1. Please use torch.load with map_location to map your storages to an existing device.