国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)錯誤解決辦法

這篇具有很好參考價值的文章主要介紹了IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)錯誤解決辦法。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

在使用pytorch交叉熵損失函數(shù)loss = nn.CrossEntropyLos(v, targets)時發(fā)現(xiàn)報錯

IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)。

排查后發(fā)現(xiàn)模型預(yù)測值V在訓(xùn)練階段最后一個step時,tensor維度從二維變成一維,導(dǎo)致出錯。

例:訓(xùn)練集個數(shù)81,bs設(shè)置為4時,最后一個step 只剩1張圖片。最后step時tensor維度從二維變成一維。

于是添加以下判斷語句解決問題。如果有其他方法,可以交流一下。

1.

 v = net(inputs)
 if len(v.shape)==1:  #方式出現(xiàn)訓(xùn)練最后一個step時,出現(xiàn)v是一維的情況
     v=torch.unsqueeze(v,0)  
 loss = loss_func(v, targets)

2.可以在DataLoader中drop_last參數(shù)設(shè)置False文章來源地址http://www.zghlxwxcb.cn/news/detail-690734.html

到了這里,關(guān)于IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)錯誤解決辦法的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • 【Python&目標識別】labelimg報錯IndexError: list index out of range

    【Python&目標識別】labelimg報錯IndexError: list index out of range

    ????????博主在使用labelimg選取深度學(xué)習(xí)樣本時,命令行報錯IndexError: list index out of range,幾經(jīng)周折終于解決了,所以跟大家分享一下。 ???????? 目前已知可解決: ? ? ? ? ? ? ? ? 1.選擇樣本時,cmd報錯IndexError: list index out of range。 ????????????????2.“斷點續(xù)

    2024年02月12日
    瀏覽(50)
  • labelImg無法保存classes文件的解決方法(IndexError: list index out of range)

    labelImg無法保存classes文件的解決方法(IndexError: list index out of range)

    憨憨程序員,其實是有做讀取舊classes保存到新classes功能的,但是看完代碼發(fā)現(xiàn)就啟動程序初始化的時候調(diào)用了一次,change save dir的時候根本沒有調(diào)用。 我實力有限,只能靠比較愚蠢的方法解決了。 首先找到我們安裝labelImg的地址,比如我就是放到conda環(huán)境里面,所以在這個

    2024年04月24日
    瀏覽(26)
  • 關(guān)于Pyinstaller在打包Streamlit程序時遇到的IndexError:tuple index out of range

    關(guān)于Pyinstaller在打包Streamlit程序時遇到的IndexError:tuple index out of range

    如題,在使用Pyinstaller庫打包過程中,如果遇到 IndexError:tuple index out of range ,不必驚慌,本質(zhì)上是庫函數(shù)在傳參過程中出現(xiàn)異常 下面是解決方案: 找到 ..envssteamlitlibdis.py 這個文件。 如果你是用的虛擬環(huán)境,比如conda,那這個文件位于anaconda的安裝目錄 \\\"E:SOFTWAREANACONDAenv

    2024年03月11日
    瀏覽(28)
  • VUE中eslint報錯: Expected linebreaks to be ‘LF‘ but found ‘CRLF

    VUE中eslint報錯: Expected linebreaks to be ‘LF‘ but found ‘CRLF

    出現(xiàn)這個的原因 :windows 環(huán)境下, git 在我們 pull 代碼的時候,會自動識別當前的系統(tǒng)環(huán)境。將原本的(linux/unix)換行改成對應(yīng)系統(tǒng)的,在我們提交代碼的時候又會轉(zhuǎn)成遠程系統(tǒng)環(huán)境的(Linux/unix),然后又裝了 eslint,默認就是使用 LF,所以就會報這個錯誤 各種環(huán)境下?lián)Q行符格

    2023年04月08日
    瀏覽(28)
  • RuntimeError: stack expects each tensor to be equal size, but got at entry

    RuntimeError: stack expects each tensor to be equal size, but got at entry

    參考鏈接:??????解決Pytorch dataloader時報錯每個tensor維度不一樣的問題_python_腳本之家 記錄一下自己遇到的bug: 問題描述:? 問題分析: torch.stack(batch, 0, out=out)出錯,原因可能是: 同一個batch的數(shù)據(jù)圖片的維度(H, W, C)要相同(可以見官方文檔:其shape必須一致) 問

    2024年02月15日
    瀏覽(23)
  • Expected one result (or null) to be returned by selectOne(), but found: 3報錯解決方案

    Expected one result (or null) to be returned by selectOne(), but found: 3 Expected one result (or null) to be returned by selectOne(), but found: 3 意思是“期望selectOne()返回一個結(jié)果(或null),但發(fā)現(xiàn)3個”這說明你的返回結(jié)果有三個對象,但是selectOne只能返回一個。 第一種方案:可以把selectOne更換為selectList,

    2023年04月13日
    瀏覽(27)
  • DataLoader問題解決:RuntimeError: stack expects each tensor to be equal size, but got [3, 200, 200]entry1

    DataLoader問題解決:RuntimeError: stack expects each tensor to be equal size, but got [3, 200, 200]entry1

    ? ? ? ? 最近,在數(shù)據(jù)集處理并載入DataLoader進行訓(xùn)練的時候出現(xiàn)了問題: ? ? ? ? 我看了一下,大意就是維度也就是通道數(shù)不匹配,所以我覺得應(yīng)該是數(shù)據(jù)集圖片出現(xiàn)了問題。以下是我的普通數(shù)據(jù)集處理代碼: ? ? ? ? ? 我一張一張圖片放入DataLoader,然后按順序一張一張的

    2023年04月25日
    瀏覽(18)
  • Caused by: ParsingException[Failed to parse object: expecting token of type [START_OBJECT] but found

    Caused by: ParsingException[Failed to parse object: expecting token of type [START_OBJECT] but found

    Docker配置Elasticsearch啟動報錯 docker ps -a ????????#查看所有容器啟動狀態(tài) ?發(fā)現(xiàn)elasticsearch的啟動status為exited docker logs elasticsearch 配置elasticsearch.yml文件時http.host:與0.0.0.0的中間要有空格

    2024年02月11日
    瀏覽(19)
  • only batches of spatial targets supported (3D tensors) but got targets of dimension

    only batches of spatial targets supported (3D tensors) but got targets of dimension

    問題產(chǎn)生的原因是使用nn.CrossEntropyLoss()來計算損失的時候,target的維度超過4 如實target中的C不是1,則可以: ?可以看到代碼里面有個.long(),如果不用的話則會報錯: RuntimeError: expected scalar type Long but found Float

    2024年02月11日
    瀏覽(18)
  • 已解決IndexError: positional indexers are out-of-bounds

    已解決IndexError: positional indexers are out-of-bounds

    已解決IndexError: positional indexers are out-of-bounds 粉絲群里面的一個小伙伴遇到問題跑來私信我,想用pandas,但是發(fā)生了報錯(當時他心里瞬間涼了一大截,跑來找我求助,然后順利幫助他解決了,順便記錄一下希望可以幫助到更多遇到這個bug不會解決的小伙伴),報錯代碼如下

    2024年02月05日
    瀏覽(20)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包