【问题标题】:Pytorch - RuntimeError: invalid multinomial distribution (encountering probability entry < 0)Pytorch - RuntimeError:无效的多项分布(遇到概率条目 < 0)
【发布时间】:2021-02-03 01:51:59
【问题描述】:

我正在使用 Stable Baselines 3 来训练代理玩 Connect 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])

我认为问题与 Pytorch 库有关。我的问题是如何解决这个问题?

【问题讨论】:

    标签: pytorch reinforcement-learning stable-baselines


    【解决方案1】:

    检查您提供的代码后,问题似乎不是来自什么代理启动游戏,而是游戏完成后没有重新启动环境。

    我刚刚改变了你的步骤功能,如图所示:

    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, _
    

    这样,模型就可以训练了,您可以使用以下 sn-p 检查它是否按预期工作:

    done = True
    for step in range(500):
        if done:
            state = env.reset()
        state, reward, done, info = env.step(env.action_space.sample())
        print(reward)
    

    链接到我的your notebook 版本

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-27
      • 2014-08-14
      • 2021-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多