Pytorch 调试记录及常见错误的处理

2020-02-17T22:31:46

1. RuntimeError: Expected object of scalar type Float but got scalar type Double for sequence elment 2 in sequence at position #1 ‘tensors’

在跑python代码的时候,遇到的有关pytorch的错误,RuntimeError: Expected object of scalar type Float but got scalar type Double for sequence elment 2 in sequence at position #1 ‘tensors’,折腾了好久,最后终于调通,把类型改成float,如果大家也遇到类似的问题,可以借鉴下。
下面是我的错误代码:

def forward(self, x, neigbor, flow):       
 ### initial feature extraction        
 feat_input = self.feat0(x)       
  feat_frame=[]     
      for j in range(len(neigbor)):   
              feat_frame.append(self.feat1(torch.cat((x, neigbor[j], flow[j]),1))) #这一行代码显示有error

后面改成float格式就没有问题了,修正代码如下:

def forward(self, x, neigbor, flow): 
       ### initial feature extraction     
    feat_input = self.feat0(x)     
    feat_frame=[]     
           for j in range(len(neigbor)):    
                    neigbor_f= neigbor[j]     
                     neigbor_f=neigbor_f.float()     
                      flow_f=flow[j]      
                     flow_f=flow_f.float()         
                     feat_frame.append(self.feat1(torch.cat((x, neigbor_f, flow_f),1))) 

2.File"/home/jerry/anaconda3/envs/pytorch/lib/python3.7/site-packages/PIL/Image.py", line 2634, in open fp = builtins.open(filename, “rb”) IsADirectoryError: [Errno 21] Is a directory: ‘./Data/’

在跑python代码的时候,遇到的有关pytorch的错误,
File “/home/jerry/anaconda3/envs/pytorch/lib/python3.7/site-packages/PIL/Image.py”, line 2634, in open
fp = builtins.open(filename, “rb”)
IsADirectoryError: [Errno 21] Is a directory: ‘./Data/’,由于图片输入路径没有转义
下面是我的错误代码:

from PIL import Image
Image.open('C:\Users\ASUS\Desktop\1.tif')#这一行代码显示有error

后面转义以后就没有问题了,修正代码如下:

方法一
 Image.open('C:\\Users\\ASUS\\Desktop\\1.tif')
 方法二
 Image.open(r'C:\Users\ASUS\Desktop\1.tif')

3. ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: ‘/usr/local/lib/python2.7/dist-packages/tensorboardX

Consider using the --user option or check the permissions.

在unix系统输入pip install tensorboardX指令时,出现ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: ‘/usr/local/lib/python2.7/dist-packages/tensorboardX’ 的错误。
解决方法,如提示一样,在指令后面加上**–user**给与相应权限即可。更正后命令输入为:pip install tensorboardX --user 即可。

4.ModuleNotFoundError: No module named ‘yaml’

代码中用到了yaml库,import yaml,然而运行错误:ModuleNotFoundError: No module named ‘yaml’,此时可以输入以下代码加载yaml库:
方法1:python3 -m pip install pyyaml
方法2:使用“ virtualenv”

 virtualenv --python=python3 venv
      source venv/bin/activate
      pip install pyyaml
      python env/common_config/add_imagepullsecret.py

方法3: install python 3.6 and ln python3 to it

export $PYPATH=`which python3`
    wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tar.xz
    tar -Jxf Python-3.6.5.tar.xz
    cd Python-3.6.5/
    ./configure && make && make altinstall
    rm $PYPATH
    ln -s `which python3.6` $PYPATH
    python3 -m pip install pyyaml
    python3 env/common_config/add_imagepullsecret.py
当前页面是本站的「Baidu MIP」版。发表评论请点击:完整版 »