pytorch-errors

0.

RuntimeError: save_for_backward can only save input or output tensors, but argument 0 doesn't satisfy this condition


When custom Funciton & Module, and the module need backward, the input should be Variable not Tensor


1. 

RuntimeError: Assertion `cur_target >= 0 && cur_target < n_classes' failed

e.g. lab[lab>=n_classes] = 0


2. RuntimeError: std::bad_cast pytorch

check date type

e.g.

Variable( torch.from_numpy(data) ).float().cuda()

Variable( torch.from_numpy(label).long().cuda()


3. RuntimeError: tensors are on different GPUs

some part not use gpu eg. model

but data use gpu


4. RuntimeError: CUDNN_STATUS_BAD_PARAM

check input and output channels of some layer


5. THCudaCheck FAIL file=/b/wheel/pytorch-src/torch/lib/THC/generic/THCStorage.c line=79 error=2 : out of memory
Segmentation fault

https://discuss.pytorch.org/t/segmentation-fault-when-loading-weight/1381/8


6. RuntimeError: CHECK_ARG(input->nDimension == output->nDimension) failed at torch/csrc/cudnn/Conv.cpp:275

input data shape is different from desired input shape of model 


7. torch.utils.data Dataset...  

File "//anaconda3/lib/python3.6/site-packages/torch/functional.py", line 60, in stack
    return torch.cat(inputs, dim, out=out)
TypeError: cat received an invalid combination of arguments - got (list, int, out=torch.ByteTensor), but expected one of:
 * (sequence[torch.ByteTensor] seq)
 * (sequence[torch.ByteTensor] seq, int dim)


TypeError: cat received an invalid combination of arguments - got (list, int), but expected one of:
 * (sequence[torch.ByteTensor] seq)
 * (sequence[torch.ByteTensor] seq, int dim)
      didn't match because some of the arguments have invalid types: (list, int)


Important: each iteration should return same data type 

convert to same dtype then in train process convert it to desired dtype

concatenate operation operate on the items of same dtype 


8.   File "/home/wenyu/anaconda3/lib/python3.6/site-packages/torch/autograd/variable.py", line 167, in backward
    torch.autograd.backward(self, gradient, retain_graph, create_graph, retain_variables)
  File "/home/wenyu/anaconda3/lib/python3.6/site-packages/torch/autograd/__init__.py", line 99, in backward
    variables, grad_variables, retain_graph)

RuntimeError: CUDNN_STATUS_MAPPING_ERROR


class_num with loss maybe not match


9 . RuntimeError: CUDNN_STATUS_INTERNAL_ERROR

model and data maybe on the different GPU




抱歉,我是一个语言模型AI,没有实际的代码能力。但是,以下是一个多智能体编队actor-critic算法的PyTorch示例代码,供您参考: ```python import torch import torch.nn as nn import torch.optim as optim import torch.nn.functional as F import numpy as np class Actor(nn.Module): def __init__(self, state_dim, action_dim): super(Actor, self).__init__() self.fc1 = nn.Linear(state_dim, 128) self.fc2 = nn.Linear(128, 64) self.fc3 = nn.Linear(64, action_dim) def forward(self, state): x = F.relu(self.fc1(state)) x = F.relu(self.fc2(x)) x = torch.tanh(self.fc3(x)) return x class Critic(nn.Module): def __init__(self, state_dim): super(Critic, self).__init__() self.fc1 = nn.Linear(state_dim, 128) self.fc2 = nn.Linear(128, 64) self.fc3 = nn.Linear(64, 1) def forward(self, state): x = F.relu(self.fc1(state)) x = F.relu(self.fc2(x)) x = self.fc3(x) return x class A2C: def __init__(self, state_dim, action_dim, num_agents, gamma=0.99, lr=0.001): self.num_agents = num_agents self.gamma = gamma self.actor = Actor(state_dim, action_dim) self.critic = Critic(state_dim) self.actor_optimizer = optim.Adam(self.actor.parameters(), lr=lr) self.critic_optimizer = optim.Adam(self.critic.parameters(), lr=lr) def act(self, states): actions = [] for state in states: state = torch.FloatTensor(state).unsqueeze(0) action = self.actor(state) actions.append(action.detach().numpy().flatten()) return actions def update(self, states, actions, rewards, next_states, dones): states = torch.FloatTensor(states) actions = torch.FloatTensor(actions) rewards = torch.FloatTensor(rewards).unsqueeze(1) next_states = torch.FloatTensor(next_states) dones = torch.FloatTensor(dones).unsqueeze(1) values = self.critic(states) next_values = self.critic(next_states) td_targets = rewards + self.gamma * next_values * (1 - dones) td_errors = td_targets - values actor_loss = 0 critic_loss = 0 for i in range(self.num_agents): advantage = td_errors[i].detach() log_prob = -torch.log(2 * np.pi * torch.ones(1, 1)) - \ torch.log(torch.FloatTensor([0.2])) - \ (actions[i] - self.actor(states[i].unsqueeze(0))) ** 2 / (2 * 0.2 ** 2) actor_loss += -(log_prob * advantage).mean() critic_loss += td_errors[i].pow(2).mean() self.actor_optimizer.zero_grad() actor_loss.backward() self.actor_optimizer.step() self.critic_optimizer.zero_grad() critic_loss.backward() self.critic_optimizer.step() ``` 在这个代码中,我们定义了Actor和Critic两个神经网络,并用PyTorch内置的优化器Adam来更新它们的参数。在每个时间步骤中,我们使用actor网络来选择动作,然后将动作传递给环境并获得奖励和下一个状态。然后我们使用critic网络来估计当前状态的值,并计算TD误差。最后,我们使用这些TD错误来更新actor和critic网络的参数,以最大化预期回报。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值