日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Pytorch使用过程错误与解决 -汇总~

發(fā)布時(shí)間:2025/4/5 编程问答 53 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Pytorch使用过程错误与解决 -汇总~ 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Pytorch使用過(guò)程錯(cuò)誤與解決

  • error1:關(guān)鍵詞 copy tensor
  • error2:關(guān)鍵詞 張量相加
  • error3:關(guān)鍵詞 nn.Linear()的使用
    • 報(bào)錯(cuò)1:
      • 報(bào)錯(cuò)代碼:
      • 錯(cuò)誤原因:
    • 報(bào)錯(cuò)2:
      • 報(bào)錯(cuò)代碼:
      • 錯(cuò)誤原因:
    • 解決辦法
      • 錯(cuò)誤原因:
      • 正確代碼

error1:關(guān)鍵詞 copy tensor

報(bào)錯(cuò)信息:

UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).

解決辦法1:
*** 當(dāng)轉(zhuǎn)換某個(gè)變量為tensor時(shí),盡量使用torch.as_tensor()***

原錯(cuò)誤代碼: state_tensor_list = [torch.tensor(i) for i in batch.state]修改為: state_tensor_list = [torch.as_tensor(i) for i in batch.state]

解決辦法2:
*** 當(dāng)轉(zhuǎn)換某個(gè)變量x為tensor時(shí),盡量使用x.clone().detach() or x.clone().detach().requires_grad_(True) ***

原錯(cuò)誤代碼: state_tensor_list = [torch.tensor(i) for i in batch.state]修改為: state_tensor_list = [i.clone().detach() for i in batch.state]

error2:關(guān)鍵詞 張量相加

參考鏈接:

報(bào)錯(cuò)信息:
RuntimeError: The size of tensor a (3) must match the size of tensor b (4) at non-singleton dimension 1
解決辦法:
檢查張量維度!!!

錯(cuò)誤代碼 next_state_values = torch.tensor([0.7056, 0.7165, 0.6326]) state_action_values=torch.tensor([[ 0.1139, 0.1139, 0.1139, 0.1139],[ 0.0884, 0.0884, 0.0884, 0.0884],[ 0.0019, 0.0019, 0.0019, 0.0019]]) print(next_state_values.shape) print(state_action_values.shape) print(next_state_values.size()) print(state_action_values.size()) next_state_values + state_action_values結(jié)果: torch.Size([3]) torch.Size([3, 4]) torch.Size([3]) torch.Size([3, 4]) 修改代碼: next_state_values = torch.tensor([0.7056, 0.7165, 0.6326]) state_action_values=torch.tensor([[ 0.1139, 0.1139, 0.1139, 0.1139],[ 0.0884, 0.0884, 0.0884, 0.0884],[ 0.0019, 0.0019, 0.0019, 0.0019]]).max(1)[0] print(next_state_values.shape) print(state_action_values.shape) print(next_state_values.size()) print(state_action_values.size()) next_state_values + state_action_values結(jié)果: torch.Size([3]) torch.Size([3]) torch.Size([3]) torch.Size([3]) tensor([0.8195, 0.8049, 0.6345])

error3:關(guān)鍵詞 nn.Linear()的使用

參考鏈接:

報(bào)錯(cuò)1:

報(bào)錯(cuò):RuntimeError: expected scalar type Long but found Float

報(bào)錯(cuò)代碼:

import torch import torch.nn as nn class Net(nn.Module):def __init__(self,state_dim,mid_dim,action_dim):super().__init__()self.net = nn.Sequential(nn.Linear(state_dim, mid_dim))def forward(self,state):res = self.net(state)return res net = Net(9,5,4) print(net) current_state = torch.tensor([0,0,0,0,0,0,0,0,0]) print(current_state.shape) action = net(current_state) print(action)結(jié)果: Net((net): Sequential((0): Linear(in_features=9, out_features=5, bias=True)) ) torch.Size([9]) torch.Size([9])

錯(cuò)誤原因:

將一維張量轉(zhuǎn)換成二維張量后才能輸入

報(bào)錯(cuò)2:

報(bào)錯(cuò):RuntimeError: expected scalar type Float but found Long

報(bào)錯(cuò)代碼:

import torch import torch.nn as nn class Net(nn.Module):def __init__(self,state_dim,mid_dim,action_dim):super().__init__()self.net = nn.Sequential(nn.Linear(state_dim, mid_dim))def forward(self,state):res = self.net(state)return res net = Net(9,5,4) print(net) current_state = torch.tensor([0,0,0,0,0,0,0,0,0]) print(current_state.shape) current_state = current_state.view(1,9) print(current_state.shape) action = net(current_state) print(action) 結(jié)果: Net((net): Sequential((0): Linear(in_features=9, out_features=5, bias=True)) ) torch.Size([9]) torch.Size([1, 9])

錯(cuò)誤原因:

傳入的張量數(shù)據(jù)類(lèi)型應(yīng)該為float32

解決辦法

錯(cuò)誤原因:

1、將張量轉(zhuǎn)為二維使用.view(1,shape)
2、指定張量數(shù)據(jù)類(lèi)型 dtype=torch.float32

正確代碼

import torch import torch.nn as nn class Net(nn.Module):def __init__(self,state_dim,mid_dim,action_dim):super().__init__()self.net = nn.Sequential(nn.Linear(state_dim, mid_dim))def forward(self,state):res = self.net(state)return res net = Net(9,5,4) print(net) current_state = torch.tensor([0,0,0,0,0,0,0,0,0],**dtype=torch.float32**) print(current_state.shape) **current_state = current_state.view(1,9)** print(current_state.shape) action = net(current_state) print(action)

總結(jié)

以上是生活随笔為你收集整理的Pytorch使用过程错误与解决 -汇总~的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。