Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Transpose bug in reward2go when the last dim is not 1 #2086

Closed
3 tasks done
SamAdamDay opened this issue Apr 18, 2024 · 0 comments · Fixed by #2087
Closed
3 tasks done

[BUG] Transpose bug in reward2go when the last dim is not 1 #2086

SamAdamDay opened this issue Apr 18, 2024 · 0 comments · Fixed by #2087
Assignees
Labels
bug Something isn't working

Comments

@SamAdamDay
Copy link

Describe the bug

When calling reward2go with a shape whose last dimension is not 1, the results are incorrect due to reshaping rather than transposing.

To Reproduce

Minimal example:

from torchrl.objectives.value.functional import reward2go
import torch
reward = torch.zeros(4, 2)
reward[3, 0] = 1
reward[3, 1] = -1
done = torch.zeros(4, 2, dtype=bool)
done[3, :] = True
reward2go(reward, done, 0.9)

Output:

tensor([[ 0.7290,  0.8100],
        [ 0.9000,  1.0000],
        [-0.7290, -0.8100],
        [-0.9000, -1.0000]])

Expected behavior

tensor([[ 0.7290, -0.7290],
        [ 0.8100, -0.8100],
        [ 0.9000, -0.9000],
        [ 1.0000, -1.0000]])

System info

import torchrl, numpy, sys
print(torchrl.__version__, numpy.__version__, sys.version, sys.platform)
0.4.0+8570bd3 1.26.2 3.11.6 (main, Oct  8 2023, 05:06:43) [GCC 13.2.0] linux

Reason and Possible fixes

In the last step of reward2go the cumsum is reshaped:

    if cumsum.shape != shape:
        cumsum = cumsum.view(shape)

It should be:

    cumsum = cumsum.transpose(-2, -1)

to undo an earlier transpose.

I'm happy to contribute a PR with a fix and an extra test.

Checklist

  • I have checked that there is no similar issue in the repo (required)
  • I have read the documentation (required)
  • I have provided a minimal working example to reproduce the bug (required)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants