DLR-RM / stable-baselines3

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

Discrete.__init__() got an unexpected keyword argument 'start' #1509

Closed fede72bari closed 1 year ago

fede72bari commented 1 year ago

🐛 Bug

I am creating a custom environment, but from my understanding, the problem is due to conflicts with gym/gymnasium releases. This is the context:

# RL ENVS
import gymnasium as gym
from gym import spaces
print(gymnasium.__version__)
0.26.3
!pip install "stable-baselines3"
!pip install sb3_contrib
batch_size = 512
env = myEnv(prices = train_prices, indicators = train_indicators, sampling_window = batch_size, stop_loss = 20)

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[13], line 2
      1 batch_size = 512
----> 2 env = QTrading(prices = train_prices, indicators = train_indicators, sampling_window = batch_size, stop_loss = 20)

Cell In[12], line 44, in QTrading.__init__(self, indicators, prices, currency, initial_capital, actions_commission, stop_loss, margin_call, time_penalty, in_trade_premium, max_total_loss, max_open_positions, sampling_window)
     41 self.action_space_lowest = -(self.max_open_positions*2)
     43 self.obs_dim = self.indicators.shape[0] + 12
---> 44 self.action_space = spaces.Discrete(self.actions_space_dim, start = self.action_space_lowest)
     45 self.observation_space = spaces.Box(low=-10e7, high=10e7, shape=(self.obs_dim,), dtype=np.float32)
     48 self.reset()

TypeError: Discrete.__init__() got an unexpected keyword argument 'start'
------------------------------------------------------------

as if it was used a very old Gym version in which the spaces.Discret had not the attribute start (that in my case is absolutely needed). Printing again the Gym version it seems equal to the first working case

print(gym.__version__)
0.26.3

making me think, from my deep ignorance, that there could be a conflict between two existing versions of Gym/Gymnasium, the latest existing by default in the Kaggle context and explicitly imported by my code; he first somehow imported by SB libraries.

I have to note that during the pip installation of the two libraries, some errors occur in the gym wheel installation and I copy a part of them in the "Relevant log output / Error message" section.

To Reproduce

It should be very simple

Relevant log output / Error message

!pip install "stable-baselines3"

Here a part of the long log with the first part and last part describing the installation troubles

Building wheels for collected packages: gym
  Building wheel for gym (setup.py) ... error
  error: subprocess-exited-with-error

  × python setup.py bdist_wheel did not run successfully.
  │ exit code: 1
  ╰─> [476 lines of output]
      running bdist_wheel
      running build
      running build_py
      creating build
      creating build/lib
      creating build/lib/gym
      copying gym/__init__.py -> build/lib/gym
      copying gym/version.py -> build/lib/gym
      copying gym/logger.py -> build/lib/gym
      copying gym/error.py -> build/lib/gym
      copying gym/core.py -> build/lib/gym

[.........]

for key, value in generate_requirements({extra: reqs}):
        File "/opt/conda/lib/python3.10/site-packages/wheel/metadata.py", line 138, in generate_requirements
          for new_req in convert_requirements(depends):
        File "/opt/conda/lib/python3.10/site-packages/wheel/metadata.py", line 103, in convert_requirements
          parsed_requirement = Requirement(req)
        File "/opt/conda/lib/python3.10/site-packages/wheel/vendored/packaging/requirements.py", line 37, in __init__
          raise InvalidRequirement(str(e)) from e
      wheel.vendored.packaging.requirements.InvalidRequirement: Expected end or semicolon (after version specifier)
          opencv-python>=3.
                       ~~~^
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for gym
  Running setup.py clean for gym
Failed to build gym
Installing collected packages: importlib-metadata, gym, stable-baselines3
  Attempting uninstall: importlib-metadata
    Found existing installation: importlib-metadata 6.0.1
    Uninstalling importlib-metadata-6.0.1:
      Successfully uninstalled importlib-metadata-6.0.1
  Attempting uninstall: gym
    Found existing installation: gym 0.26.2
    Uninstalling gym-0.26.2:
      Successfully uninstalled gym-0.26.2
  Running setup.py install for gym ... done
  DEPRECATION: gym was installed using the legacy 'setup.py install' method, because a wheel could not be built for it. pip 23.1 will enforce this behaviour change. A possible replacement is to fix the wheel build issue reported above. Discussion can be found at https://github.com/pypa/pip/issues/8368 class="ansi-yellow-fg">
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
dask-cudf 23.4.0 requires cupy-cuda11x<12.0.0a0,>=9.5.0, which is not installed.
cuml 23.4.0 requires cupy-cuda11x<12.0.0a0,>=9.5.0, which is not installed.
opentelemetry-api 1.17.0 requires importlib-metadata~=6.0.0, but you have importlib-metadata 4.13.0 which is incompatible.
dask-cudf 23.4.0 requires dask==2023.3.2, but you have dask 2023.4.0 which is incompatible.
dask-cuda 23.4.0 requires dask==2023.3.2, but you have dask 2023.4.0 which is incompatible.
cuml 23.4.0 requires dask==2023.3.2, but you have dask 2023.4.0 which is incompatible.
cmudict 1.0.13 requires importlib-metadata<6.0.0,>=5.1.0, but you have importlib-metadata 4.13.0 which is incompatible.
Successfully installed gym-0.21.0 importlib-metadata-5.2.0 stable-baselines3-1.8.0
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv class="ansi-yellow-fg">

same for 

!pip install "stable-baselines3"

[........]
m sympy->torch>=1.11->stable-baselines3) (1.3.0)
Installing collected packages: importlib-metadata
  Attempting uninstall: importlib-metadata
    Found existing installation: importlib-metadata 5.2.0
    Uninstalling importlib-metadata-5.2.0:
      Successfully uninstalled importlib-metadata-5.2.0
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
dask-cudf 23.4.0 requires cupy-cuda11x<12.0.0a0,>=9.5.0, which is not installed.
cuml 23.4.0 requires cupy-cuda11x<12.0.0a0,>=9.5.0, which is not installed.
opentelemetry-api 1.17.0 requires importlib-metadata~=6.0.0, but you have importlib-metadata 4.13.0 which is incompatible.
dask-cudf 23.4.0 requires dask==2023.3.2, but you have dask 2023.4.0 which is incompatible.
dask-cuda 23.4.0 requires dask==2023.3.2, but you have dask 2023.4.0 which is incompatible.
cuml 23.4.0 requires dask==2023.3.2, but you have dask 2023.4.0 which is incompatible.
cmudict 1.0.13 requires importlib-metadata<6.0.0,>=5.1.0, but you have importlib-metadata 4.13.0 which is incompatible.
Successfully installed importlib-metadata-4.13.0
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv class="ansi-yellow-fg">

System Info

As already described in a new Kaggle notebook

!pip install "stable-baselines3"
!pip install sb3_contrib

besides

# ---------------------------------------------------------------------------
#                              Libraries Import
# ---------------------------------------------------------------------------

# Files Management
import gzip
import joblib

# Stocks Indicators
#import talib

# Time Management
import datetime
from datetime import datetime, timedelta
import pytz

# Math and Sci
import numpy as np
import math
from scipy.signal import argrelextrema

# Reporting
from plotly.figure_factory import create_candlestick
from plotly.subplots import make_subplots
import plotly.graph_objects as go
import matplotlib.pyplot as plt
import matplotlib    
from matplotlib.pyplot import plot  
from matplotlib.pylab import rcParams
from xgboost import plot_tree
import seaborn as sns

# Data Management
import pandas as pd
from sklearn.model_selection import train_test_split
from statsmodels.tsa.stattools import adfuller
from imblearn.over_sampling import SMOTE
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import RobustScaler

# RL ENVS
import gymnasium as gym
from gym import spaces

# Machine Learning
import torch as th
from stable_baselines3.common.monitor import Monitor
from stable_baselines3.common.results_plotter import load_results, ts2xy
from stable_baselines3 import PPO
from stable_baselines3.common import results_plotter
from stable_baselines3.common.vec_env import DummyVecEnv, SubprocVecEnv, VecNormalize,VecMonitor
from stable_baselines3.common.env_util import make_vec_env
from stable_baselines3.common.callbacks import ProgressBarCallback
from sb3_contrib import RecurrentPPO
from stable_baselines3.common.evaluation import evaluate_policy

Checklist

araffin commented 1 year ago

I have checked that there is no similar issue in the repo

Try harder next time =)

Duplicate of #1295 and #913, the env checker should also warn you.

You also need SB3 2.x version for gymnasium support (see doc to install master version and related issues)