You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyTorch调用loss.backward()时触发RuntimeError: Found dtype Float but expected Double错误求助

PyTorch调用loss.backward()时触发RuntimeError: Found dtype Float but expected Double错误求助

各位大佬好,我在运行多智能体强化学习(MARL)代码时,学习循环里调用loss.backward()触发了数据类型不匹配的错误,提示发现Float类型但预期是Double。

先贴出学习循环里的关键代码:

critic_value_ = self.agents[agent].target_critic.forward(states_, new_actions).flatten()
critic_value_[dones[:, 0]] = 0.0
critic_value = self.agents[agent].critic.forward(states, old_actions).flatten()

target = rewards[:, agent_idx] + self.agents[agent].gamma * critic_value_
loss = self.agents[agent].critic.loss(target, critic_value)

self.agents[agent].critic.optimizer.zero_grad()
# T.autograd.set_detect_anomaly(True)
loss.backward(retain_graph=True)
self.agents[agent].critic.optimizer.step()

下面是我的Critic网络定义:

import os
import torch as T
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim

class CriticNetwork(nn.Module):
    def __init__(self, beta, input_dims, fc1_dims, fc2_dims,
                 n_agents, n_actions, name, chkpt_dir):
        super(CriticNetwork, self).__init__()
        self.chkpt_file = os.path.join(chkpt_dir, name)
        self.fc1 = nn.Linear(input_dims + n_agents * n_actions, fc1_dims)
        self.fc2 = nn.Linear(fc1_dims, fc2_dims)
        self.q = nn.Linear(fc2_dims, 1)

        self.optimizer = optim.Adam(self.parameters(), lr=beta)
        self.loss = nn.MSELoss()
        self.device = T.device('cuda:0' if T.cuda.is_available() else 'cpu')
        # self.double()
        self.to(self.device)

    def forward(self, state, action):
        x = F.relu(self.fc1(T.cat([state, action], dim=1)))
        x = F.relu(self.fc2(x))
        q = self.q(x)
        return q

错误栈信息如下:

Traceback (most recent call last):
File "PycharmProjects/MARL/source/main.py", line 86, in
maddpg_agents.learn(memory)
File "PycharmProjects/MARL/source/maddpg.py", line 84, in learn
loss.backward(retain_graph=True)
File "PycharmProjects/MARL/venv/lib/python3.10/site-packages/torch/_tensor.py", line 487, in backward
torch.autograd.backward(
File "PycharmProjects/MARL/venv/lib/python3.10/site-packages/torch/autograd/init.py", line 200, in backward
Variable._execution_engine.run_backward( # Calls into the C++ engine to run the backward pass
RuntimeError: Found dtype Float but expected Double

错误触发前的张量细节:

  • loss: tensor(2.1452, dtype=torch.float64, grad_fn=<MseLossBackward0>)
  • target: tensor([-1.9751, -1.8311, -1.1488, ..., -1.9725, -0.5983, -1.1475], dtype=torch.float64, grad_fn=<AddBackward0>)
  • critic_value: tensor([0.2046, 0.2089, 0.1132, ..., 0.1629, 0.1426, 0.0946], grad_fn=<ReshapeAliasBackward0>)

我已经尝试过强制转换张量为float类型,但没有效果;也参考过类似的反向错误案例(提示“Found dtype Double but expected Float”),按照思路反向调整后也没能解决问题,有没有大佬能帮我排查下问题所在?

备注:内容来源于stack exchange,提问作者uri_m

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.22 08:38:16