佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > 解決pytorch trainloader遇到的多進程問題

解決pytorch trainloader遇到的多進程問題

熱門標(biāo)簽:電話運營中心 企業(yè)做大做強 客戶服務(wù) 百度AI接口 呼叫中心市場需求 硅谷的囚徒呼叫中心 語音系統(tǒng) Win7旗艦版

pytorch中嘗試用多進程加載訓(xùn)練數(shù)據(jù)集,源碼如下:

trainloader = torch.utils.data.DataLoader(trainset, batch_size=4, shuffle=True, num_workers=3)

結(jié)果報錯:

RuntimeError: 
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.

        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:

            if __name__ == '__main__':
                freeze_support()
                ...

        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.

從報錯信息可以看到,當(dāng)前進程在運行可執(zhí)行代碼時,產(chǎn)生了一個新進程。這可能意味著您沒有使用fork來啟動子進程或者是未在主模塊中正確使用。

后來經(jīng)過查閱發(fā)現(xiàn)了原因,因為windows系統(tǒng)下默認用spawn方法部署多線程,如果代碼沒有受到__main__模塊的保護,新進程都認為是要再次運行的代碼,將嘗試再次執(zhí)行與父進程相同的代碼,生成另一個進程,依此類推,直到程序崩潰。

解決方法很簡單

把調(diào)用多進程的代碼放到__main__模塊下即可。

if __name__ == '__main__':
    transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
    trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
    trainloader = torch.utils.data.DataLoader(trainset, batch_size=4, shuffle=True, num_workers=3)

補充:pytorch-Dataloader多進程使用出錯

使用Dataloader進行多進程數(shù)據(jù)導(dǎo)入訓(xùn)練時,會因為多進程的問題而出錯

dataloader = DataLoader(transformed_dataset, batch_size=4,shuffle=True, num_workers=4)

其中參數(shù)num_works=表示載入數(shù)據(jù)時使用的進程數(shù),此時如果參數(shù)的值不為0而使用多進程時會出現(xiàn)報錯

RuntimeError: An attempt has been made to start a new process before the current process has finished its bootstrapping phase. This probably means that you are not using fork to start your child processes and you have forgotten to use the proper idiom in the main module: if __name__ == '__main__': freeze_support() ... The "freeze_support()" line can be omitted if the program is not going to be frozen to produce an executable.

此時在數(shù)據(jù)的調(diào)用之前加上if __name__ == '__main__':即可解決問題

if __name__ == '__main__':#這個地方可以解決多線程的問題

        for i_batch, sample_batched in enumerate(dataloader):

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • pytorch多進程加速及代碼優(yōu)化方法
  • PyTorch 解決Dataset和Dataloader遇到的問題
  • 解決pytorch DataLoader num_workers出現(xiàn)的問題

標(biāo)簽:長沙 崇左 海南 安康 喀什 山西 山西 濟南

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《解決pytorch trainloader遇到的多進程問題》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    潼关县| 常熟市| 久治县| 准格尔旗| 界首市| 凤阳县| 河北区| 达州市| 琼结县| 桐庐县| 岗巴县| 含山县| 巧家县| 镇赉县| 武城县| 林周县| 东乡县| 兴宁市| 会理县| 衡水市| 柘荣县| 青阳县| 荔波县| 枣强县| 阳高县| 曲水县| 堆龙德庆县| 枝江市| 永济市| 杭锦后旗| 邵武市| 临海市| 鄢陵县| 新绛县| 巫山县| 汝阳县| 涞源县| 莲花县| 陆川县| 鄂托克旗| 济阳县|