小编Yas*_*arL的帖子

Pytorch RuntimeError:张量的元素 0 不需要 grad 并且没有 grad_fn

该代码的构建如下:我的机器人拍摄一张照片,一些 tf 计算机视觉模型计算目标对象在图片中的起始位置。该信息(x1 和 x2 坐标)被传递到 pytorch 模型。它应该学会预测正确的运动激活,以便更接近目标。执行运动后,机器人再次拍照,tf cv 模型应计算电机激活是否使机器人更接近所需状态(x1 为 10,x2 坐标为 at31)

然而,每次我运行代码时,pytorch 都无法计算梯度。

我想知道这是否是某种数据类型问题,或者是否是一个更普遍的问题:如果不直接从 pytorch 网络的输出计算损失,是否无法计算梯度?

任何帮助和建议将不胜感激。

#define policy model (model to learn a policy for my robot)
import torch
import torch.nn as nn
import torch.nn.functional as F 
class policy_gradient_model(nn.Module):
    def __init__(self):
        super(policy_gradient_model, self).__init__()
        self.fc0 = nn.Linear(2, 2)
        self.fc1 = nn.Linear(2, 32)
        self.fc2 = nn.Linear(32, 64)
        self.fc3 = nn.Linear(64,32)
        self.fc4 = nn.Linear(32,32)
        self.fc5 = nn.Linear(32, 2)
    def forward(self,x):
        x = self.fc0(x)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x)) …
Run Code Online (Sandbox Code Playgroud)

python gradient reinforcement-learning deep-learning pytorch

15
推荐指数
3
解决办法
8万
查看次数