VC1Transform¶
- class torchrl.envs.transforms.VC1Transform(in_keys, out_keys, model_name, del_keys: bool = True)[原始檔]¶
VC1 轉換類。
VC1 提供了預訓練的 ResNet 權重,旨在促進機器人任務的視覺嵌入。這些模型使用 Ego4d 進行訓練。
- 參閱論文
- VC1: 一種用於機器人操作的通用視覺表示 (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(tensordict)¶
讀取輸入的 tensordict,並對選定的鍵應用轉換。
- to(dest: Union[device, str, int, dtype])[原始檔]¶
移動和/或轉換引數和緩衝區。
可以按如下方式呼叫
- to(device=None, dtype=None, non_blocking=False)[原始檔]
- to(dtype, non_blocking=False)[原始檔]
- to(tensor, non_blocking=False)[原始檔]
- to(memory_format=torch.channels_last)[原始檔]
它的簽名類似於
torch.Tensor.to(),但只接受浮點數或複數dtype。此外,此方法只會將浮點數或複數引數和緩衝區轉換為dtype(如果給定)。整數引數和緩衝區將被移動到device(如果給定),但其 dtype 不會改變。當non_blocking設定時,它會嘗試儘可能與主機非同步轉換/移動,例如,將具有固定記憶體的 CPU 張量移動到 CUDA 裝置。請參閱下面的示例。
注意
此方法會就地修改模組。
- 引數:
device (
torch.device) – 此模組中引數和緩衝區所需的目標裝置dtype (
torch.dtype) – 此模組中引數和緩衝區所需的浮點數或複數 dtypetensor (torch.Tensor) – 張量,其 dtype 和 device 是此模組中所有引數和緩衝區所需的目標 dtype 和 device
memory_format (
torch.memory_format) – 此模組中 4D 引數和緩衝區所需的記憶體格式(僅限關鍵字引數)
- 返回值:
self
- 返回型別:
Module
示例
>>> # 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[原始檔]¶
轉換 observation spec,使結果 spec 與轉換對映匹配。
- 引數:
observation_spec (TensorSpec) – 轉換前的 spec
- 返回值:
轉換後預期的 spec