我正在尝试使用 torchrl 的
ClipPPOLoss
在多智能体强化学习环境中训练多个智能体。由于某种原因,该损失模块不希望接收带有 requires_grad=True
的样本对数概率。使用 SyncDataCollector
从环境中收集几个 Tensordict 帧并将它们存储在 ReplayBuffer
中后,我尝试运行以下循环:
for ep in range(num_epochs):
for _ in range(frames_per_batch // minibatch_size):
subdata = replay_buffer.sample()
subdata.get(("agents","sample_log_prob")).requires_grad = False
# this line prints False!
print(subdata.get(("agents","sample_log_prob")).requires_grad)
loss_vals = loss_module(subdata)
尽管打印
subdata.get(("agents","sample_log_prob")).requires_grad
会打印 False
,但上面的代码会导致 torchrl ClipPPOLoss
损失模块出错:
/usr/local/lib/python3.10/dist-packages/torchrl/objectives/ppo.py in _log_weight(self, tensordict)
477 prev_log_prob = tensordict.get(self.tensor_keys.sample_log_prob)
478 if prev_log_prob.requires_grad:
--> 479 raise RuntimeError("tensordict prev_log_prob requires grad.")
480
481 log_weight = (log_prob - prev_log_prob).unsqueeze(-1)
RuntimeError: tensordict prev_log_prob requires grad.
我已检查
loss_module.tensor_keys.sample_log_prob
以确保它指向("agents", "sample_log_prob")
。如果我在训练循环期间保存 subdata
并在训练循环之外访问 ("agents", "sample_log_prob")
,我仍然会找到 requires_grad = False
。当损失模块查看时,哪些因素可能会导致需要梯度的张量,但在训练循环之外则不会?我正在努力进一步调试,因为我什至不知道该去哪里查看。
您找到问题的答案了吗?我有类似的设置同样的问题!