ray.rllib.core.learner.learner.Learner#
- class ray.rllib.core.learner.learner.Learner(*, config: AlgorithmConfig, module_spec: SingleAgentRLModuleSpec | MultiAgentRLModuleSpec | None = None, module: RLModule | None = None, learner_group_scaling_config=None, learner_hyperparameters=None, framework_hyperparameters=None)[source]#
Base class for Learners.
This class will be used to train RLModules. It is responsible for defining the loss function, and updating the neural network weights that it owns. It also provides a way to add/remove modules to/from RLModules in a multi-agent scenario, in the middle of training (This is useful for league based training).
TF and Torch specific implementation of this class fills in the framework-specific implementation details for distributed training, and for computing and applying gradients. User should not need to sub-class this class, but instead inherit from the TF or Torch specific sub-classes to implement their algorithm-specific update logic.
- Parameters:
config – The AlgorithmConfig object from which to derive most of the settings needed to build the Learner.
module_spec – The module specification for the RLModule that is being trained. If the module is a single agent module, after building the module it will be converted to a multi-agent module with a default key. Can be none if the module is provided directly via the
module
argument. Refer to ray.rllib.core.rl_module.SingleAgentRLModuleSpec or ray.rllib.core.rl_module.MultiAgentRLModuleSpec for more info.module – If learner is being used stand-alone, the RLModule can be optionally passed in directly instead of the through the
module_spec
.
Note: We use PPO and torch as an example here because many of the showcased components need implementations to come together. However, the same pattern is generally applicable.
import gymnasium as gym from ray.rllib.algorithms.ppo.ppo import PPOConfig from ray.rllib.algorithms.ppo.ppo_catalog import PPOCatalog from ray.rllib.algorithms.ppo.torch.ppo_torch_rl_module import ( PPOTorchRLModule ) from ray.rllib.core.rl_module.rl_module import SingleAgentRLModuleSpec env = gym.make("CartPole-v1") # Create a PPO config object first. config = ( PPOConfig() .framework("torch") .training(model={"fcnet_hiddens": [128, 128]}) ) # Create a learner instance directly from our config. All we need as # extra information here is the env to be able to extract space information # (needed to construct the RLModule inside the Learner). learner = config.build_learner(env=env) # Take one gradient update on the module and report the results. # results = learner.update(...) # Add a new module, perhaps for league based training. learner.add_module( module_id="new_player", module_spec=SingleAgentRLModuleSpec( module_class=PPOTorchRLModule, observation_space=env.observation_space, action_space=env.action_space, model_config_dict={"fcnet_hiddens": [64, 64]}, catalog_class=PPOCatalog, ) ) # Take another gradient update with both previous and new modules. # results = learner.update(...) # Remove a module. learner.remove_module("new_player") # Will train previous modules only. # results = learner.update(...) # Get the state of the learner. state = learner.get_state() # Set the state of the learner. learner.set_state(state) # Get the weights of the underly multi-agent RLModule. weights = learner.get_module_state() # Set the weights of the underly multi-agent RLModule. learner.set_module_state(weights)
Extension pattern:
from ray.rllib.core.learner.torch.torch_learner import TorchLearner class MyLearner(TorchLearner): def compute_loss(self, fwd_out, batch): # compute the loss based on batch and output of the forward pass # to access the learner hyper-parameters use `self._hps` return {ALL_MODULES: loss}
PublicAPI (alpha): This API is in alpha and may change before becoming stable.
Methods
Add a module to the underlying MultiAgentRLModule and the Learner.
Apply additional non-gradient based updates to this Algorithm.
Apply additional non-gradient based updates for a single module.
Applies the gradients to the MultiAgentRLModule parameters.
Builds the Learner.
Computes the gradients based on the given losses.
Computes the loss for the module being optimized.
Computes the loss for a single module.
Configures, creates, and registers the optimizers for this Learner.
Configures an optimizer for the given module_id.
Reduces the given ParamDict to contain only parameters for given optimizer.
Returns the state of the underlying MultiAgentRLModule.
Returns the optimizer object, configured under the given module_id and name.
Returns the state of all optimizers currently registered in this Learner.
Returns a list of (optimizer_name, optimizer instance)-tuples for module_id.
Returns a hashable reference to a trainable parameter.
Returns the list of parameters of a module.
Get the state of the learner.
Load the state of the learner from path
Applies potential postprocessing operations on the gradients.
Applies postprocessing operations on the gradients of the given module.
Registers an optimizer with a ModuleID, name, param list and lr-scheduler.
Remove a module from the Learner.
Save the state of the learner to path
Sets the state of the underlying MultiAgentRLModule
Sets the state of all optimizers currently registered in this Learner.
Set the state of the learner.
Returns whether a module should be updated or not based on
self.config
.Do
num_iters
minibatch updates given a train batch.Do
num_iters
minibatch updates given a list of episodes.Attributes
Whether the learner is running in distributed mode.
The multi-agent RLModule that is being trained.