AminHP / gym-anytrading

The most simple, flexible, and comprehensive OpenAI Gym trading environment (Approved by OpenAI Gym)
MIT License
2.1k stars 465 forks source link

ValueError: Cannot feed value of shape (1, 3926, 2) for Tensor 'deepq/input/Ob:0', which has shape '(?, 3927, 2)' #19

Closed sword134 closed 4 years ago

sword134 commented 4 years ago

Hello, with the below code I am presented with the error stated in the title. I want my window to be as big as my df frame.

custom_env = gym.make('stocks-v0', df = data, window_size = 3927, frame_bound = (1, 3927))

The error:

Traceback (most recent call last):
  File "d:\ReinforcementLearning\BaseLines\Trading\RL Trading.py", line 131, in <module>
    results = model.learn(int(100000))
  File "D:\Anaconda\envs\RL\lib\site-packages\stable_baselines\deepq\dqn.py", line 216, in learn
    action = self.act(np.array(obs)[None], update_eps=update_eps, **kwargs)[0]
  File "D:\Anaconda\envs\RL\lib\site-packages\stable_baselines\deepq\build_graph.py", line 159, in act
    return _act(obs, stochastic, update_eps)
  File "D:\Anaconda\envs\RL\lib\site-packages\stable_baselines\common\tf_util.py", line 287, in <lambda>
    return lambda *args, **kwargs: func(*args, **kwargs)[0]
  File "D:\Anaconda\envs\RL\lib\site-packages\stable_baselines\common\tf_util.py", line 330, in __call__
    results = sess.run(self.outputs_update, feed_dict=feed_dict, **kwargs)[:-1]
  File "D:\Anaconda\envs\RL\lib\site-packages\tensorflow\python\client\session.py", line 900, in run
    run_metadata_ptr)
  File "D:\Anaconda\envs\RL\lib\site-packages\tensorflow\python\client\session.py", line 1111, in _run
    str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (1, 3926, 2) for Tensor 'deepq/input/Ob:0', which has shape '(?, 3927, 2)'
AminHP commented 4 years ago

The window size should be one unit less than the dataframe size.

df_size = len(df)
window_size = df_size - 1
env = gym.make('stocks-v0', df=df, window_size=window_size, frame_bound=(window_size, df_size))
sword134 commented 4 years ago

Doing exactly that I get the following error:

Traceback (most recent call last):
  File "d:\ReinforcementLearning\BaseLines\Trading\RL Trading.py", line 225, in <module>
    results = model.learn(int(100000))
  File "D:\Anaconda\envs\RL\lib\site-packages\stable_baselines\deepq\dqn.py", line 219, in learn
    new_obs, rew, done, info = self.env.step(env_action)
  File "D:\Anaconda\envs\RL\lib\site-packages\gym_anytrading\envs\trading_env.py", line 76, in step
    step_reward = self._calculate_reward(action)
  File "d:\ReinforcementLearning\BaseLines\Trading\RL Trading.py", line 142, in _calculate_reward
    current_price = self.prices[self._current_tick]
IndexError: index 3926 is out of bounds for axis 0 with size 3926
AminHP commented 4 years ago

Sorry, the window size should be two units less than the dataframe size; One for the reset method and another for the step method. The code below works properly with stable_baselines.

df_size = len(df)
window_size = df_size - 2
env = gym.make('stocks-v0', df=df, window_size=window_size, frame_bound=(window_size, df_size))