注意
跳转到末尾 以下载完整的示例代码。
切片、索引和掩码¶
作者: Tom Begley
在本教程中,您将学习如何对 TensorDict
进行切片、索引和掩码操作。
正如在教程 Manipulating the shape of a TensorDict 中讨论的那样,当我们创建一个 TensorDict
时,我们会指定一个 batch_size
,它必须与 TensorDict
中的所有条目的前导维度一致。由于我们保证所有条目都共享这些公共维度,因此我们可以像索引 torch.Tensor
一样来索引和掩码这些批处理维度。这些索引沿着批处理维度应用于 TensorDict
中的所有条目。
例如,给定一个具有两个批处理维度的 TensorDict
,tensordict[0]
将返回一个结构相同的新的 TensorDict
,其值对应于原始 TensorDict
中每个条目的第一个“行”。
import torch
from tensordict import TensorDict
tensordict = TensorDict(
{"a": torch.zeros(3, 4, 5), "b": torch.zeros(3, 4)}, batch_size=[3, 4]
)
print(tensordict[0])
TensorDict(
fields={
a: Tensor(shape=torch.Size([4, 5]), device=cpu, dtype=torch.float32, is_shared=False),
b: Tensor(shape=torch.Size([4]), device=cpu, dtype=torch.float32, is_shared=False)},
batch_size=torch.Size([4]),
device=None,
is_shared=False)
与常规张量相同的语法适用。例如,如果我们想删除每个条目的第一行,可以进行如下索引:
print(tensordict[1:])
TensorDict(
fields={
a: Tensor(shape=torch.Size([2, 4, 5]), device=cpu, dtype=torch.float32, is_shared=False),
b: Tensor(shape=torch.Size([2, 4]), device=cpu, dtype=torch.float32, is_shared=False)},
batch_size=torch.Size([2, 4]),
device=None,
is_shared=False)
我们可以同时索引多个维度
print(tensordict[:, 2:])
TensorDict(
fields={
a: Tensor(shape=torch.Size([3, 2, 5]), device=cpu, dtype=torch.float32, is_shared=False),
b: Tensor(shape=torch.Size([3, 2]), device=cpu, dtype=torch.float32, is_shared=False)},
batch_size=torch.Size([3, 2]),
device=None,
is_shared=False)
我们还可以使用 Ellipsis
来表示任意数量的 :
,以使选择元组的长度与 tensordict.batch_dims
的长度相同。
print(tensordict[..., 2:])
TensorDict(
fields={
a: Tensor(shape=torch.Size([3, 2, 5]), device=cpu, dtype=torch.float32, is_shared=False),
b: Tensor(shape=torch.Size([3, 2]), device=cpu, dtype=torch.float32, is_shared=False)},
batch_size=torch.Size([3, 2]),
device=None,
is_shared=False)
使用索引设置值¶
通常,只要批处理大小兼容,tensordict[index] = new_tensordict
就会起作用。
tensordict = TensorDict(
{"a": torch.zeros(3, 4, 5), "b": torch.zeros(3, 4)}, batch_size=[3, 4]
)
td2 = TensorDict({"a": torch.ones(2, 4, 5), "b": torch.ones(2, 4)}, batch_size=[2, 4])
tensordict[:-1] = td2
print(tensordict["a"], tensordict["b"])
tensor([[[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]],
[[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]],
[[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.]]]) tensor([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[0., 0., 0., 0.]])
掩码¶
我们像掩码张量一样掩码 TensorDict
。
TensorDict(
fields={
a: Tensor(shape=torch.Size([6, 5]), device=cpu, dtype=torch.float32, is_shared=False),
b: Tensor(shape=torch.Size([6]), device=cpu, dtype=torch.float32, is_shared=False)},
batch_size=torch.Size([6]),
device=None,
is_shared=False)
脚本总运行时间: (0 分钟 0.005 秒)