automl / DAC4SGD

5 stars 3 forks source link

Running the base example loop raise an exception for n_instances #3

Open Deathn0t opened 2 years ago

Deathn0t commented 2 years ago

Hello,

I cloned the repos of the competition and tried running the base example loop from the readme which is:

import gym
import numpy as np
import sgd_env
from examples.ac_for_dac.schedulers import CosineAnnealingLRPolicy

env = gym.make("sgd-v0", n_instances=np.inf)
env.seed(123)
obs = env.reset()
done = False

lr = 0.01
policy = CosineAnnealingLRPolicy(lr)
policy.reset(env.current_instance)

while not done:
    lr = policy.act(obs)
    obs, reward, done, info = env.step(lr)
    print(done)

but the following exception is raised:

$ python simple_loop.py
/Users/romainegele/miniforge3/envs/dh-env-test/lib/python3.9/site-packages/torchvision/transforms/functional_pil.py:228: DeprecationWarning: BILINEAR is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.BILINEAR instead.
  interpolation: int = Image.BILINEAR,
/Users/romainegele/miniforge3/envs/dh-env-test/lib/python3.9/site-packages/torchvision/transforms/functional_pil.py:295: DeprecationWarning: NEAREST is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.NEAREST or Dither.NONE instead.
  interpolation: int = Image.NEAREST,
/Users/romainegele/miniforge3/envs/dh-env-test/lib/python3.9/site-packages/torchvision/transforms/functional_pil.py:328: DeprecationWarning: BICUBIC is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.BICUBIC instead.
  interpolation: int = Image.BICUBIC,
/Users/romainegele/miniforge3/envs/dh-env-test/lib/python3.9/site-packages/gym/utils/passive_env_checker.py:42: UserWarning: WARN: Agent's maximum observation space value is infinity. This is probably too high.
  logger.warn(
Traceback (most recent call last):
  File "/Users/romainegele/Documents/Competitions/dac4automlcomp/DAC4SGD/examples/dh_for_dac/simple_loop.py", line 6, in <module>
    env = gym.make("sgd-v0", n_instances=np.inf)
  File "/Users/romainegele/miniforge3/envs/dh-env-test/lib/python3.9/site-packages/gym/envs/registration.py", line 601, in make
    env = PassiveEnvChecker(env)
  File "/Users/romainegele/miniforge3/envs/dh-env-test/lib/python3.9/site-packages/gym/wrappers/env_checker.py", line 26, in __init__
    assert hasattr(
  File "/Users/romainegele/Documents/Competitions/dac4automlcomp/DAC4SGD/sgd_env/envs/sgd_env.py", line 57, in observation_space
    raise ValueError(
ValueError: Observation space size changes for every instance. It is set after every reset. Use a provided wrapper or handle it manually. If batch size is fixed for every instance, observation space will stay fixed.
goktug97 commented 2 years ago

Hello,

In a new gym version a checker is implemented which causes this error while trying to access to observation_space which changes in each reset (Because the loss feature is per data point and its size depends on the batch size which is an instance variable). You can bypass this error if you don't relly on observation_space variable by using env = gym.make("sgd-v0", disable_env_checker=True). If your optimizer needs the observation_space variable, you can implement an ObservationWrapper to transform the observation to a fixed value (for example mean of loss feature) and set observation_space accordingly. You will still need to create the environment with disable_env_checker=True

Deathn0t commented 2 years ago

Thank you @goktug97 I will try this.