Error合集TypeError、UnicodeDecodeError、KeyError、AttributeError、ValueError、RuntimeError、OSError、段错误--更新中

目录

一、TypeError

1.TypeError: stat: path should be string, bytes, os.PathLike or integer, not NoneType

#问题来源:加载模型时候出错
#解决方法:将模型path的双引号改为单引号

2.TypeError: Got unsupported ScalarType BFloat16

#问题来源:tensor(cuda+grad) 存成 ndarray(cpu+grad=false)
#解决方法:↓

mask_embedding[i] = batch_mask_embedding.detach().to(torch.float).cpu().numpy()

3.TypeError: Object of type int64 is not JSON serializable

#问题来源:json.dump存numpy数据报错
#解决方法:转json报错:TypeError: Object of type int64 is not JSON serializable-CSDN博客

上面博客内容能完美解决!先序列化,然后在json.dump后面加一个cls即可

class NpEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, np.integer):
            return int(obj)
        elif isinstance(obj, np.floating):
            return float(obj)
        elif isinstance(obj, np.ndarray):
            return obj.tolist()
        else:
            return super(NpEncoder, self).default(obj)
        
with open('a','w') as f:
    json.dump(a, f,cls=NpEncoder)

#a为np数据

4.TypeError: issubclass() arg 1 must be a class

#问题来源:1. OSError: [E050] Can’t find model ‘en_core_web_lg’. It doesn’t seem to be a Python package or a valid path to a data directory. 这行代码运行完的问题
#解决办法:用conda装指定的版本,降低spacy版本

conda install -c conda-forge spacy==xx  ## 如果code的requirements中有指定版本,加  ==版本号
python -m pip install -U pydantic spacy  #对我的代码没用,会套娃出现了另一个问题

二、UnicodeDecodeError

1.UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0x93 in position 2285: illegal multibyte sequenc

#问题来源:win10系统打开文件报错,加 encoding=‘utf-8’ 就行
#解决方法:↓

with open(os.path.join(data_path, 'entities.dict'),encoding='utf-8') as fin:
    entity2id = dict()

三、KeyError

1.KeyError: ‘dish酶norable horrofic situation’

#问题来源:win10读文件,将文件中内容添加为键值对,如果文件中包含特异字符,会报错。。。linux不会

#解决方法:将特异字符替换掉=正则化/手动/删除 /转到linux系统

四、AttributeError

1.AttributeError: module ‘grpc’ has no attribute ‘AuthMetadataPlugin’

#问题来源:openprompt的demo报错,github上了找了好久没看明白,翻到一条回答 可以试试更新包,我信了,然后没报错了

#解决方法:↓

pip install --upgrade grpcio

2.AttributeError:module tensorflow no attribute app

#问题来源:tensorflow运行报错,版本1.x和2.x的区别
#解决方法:↓
用tf.compat.v1.flags替换tf.app.flags

3.AttributeError:module ‘tensorflow’ has no attribute ‘set_random_seed’

#问题来源:tensorflow运行报错,版本1.x和2.x的区别
#解决方法:↓ (在这个博客找到的解决方法 module ‘tensorflow’ has no attribute ‘set_random_seed’

tf.set_random_seed(seed)  #tf 1.x
tf.random.set_seed(seed)  #tf 2.x

4.AttributeError: module ‘scipy’ has no attribute ‘spatial’

#问题来源:import scipy报错
#解决办法:↓(解决

import scipy.spatial 

5.AttributeError: module ‘networkx’ has no attribute ‘from_scipy_sparse_matrix’

#问题来源:nx_graph = nx.from_scipy_sparse_matrix(adj)报错
#解决办法:github找到的,networkx版本不同,有更新(github具体解释)

nx_graph = nx.from_scipy_sparse_array(adj)

6.AttributeError: module ‘dgl’ has no attribute ‘contrib’

#问题来源:和5同理,版本更新有些Attribute
#解决办法:安装的dgl1.0,但是代码环境要求0.4.1,由于找不到安装包,直接安装了0.4.3

pip install dgl==0.4.3

7.AttributeError: ‘Tensor’ object has no attribute ‘np’

#问题来源:tensor转list报错
#解决办法:参考解决的博客

import torch
import numpy as np
x = torch.randn(3, 4)
indices = torch.tensor([0])
y = torch.index_select(x, 1, indices)  
print(x)
print(y.numpy().tolist())  # √
#print(y.np().tolist())  # x

五、ValueError

1.ValueError: cannot reshape array of size 6137118 into shape (11653479,)

#问题来源:读取npy数据报错
#解决方法:找了很多博客之后,发现可能是在github上下载几年前的代码中npy数据损坏;在github上单个下载npy数据,不整体打包下载

六、RuntimeError

1.RuntimeError: Error(s) in loading state_dict for BertForTripleClassification: Unexpected key(s) in state_dict: “bert.embeddings.position_ids”.

#问题来源:加载之前finetune的模型出错
#解决方法:加载环境与模型产生环境不一致,在load那块加上 False即可

model.load_state_dict(state_dict["model_state_dict"])  ×
model.load_state_dict(state_dict["model_state_dict"],False)   

2.RuntimeError: CUDA error: CUBLAS_STATUS_INVALID_VALUE when calling `cublasSgemm( handle, opa, o

#问题来源:torch1.12,cuda11.6,3090,重装torch后就出问题了;
#解决方法:在终端输入下列指令,再跑代码就ok [解决方法来源](ps:其他试过了,未解决)(https://blog.csdn.net/BetrayFree/article/details/133868929)

unset LD_LIBRARY_PATH 

3.RuntimeError: CUDA error: CUBLAS_STATUS_ALLOC_FAILED when calling cublasCreate(handle)

#问题来源:代码运行报错,cuda都是适配的
#解决方法:代码的batchsize已经很小了,逐行找错误,发现参数设定和数据集有出入,例如代码指定节点数为10,实际节点数为11。

4.RuntimeError: CUDA error: device-side assert triggered CUDA kernel errors might be asynchronously reported at some other API call,so the stacktrace below might be incorrect. For debugging consider passing CUDA_LAUNCH_BLOCKING=1.

#问题来源:新处理的数据集输入到GAT层报错
#解决办法:找了很久,其实是索引超出,数据处理有问题

七、OSError

1. OSError: [E050] Can’t find model ‘en_core_web_lg’. It doesn’t seem to be a Python package or a valid path to a data directory.

#问题来源:跑跨语言的知识融合代码出现的问题,zn,en,lg
#解决办法:解决↓,但出现了type error的第四个问题,hhhhh

# python -m spacy download en_core_web_sm  # 国内装不上。。
conda install -c conda-forge spacy-model-en_core_web_lg  

八、ImportError

1.ImportError: blis.cy does not export expected C function sgemm

#问题来源:安装spacy!!!(尝试过的解法见七.1以及一.4)
#解决办法:还得重装,conda和pip的区别,用conda装,请用conda装!!!spacy的github

conda install -c conda-forge spacy  ## 最新的版本,如果别人的code有指定版本
conda install -c conda-forge spacy==xx  ## 如果code的requirements中有指定版本,加  ==版本号

九、IndexError: index out of range in self

1.IndexError: index out of range in self

#问题来源:传入embedding层有超出范围的ids,例如-1这种
##解决办法:修改输入的inputs ids,保证ids全在范围内

其他、未知错误

1.段错误 (核心已转储)

#问题来源:加载chatglm一直报错,不管是直接加载还是本地下载好的模型
#解决方法:注释掉不用的库/包;网上各种复杂方法试了,最后还是知乎的大佬(磨人的Segmentation fault (core dumped) - 知乎

#新的问题来源:清华OpenKE换成自己的数据集报段错误
#解决方法:检查自己的数据集中的形式是否与示例数据集一模一样+type_constrain.txt 不需要使用要在 OpenKE/openke/data/TestDataLoader.py line27 def init(self, in_path = “./”, sampling_mode = ‘link’, type_constrain = True):
将type_constrain = True→ type_constrain = False
(清华OpenKE项目issue)

2.run.sh: 3: source: not found

#问题来源:利用sh 执行命令时出错
#解决办法:↓ (Linux Shell 脚本提示 “source: not found” 的原因和解决方法

sh run.sh configs/countries_s3.sh  # 原命令
bash run.sh configs/countries_s3.sh  #改后

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值