boyu-ai/Hands-on-RL

21章MADDPG代码问题,存在维度不匹配

Opened this issue · 2 comments

{
"name": "ValueError",
"message": "setting an array element with a sequence. The requested array has an inhomogeneous shape after 2 dimensions. The detected shape was (1024, 3) + inhomogeneous part.",
"stack": "---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/Users/corneliusdeng/UESTC/Dive Into RL/MADDPG.ipynb Cell 7 line 2
26 total_step += 1
27 if replay_buffer.size(
28 ) >= minimal_size and total_step % update_interval == 0:
---> 29 sample = replay_buffer.sample(batch_size)
31 def stack_array(x):
32 rearranged = [[sub_x[i] for sub_x in x]
33 for i in range(len(x[0]))]

File ~/UESTC/Dive Into RL/rl_utils.py:17, in ReplayBuffer.sample(self, batch_size)
15 transitions = random.sample(self.buffer, batch_size)
16 state, action, reward, next_state, done = zip(*transitions)
---> 17 return np.array(state), action, reward, np.array(next_state), done

ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 2 dimensions. The detected shape was (1024, 3) + inhomogeneous part."
}

image jupyter has noted, you need to add "dtype=object" into "return np.array(state), action, reward, np.array(next_state), done" like "return np.array(state, dtype=object), action, reward, np.array(next_state, dtype=object), done"

另外,注意版本问题,若numpy版本较新,仍无法解决问题,需要回退版本pip install numpy==1.21.6ref