• 文档 >
  • 操纵 TensorDict 的形状
快捷方式

操纵 TensorDict 的形状

作者: Tom Begley

在本教程中,你将学习如何操纵 TensorDict 及其内容的形状。

当我们创建一个 TensorDict 时,会指定一个 batch_size,它必须与 TensorDict 中所有条目的前导维度一致。由于我们保证所有条目共享这些公共维度,因此 TensorDict 能够提供多种方法来操纵 TensorDict 及其内容的形状。

import torch
from tensordict.tensordict import TensorDict

索引 TensorDict

由于批处理维度保证存在于所有条目上,我们可以随意对它们进行索引,并且 TensorDict 的每个条目都将以相同的方式被索引。

a = torch.rand(3, 4)
b = torch.rand(3, 4, 5)
tensordict = TensorDict({"a": a, "b": b}, batch_size=[3, 4])

indexed_tensordict = tensordict[:2, 1]
assert indexed_tensordict["a"].shape == torch.Size([2])
assert indexed_tensordict["b"].shape == torch.Size([2, 5])

重塑 TensorDict

TensorDict.reshape 的工作方式与 torch.Tensor.reshape() 完全一样。它沿批处理维度应用于 TensorDict 的所有内容——注意下面示例中 b 的形状。它还会更新 batch_size 属性。

reshaped_tensordict = tensordict.reshape(-1)
assert reshaped_tensordict.batch_size == torch.Size([12])
assert reshaped_tensordict["a"].shape == torch.Size([12])
assert reshaped_tensordict["b"].shape == torch.Size([12, 5])

分割 TensorDict

TensorDict.splittorch.Tensor.split() 类似。它将 TensorDict 分割成块。每个块都是一个与原始 TensorDict 结构相同的 TensorDict,但其条目是原始 TensorDict 中相应条目的视图(view)。

chunks = tensordict.split([3, 1], dim=1)
assert chunks[0].batch_size == torch.Size([3, 3])
assert chunks[1].batch_size == torch.Size([3, 1])
torch.testing.assert_close(chunks[0]["a"], tensordict["a"][:, :-1])

注意

每当函数或方法接受 dim 参数时,负维度会相对于调用该函数或方法的 TensorDictbatch_size 进行解释。特别是,如果存在具有不同批处理大小的嵌套 TensorDict 值,负维度始终相对于根 TensorDict 的批处理维度进行解释。

>>> tensordict = TensorDict(
...     {
...         "a": torch.rand(3, 4),
...         "nested": TensorDict({"b": torch.rand(3, 4, 5)}, [3, 4, 5])
...     },
...     [3, 4],
... )
>>> # dim = -2 will be interpreted as the first dimension throughout, as the root
>>> # TensorDict has 2 batch dimensions, even though the nested TensorDict has 3
>>> chunks = tensordict.split([2, 1], dim=-2)
>>> assert chunks[0].batch_size == torch.Size([2, 4])
>>> assert chunks[0]["nested"].batch_size == torch.Size([2, 4, 5])

从这个例子可以看出,TensorDict.split 方法的行为与我们在调用前将 dim=-2 替换为 dim=tensordict.batch_dims - 2 时完全相同。

解绑

TensorDict.unbindtorch.Tensor.unbind() 类似,概念上与 TensorDict.split 相似。它移除指定的维度并返回沿该维度的所有切片的 tuple

slices = tensordict.unbind(dim=1)
assert len(slices) == 4
assert all(s.batch_size == torch.Size([3]) for s in slices)
torch.testing.assert_close(slices[0]["a"], tensordict["a"][:, 0])

堆叠和拼接

TensorDict 可以与 torch.cattorch.stack 结合使用。

堆叠 TensorDict

堆叠可以懒惰地或连续地完成。懒惰堆叠(lazy stack)只是一个 tensordict 列表,表示为一个 tensordict 堆叠。它允许用户携带内容形状、设备或键集不同的 tensordict 集合。另一个优点是堆叠操作可能开销很大,如果只需要一小部分键,懒惰堆叠会比真正的堆叠快得多。它依赖于 LazyStackedTensorDict 类。在这种情况下,值只会在访问时按需堆叠。

from tensordict import LazyStackedTensorDict

cloned_tensordict = tensordict.clone()
stacked_tensordict = LazyStackedTensorDict.lazy_stack(
    [tensordict, cloned_tensordict], dim=0
)
print(stacked_tensordict)

# Previously, torch.stack was always returning a lazy stack. For consistency with
# the regular PyTorch API, this behaviour will soon be adapted to deliver only
# dense tensordicts. To control which behaviour you are relying on, you can use
# the :func:`~tensordict.utils.set_lazy_legacy` decorator/context manager:

from tensordict.utils import set_lazy_legacy

with set_lazy_legacy(True):  # old behaviour
    lazy_stack = torch.stack([tensordict, cloned_tensordict])
assert isinstance(lazy_stack, LazyStackedTensorDict)

with set_lazy_legacy(False):  # new behaviour
    dense_stack = torch.stack([tensordict, cloned_tensordict])
assert isinstance(dense_stack, TensorDict)
LazyStackedTensorDict(
    fields={
        a: Tensor(shape=torch.Size([2, 3, 4]), device=cpu, dtype=torch.float32, is_shared=False),
        b: Tensor(shape=torch.Size([2, 3, 4, 5]), device=cpu, dtype=torch.float32, is_shared=False)},
    exclusive_fields={
    },
    batch_size=torch.Size([2, 3, 4]),
    device=None,
    is_shared=False,
    stack_dim=0)

如果我们沿堆叠维度索引 LazyStackedTensorDict,我们会恢复原始的 TensorDict

assert stacked_tensordict[0] is tensordict
assert stacked_tensordict[1] is cloned_tensordict

访问 LazyStackedTensorDict 中的键会导致这些值被堆叠。如果键对应于嵌套的 TensorDict,那么我们将恢复另一个 LazyStackedTensorDict

assert stacked_tensordict["a"].shape == torch.Size([2, 3, 4])

注意

由于值是按需堆叠的,多次访问一个项目意味着它会被多次堆叠,这效率很低。如果你需要多次访问堆叠的 TensorDict 中的值,你可能需要考虑将 LazyStackedTensorDict 转换为连续的 TensorDict,这可以通过 LazyStackedTensorDict.to_tensordictLazyStackedTensorDict.contiguous 方法完成。

>>> assert isinstance(stacked_tensordict.contiguous(), TensorDict)
>>> assert isinstance(stacked_tensordict.contiguous(), TensorDict)

调用这些方法中的任一个后,我们将得到一个包含堆叠值的常规 TensorDict,并且在访问值时不会执行额外的计算。

拼接 TensorDict

拼接不是懒惰进行的,而是对 TensorDict 实例列表调用 torch.cat() 会直接返回一个 TensorDict,其条目是列表中元素的拼接条目。

concatenated_tensordict = torch.cat([tensordict, cloned_tensordict], dim=0)
assert isinstance(concatenated_tensordict, TensorDict)
assert concatenated_tensordict.batch_size == torch.Size([6, 4])
assert concatenated_tensordict["b"].shape == torch.Size([6, 4, 5])

扩展 TensorDict

我们可以使用 TensorDict.expand 扩展 TensorDict 的所有条目。

exp_tensordict = tensordict.expand(2, *tensordict.batch_size)
assert exp_tensordict.batch_size == torch.Size([2, 3, 4])
torch.testing.assert_close(exp_tensordict["a"][0], exp_tensordict["a"][1])

压缩(Squeezing)和解压缩(Unsqueezing)TensorDict

我们可以使用 squeeze()unsqueeze() 方法压缩或解压缩 TensorDict 的内容。

tensordict = TensorDict({"a": torch.rand(3, 1, 4)}, [3, 1, 4])
squeezed_tensordict = tensordict.squeeze()
assert squeezed_tensordict["a"].shape == torch.Size([3, 4])
print(squeezed_tensordict, end="\n\n")

unsqueezed_tensordict = tensordict.unsqueeze(-1)
assert unsqueezed_tensordict["a"].shape == torch.Size([3, 1, 4, 1])
print(unsqueezed_tensordict)
TensorDict(
    fields={
        a: Tensor(shape=torch.Size([3, 4]), device=cpu, dtype=torch.float32, is_shared=False)},
    batch_size=torch.Size([3, 4]),
    device=None,
    is_shared=False)

TensorDict(
    fields={
        a: Tensor(shape=torch.Size([3, 1, 4, 1]), device=cpu, dtype=torch.float32, is_shared=False)},
    batch_size=torch.Size([3, 1, 4, 1]),
    device=None,
    is_shared=False)

注意

到目前为止,像 unsqueeze()squeeze()view()permute()transpose() 等操作都会返回这些操作的懒惰版本(即一个容器,其中存储了原始 tensordict,并且每次访问键时都会应用这些操作)。此行为将来将被弃用,并且已经可以通过 set_lazy_legacy() 函数进行控制。

>>> with set_lazy_legacy(True):
...     lazy_unsqueeze = tensordict.unsqueeze(0)
>>> with set_lazy_legacy(False):
...     dense_unsqueeze = tensordict.unsqueeze(0)

请记住,与往常一样,这些方法仅应用于批处理维度。条目的任何非批处理维度都不会受到影响。

tensordict = TensorDict({"a": torch.rand(3, 1, 1, 4)}, [3, 1])
squeezed_tensordict = tensordict.squeeze()
# only one of the singleton dimensions is dropped as the other
# is not a batch dimension
assert squeezed_tensordict["a"].shape == torch.Size([3, 1, 4])

查看(View)TensorDict

TensorDict 也支持 view。这会创建一个 _ViewedTensorDict,它在内容被访问时懒惰地创建视图。

tensordict = TensorDict({"a": torch.arange(12)}, [12])
# no views are created at this step
viewed_tensordict = tensordict.view((2, 3, 2))

# the view of "a" is created on-demand when we access it
assert viewed_tensordict["a"].shape == torch.Size([2, 3, 2])

置换(Permuting)批处理维度

TensorDict.permute 方法可用于置换批处理维度,就像 torch.permute() 一样。非批处理维度保持不变。

此操作是懒惰的,因此只有在尝试访问条目时才会置换批处理维度。与往常一样,如果你可能需要多次访问某个特定条目,请考虑将其转换为 TensorDict

tensordict = TensorDict({"a": torch.rand(3, 4), "b": torch.rand(3, 4, 5)}, [3, 4])
# swap the batch dimensions
permuted_tensordict = tensordict.permute([1, 0])

assert permuted_tensordict["a"].shape == torch.Size([4, 3])
assert permuted_tensordict["b"].shape == torch.Size([4, 3, 5])

将 tensordicts 用作装饰器

对于一系列可逆操作,tensordicts 可以用作装饰器。这些操作包括用于函数调用的 to_module()unlock_()lock_(),或者形状操作如 view()permute()transpose()squeeze()unsqueeze()。这是一个使用 transpose 函数的快速示例。

tensordict = TensorDict({"a": torch.rand(3, 4), "b": torch.rand(3, 4, 5)}, [3, 4])

with tensordict.transpose(1, 0) as tdt:
    tdt.set("c", torch.ones(4, 3))  # we have permuted the dims

# the ``"c"`` entry is now in the tensordict we used as decorator:
#

assert (tensordict.get("c") == 1).all()

在 TensorDict 中收集值

TensorDict.gather 方法可用于沿批处理维度进行索引并将结果收集到单个维度中,这与 torch.gather() 非常相似。

index = torch.randint(4, (3, 4))
gathered_tensordict = tensordict.gather(dim=1, index=index)
print("index:\n", index, end="\n\n")
print("tensordict['a']:\n", tensordict["a"], end="\n\n")
print("gathered_tensordict['a']:\n", gathered_tensordict["a"], end="\n\n")
index:
 tensor([[0, 2, 2, 1],
        [1, 0, 2, 2],
        [1, 3, 1, 2]])

tensordict['a']:
 tensor([[0.9580, 0.6498, 0.6842, 0.3068],
        [0.7585, 0.6647, 0.1465, 0.9081],
        [0.0090, 0.7241, 0.4385, 0.4466]])

gathered_tensordict['a']:
 tensor([[0.9580, 0.6842, 0.6842, 0.6498],
        [0.6647, 0.7585, 0.1465, 0.1465],
        [0.7241, 0.4466, 0.7241, 0.4385]])

脚本总运行时间: (0 分 0.008 秒)

图库由 Sphinx-Gallery 生成

文档

访问全面的 PyTorch 开发者文档

查看文档

教程

获取面向初学者和高级开发者的深度教程

查看教程

资源

查找开发资源并获得问题解答

查看资源