快捷方式

VC1Transform

class torchrl.envs.transforms.VC1Transform(in_keys, out_keys, model_name, del_keys: bool = True)[source]

VC1 转换类。

VC1 提供预训练的 ResNet 权重,旨在促进机器人任务的视觉嵌入。模型使用 Ego4d 进行训练。

参见论文
VC1: A Universal Visual Representation for Robot Manipulation (Suraj Nair,

Aravind Rajeswaran, Vikash Kumar, Chelsea Finn, Abhinav Gupta) https://arxiv.org/abs/2203.12601

VC1Transform 以懒惰的方式创建:对象将在查询某个属性(spec 或 forward 方法)时才会被初始化。原因是 `_init()` 方法需要访问父环境(如果存在)的某些属性:通过使类具有惰性,我们可以确保以下代码片段按预期工作。

示例

>>> transform = VC1Transform("default", in_keys=["pixels"])
>>> env.append_transform(transform)
>>> # the forward method will first call _init which will look at env.observation_spec
>>> env.reset()
参数:
  • in_keys (list of NestedKeys) – 输入键列表。如果留空,则假定为“pixels”键。

  • out_keys (list of NestedKeys, optional) – 输出键列表。如果留空,则假定为“VC1_vec”。

  • model_name (str) – “large”、“base”或任何其他兼容的模型名称(有关更多信息,请参见 github 仓库)。默认为“default”,它提供了一个小型、未训练的模型用于测试。

  • del_keys (bool, optional) – 如果为 `True`(默认值),则输入键将从返回的 tensordict 中删除。

forward(next_tensordict)

读取输入 tensordict,并对选定的键应用转换。

默认情况下,此方法

  • 直接调用 _apply_transform()

  • 不调用 _step()_call()

此方法未在任何时候在 `env.step` 中调用。但是,它在 `sample()` 中被调用。

注意

`forward` 也使用 `dispatch` 与常规关键字参数一起工作,以将参数名称强制转换为键。

示例

>>> class TransformThatMeasuresBytes(Transform):
...     '''Measures the number of bytes in the tensordict, and writes it under `"bytes"`.'''
...     def __init__(self):
...         super().__init__(in_keys=[], out_keys=["bytes"])
...
...     def forward(self, tensordict: TensorDictBase) -> TensorDictBase:
...         bytes_in_td = tensordict.bytes()
...         tensordict["bytes"] = bytes
...         return tensordict
>>> t = TransformThatMeasuresBytes()
>>> env = env.append_transform(t) # works within envs
>>> t(TensorDict(a=0))  # Works offline too.
classmethod make_noload_model()[source]

在自定义目标位置创建了一个朴素模型。

to(dest: DEVICE_TYPING | torch.dtype)[source]

移动和/或转换参数和缓冲区。

这可以这样调用

to(device=None, dtype=None, non_blocking=False)[source]
to(dtype, non_blocking=False)[source]
to(tensor, non_blocking=False)[source]
to(memory_format=torch.channels_last)[source]

其签名类似于 `torch.Tensor.to()`,但只接受浮点或复数 `dtype`。此外,此方法仅将浮点或复数参数和缓冲区转换为 `dtype`(如果已给出)。整数参数和缓冲区将移动到 `device`(如果已给出),但 dtype 不变。当设置 `non_blocking` 时,它会尝试与主机异步转换/移动(如果可能),例如,将具有固定内存的 CPU Tensor 移动到 CUDA 设备。

有关示例,请参阅下文。

注意

此方法就地修改模块。

参数:
  • device (torch.device) – 此模块中的参数和缓冲区的所需设备

  • dtype (torch.dtype) – 此模块中的参数和缓冲区的所需浮点或复数 dtype

  • tensor (torch.Tensor) – 此模块所有参数和缓冲区的所需 dtype 和设备的原 Tensor

  • memory_format (torch.memory_format) – 此模块中 4D 参数和缓冲区的所需内存格式(仅限关键字参数)

返回:

self

返回类型:

模块

示例

>>> # xdoctest: +IGNORE_WANT("non-deterministic")
>>> linear = nn.Linear(2, 2)
>>> linear.weight
Parameter containing:
tensor([[ 0.1913, -0.3420],
        [-0.5113, -0.2325]])
>>> linear.to(torch.double)
Linear(in_features=2, out_features=2, bias=True)
>>> linear.weight
Parameter containing:
tensor([[ 0.1913, -0.3420],
        [-0.5113, -0.2325]], dtype=torch.float64)
>>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CUDA1)
>>> gpu1 = torch.device("cuda:1")
>>> linear.to(gpu1, dtype=torch.half, non_blocking=True)
Linear(in_features=2, out_features=2, bias=True)
>>> linear.weight
Parameter containing:
tensor([[ 0.1914, -0.3420],
        [-0.5112, -0.2324]], dtype=torch.float16, device='cuda:1')
>>> cpu = torch.device("cpu")
>>> linear.to(cpu)
Linear(in_features=2, out_features=2, bias=True)
>>> linear.weight
Parameter containing:
tensor([[ 0.1914, -0.3420],
        [-0.5112, -0.2324]], dtype=torch.float16)

>>> linear = nn.Linear(2, 2, bias=None).to(torch.cdouble)
>>> linear.weight
Parameter containing:
tensor([[ 0.3741+0.j,  0.2382+0.j],
        [ 0.5593+0.j, -0.4443+0.j]], dtype=torch.complex128)
>>> linear(torch.ones(3, 2, dtype=torch.cdouble))
tensor([[0.6122+0.j, 0.1150+0.j],
        [0.6122+0.j, 0.1150+0.j],
        [0.6122+0.j, 0.1150+0.j]], dtype=torch.complex128)
transform_observation_spec(observation_spec: TensorSpec) TensorSpec[source]

转换观察规范,使结果规范与转换映射匹配。

参数:

observation_spec (TensorSpec) – 转换前的规范

返回:

转换后的预期规范

文档

访问全面的 PyTorch 开发者文档

查看文档

教程

为初学者和高级开发者提供深入的教程

查看教程

资源

查找开发资源并让您的问题得到解答

查看资源