pytorch训练错误记录

目录

错误1 :IndexError: too many indices for array: array is 0-dimensional, but 1 were indexed

1.1 针对numpy array内部的数据为list形式的情况

1.2 针对numpy array内部的数据为dict形式的情况

错误2: ValueError: Only one class present in y_true. ROC AUC score is not defined in that case

错误3: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first



错误1 :IndexError: too many indices for array: array is 0-dimensional, but 1 were indexed

1. 由于 Numpy  ndarray 中各元素长度不同引起的;

1.1 针对numpy array内部的数据为list形式的情况

例如:

>>> import numpy as np
>>> b = np.array([[1,2,3,4], [5,6,7,8], [9,10,11]])
>>> b[:,2]
Traceback (most recent call last):
  File "", line 1, in 
IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed

上述代码中 b 中的第三维数组与第1,2维数组维度不同,因此从数组中筛选取元素就会报上述错误;

但是构建这个b的时候是可以构建,numpy将b构建成如下的形式:

>>> b
array([list([1, 2, 3, 4]), list([5, 6, 7, 8]), list([9, 10, 11])],
      dtype=object)
>>> b.shape
(3,)

构建成了一个列表数组。

1.2 针对numpy array内部的数据为dict形式的情况

那么如果我们将字典类型数据保存成了numpy的形式,将如何获取数据:

例如数据形式为:

>>> tweet = {k:[] for k in range(3)}
>>> tweet[0].append('The ScreenWeek h15 Daily is out! http://t.co/yi5z7oD9j9')
>>> tweet[0].append('The ScreenWeek ')
>>> tweet[1].append('The nWeek ')
>>> tweet[1].append('The n ')
>>> tweet[2].append('The scscasn ')
>>> tweet[2].append('The scscacsdcsv ')
>>> tweet = np.array(tweet)
>>> tweet
array({0: ['The ScreenWeek h15 Daily is out! http://t.co/yi5z7oD9j9', 'The ScreenWeek '], 1: ['The nWeek ', 'The n '], 2: ['The scscasn ', 'The scscacsdcsv ']},
      dtype=object)

当实验numpy形式获取数据时,经常会报上述错误:

>>> tweet[0]
Traceback (most recent call last):
  File "", line 1, in 
IndexError: too many indices for array: array is 0-dimensional, but 1 were indexed

因此,如果我们要获取数据,应该采用如下方式:(转化为list形式获取数据)

>>> tweet = tweet.tolist()
>>> tweet[0]
['The ScreenWeek h15 Daily is out! http://t.co/yi5z7oD9j9', 'The ScreenWeek ']

错误2: ValueError: Only one class present in y_true. ROC AUC score is not defined in that case

我在使用sklearn.metrics类 的 roc_auc_score 方法计算AUC时,出现了错误2;

AUC 是需要分类数据的任一类都有足够的数据,这样才有意义;

这可能是由于数据集不平衡引起的;

可以使用try-except还防止错误;

import numpy as np
from sklearn.metrics import roc_auc_score
y_true = np.array([0, 0, 0, 0])
y_scores = np.array([1, 0, 0, 0])
try:
    roc_auc_score(y_true, y_scores)
except ValueError:
    pass

参考:python - roc_auc_score - y_true 中只有一个类 - IT工具网

错误3: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first

list中元素为tensor,需要将list类型数据转换为tensor类型数据;

转换方式,先将list中tensor元素放到cpu, 再转换为numpy;

转化方式,如下面代码:

des_features = [des.cpu().detach().numpy() for des in des_features]
des_features = torch.tensor(des_features)

错误4: RuntimeError:  [enforce fail at ..\caffe2\serialize\inline_container.cc:300] . unexpected pos 158720512 vs 158720408

磁盘空间不足,删点数据

你可能感兴趣的:(编程,pytorch,python,深度学习)