[go: nahoru, domu]

Skip to content
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

[BUG] Stable-Baselines3 integration not working #3174

Open
eltonjohnfanboy opened this issue Jun 24, 2024 · 4 comments
Open

[BUG] Stable-Baselines3 integration not working #3174

eltonjohnfanboy opened this issue Jun 24, 2024 · 4 comments
Assignees
Labels
area / integrations Issue area: integrations with other tools and libs help wanted Extra attention is needed phase / shipped Issue phase: shipped type / bug Issue type: something isn't working

Comments

@eltonjohnfanboy
Copy link

🐛 Bug

Hi there! I've been trying to use AIM to track metrics for Stable-Baselines3 project. I've been trying to use the AimCallback (https://aimstack.readthedocs.io/en/latest/quick_start/integrations.html) with Stable-Baselines3 to monitor logs and metrics as the model learns. However, I've run into an issue where the AIM run isn't tracking any metrics at all. Specifically, I'm interested in tracking metrics such as explained_variance and loss. Despite this, when I check the metrics tab, no metrics are displayed.

Screenshot 2024-06-24 at 13 15 48

To reproduce

import os
import gymnasium
from stable_baselines3 import PPO
from stable_baselines3.common.vec_env import DummyVecEnv
from stable_baselines3.common.evaluation import evaluate_policy
from aim.sb3 import AimCallback

environment_name = 'CartPole-v1'
env = gymnasium.make(environment_name)

env = DummyVecEnv([lambda: env])
model = PPO('MlpPolicy', env, verbose = 1)

model.learn(total_timesteps=10_000, callback=AimCallback(repo='.', experiment_name='example_experiment'))

Am I doing anything wrong? I'm following the steps indicated in AIM documentation, but no metrics are getting tracked (https://aimstack.readthedocs.io/en/latest/quick_start/integrations.html)

Thanks a lot!

@eltonjohnfanboy eltonjohnfanboy added help wanted Extra attention is needed type / bug Issue type: something isn't working labels Jun 24, 2024
@mihran113
Copy link
Contributor
mihran113 commented Jul 11, 2024

Hey @eltonjohnfanboy! Sorry for late response and thanks for opening the issue. There's an issue in our callback, I'll make sure to include the fix in the upcoming release. In the meantime you can use the following script as a workaround to track the metrics:

from typing import Any, Dict, Tuple, Union
import os
import gymnasium
from stable_baselines3 import PPO
from stable_baselines3.common.vec_env import DummyVecEnv
from stable_baselines3.common.evaluation import evaluate_policy
from stable_baselines3.common.logger import HumanOutputFormat, KVWriter, Logger

from aim import Run
import numpy as np


class AimOutputFormat(KVWriter):
    """
    Track key/value pairs into Aim run.
    """

    def __init__(
        self,
        aim_run
    ):
        self.aim_run = aim_run

    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 'aim' in excluded:
                continue

            if isinstance(value, np.ScalarType):
                if not isinstance(value, str):
                    tag, key = key.split('/')
                    if tag in ['train', 'valid']:
                        context = {'subset': tag}
                    else:
                        context = {'tag': tag}

                    self.aim_run.track(value, key, step=step, context=context)


run = Run()
loggers = Logger(
    folder=None,
    output_formats=[AimOutputFormat(run)],
)

environment_name = 'CartPole-v1'
env = gymnasium.make(environment_name)

env = DummyVecEnv([lambda: env])
model = PPO('MlpPolicy', env, verbose = 1)
model.set_logger(loggers)
model.learn(total_timesteps=10_000)

@mihran113 mihran113 self-assigned this Jul 11, 2024
@mihran113 mihran113 added phase / ready-to-go Issue phase: issues that are merged and will be included in the upcoming release area / integrations Issue area: integrations with other tools and libs labels Jul 11, 2024
@eltonjohnfanboy
Copy link
Author

Hi @mihran113, no worries at all. Thanks a lot for your work and the update! Looking forward to the upcoming release and will be using the script in the meantime. :))

@mihran113
Copy link
Contributor

Hey @eltonjohnfanboy! The new version of aim has been shipped (3.23.0) which includes the fix for this issue. Please let me know if everything works as expected so I can close the issue.

@mihran113 mihran113 added phase / shipped Issue phase: shipped and removed phase / ready-to-go Issue phase: issues that are merged and will be included in the upcoming release labels Jul 15, 2024
@eltonjohnfanboy
Copy link
Author

Hi @mihran113, sorry for the delayed response. Thanks for the update; it works correctly now! :))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area / integrations Issue area: integrations with other tools and libs help wanted Extra attention is needed phase / shipped Issue phase: shipped type / bug Issue type: something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants