DLR-RM / stable-baselines3

PyTorch version of Stable Baselines, reliable implementations of reinforcement learning algorithms.
https://stable-baselines3.readthedocs.io
MIT License
9.05k stars 1.7k forks source link

[Feature Request] add_scalars to wirte func in TensorBoardOutputFormat in logger #1994

Open shimonShouei opened 2 months ago

shimonShouei commented 2 months ago

🚀 Feature

Add support for multi-variable logging in the logger module using add_scalars. Enable the logger to record and visualize multiple related scalar values simultaneously using a single record call. For example:

self.logger.record('run_14h', {'xsinx': i*np.sin(i/r),
                               'xcosx': i*np.cos(i/r),
                               'tanx': np.tan(i/r)}, i)

Motivation

Currently, the logger module only supports recording single scalar values per call. When working with complex experiments where multiple related metrics need to be tracked and compared, the lack of multi-variable logging can be limiting. Allowing multi-variable logging would provide a clearer, more organized way to monitor and visualize these metrics, enhancing the experiment analysis process.

Pitch

Introduce an option in the logger module to use the add_scalars function, enabling the recording of multiple related scalar values with a single record call. This feature will simplify the process of logging and visualizing multiple metrics in TensorBoard or similar tools, improving the monitoring and analysis of DRL experiments. To implement this, add the following code to the writer method in the TensorBoardOutputFormat class within the logger module:

if isinstance(value, dict):
    self.writer.add_scalars(key, value, step)

Alternatives

Getting through the SummaryWriter to the writer

Additional context

Including this feature would help users who track multiple metrics to maintain cleaner and more organized logs. It would also align the logger module with the capabilities of TensorBoard's add_scalars function, offering a more consistent user experience.

Checklist

araffin commented 1 month ago

Hello,

what you are proposing only works for tensorboard logger, right? what do you propose as behavior for the other loggers?

Getting through the SummaryWriter to the writer

Sounds like a simple enough alternative? (3 lines change: https://stable-baselines3.readthedocs.io/en/master/guide/tensorboard.html#directly-accessing-the-summary-writer)

shimonShouei commented 1 month ago

What you are proposing only works for tensorboard logger, right? what do you propose as behavior for the other loggers?

It works for me in stdout too. I don't use the others.

Sounds like a simple enough alternative?

The Record func is very convenient and powerful and I would rather use it than involve the writer. though, even that solution isn't good enough for my problem because it doesn't dump them (the scalars) under the same run.