torchrl.objectives package¶
TorchRL provides a series of losses to use in your training scripts. The aim is to have losses that are easily reusable/swappable and that have a simple signature.
The main characteristics of TorchRL losses are:
they are stateful objects: they contain a copy of the trainable parameters such that
loss_module.parameters()
gives whatever is needed to train the algorithm.They follow the
tensordict
convention: thetorch.nn.Module.forward()
method will receive a tensordict as input that contains all the necessary information to return a loss value.They output a
tensordict.TensorDict
instance with the loss values written under a"loss_<smth>"
wheresmth
is a string describing the loss. Additional keys in the tensordict may be useful metrics to log during training time.
Note
The reason we return independent losses is to let the user use a different optimizer for different sets of parameters for instance. Summing the losses can be simply done via
>>> loss_val = sum(loss for key, loss in loss_vals.items() if key.startswith("loss_"))
Training value functions¶
TorchRL provides a range of value estimators such as TD(0), TD(1), TD(\(\lambda\)) and GAE. In a nutshell, a value estimator is a function of data (mostly rewards and done states) and a state value (ie. the value returned by a function that is fit to estimate statevalues). To learn more about value estimators, check the introduction to RL from Sutton and Barto, in particular the chapters about value iteration and TD learning. It gives a somewhat biased estimation of the discounted return following a state or a stateaction pair based on data and proxy maps. These estimators are used in two contexts:
To train the value network to learn the “true” state value (or stateaction value) map, one needs a target value to fit it to. The better (less bias, less variance) the estimator, the better the value network will be, which in turn can speed up the policy training significantly. Typically, the value network loss will look like:
>>> value = value_network(states) >>> target_value = value_estimator(rewards, done, value_network(next_state)) >>> value_net_loss = (value  target_value).pow(2).mean()
Computing an “advantage” signal for policyoptimization. The advantage is the delta between the value estimate (from the estimator, ie from “real” data) and the output of the value network (ie the proxy to this value). A positive advantage can be seen as a signal that the policy actually performed better than expected, thereby signaling that there is room for improvement if that trajectory is to be taken as example. Conversely, a negative advantage signifies that the policy underperformed compared to what was to be expected.
Thins are not always as easy as in the example above and the formula to compute the value estimator or the advantage may be slightly more intricate than this. To help users flexibly use one or another value estimator, we provide a simple API to change it onthefly. Here is an example with DQN, but all modules will follow a similar structure:
>>> from torchrl.objectives import DQNLoss, ValueEstimators
>>> loss_module = DQNLoss(actor)
>>> kwargs = {"gamma": 0.9, "lmbda": 0.9}
>>> loss_module.make_value_estimator(ValueEstimators.TDLambda, **kwargs)
The ValueEstimators
class enumerates the value
estimators to choose from. This makes it easy for the users to rely on
autocompletion to make their choice.

A parent class for RL losses. 
DQN¶

The DQN Loss class. 

A distributional DQN loss class. 
DDPG¶

The DDPG Loss class. 
SAC¶

TorchRL implementation of the SAC loss. 

Discrete SAC Loss module. 
REDQ¶

REDQ Loss module. 
IQL¶

TorchRL implementation of the IQL loss. 

TorchRL implementation of the discrete IQL loss. 
CQL¶

TorchRL implementation of the continuous CQL loss. 

TorchRL implementation of the discrete CQL loss. 
DT¶

TorchRL implementation of the Online Decision Transformer loss. 

TorchRL implementation of the Online Decision Transformer loss. 
TD3¶

TD3 Loss module. 
PPO¶

A parent PPO loss class. 

Clipped PPO loss. 

KL Penalty PPO loss. 
A2C¶

TorchRL implementation of the A2C loss. 
Reinforce¶

Reinforce loss module. 
Dreamer¶

Dreamer Actor Loss. 

Dreamer Model Loss. 

Dreamer Value Loss. 
Multiagent objectives¶
These objectives are specific to multiagent algorithms.
QMixer¶

The QMixer loss class. 
Returns¶

An abstract parent class for value function modules. 

Temporal Difference (TD(0)) estimate of advantage function. 

\(\infty\)Temporal Difference (TD(1)) estimate of advantage function. 

TD(\(\lambda\)) estimate of advantage function. 

A class wrapper around the generalized advantage estimate functional. 

TD(0) discounted return estimate of a trajectory. 

TD(0) advantage estimate of a trajectory. 

TD(1) return estimate. 

Vectorized TD(1) return estimate. 

TD(1) advantage estimate. 

Vectorized TD(1) advantage estimate. 

TD(\(\lambda\)) return estimate. 
Vectorized TD(\(\lambda\)) return estimate. 

TD(\(\lambda\)) advantage estimate. 

Vectorized TD(\(\lambda\)) advantage estimate. 

Generalized advantage estimate of a trajectory. 

Vectorized Generalized advantage estimate of a trajectory. 


Compute the discounted cumulative sum of rewards given multiple trajectories and the episode ends. 
Utils¶

Computes a distance loss between two tensors. 

Context manager to hold a network out of a computational graph. 

Context manager to hold a list of parameters out of a computational graph. 

Computes the next state value (without gradient) to compute a target value. 

A softupdate class for target network update in Double DQN/DDPG. 

A hardupdate class for target network update in Double DQN/DDPG (by contrast with soft updates). 

Value function enumerator for custombuilt estimators. 

Default value function keyword argument generator. 