{"id":3826,"library":"tensordict","title":"TensorDict","description":"TensorDict is a PyTorch-dedicated tensor container, offering a dictionary-like class that inherits properties from `torch.Tensor`. It simplifies working with collections of tensors, enabling tensor-like operations, efficient data management, and re-usable training loops across various machine learning paradigms. It's currently at version 0.12.0 and is actively developed.","status":"active","version":"0.12.0","language":"en","source_language":"en","source_url":"https://github.com/pytorch/tensordict","tags":["pytorch","tensors","data structures","machine learning","deep learning"],"install":[{"cmd":"pip install tensordict","lang":"bash","label":"PyPI"},{"cmd":"conda install -c conda-forge tensordict","lang":"bash","label":"Conda"}],"dependencies":[{"reason":"TensorDict is built on and for PyTorch tensors, providing a specialized container for them.","package":"torch"}],"imports":[{"symbol":"TensorDict","correct":"from tensordict import TensorDict"},{"symbol":"tensorclass","correct":"from tensordict import tensorclass"},{"symbol":"MemoryMappedTensor","correct":"from tensordict import MemoryMappedTensor"}],"quickstart":{"code":"import torch\nfrom tensordict import TensorDict\n\ndata = TensorDict(\n    obs=torch.randn(128, 84),\n    action=torch.randn(128, 4),\n    reward=torch.randn(128, 1),\n    batch_size=[128],\n)\n\ndevice = \"cuda\" if torch.cuda.is_available() else \"cpu\"\ndata_gpu = data.to(device) # all tensors move together\nsub = data_gpu[:64] # all tensors are sliced\nstacked = torch.stack([data, data]) # works like a tensor\n\nprint(f\"Original TensorDict:\\n{data}\")\nprint(f\"Device of data_gpu: {data_gpu.device}\")\nprint(f\"Batch size of sliced TensorDict: {sub.batch_size}\")\nprint(f\"Batch size of stacked TensorDict: {stacked.batch_size}\")","lang":"python","description":"This quickstart demonstrates the creation of a TensorDict, moving it to a device, slicing it like a tensor, and stacking multiple TensorDicts."},"warnings":[{"fix":"Upgrade Python to version 3.10 or higher.","message":"Python 3.9 support was dropped in TensorDict v0.11.0. Python 3.10 or newer is now required.","severity":"breaking","affected_versions":">=0.11.0"},{"fix":"Replace `td.lock()` with `td.lock_()`, `td.unlock()` with `td.unlock_()`, and `td.rename_key('old', 'new')` with `td.rename_key_('old', 'new')`.","message":"Deprecated methods `lock`, `unlock`, and `rename_key` (without a trailing underscore) were removed in v0.11.0. Use `lock_`, `unlock_`, and `rename_key_` instead for in-place modifications.","severity":"breaking","affected_versions":">=0.11.0"},{"fix":"Access the tensor content directly, e.g., `my_memmap_tensor` instead of `my_memmap_tensor._tensor`.","message":"The `MemoryMappedTensor._tensor` property now raises a `RuntimeError` since v0.11.0. Users should interact with the `MemoryMappedTensor` instance directly as it is a tensor subclass.","severity":"breaking","affected_versions":">=0.11.0"},{"fix":"Review code that assigns lists to TensorDicts and explicitly use context managers like `td.set_` if list stacking is not the desired default behavior, or if you want to suppress the warning.","message":"From v0.10.0, lists assigned to a TensorDict will be automatically stacked by default, potentially raising a `FutureWarning`. Explicit context managers should be used for specific behavior.","severity":"gotcha","affected_versions":">=0.10.0"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}