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
ValueError: could not broadcast input array from shape (2) into shape (7,3,5) #242
Comments
Hello again, as written in the previous topic (issue #239 ):
EDIT: Please note the word in uppercase ;) |
Thanks.
I tried reshaping the observation (in both step and reset methods) as obs = obs.reshape(-1)
Is this the correct way?
Still getting
ValueError: could not broadcast input array from shape (2) into shape (7,1,5)
From: Antonin RAFFIN [mailto:notifications@github.com]
Sent: Sunday, 24 March 2019 11:24
To: hill-a/stable-baselines
Cc: hn2; Author
Subject: Re: [hill-a/stable-baselines] ValueError: could not broadcast input array from shape (2) into shape (7,3,5) (#242)
Hello again,
as written in the previous topic:
A simple solution consists in reshaping your observation to a 1D vector, so you can use MlpPolicy on it.
Otherwise, if you want to keep your observation with that exact shape, then you have to define a custom policy, as the default CnnPolicy was made for images (of shape 64x64xn) and normalization (dividing by 255.) is automatically applied in this case.
Again, do not forget to add more context for your problem next time by filling COMPLETELY the issue template ;)
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub <#242 (comment)> , or mute the thread <https://github.com/notifications/unsubscribe-auth/AHM1Sgj-6V7dXrBLOPj1jcKwUY6TfdRqks5vZ0Q2gaJpZM4cFXCI> . <https://github.com/notifications/beacon/AHM1SghaqHFHNthOh1b3MOm2qB1JJcY1ks5vZ0Q2gaJpZM4cFXCI.gif>
…---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
|
Still not able to solve this.
I tried to use the flatten() method to return flattened observations (return obs.flatten(), info)
I still get:
model.learn(total_timesteps=10000)
File "/home/ubuntu/anaconda2/lib/python3.6/site-packages/stable_baselines/ppo1/pposgd_simple.py", line 230, in learn
seg = seg_gen.__next__()
File "/home/ubuntu/anaconda2/lib/python3.6/site-packages/stable_baselines/trpo_mpi/utils.py", line 35, in traj_segment_generator
observation = env.reset()
File "/home/ubuntu/anaconda2/lib/python3.6/site-packages/stable_baselines/common/base_class.py", line 523, in reset
return self.venv.reset()[0]
File "/home/ubuntu/anaconda2/lib/python3.6/site-packages/stable_baselines/common/vec_env/dummy_vec_env.py", line 57, in reset
self._save_obs(env_idx, obs)
File "/home/ubuntu/anaconda2/lib/python3.6/site-packages/stable_baselines/common/vec_env/dummy_vec_env.py", line 75, in _save_obs
self.buf_obs[key][env_idx] = obs
ValueError: could not broadcast input array from shape (2) into shape (7,1,5)
From: Antonin RAFFIN [mailto:notifications@github.com]
Sent: Sunday, 24 March 2019 11:24
To: hill-a/stable-baselines
Cc: hn2; Author
Subject: Re: [hill-a/stable-baselines] ValueError: could not broadcast input array from shape (2) into shape (7,3,5) (#242)
Hello again,
as written in the previous topic:
A simple solution consists in reshaping your observation to a 1D vector, so you can use MlpPolicy on it.
Otherwise, if you want to keep your observation with that exact shape, then you have to define a custom policy, as the default CnnPolicy was made for images (of shape 64x64xn) and normalization (dividing by 255.) is automatically applied in this case.
Again, do not forget to add more context for your problem next time by filling COMPLETELY the issue template ;)
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub <#242 (comment)> , or mute the thread <https://github.com/notifications/unsubscribe-auth/AHM1Sgj-6V7dXrBLOPj1jcKwUY6TfdRqks5vZ0Q2gaJpZM4cFXCI> . <https://github.com/notifications/beacon/AHM1SghaqHFHNthOh1b3MOm2qB1JJcY1ks5vZ0Q2gaJpZM4cFXCI.gif>
…---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
|
I tried CnnPolicy instead of MlpPolicy and got this error: |
For the fourth and last time, please fill in the issue template COMPLETELY otherwise we cannot help you, and i will have to close this issue. |
Hello, |
It is consistent. It should be 3d: first dimension is the list of assets, second dimension are historical prices (defined by window_length = 1..50, third dimension is open, high, low, close, volume. Where do you see that I am feeding the agent with 1D vector? |
sooner in the discussion...
|
As said before, I tried that but it didn't work (with reshape, flatten, ravel). |
This is a Numpy error, cause by The solution as such is to drop to a one dimensional vector: self.observation_space = gym.spaces.Box(low=-np.inf, high=np.inf, shape=(len(instruments) * window_length * history.shape[-1],), dtype=np.float32) NOTE the multiplication in If you need more help, we are going to need you entire environment code, as I am incapable of deducing Numpy broadcasting errors without knowing how the numpy array was formed AND how it was used. |
Ok I changed it as suggested I now get: |
I have no idea what your environment is sending down to the DummyVecEnv, as such I'm not capable of helping you without the original code, for me you are simply sending the wrong information in Other than that, this does not seem like a stable baselines issues, rather an implementation issue, make sure you followed the guide for custom environment https://stable-baselines.readthedocs.io/en/master/guide/custom_env.html and that your |
I agree with @hill-a , this is apparently not an error due to stable-baselines but to a custom environment. I think we gave you already enough information to help you debug your environment. We do not do personal debugging and focus on SB issues, so I will close this one. |
It looks like it did flattened the array as it now has dimension 35 |
You may have the same issue as here: #214 |
Here is the code of my custom env reset:
|
you are returning a tuple in the reset method. It should be only an observation, @hill-a was right. |
I finally succeeded in running the model. Do you have documentation how to interpret the output?
|
There is no coordination between the logging for the methods unfortunatly. I'm guessing from the losses in the log that you are using PPO1 here, so here are the descriptions:
EDIT: if you wish to measure the performance of the method, please have a glance at tensorboard and the example code given in the doc for validating the method after learning. |
I am using the tensorboard integration. Is it possible to print additional info returning from the step on the tensorboard web page? |
When I changed to: self.action_space = gym.spaces.Box(-1., 1., shape=(len(self.src.asset_names) + 1,), dtype=np.float32) and I do: def step(self, action):
I always get: [nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan .. It is a problem with my env or with the agent? |
Another error that I get on windows when trying to import stable_baseline: in logger.py line 56: |
No, that would be quite difficult to do. It would be better to use the validation part of the example code given in the doc: obs = env.reset()
for i in range(1000):
action, _states = model.predict(obs)
obs, rewards, dones, info = env.step(action)
print(info)
env.render()
As discussed earlier, this will not work, observation space needs to be one dimensional: self.observation_space = gym.spaces.Box(low=-np.inf, high=np.inf, shape=(len(instruments) * window_length * history.shape[-1],), dtype=np.float32)
Next time, tell me what you are using, it helps avoid guessing. So, I'm assuming you are using Pycharm, since it is a |
I am using VS code. |
Ok, this is what stable-baselines is crashing on, it seems that for some reason the object does not have a read attribute, not sure why though as this code should work and dates from the initial commit of VS... Again, comment that assertation line 56 in logger.py. it is only there as a safeguard for those who are playing with the logger, which I am assuming you are not. |
What is the solution then? move to another ide? |
For the last time
In stable-baselines folder there is the file logger.py, comment line 56. |
Describe the bug
I am trying to run stable_baseline alogs such as ppo1, ddpg and get this error:
ValueError: could not broadcast input array from shape (2) into shape (7,1,5)
Code example
action will be the portfolio weights from 0 to 1 for each asset
I tried using obs.reshape(-1), obs.flatten(), obs.ravel() nothing works. Also tried CnnPolicy onstead of MlpPolicy and got:
ValueError: Negative dimension size caused by subtracting 8 from 7 for 'model/c1/Conv2D' (op: 'Conv2D') with input shapes: [?,7,1,5], [8,8,5,32].
System Info
Describe the characteristic of your environment:
*library was installed using:
git clone https://github.com/hill-a/stable-baselines.git
cd stable-baselines
pip install -e .
Additional context
Add any other context about the problem here.
tensorflow==1.13.1 cpu
The text was updated successfully, but these errors were encountered: