我正在使用稳定的基线3来训练一个代理来玩连接4游戏。当一个经纪人作为第二个玩家开始一场比赛的时候,我会考虑到这个问题。
self.env = self.ks_env.train([opponent, None])
当我试图运行代码时,我会得到以下错误:
invalid multinomial distribution (encountering probability entry < 0)
/opt/conda/lib/python3.7/site-packages/torch/distributions/categorical.py in sample(self, sample_shape)
samples_2d = torch.multinomial(probs_2d, sample_shape.numel(), True).T
但是,当代理是第一个玩家时,没有问题:
self.env = self.ks_env.train([None, opponent])
我认为问题与毕火炬图书馆有关。我的问题是如何解决这个问题?
发布于 2020-11-03 06:17:32
在检查您提供的代码后,问题似乎不是来自哪个代理启动游戏,而是来自于在游戏完成后不重新启动环境。
我刚刚更改了您的step功能,如下所示:
def step(self, action):
# Check if agent's move is valid
is_valid = (self.obs['board'][int(action)] == 0)
if is_valid: # Play the move
self.obs, old_reward, done, _ = self.env.step(int(action))
reward = self.change_reward(old_reward, done)
else: # End the game and penalize agent
reward, done, _ = -10, True, {}
if done:
self.reset()
return board_flip(self.obs.mark,
np.array(self.obs['board']).reshape(1, self.rows, self.columns) / 2),
reward, done, _
有了这个功能,模型就可以训练了,您可以检查它是否能像预期的那样与下面的片段一起工作:
done = True
for step in range(500):
if done:
state = env.reset()
state, reward, done, info = env.step(env.action_space.sample())
print(reward)
链接到我的你的笔记本版本
https://stackoverflow.com/questions/64441708
复制相似问题