-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature]: "Weights & Biases" integration #172
Comments
Hi, Firstly, thank you for your work on this library. I have a potential solution to have wandb working with sinergym and sb3. There isn't much to be changed from the example given for MLFlow here. I have added a minimal working example. import sys
import gym
import wandb
import numpy as np
from typing import Any, Dict, Tuple, Union
import sinergym
from sinergym.utils.wrappers import LoggerWrapper, NormalizeObservation
from sinergym.utils.constants import RANGES_5ZONE
from sinergym.utils.callbacks import LoggerCallback
from stable_baselines3 import PPO
from stable_baselines3.common.logger import HumanOutputFormat, Logger, KVWriter
# The only addition required.
class WandBOutputFormat(KVWriter):
"""
Dumps key/value pairs onto WandB.
"""
def write(
self,
key_values: Dict[str, Any],
key_excluded: Dict[str, Union[str, Tuple[str, ...]]],
step: int = 0,
) -> None:
for (key, value), (_, excluded) in zip(
sorted(key_values.items()), sorted(key_excluded.items())
):
if excluded is not None and "wandb" in excluded:
continue
if isinstance(value, np.ScalarType):
if not isinstance(value, str):
wandb.log({key: value}, step=step)
# Init wandb here.
wandb.init(
project="project_name",
entity="entity_name",
name="random_agent_" + wandb.util.generate_id(),
)
env = gym.make('Eplus-5Zone-hot-continuous-v1')
env = NormalizeObservation(env, ranges=RANGES_5ZONE)
env = LoggerWrapper(env)
loggers = Logger(
folder=None,
output_formats=[HumanOutputFormat(sys.stdout, max_length=90), WandBOutputFormat()],
)
model = PPO("MlpPolicy", env, verbose=2)
# Set custom logger
model.set_logger(loggers)
model.learn(total_timesteps=10_000, log_interval=1, callback=LoggerCallback(sinergym_logger=True)) Please do let me know if this helps with this issue. Let me know if I should make a PR or if I could help in another way. Thanks! |
Hi @kad99kev !
Then, I removed the callback and we can only register this params currently: Do you know why our Loggercallback is not compatible with wandb? Or did the code you put in work for you? |
Hi @AlejandroCN7, I got that error as well. It is because when creating the keys, large names are truncated which often results in duplicate keys. Hence, that error. To solve that I had changed it to I believe adding this would help solve the error: loggers = Logger(
folder=None,
output_formats=[HumanOutputFormat(sys.stdout, max_length=90), WandBOutputFormat()],
) Let me know if you already added this. I would be happy to contribute with a PR, let me know what API structure you have in mind and I can work on it. Thanks! Here is the link to my WandB project - https://wandb.ai/kad99kev/sinergym-tests?workspace=user-kad99kev |
Hi @kad99kev Yes, I had the In any case, looking at your project, I see that it works! It's great because I see that it doesn't need any big changes to make it work, thank you very much! Let's see if I can make it work for me too and plain how we can integrate it into the tool (maybe substitute tensorboard+mlflow by wandb in DRL_battery.py), once again thank you very much for everything! :) |
Hi @AlejandroCN7, That is indeed weird. I removed the Thank you! |
Hi @AlejandroCN7, Sorry for the delay in getting back with this. I have some code ready, I just need to run a few more experiments with different algorithms to make sure it is working correctly. When it is done, should I make a PR or would you like me to share the code another way? Link to WandB project - https://wandb.ai/kad99kev/sinergym_tests?workspace=user-kad99kev Thanks! |
Hi @kad99kev From what I can see, it looks very good. Thank you very much! If you agree, please finish testing everything with the algorithms and generate a PR directly, we'll check it out from there ;) |
Hi @AlejandroCN7, I have tried running the experiments for PPO, A2C, DDPG, SAC and TD3 algorithms on the |
Hi @kad99kev, |
Hi @AlejandroCN7, I did create a new script and integrated WandB there. I will make a PR now. It is a bit hacky as it is just a proof-of-concept that WandB works with Sinergym. Let me know what I could do to further improve it. |
Hi @kad99kev, I have added a comment in the PR code. If you need help, let me know :) |
Hi @AlejandroCN7, I don't see the comment. Could you please send the link to the comment? |
Sorry, my bad: https://github.com/ugr-sail/sinergym/pull/258/files#diff-4ba35a8ebc05be86e66cc95ff335266bcf9cb76b9d22cf98048d0c2949f8504c |
Hi @kad99kev, |
Hi @AlejandroCN7, thank you! I can see the comments now. I have replied to them. |
Feature 🚀
The integration of the "Weights & Biases" (wandb) library in Sinergym.
Motivation
This library may be specially interesting, as it integrates both the functionalities of TensorBoard and MLflow.
As explained by @jajimer:
Solution
Deeply study how wandb could be integrated, even considering if it can completely replace TensorBoard and MLflow or not.
Checklist
📝 Please, don't forget to include more labels besides
Feature request
if it is necessary.The text was updated successfully, but these errors were encountered: