当前位置: 首页>>代码示例>>Python>>正文


Python Buffer.sample方法代码示例

本文整理汇总了Python中buffer.Buffer.sample方法的典型用法代码示例。如果您正苦于以下问题:Python Buffer.sample方法的具体用法?Python Buffer.sample怎么用?Python Buffer.sample使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在buffer.Buffer的用法示例。


在下文中一共展示了Buffer.sample方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Q

# 需要导入模块: from buffer import Buffer [as 别名]
# 或者: from buffer.Buffer import sample [as 别名]
        #print "mu:", u, "action:", action
        #print "Q(mu):", Q(x, np.array([u])), "Q(action):", Q(x, np.array([action]))

        # take the action and record reward
        observation, reward, done, info = env.step(action)
        episode_reward += reward
        #print "reward:", reward
        #print "poststate:", observation

        # add experience to replay memory
        R.add(x[0], action, reward, observation, done)

        loss = 0
        # perform train_repeat Q-updates
        for k in range(args.train_repeat):
          preobs, actions, rewards, postobs, terminals = R.sample(args.batch_size)

          # Q-update
          v = V(postobs)
          y = rewards + args.gamma * np.squeeze(v)
          loss += model.train_on_batch([preobs, actions], y)

          # copy weights to target model, averaged by tau
          weights = model.get_weights()
          target_weights = target_model.get_weights()
          for i in range(len(weights)):
            target_weights[i] = args.tau * weights[i] + (1 - args.tau) * target_weights[i]
          target_model.set_weights(target_weights)
        #print "average loss:", loss/k

        if done:
开发者ID:tambetm,项目名称:gymexperiments,代码行数:33,代码来源:naf.py

示例2: xrange

# 需要导入模块: from buffer import Buffer [as 别名]
# 或者: from buffer.Buffer import sample [as 别名]
            action = env.action_space.sample()
        else:
            s = np.array([observation])
            q = model.predict_on_batch(s)
            #print "q:", q
            action = np.argmax(q[0])
        #print "action:", action

        prev_observation = observation
        observation, reward, done, info = env.step(action)
        episode_reward += reward
        #print "reward:", reward
        mem.add(prev_observation, np.array([action]), reward, observation, done)

        for k in xrange(args.train_repeat):
            prestates, actions, rewards, poststates, terminals = mem.sample(args.batch_size)

            qpre = model.predict_on_batch(prestates)
            qpost = target_model.predict_on_batch(poststates)
            for i in xrange(qpre.shape[0]):
                if terminals[i]:
                    qpre[i, actions[i]] = rewards[i]
                else:
                    qpre[i, actions[i]] = rewards[i] + args.gamma * np.amax(qpost[i])
            model.train_on_batch(prestates, qpre)

            weights = model.get_weights()
            target_weights = target_model.get_weights()
            for i in xrange(len(weights)):
                target_weights[i] = args.tau * weights[i] + (1 - args.tau) * target_weights[i]
            target_model.set_weights(target_weights)
开发者ID:tambetm,项目名称:gymexperiments,代码行数:33,代码来源:duel.py


注:本文中的buffer.Buffer.sample方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。