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

PINN模型中使用torch.autograd.grad计算梯度时触发RuntimeError的问题排查求助

PINN模型中使用torch.autograd.grad计算梯度时触发RuntimeError的问题排查求助

各位大佬好,我现在在开发PINN模型的代码,在为PDE损失项计算梯度时使用了torch.autograd.grad(),但运行时触发了如下错误:

RuntimeError: One of the differentiated Tensors appears to not have been used in the graph. Set allow_unused=True if this is the desired behavior.

出错的代码行是:

dphidx = torch.autograd.grad(train_output[:, 0], X_train_tensor[:,0], torch.ones_like(train_output[:, 0]), create_graph=True)[0]

我已经检查过train_output[:, 0]和X_train_tensor[:,0]的requires_grad属性都是True,但还是搞不清楚问题到底出在哪,实在有点困惑。

为了方便大家排查,我附上模型的代码片段:

import torch.nn as nn

class PINNFP(nn.Module):
    def __init__(self):
        super().__init__()
        self.manual_layers = nn.Sequential(
            nn.Linear(in_features = 3, out_features = 5),
            nn.Linear(in_features = 5, out_features = 5),
            nn.Linear(in_features = 5, out_features = 5),
            nn.Linear(in_features = 5, out_features = ...)  # 原代码此处内容截断
        )

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.16 07:04:33