[bug 1] TypeError: ‘method’ object is not subscriptable
問(wèn)題代碼:
print(item.full_name(), x.shape, item.parameters()[0].shape, item.parameters[1].shape)
原因:parameters
后面少了個(gè)括號(hào)。
[bug 2] TypeError: unsupported format string passed to numpy.ndarray.format
這段報(bào)錯(cuò)可以用如下代碼重現(xiàn)出來(lái):
import numpy as np
x = np.array([1.])
print('{:4f}'.format(x))
原因:
傳給{:4f}
的應(yīng)該是一個(gè)浮點(diǎn)數(shù)數(shù)值,而 x 是 numpy 的數(shù)組,于是類型不匹配。我們只需將 x 轉(zhuǎn)為浮點(diǎn)型即可,正確代碼如下:
import numpy as np
x = np.array([1.])
x = float(x)
print('{:4f}'.format(x))
[bug 3] ValueError:Hint: Expected dtype() == paddle::experimental::CppTypeToDataType::Type()
描述
學(xué)習(xí)預(yù)訓(xùn)練模型的 fine-tune 時(shí),將 AI Studio 上能跑的代碼拷下來(lái),到本地就報(bào)錯(cuò)了,我真的一臉懵。
當(dāng)時(shí)上網(wǎng)查 ValueError,大多說(shuō)將 ‘float64’ 換成 'float32‘,但我將輸入的特征astype('float32')
后,還是沒(méi)用。
報(bào)錯(cuò)信息:
ValueError: (InvalidArgument) The type of data we are trying to retrieve does not match the type of data currently contained in the container.
[Hint: Expected dtype() == paddle::experimental::CppTypeToDataType<T>::Type(), but received dtype():5 != paddle::experimental::CppTypeToDataType<T>::Type():7.] (at ..\paddle\phi\core\dense_tensor.cc:137)
[operator < accuracy > error]
解決
最后發(fā)現(xiàn)要改的是這里:
return im, int(grt)
int
是 python 內(nèi)置的數(shù)據(jù)類型,我將它轉(zhuǎn)成 numpy
的 int64
就好了。真沒(méi)想到,python 內(nèi)置的 int 居然不行。
# grt原本是給字符串
grt = np.int64(int(grt))
return im, grt
[bug 4] CondaSSLError: Encountered an SSL error.
使用 pip
或 conda
安裝 python 包時(shí),如果開(kāi)了梯子,可能會(huì)出現(xiàn)這樣的報(bào)錯(cuò),把梯子關(guān)掉就好了。
完整的報(bào)錯(cuò)如下:
CondaSSLError: Encountered an SSL error. Most likely a certificate verification issue.
Exception: HTTPSConnectionPool(host='mirrors.tuna.tsinghua.edu.cn', port=443):
Max retries exceeded with url: /anaconda/pkgs/main/win-64/current_repodata.json
(Caused by SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol(_ssl.c:1125)')))
翻譯:
CondaSSLError:遇到 SSL 錯(cuò)誤。很可能是證書(shū)驗(yàn)證問(wèn)題。
異常:HTTPSConnectionPool(host='mirrors.tuna.tsinghua.edu.cn', port=443):
超出最大重試次數(shù),網(wǎng)址為:/anaconda/pkgs/main/win-64/current_repodata.json
(由 SSLError(SSLEOFError(8, 'EOF 發(fā)生違反協(xié)議 (_ssl.c:1125)')))
[bug 5] pip install paddleclas失敗
描述:
一開(kāi)始看到好像是faiss
包的問(wèn)題,一頓操作之后,我把 faiss 包下下來(lái)了,但還是無(wú)法下載 paddleclas。這應(yīng)該是 python 版本的問(wèn)題,現(xiàn)在的版本是 python3.10.6,但我之前在 python3.8.4 的版本下使用 pip 下載成功了。
處理方案:
從 github 上下載 PaddleClas
的代碼庫(kù)后,直接 import
導(dǎo)入,跳過(guò) pip
這一步。
import sys
sys.path.append('D:/code_all/gitCode/PaddleClas') # 這里是代碼庫(kù)的路徑
import ppcls
可能編輯器會(huì)提示 "沒(méi)有名稱為'ppcls'的模塊”
,但不用管,只要運(yùn)行時(shí)不報(bào)錯(cuò)就行。因?yàn)?code>sys.path.append()在運(yùn)行的時(shí)候才會(huì)執(zhí)行,而在它執(zhí)行之前你確實(shí)沒(méi)有 ppcls
這個(gè)包。
[bug 6] 想刪除原來(lái)的文件夾后新建一個(gè)(邏輯錯(cuò)誤)
錯(cuò)誤代碼片段:
import os
import shutil
isExist = os.path.exists(saveDir)
if isExist and deleteOld:
shutil.rmtree(saveDir)
if not isExist:
os.makedirs(saveDir)
但如果原本路徑saveDir
存在,則isExist
為True
,執(zhí)行刪除,但是后面isExist
依然為True
,并不會(huì)重新創(chuàng)建路徑。
修改:創(chuàng)建前再插入一段判斷。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-786767.html
isExist = os.path.exists(saveDir)
if isExist and deleteOld:
shutil.rmtree(saveDir)
isExist = os.path.exists(saveDir)
if not isExist:
os.makedirs(saveDir)
完文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-786767.html
到了這里,關(guān)于一篇普通的bug日志——bug的盡頭是next嗎?的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!