赞
踩
报错信息:
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:
*** 当转换某个变量为tensor时,尽量使用torch.as_tensor()***
原错误代码:
state_tensor_list = [torch.tensor(i) for i in batch.state]
修改为:
state_tensor_list = [torch.as_tensor(i) for i in batch.state]
解决办法2:
*** 当转换某个变量x为tensor时,尽量使用x.clone().detach() or x.clone().detach().requires_grad_(True) ***
原错误代码:
state_tensor_list = [torch.tensor(i) for i in batch.state]
修改为:
state_tensor_list = [i.clone().detach() for i in batch.state]
报错信息:
RuntimeError: The size of tensor a (3) must match the size of tensor b (4) at non-singleton dimension 1
解决办法:
检查张量维度!!!
错误代码 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 结果: 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 结果: torch.Size([3]) torch.Size([3]) torch.Size([3]) torch.Size([3]) tensor([0.8195, 0.8049, 0.6345])
报错:RuntimeError: expected scalar type Long but found Float
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) 结果: Net( (net): Sequential( (0): Linear(in_features=9, out_features=5, bias=True) ) ) torch.Size([9]) torch.Size([9])
将一维张量转换成二维张量后才能输入
报错:RuntimeError: expected scalar type Float but found Long
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) 结果: Net( (net): S equential( (0): Linear(in_features=9, out_features=5, bias=True) ) ) torch.Size([9]) torch.Size([1, 9])
传入的张量数据类型应该为float32
1、将张量转为二维使用.view(1,shape)
2、指定张量数据类型 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)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。