Skip to content

Commit

Permalink
Added support for minibatch in PPO process_fn (#1168)
Browse files Browse the repository at this point in the history
Closes #1164

In PPOPolicy, the method `process_fn()` now computes `logp_old` in
minibatch instead of all at once.

---------

Co-authored-by: Michael Panchenko <[email protected]>
  • Loading branch information
jvasso and MischaPanch authored Jul 20, 2024
1 parent db8072a commit 324e3d2
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion tianshou/policy/modelfree/ppo.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,11 @@ def process_fn(
self._buffer, self._indices = buffer, indices
batch = self._compute_returns(batch, buffer, indices)
batch.act = to_torch_as(batch.act, batch.v_s)
logp_old = []
with torch.no_grad():
batch.logp_old = self(batch).dist.log_prob(batch.act)
for minibatch in batch.split(self.max_batchsize, shuffle=False, merge_last=True):
logp_old.append(self(minibatch).dist.log_prob(minibatch.act))
batch.logp_old = torch.cat(logp_old, dim=0).flatten()
batch: LogpOldProtocol
return batch

Expand Down

0 comments on commit 324e3d2

Please sign in to comment.