wassname / rl-portfolio-management

Attempting to replicate "A Deep Reinforcement Learning Framework for the Financial Portfolio Management Problem" https://arxiv.org/abs/1706.10059 (and an openai gym environment)
544 stars 179 forks source link

Env creating error - 'int' object is not iterable #16

Closed guidesc closed 6 years ago

guidesc commented 6 years ago

Hi Wassname,

Thanks for open source your code. However, when I tried to run the following code to create an env.

import rl_portfolio_management.environments  # this registers them

env = gym.envs.spec('CryptoPortfolioEIIE-v0').make()
print("CryptoPortfolioEIIE has an history shape suitable for an EIIE model (see https://arxiv.org/abs/1706.10059)")
observation = env.reset()
print("shape =", observation["history"].shape)
# shape = (5, 50, 3)```

I encountered the following error: 
```---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-10-f00e24a5d49e> in <module>()
      2 import rl_portfolio_management.environments  # this registers them
      3 
----> 4 env = gym.envs.spec('CryptoPortfolioEIIE-v0').make()
      5 print("CryptoPortfolioEIIE has an history shape suitable for an EIIE model (see https://arxiv.org/abs/1706.10059)")
      6 observation = env.reset()

D:\Users\ThinkPad\Anaconda3\lib\site-packages\gym\envs\registration.py in make(self)
     84         else:
     85             cls = load(self._entry_point)
---> 86             env = cls(**self._kwargs)
     87 
     88         # Make the enviroment aware of which spec it came from.

F:\DSBA\T2\Advanced ML\Final\rl-portfolio-management-master\rl-portfolio-management-master\rl_portfolio_management\environments\portfolio.py in __init__(self, df, steps, trading_cost, time_cost, window_length, augment, output_mode, log_dir, scale, scale_extra_cols, random_reset)
    266         nb_assets = len(self.src.asset_names)
    267         self.action_space = gym.spaces.Box(
--> 268             0.0, 1.0, shape=nb_assets + 1)
    269 
    270 

D:\Users\ThinkPad\Anaconda3\lib\site-packages\gym\spaces\box.py in __init__(self, low, high, shape, dtype)
     32         self.low = low.astype(dtype)
     33         self.high = high.astype(dtype)
---> 34         gym.Space.__init__(self, shape, dtype)
     35 
     36     def sample(self):

D:\Users\ThinkPad\Anaconda3\lib\site-packages\gym\core.py in __init__(self, shape, dtype)
    200     """
    201     def __init__(self, shape=None, dtype=None):
--> 202         self.shape = None if shape is None else tuple(shape)
    203         self.dtype = None if dtype is None else np.dtype(dtype)
    204 

TypeError: 'int' object is not iterable

I also tried gym.spaces.Box(0.0, 1.0, shape=5), it has the same error. While then I tried gym.spaces.Box(0.0, 1.0, shape=(5,)), it works well.

However, I tried to update your code with self.action_space = gym.spaces.Box(0.0, 1.0, shape=(nb_assets + 1, )), it still did not work well.

Could you please help me on that?

Thx in advance.

wassname commented 6 years ago

It could be a version difference, could you please run conda list and compare with requirements.txt? Or you can run gym.__version__ from python and compare that.

guidesc commented 6 years ago

Thanks, it is a version issue. My gym version is 0.10.4. Then I tried to create a virtualenv and pip install gym==0.9.3, it works well.

Thank you. I'm really curious what has been updating in 0.10.4 version of gym.

wassname commented 6 years ago

No problems, glad it works now

Looks like breaking changes were probobly in 0.9.6. They probably did it because of the robotics environments release that happened around the same time (I guess).

yutingsz commented 5 years ago

my gym version is 0.10.5. Get the same error. Any update to the code apart from the virtualenv? Thanks