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] - "NameError: name 'gc' is not defined" in "Using Replay Buffers" tutorial #3294

Closed
BrikeX opened this issue Mar 18, 2025 · 3 comments
Closed
Labels
rl Issues related to reinforcement learning tutorial, DQN, and so on

Comments

@BrikeX
Copy link

BrikeX commented Mar 18, 2025

In the section "Storing trajectories", an error is reported in the following code

from torchrl.data import SliceSampler

rb = TensorDictReplayBuffer(
    storage=LazyTensorStorage(size),
    sampler=SliceSampler(traj_key="episode", num_slices=4),
    batch_size=8,
)
episode = [torch.zeros](https://pytorch.org/docs/stable/generated/torch.zeros.html#torch.zeros)(10, dtype=torch.int)
episode[:3] = 1
episode[3:5] = 2
episode[5:7] = 3
episode[7:] = 4
steps = [torch.cat](https://pytorch.org/docs/stable/generated/torch.cat.html#torch.cat)([[torch.arange](https://pytorch.org/docs/stable/generated/torch.arange.html#torch.arange)(3), [torch.arange](https://pytorch.org/docs/stable/generated/torch.arange.html#torch.arange)(2), [torch.arange](https://pytorch.org/docs/stable/generated/torch.arange.html#torch.arange)(2), [torch.arange](https://pytorch.org/docs/stable/generated/torch.arange.html#torch.arange)(3)])
data = TensorDict(
    {
        "episode": episode,
        "obs": [torch.randn](https://pytorch.org/docs/stable/generated/torch.randn.html#torch.randn)((3, 4, 5)).expand(10, 3, 4, 5),
        "act": [torch.randn](https://pytorch.org/docs/stable/generated/torch.randn.html#torch.randn)((20,)).expand(10, 20),
        "other": [torch.randn](https://pytorch.org/docs/stable/generated/torch.randn.html#torch.randn)((20, 50)).expand(10, 20, 50),
        "steps": steps,
    },
    [10],
)
rb.extend(data)
sample = rb.sample()
print("episode are grouped", sample["episode"])
print("steps are successive", sample["steps"])

gc.collect()

as

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[44], line 29
     26 print("episode are grouped", sample["episode"])
     27 print("steps are successive", sample["steps"])
---> 29 gc.collect()

NameError: name 'gc' is not defined

cc @vmoens @nairbv

@BrikeX BrikeX changed the title "NameError: name 'gc' is not defined" in "Using Replay Buffers" tutorial [BUG] - "NameError: name 'gc' is not defined" in "Using Replay Buffers" tutorial Mar 18, 2025
@svekars svekars added the rl Issues related to reinforcement learning tutorial, DQN, and so on label Mar 18, 2025
@svekars
Copy link
Contributor

svekars commented Mar 18, 2025

Can you please provide a link to the tutorial

@vmoens
Copy link
Contributor

vmoens commented Mar 18, 2025

I think it's in torchrl
https://github.com/pytorch/rl/blob/619fec69c33966fc92cbb4527d6dad567e094752/tutorials/sphinx-tutorials/rb_tutorial.py#L794
Let's move the discussion there @BrikeX if you don't mind!

@vmoens vmoens closed this as not planned Won't fix, can't repro, duplicate, stale Mar 18, 2025
@BrikeX
Copy link
Author

BrikeX commented Mar 19, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rl Issues related to reinforcement learning tutorial, DQN, and so on
Projects
None yet
Development

No branches or pull requests

3 participants