Training with an RL Agent
Train a Stable-Baselines3 policy to balance Cartpole using the task from Registering an Environment.
Isaac Lab environments return batched PyTorch tensors, while each RL library expects
its own environment interface. The wrappers in isaaclab_rl adapt observations,
actions, and episode information to that interface. This tutorial uses the
Stable-Baselines3 VecEnv wrapper; see Wrapping environments for the other
wrappers and Reinforcement Learning for the training command reference.
Apply Gymnasium wrappers before the Isaac Lab wrapper for your RL library. The RL wrapper changes the environment interface and may no longer support Gymnasium wrappers.
The Code
For this tutorial, we use the training implementation from Stable-Baselines3 workflow in the
isaaclab_rl.entrypoints.backends.train_sb3 module.
Code for train_sb3.py
The Code Explained
Most of the code above is boilerplate code to create logging directories, saving the parsed configurations, and setting up different Stable-Baselines3 components. For this tutorial, the important part is creating the environment and wrapping it with the Stable-Baselines3 wrapper.
There are three wrappers used in the code above:
gymnasium.wrappers.RecordVideo: This wrapper records a video of the environment and saves it to the specified directory. This is useful for visualizing the agent’s behavior during training.wrappers.sb3.Sb3VecEnvWrapper: This wrapper converts the environment into a Stable-Baselines3 compatible environment.- stable_baselines3.common.vec_env.VecNormalize: This wrapper normalizes the environment’s observations and rewards.
Each of these wrappers wrap around the previous wrapper by following env = wrapper(env, *args, **kwargs)
repeatedly. The final environment is then used to train the agent. For more information on how these
wrappers work, please refer to the Wrapping environments documentation.
The Code Execution
We train a PPO agent from Stable-Baselines3 to solve the cartpole balancing task.
Training the agent
Train without a visualizer, watch the simulation live, or record a video using the options below.
Headless execution
When no visualizer is requested, no interactive visualizer window is opened during training. This is useful when training on a remote server or when you do not need live visual feedback, which can add some compute cost. Rendering can still be active for sensor/camera data capture when enabled by the workflow.
uv (Recommended)
isaaclab.sh / isaaclab.bat
Headless execution with off-screen render
To inspect behavior from a training run without an interactive window, pass --video.
By default, it records the scene from a headless Kit visualizer, which requires Isaac Sim. See
/source/how-to/record_video to choose a different visualizer or a camera-sensor output.
uv (Recommended)
isaaclab.sh / isaaclab.bat
The videos are saved to the logs/sb3/Isaac-Cartpole/<run-dir>/videos/train directory. You can open these videos
using any video player.
For tasks with on-scene cameras, you can also save the sensor image outputs directly during training
with --capture_env_sensors. See /source/how-to/capture_sensor_frames for the available
options and output formats.
Interactive execution
While the above two methods are useful for training the agent, they don’t allow you to interact with the simulation to see what is happening. In this case, run the training command as follows:
uv (Recommended)
isaaclab.sh / isaaclab.bat
This will open the Kit visualizer window and you can see the agent training in the environment. However, this
can slow down the training process because interactive visual feedback is enabled. As a workaround, you
can switch between different render modes in the "Isaac Lab" window that is docked on the bottom-right
corner of the screen. To learn more about these render modes, please check the
sim.SimulationContext.RenderMode class.
Viewing the logs
On a separate terminal, you can monitor the training progress by executing the following command:
uv (Recommended)
isaaclab.sh / isaaclab.bat
Playing the trained agent
Once the training is complete, you can visualize the trained agent by executing the following command:
uv (Recommended)
isaaclab.sh / isaaclab.bat
The above command will load the latest checkpoint from the logs/sb3/Isaac-Cartpole
directory. You can also specify a specific checkpoint by passing the --checkpoint flag.