當前位置: 首頁>>代碼示例>>Python>>正文


Python monitoring.VideoRecorder方法代碼示例

本文整理匯總了Python中gym.monitoring.VideoRecorder方法的典型用法代碼示例。如果您正苦於以下問題:Python monitoring.VideoRecorder方法的具體用法?Python monitoring.VideoRecorder怎麽用?Python monitoring.VideoRecorder使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在gym.monitoring的用法示例。


在下文中一共展示了monitoring.VideoRecorder方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: play

# 需要導入模塊: from gym import monitoring [as 別名]
# 或者: from gym.monitoring import VideoRecorder [as 別名]
def play(env, act, stochastic, video_path):
    num_episodes = 0
    video_recorder = None
    video_recorder = VideoRecorder(
        env, video_path, enabled=video_path is not None)
    obs = env.reset()
    while True:
        env.unwrapped.render()
        video_recorder.capture_frame()
        action = act(np.array(obs)[None], stochastic=stochastic)[0]
        obs, rew, done, info = env.step(action)
        if done:
            obs = env.reset()
        if len(info["rewards"]) > num_episodes:
            if len(info["rewards"]) == 1 and video_recorder.enabled:
                # save video of first episode
                print("Saved video.")
                video_recorder.close()
                video_recorder.enabled = False
            print(info["rewards"][-1])
            num_episodes = len(info["rewards"]) 
開發者ID:AdamStelmaszczyk,項目名稱:learning2run,代碼行數:23,代碼來源:enjoy.py

示例2: play

# 需要導入模塊: from gym import monitoring [as 別名]
# 或者: from gym.monitoring import VideoRecorder [as 別名]
def play(env, act, stochastic, video_path):
    num_episodes = 0
    video_recorder = None
    video_recorder = VideoRecorder(
        env, video_path, enabled=video_path is not None)
    obs = env.reset()
    if args.visual:
        action_names = distdeepq.actions_from_env(env)
        plot_machine = distdeepq.PlotMachine(dist_params, env.action_space.n, action_names)
    while True:
        env.unwrapped.render()
        video_recorder.capture_frame()
        action = act(np.array(obs)[None], stochastic=stochastic)[0]
        obs, rew, done, info = env.step(action)
        if args.visual:
            plot_machine.plot_distribution(np.array(obs)[None])

        if done:
            obs = env.reset()
        if len(info["rewards"]) > num_episodes:
            if len(info["rewards"]) == 1 and video_recorder.enabled:
                # save video of first episode
                print("Saved video.")
                video_recorder.close()
                video_recorder.enabled = False
            print(info["rewards"][-1])
            num_episodes = len(info["rewards"]) 
開發者ID:Silvicek,項目名稱:distributional-dqn,代碼行數:29,代碼來源:enjoy_atari.py

示例3: play

# 需要導入模塊: from gym import monitoring [as 別名]
# 或者: from gym.monitoring import VideoRecorder [as 別名]
def play(env, act, craft_adv_obs, craft_adv_obs2, stochastic, video_path,
         attack, m_target, m_adv):
    num_episodes = 0
    num_moves = 0
    num_transfer = 0

    video_recorder = None
    video_recorder = VideoRecorder(
        env, video_path, enabled=video_path is not None)
    obs = env.reset()
    while True:
        env.unwrapped.render()
        video_recorder.capture_frame()

        # V: Attack #
        if attack is not None:
            # Craft adv. examples
            with m_adv.get_session().as_default():
                adv_obs = \
                    craft_adv_obs(np.array(obs)[None],
                                  stochastic_adv=stochastic)[0]
            with m_target.get_session().as_default():
                action = act(np.array(adv_obs)[None],
                             stochastic=stochastic)[0]
                action2 = act(np.array(obs)[None], stochastic=stochastic)[0]
                num_moves += 1
                if action != action2:
                    num_transfer += 1
        else:
            # Normal
            action = act(np.array(obs)[None], stochastic=stochastic)[0]

        obs, rew, done, info = env.step(action)
        if done:
            obs = env.reset()

        if len(info["rewards"]) > num_episodes:
            if len(info["rewards"]) == 1 and video_recorder.enabled:
                # save video of first episode
                print("Saved video.")
                video_recorder.close()
                video_recorder.enabled = False
            print('Reward: ' + str(info["rewards"][-1]))
            num_episodes = len(info["rewards"])
            print('Episode: ' + str(num_episodes))
            success = float(num_transfer / num_moves) * 100.0
            print("Percentage of successful attacks: " + str(success))
            num_moves = 0
            num_transfer = 0 
開發者ID:StephanZheng,項目名稱:neural-fingerprinting,代碼行數:51,代碼來源:enjoy-adv.py

示例4: play

# 需要導入模塊: from gym import monitoring [as 別名]
# 或者: from gym.monitoring import VideoRecorder [as 別名]
def play(env, act, craft_adv_obs, stochastic, video_path, game_name, attack, defense):
    if defense == 'foresight':
        vf, game_screen_mean = load_visual_foresight(game_name)
        pred_obs = deque(maxlen=4)

    num_episodes = 0
    video_recorder = None
    video_recorder = VideoRecorder(
        env, video_path, enabled=video_path is not None)

    t = 0
    obs = env.reset()
    while True:
        #env.unwrapped.render()
        video_recorder.capture_frame()

	# Attack
        if craft_adv_obs != None:
            # Craft adv. examples
            adv_obs = craft_adv_obs(np.array(obs)[None], stochastic=stochastic)[0]
            action = act(np.array(adv_obs)[None], stochastic=stochastic)[0]
        else:
            # Normal
            action = act(np.array(obs)[None], stochastic=stochastic)[0]

	# Defense
        if t > 4 and defense == 'foresight':
            pred_obs.append(
                foresee(U.get_session(), old_obs, old_action, np.array(obs), game_screen_mean, vf,
                        env.action_space.n, t)
            )
            if len(pred_obs) == 4:
                action = act(np.stack(pred_obs, axis=2)[None], stochastic=stochastic)[0]

        old_obs = obs
        old_action = action

        # RL loop
        obs, rew, done, info = env.step(action)
        t += 1
        if done:
            t = 0
            obs = env.reset()
        if len(info["rewards"]) > num_episodes:
            if len(info["rewards"]) == 1 and video_recorder.enabled:
                # save video of first episode
                print("Saved video.")
                video_recorder.close()
                video_recorder.enabled = False
            print(info["rewards"][-1])
            num_episodes = len(info["rewards"]) 
開發者ID:yenchenlin,項目名稱:rl-attack-detection,代碼行數:53,代碼來源:enjoy.py

示例5: sample

# 需要導入模塊: from gym import monitoring [as 別名]
# 或者: from gym.monitoring import VideoRecorder [as 別名]
def sample(self, horizon, policy, record_fname=None):
        """Samples a rollout from the agent.

        Arguments: 
            horizon: (int) The length of the rollout to generate from the agent.
            policy: (policy) The policy that the agent will use for actions.
            record_fname: (str/None) The name of the file to which a recording of the rollout
                will be saved. If None, the rollout will not be recorded.

        Returns: (dict) A dictionary containing data from the rollout.
            The keys of the dictionary are 'obs', 'ac', and 'reward_sum'.
        """
        video_record = record_fname is not None
        recorder = None if not video_record else VideoRecorder(self.env, record_fname)

        times, rewards = [], []
        O, A, reward_sum, done = [self.env.reset()], [], 0, False

        policy.reset()
        for t in range(horizon):
            if video_record:
                recorder.capture_frame()
            start = time.time()
            A.append(policy.act(O[t], t))
            times.append(time.time() - start)

            if self.noise_stddev is None:
                obs, reward, done, info = self.env.step(A[t])
            else:
                action = A[t] + np.random.normal(loc=0, scale=self.noise_stddev, size=[self.dU])
                action = np.minimum(np.maximum(action, self.env.action_space.low), self.env.action_space.high)
                obs, reward, done, info = self.env.step(action)
            O.append(obs)
            reward_sum += reward
            rewards.append(reward)
            if done:
                break

        if video_record:
            recorder.capture_frame()
            recorder.close()

        print("Average action selection time: ", np.mean(times))
        print("Rollout length: ", len(A))

        return {
            "obs": np.array(O),
            "ac": np.array(A),
            "reward_sum": reward_sum,
            "rewards": np.array(rewards),
        } 
開發者ID:kchua,項目名稱:handful-of-trials,代碼行數:53,代碼來源:Agent.py

示例6: play

# 需要導入模塊: from gym import monitoring [as 別名]
# 或者: from gym.monitoring import VideoRecorder [as 別名]
def play(env, act, craft_adv_obs, craft_adv_obs2, stochastic, video_path,
         attack, m_target, m_adv):
  num_episodes = 0
  num_moves = 0
  num_transfer = 0

  video_recorder = None
  video_recorder = VideoRecorder(
      env, video_path, enabled=video_path is not None)
  obs = env.reset()
  while True:
    env.unwrapped.render()
    video_recorder.capture_frame()

    # V: Attack #
    if attack is not None:
      # Craft adv. examples
      with m_adv.get_session().as_default():
        adv_obs = \
            craft_adv_obs(np.array(obs)[None],
                          stochastic_adv=stochastic)[0]
      with m_target.get_session().as_default():
        action = act(np.array(adv_obs)[None],
                     stochastic=stochastic)[0]
        action2 = act(np.array(obs)[None], stochastic=stochastic)[0]
        num_moves += 1
        if action != action2:
          num_transfer += 1
    else:
      # Normal
      action = act(np.array(obs)[None], stochastic=stochastic)[0]

    obs, rew, done, info = env.step(action)
    if done:
      obs = env.reset()

    if len(info["rewards"]) > num_episodes:
      if len(info["rewards"]) == 1 and video_recorder.enabled:
        # save video of first episode
        print("Saved video.")
        video_recorder.close()
        video_recorder.enabled = False
      print('Reward: ' + str(info["rewards"][-1]))
      num_episodes = len(info["rewards"])
      print('Episode: ' + str(num_episodes))
      success = float(num_transfer / num_moves) * 100.0
      print("Percentage of successful attacks: " + str(success))
      num_moves = 0
      num_transfer = 0 
開發者ID:tensorflow,項目名稱:cleverhans,代碼行數:51,代碼來源:enjoy-adv.py


注:本文中的gym.monitoring.VideoRecorder方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。