快捷方式

JumanjiWrapper

torchrl.envs.JumanjiWrapper(*args, **kwargs)[原始碼]

Jumanji 的環境包裝器。

Jumanji 提供了一個基於 Jax 的向量化模擬框架。TorchRL 的包裝器會產生一些用於 jax 到 torch 轉換的開銷,但計算圖仍然可以在模擬軌跡之上構建,從而允許透過回滾進行反向傳播。

GitHub: https://github.com/instadeepai/jumanji

文件: https://instadeepai.github.io/jumanji/

論文: https://arxiv.org/abs/2306.09884

注意

為了獲得更好的效能,在例項化此類時請開啟 jit。在程式碼執行期間也可以切換 jit 屬性。

>>> env.jit = True # Used jit
>>> env.jit = False # eager
引數:
  • env (jumanji.env.Environment) – 要包裝的環境。

  • categorical_action_encoding (bool, optional) – 如果為 True,則分類規範將轉換為 TorchRL 等效項 (torchrl.data.Categorical),否則將使用獨熱編碼 (torchrl.data.OneHot)。預設為 False

關鍵字引數:
  • batch_size (torch.Size, 可選) –

    環境的批次大小。使用 jumanji 時,這表示向量化環境的數量。如果批次大小為空,則環境不是批次鎖定的,可以同時執行任意數量的環境。預設為 torch.Size([])

    >>> import jumanji
    >>> from torchrl.envs import JumanjiWrapper
    >>> base_env = jumanji.make("Snake-v1")
    >>> env = JumanjiWrapper(base_env)
    >>> # Set the batch-size of the TensorDict instead of the env allows to control the number
    >>> #  of envs being run simultaneously
    >>> tdreset = env.reset(TensorDict(batch_size=[32]))
    >>> # Execute a rollout until all envs are done or max steps is reached, whichever comes first
    >>> rollout = env.rollout(100, break_when_all_done=True, auto_reset=False, tensordict=tdreset)
    

  • from_pixels (bool, 可選) – 環境是否應渲染其輸出。這將對環境吞吐量產生重大影響。只會渲染第一個環境。有關更多資訊,請參閱 render()。預設為 False

  • frame_skip (int, optional) – 如果提供,表示相同的動作需要重複多少步。返回的觀察值將是序列的最後一個觀察值,而獎勵將是跨步驟的獎勵總和。

  • device (torch.device, optional) – 如果提供,資料將被轉換到的裝置。預設為 torch.device("cpu")

  • allow_done_after_reset (bool, optional) – 如果為 True,則允許在呼叫 reset() 後立即將環境設定為 done。預設為 False

  • jit (bool, 可選) – step 和 reset 方法是否應包裝在 jit 中。預設為 False

變數:

available_envs – 可用於構建的環境

示例

>>> import jumanji
>>> from torchrl.envs import JumanjiWrapper
>>> base_env = jumanji.make("Snake-v1")
>>> env = JumanjiWrapper(base_env)
>>> env.set_seed(0)
>>> td = env.reset()
>>> td["action"] = env.action_spec.rand()
>>> td = env.step(td)
>>> print(td)
TensorDict(
    fields={
        action: Tensor(shape=torch.Size([]), device=cpu, dtype=torch.int32, is_shared=False),
        action_mask: Tensor(shape=torch.Size([4]), device=cpu, dtype=torch.bool, is_shared=False),
        done: Tensor(shape=torch.Size([1]), device=cpu, dtype=torch.bool, is_shared=False),
        grid: Tensor(shape=torch.Size([12, 12, 5]), device=cpu, dtype=torch.float32, is_shared=False),
        next: TensorDict(
            fields={
                action_mask: Tensor(shape=torch.Size([4]), device=cpu, dtype=torch.bool, is_shared=False),
                done: Tensor(shape=torch.Size([1]), device=cpu, dtype=torch.bool, is_shared=False),
                grid: Tensor(shape=torch.Size([12, 12, 5]), device=cpu, dtype=torch.float32, is_shared=False),
                reward: Tensor(shape=torch.Size([1]), device=cpu, dtype=torch.float32, is_shared=False),
                state: TensorDict(
                    fields={
                        action_mask: Tensor(shape=torch.Size([4]), device=cpu, dtype=torch.bool, is_shared=False),
                        body: Tensor(shape=torch.Size([12, 12]), device=cpu, dtype=torch.bool, is_shared=False),
                        body_state: Tensor(shape=torch.Size([12, 12]), device=cpu, dtype=torch.int32, is_shared=False),
                        fruit_position: TensorDict(
                            fields={
                                col: Tensor(shape=torch.Size([]), device=cpu, dtype=torch.int32, is_shared=False),
                                row: Tensor(shape=torch.Size([]), device=cpu, dtype=torch.int32, is_shared=False)},
                            batch_size=torch.Size([]),
                            device=cpu,
                            is_shared=False),
                        head_position: TensorDict(
                            fields={
                                col: Tensor(shape=torch.Size([]), device=cpu, dtype=torch.int32, is_shared=False),
                                row: Tensor(shape=torch.Size([]), device=cpu, dtype=torch.int32, is_shared=False)},
                            batch_size=torch.Size([]),
                            device=cpu,
                            is_shared=False),
                        key: Tensor(shape=torch.Size([2]), device=cpu, dtype=torch.int32, is_shared=False),
                        length: Tensor(shape=torch.Size([]), device=cpu, dtype=torch.int32, is_shared=False),
                        step_count: Tensor(shape=torch.Size([]), device=cpu, dtype=torch.int32, is_shared=False),
                        tail: Tensor(shape=torch.Size([12, 12]), device=cpu, dtype=torch.bool, is_shared=False)},
                    batch_size=torch.Size([]),
                    device=cpu,
                    is_shared=False),
                step_count: Tensor(shape=torch.Size([]), device=cpu, dtype=torch.int32, is_shared=False),
                terminated: Tensor(shape=torch.Size([1]), device=cpu, dtype=torch.bool, is_shared=False)},
            batch_size=torch.Size([]),
            device=cpu,
            is_shared=False),
        state: TensorDict(
            fields={
                action_mask: Tensor(shape=torch.Size([4]), device=cpu, dtype=torch.bool, is_shared=False),
                body: Tensor(shape=torch.Size([12, 12]), device=cpu, dtype=torch.bool, is_shared=False),
                body_state: Tensor(shape=torch.Size([12, 12]), device=cpu, dtype=torch.int32, is_shared=False),
                fruit_position: TensorDict(
                    fields={
                        col: Tensor(shape=torch.Size([]), device=cpu, dtype=torch.int32, is_shared=False),
                        row: Tensor(shape=torch.Size([]), device=cpu, dtype=torch.int32, is_shared=False)},
                    batch_size=torch.Size([]),
                    device=cpu,
                    is_shared=False),
                head_position: TensorDict(
                    fields={
                        col: Tensor(shape=torch.Size([]), device=cpu, dtype=torch.int32, is_shared=False),
                        row: Tensor(shape=torch.Size([]), device=cpu, dtype=torch.int32, is_shared=False)},
                    batch_size=torch.Size([]),
                    device=cpu,
                    is_shared=False),
                key: Tensor(shape=torch.Size([2]), device=cpu, dtype=torch.int32, is_shared=False),
                length: Tensor(shape=torch.Size([]), device=cpu, dtype=torch.int32, is_shared=False),
                step_count: Tensor(shape=torch.Size([]), device=cpu, dtype=torch.int32, is_shared=False),
                tail: Tensor(shape=torch.Size([12, 12]), device=cpu, dtype=torch.bool, is_shared=False)},
            batch_size=torch.Size([]),
            device=cpu,
            is_shared=False),
        step_count: Tensor(shape=torch.Size([]), device=cpu, dtype=torch.int32, is_shared=False),
        terminated: Tensor(shape=torch.Size([1]), device=cpu, dtype=torch.bool, is_shared=False)},
    batch_size=torch.Size([]),
    device=cpu,
    is_shared=False)
>>> print(env.available_envs)
['Game2048-v1',
 'Maze-v0',
 'Cleaner-v0',
 'CVRP-v1',
 'MultiCVRP-v0',
 'Minesweeper-v0',
 'RubiksCube-v0',
 'Knapsack-v1',
 'Sudoku-v0',
 'Snake-v1',
 'TSP-v1',
 'Connector-v2',
 'MMST-v0',
 'GraphColoring-v0',
 'RubiksCube-partly-scrambled-v0',
 'RobotWarehouse-v0',
 'Tetris-v0',
 'BinPack-v2',
 'Sudoku-very-easy-v0',
 'JobShop-v0']

為了利用 Jumanji 的優勢,通常會同時執行多個環境。

>>> import jumanji
>>> from torchrl.envs import JumanjiWrapper
>>> base_env = jumanji.make("Snake-v1")
>>> env = JumanjiWrapper(base_env, batch_size=[10])
>>> env.set_seed(0)
>>> td = env.reset()
>>> td["action"] = env.action_spec.rand()
>>> td = env.step(td)

在下面的示例中,我們將迭代地測試不同的批次大小,並報告短回滾的執行時間。

示例

>>> from torch.utils.benchmark import Timer
>>> for batch_size in [4, 16, 128]:
...     timer = Timer(
...     '''
... env.rollout(100)
... ''',
... setup=f'''
... from torchrl.envs import JumanjiWrapper
... import jumanji
... env = JumanjiWrapper(jumanji.make('Snake-v1'), batch_size=[{batch_size}])
... env.set_seed(0)
... env.rollout(2)
... ''')
...     print(batch_size, timer.timeit(number=10))
4
env.rollout(100)
setup: [...]
Median: 122.40 ms
2 measurements, 1 runs per measurement, 1 thread

16 env.rollout(100) 設定: […] 中位數: 134.39 毫秒 2 次測量,每次測量 1 次執行,1 個執行緒

128 env.rollout(100) 設定: […] 中位數: 172.31 毫秒 2 次測量,每次測量 1 次執行,1 個執行緒

文件

訪問全面的 PyTorch 開發者文件

檢視文件

教程

為初學者和高階開發者提供深入的教程

檢視教程

資源

查詢開發資源並讓您的問題得到解答

檢視資源