Tokenizer¶
- class torchrl.envs.transforms.Tokenizer(in_keys: Sequence[NestedKey] | None = None, out_keys: Sequence[NestedKey] | None = None, in_keys_inv: Sequence[NestedKey] | None = None, out_keys_inv: Sequence[NestedKey] | None = None, *, tokenizer: transformers.PretrainedTokenizerBase = None, use_raw_nontensor: bool = False, additional_tokens: list[str] | None = None, skip_special_tokens: bool = True, add_special_tokens: bool = False, padding: bool = True, max_length: int | None = None, return_attention_mask: bool = True, missing_tolerance: bool = True, call_before_reset: bool = False)[原始碼]¶
對指定輸入應用分詞操作。
- 引數:
in_keys (NestedKey 序列) – 進行標記化操作的輸入的鍵。
out_keys (NestedKey 序列) – 進行標記化操作的輸出的鍵。
in_keys_inv (NestedKey 序列, 可選) – 在反向呼叫期間進行標記化操作的輸入的鍵。
out_keys_inv (NestedKey 序列, 可選) – 在反向呼叫期間進行標記化操作的輸出的鍵。
- 關鍵字引數:
tokenizer (transformers.PretrainedTokenizerBase 或 str, 可選) – 要使用的分詞器。如果為
None,則預設使用“bert-base-uncased”。如果提供字串,則應為預訓練分詞器的名稱。use_raw_nontensor (bool, 可選) – 如果為
False,則在呼叫分詞函式之前,將從NonTensorData/NonTensorStack輸入中提取資料。如果為True,則將原始NonTensorData/NonTensorStack輸入直接傳遞給分詞函式,該函式必須支援這些輸入。預設為False。additional_tokens (List[str], 可選) – 要新增到分詞器詞彙表中的其他標記的列表。
注意
此轉換可用於將輸出字串轉換為標記,也可用於將標記化的動作或狀態轉換回字串。如果環境具有字串狀態規範,則轉換後的版本將具有標記化的狀態規範。如果它是字串動作規範,它將產生標記化的動作規範。
- forward(tensordict: TensorDictBase = None) TensorDictBase[原始碼]¶
讀取輸入 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.
- transform_done_spec(done_spec: TensorSpec) TensorSpec¶
變換 done spec,使結果 spec 與變換對映匹配。
- 引數:
done_spec (TensorSpec) – 變換前的 spec
- 返回:
轉換後的預期規範
- transform_input_spec(input_spec: Composite) Composite[原始碼]¶
轉換輸入規範,使結果規範與轉換對映匹配。
- 引數:
input_spec (TensorSpec) – 轉換前的規範
- 返回:
轉換後的預期規範
- transform_observation_spec(observation_spec: TensorSpec) TensorSpec[原始碼]¶
轉換觀察規範,使結果規範與轉換對映匹配。
- 引數:
observation_spec (TensorSpec) – 轉換前的規範
- 返回:
轉換後的預期規範
- transform_output_spec(output_spec: Composite) Composite¶
轉換輸出規範,使結果規範與轉換對映匹配。
此方法通常應保持不變。更改應透過
transform_observation_spec()、transform_reward_spec()和transform_full_done_spec()實現。 :param output_spec: 轉換之前的規範 :type output_spec: TensorSpec- 返回:
轉換後的預期規範
- transform_reward_spec(reward_spec: TensorSpec) TensorSpec¶
轉換獎勵的 spec,使其與變換對映匹配。
- 引數:
reward_spec (TensorSpec) – 變換前的 spec
- 返回:
轉換後的預期規範