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

【已解決】Pytorch RuntimeError: expected scalar type Double but found Float

這篇具有很好參考價值的文章主要介紹了【已解決】Pytorch RuntimeError: expected scalar type Double but found Float。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報違法"按鈕提交疑問。

【已解決】Pytorch RuntimeError: expected scalar type Double but found Float,bug解決,pytorch,人工智能,python
本文作者: slience_me


項(xiàng)目場景:

在訓(xùn)練模型時候,將數(shù)據(jù)集輸入到網(wǎng)絡(luò)中去,在執(zhí)行卷積nn.conv1d()的時候,報出此錯誤


問題描述

報錯堆棧信息

Traceback (most recent call last):
  File "D:\codeHub\AssumptionAnalysis\2024-01-08-ModernTCN\main.py", line 27, in <module>
    pred_series = model(data_tensor_part)
  File "C:\Users\slience_me\.conda\envs\machine-learning\lib\site-packages\torch\nn\modules\module.py", line 1518, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "C:\Users\slience_me\.conda\envs\machine-learning\lib\site-packages\torch\nn\modules\module.py", line 1527, in _call_impl
    return forward_call(*args, **kwargs)
  File "D:\codeHub\AssumptionAnalysis\2024-01-08-ModernTCN\model.py", line 191, in forward
    x_emb = self.embed_layer(x)  # [B, M, L] -> [B, M, D, N]
  File "C:\Users\slience_me\.conda\envs\machine-learning\lib\site-packages\torch\nn\modules\module.py", line 1518, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "C:\Users\slience_me\.conda\envs\machine-learning\lib\site-packages\torch\nn\modules\module.py", line 1527, in _call_impl
    return forward_call(*args, **kwargs)
  File "D:\codeHub\AssumptionAnalysis\2024-01-08-ModernTCN\model.py", line 76, in forward
    x_emb = self.conv(x_pad)  # [B*M, 1, L+P-S] -> [B*M, D, N]
  File "C:\Users\slience_me\.conda\envs\machine-learning\lib\site-packages\torch\nn\modules\module.py", line 1518, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "C:\Users\slience_me\.conda\envs\machine-learning\lib\site-packages\torch\nn\modules\module.py", line 1527, in _call_impl
    return forward_call(*args, **kwargs)
  File "C:\Users\slience_me\.conda\envs\machine-learning\lib\site-packages\torch\nn\modules\conv.py", line 310, in forward
    return self._conv_forward(input, self.weight, self.bias)
  File "C:\Users\slience_me\.conda\envs\machine-learning\lib\site-packages\torch\nn\modules\conv.py", line 306, in _conv_forward
    return F.conv1d(input, weight, bias, self.stride,
RuntimeError: expected scalar type Double but found Float

原因分析:

  • tensor的數(shù)據(jù)類型dtype不正確

這個錯誤通常是由于數(shù)據(jù)類型不匹配導(dǎo)致的。在PyTorch中,張量有不同的數(shù)據(jù)類型,如float32(FloatTensor)和float64(DoubleTensor)等。在進(jìn)行計(jì)算時,PyTorch要求輸入的張量數(shù)據(jù)類型要與操作或模型所期望的數(shù)據(jù)類型一致,否則會出現(xiàn)這個錯誤。

例如,如果你的模型或操作期望輸入的數(shù)據(jù)類型為Double(float64),但你提供的張量類型是Float(float32),就會出現(xiàn)類似的錯誤。PyTorch會提示它期望的數(shù)據(jù)類型與實(shí)際提供的數(shù)據(jù)類型不匹配。


解決方案:

  • 將數(shù)據(jù)類型轉(zhuǎn)為float32
  • 或者將數(shù)據(jù)類型轉(zhuǎn)為float64

解決這個問題的方式通常是將數(shù)據(jù)類型轉(zhuǎn)換為匹配模型或操作所期望的類型??梢允褂?.to() 方法將張量轉(zhuǎn)換為正確的數(shù)據(jù)類型。例如,將Float類型的張量轉(zhuǎn)換為Double類型:

double_tensor = float_tensor.to(torch.double)
double_tensor = float_tensor.to(torch.float64)
# 或者
float_tensor = double_tensor.to(torch.float32)

另外,還需確保模型的輸入數(shù)據(jù)類型與模型定義時期望的數(shù)據(jù)類型相匹配,這樣可以避免出現(xiàn)數(shù)據(jù)類型不一致的錯誤。文章來源地址http://www.zghlxwxcb.cn/news/detail-791939.html

到了這里,關(guān)于【已解決】Pytorch RuntimeError: expected scalar type Double but found Float的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

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

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

相關(guān)文章

  • loss.backward()處遇到“RuntimeError: Found dtype Double but expected Float”

    loss.backward()處遇到“RuntimeError: Found dtype Double but expected Float”

    類型錯誤, 計(jì)算loss值的函數(shù)傳入的參數(shù)類型不統(tǒng)一。 查看上文loss計(jì)算代碼部分的參數(shù)類型,如loss=f.mse_loss(out,label),檢查out和label的類型都是torch.float類型即可。使用label.dtype查看tensor的類型。 報錯定位在這一行 尋思著是否是loss類型的問題,于是我就添加 但是還是報錯在此

    2024年02月16日
    瀏覽(21)
  • 已解決 Kotlin Error: Type mismatch: inferred type is String but Int was expected

    已解決 Kotlin Error: Type mismatch: inferred type is String but Int was expected

    ???? 博主貓頭虎(????)帶您 Go to New World??? ?? 博客首頁 : ????貓頭虎的博客?? 《面試題大全專欄》 ?? 文章圖文并茂??生動形象??簡單易學(xué)!歡迎大家來踩踩~?? 《IDEA開發(fā)秘籍專欄》 ?? 學(xué)會IDEA常用操作,工作效率翻倍~?? 《100天精通Golang(基礎(chǔ)入門篇)》 ??

    2024年02月04日
    瀏覽(22)
  • 記錄解決RuntimeError: Sizes of tensors must match except in dimension 1. Expected size 27 but got size

    記錄解決RuntimeError: Sizes of tensors must match except in dimension 1. Expected size 27 but got size

    在做目標(biāo)檢測服務(wù)過程中,將yolov7模型通過flask打包成預(yù)測服務(wù)API,此次訓(xùn)練的圖像輸入大小是1280,輸入預(yù)測圖片是如果圖像大于1280則預(yù)測成功,小于1280則報RuntimeError: Sizes of tensors must match except in dimension 1. Expected size 27 but got size。 由于只有小圖片預(yù)測報錯,猜測是圖像處理

    2024年02月11日
    瀏覽(35)
  • 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進(jìn)行訓(xùn)練的時候出現(xiàn)了問題: ? ? ? ? 我看了一下,大意就是維度也就是通道數(shù)不匹配,所以我覺得應(yīng)該是數(shù)據(jù)集圖片出現(xiàn)了問題。以下是我的普通數(shù)據(jù)集處理代碼: ? ? ? ? ? 我一張一張圖片放入DataLoader,然后按順序一張一張的

    2023年04月25日
    瀏覽(19)
  • 解決pytorch報錯——RuntimeError: Expected to have finished reduction in the prior iteration...

    解決pytorch報錯——RuntimeError: Expected to have finished reduction in the prior iteration...

    之前寫代碼時碰到了這樣一個錯誤: RuntimeError: Expected to have finished reduction in the prior iteration before starting a new one. This error indicates that your module has parameters that were not used in producing loss. You can enable unused parameter detection by (1) passing the keyword argument find_unused_parameters=True to torch.nn.pa

    2023年04月17日
    瀏覽(19)
  • 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)
  • RuntimeError: grad can be implicitly created only for scalar outputs的原因:Pytorch不支持對張量的求導(dǎo)

    RuntimeError: grad can be implicitly created only for scalar outputs的原因:Pytorch不支持對張量的求導(dǎo)

    一、背景介紹 原則上,Pytorch不支持對張量的求導(dǎo),即如果z是張量的話,需要先將其轉(zhuǎn)為標(biāo)量。 瀏覽了很多博客,給出的解決方案都是說在求導(dǎo)時,加一個torch.ones_like(z)的參數(shù)。 下面給出一個實(shí)例來分析一下torch.ones_like(z)的作用。簡而言之, torch.ones_like(z)相當(dāng)于在對z進(jìn)行求

    2024年02月14日
    瀏覽(25)
  • pytorch異常——RuntimeError:Given groups=1, weight of size..., expected of...

    pytorch異?!猂untimeError:Given groups=1, weight of size..., expected of...

    nn.Conv2d 需要的輸入張量格式為 (batch_size, channels, height, width),但您的示例輸入張量 x 是 (batch_size, height, width, channels)。因此,需要對輸入張量進(jìn)行轉(zhuǎn)置。 注意, TensorFlow使用\\\"NHWC\\\"(批次、高度、寬度、通道)格式,而PyTorch使用\\\"NCHW\\\"(批次、通道、高度、寬度)格式 在pytorch中

    2024年02月10日
    瀏覽(27)
  • 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)
  • 【微信小程序警告】property received type-uncompatible value: expected <String> but get null value. Used empty

    【微信小程序警告】property received type-uncompatible value: expected <String> but get null value. Used empty

    微信小程序警告property received type-uncompatible value: expected but get null value. Used empty 遇到這種錯誤多半是在wxml文件中寫入了未在js文件中定義的值,上圖: 解決辦法:只需要在data中定義value即可

    2024年02月11日
    瀏覽(12)

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

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

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

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

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包