Skip to content

[BUG] - "NameError: name 'gc' is not defined" in "Using Replay Buffers" tutorial #3294

Closed as not planned
@BrikeX

Description

@BrikeX

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions