1. 程式人生 > 其它 >【求助-pytorch執行報錯】CUDA error: CUBLAS_STATUS_ALLOC_FAILED when calling `cublasCreate(handle)`

【求助-pytorch執行報錯】CUDA error: CUBLAS_STATUS_ALLOC_FAILED when calling `cublasCreate(handle)`

問題如下:
1、利用pytorch復現googlenet網路
2、定義訓練過程函式中

def train(epoch):
    running_loss = 0  # 損失值歸零
    for batch_idx, data in enumerate(train_loader, start=0): 
        images, labels = data #將輸入x和相應的標籤y從資料中拿出來

        # 將輸入和目標y都放到GPU上面
        images, labels = images.to(device), labels.to(device)

...

labels.to(device)

報錯“AttributeError: 'tuple' object has no attribute 'to'”,發現是因為Python中的元組類(tuple)沒有呼叫to的方法。
3、解決
(1)執行labels = torch.tensor(labels).to(device)試圖將tuple轉為tensor,但是報錯:"ValueError: too many dimensions 'str'";
(2)這是因為“張量表示由數值組成的多維陣列”,故字串str無法直接轉為tensor
4、將tuple字串元素數值化
(1)方法一:呼叫sklearn的preprocessing(成功執行)

from sklearn import preprocessing

le = preprocessing.LabelEncoder() #獲取一個LabelEncoder
labels = le.fit_transform(labels) #訓練LabelEncoder
labels = torch.as_tensor(labels) 

成功執行
(2)方法二:直接將tuple元素強轉為int型(執行報錯)

images, labels = data #將輸入x和相應的標籤y從資料中拿出來
print(labels)

輸出:('177', '166', '100', '136')
由於labels實際上是數字,所以思考能否直接將元素轉為int型?

labels = [int(i) for i in labels]
labels = torch.as_tensor(labels)

經過此操作成功將labels轉為了tensor,但是執行之後報錯:

RuntimeError: CUDA error: CUBLAS_STATUS_ALLOC_FAILED when calling `cublasCreate(handle)`

Q:請問方法二為什麼會報錯呢?