本文整理汇总了Python中gym.wrappers.Monitor方法的典型用法代码示例。如果您正苦于以下问题:Python wrappers.Monitor方法的具体用法?Python wrappers.Monitor怎么用?Python wrappers.Monitor使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gym.wrappers
的用法示例。
在下文中一共展示了wrappers.Monitor方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from gym import wrappers [as 别名]
# 或者: from gym.wrappers import Monitor [as 别名]
def main(env_id, policy_file, record, stochastic, extra_kwargs):
import gym
from gym import wrappers
import tensorflow as tf
from es_distributed.policies import MujocoPolicy
import numpy as np
env = gym.make(env_id)
if record:
import uuid
env = wrappers.Monitor(env, '/tmp/' + str(uuid.uuid4()), force=True)
if extra_kwargs:
import json
extra_kwargs = json.loads(extra_kwargs)
with tf.Session():
pi = MujocoPolicy.Load(policy_file, extra_kwargs=extra_kwargs)
while True:
rews, t = pi.rollout(env, render=True, random_stream=np.random if stochastic else None)
print('return={:.4f} len={}'.format(rews.sum(), t))
if record:
env.close()
return
示例2: make_env
# 需要导入模块: from gym import wrappers [as 别名]
# 或者: from gym.wrappers import Monitor [as 别名]
def make_env(gym_id, seed, idx):
def thunk():
env = gym.make(gym_id)
env = wrap_atari(env)
env = gym.wrappers.RecordEpisodeStatistics(env)
if args.capture_video:
if idx == 0:
env = Monitor(env, f'videos/{experiment_name}')
env = wrap_pytorch(
wrap_deepmind(
env,
clip_rewards=True,
frame_stack=True,
scale=False,
)
)
env.seed(seed)
env.action_space.seed(seed)
env.observation_space.seed(seed)
return env
return thunk
示例3: make_env
# 需要导入模块: from gym import wrappers [as 别名]
# 或者: from gym.wrappers import Monitor [as 别名]
def make_env(gym_id, seed, idx):
def thunk():
env = gym.make(gym_id)
env = wrap_atari(env)
env = gym.wrappers.RecordEpisodeStatistics(env)
if args.capture_video:
if idx == 0:
env = ProbsVisualizationWrapper(env)
env = Monitor(env, f'videos/{experiment_name}')
env = wrap_pytorch(
wrap_deepmind(
env,
clip_rewards=True,
frame_stack=True,
scale=False,
)
)
env.seed(seed)
env.action_space.seed(seed)
env.observation_space.seed(seed)
return env
return thunk
示例4: __init__
# 需要导入模块: from gym import wrappers [as 别名]
# 或者: from gym.wrappers import Monitor [as 别名]
def __init__(self, env_name, state_builder=ALEStateBuilder(), repeat_action=4, no_op=30, monitoring_path=None):
assert isinstance(state_builder, StateBuilder), 'state_builder should inherit from StateBuilder'
assert isinstance(repeat_action, (int, tuple)), 'repeat_action should be int or tuple'
if isinstance(repeat_action, int):
assert repeat_action >= 1, "repeat_action should be >= 1"
elif isinstance(repeat_action, tuple):
assert len(repeat_action) == 2, 'repeat_action should be a length-2 tuple: (min frameskip, max frameskip)'
assert repeat_action[0] < repeat_action[1], 'repeat_action[0] should be < repeat_action[1]'
super(GymEnvironment, self).__init__()
self._state_builder = state_builder
self._env = gym.make(env_name)
self._env.env.frameskip = repeat_action
self._no_op = max(0, no_op)
self._done = True
if monitoring_path is not None:
self._env = Monitor(self._env, monitoring_path, video_callable=need_record)
示例5: get_env
# 需要导入模块: from gym import wrappers [as 别名]
# 或者: from gym.wrappers import Monitor [as 别名]
def get_env(env_name, exp_name, seed):
env = gym.make(env_name)
set_global_seeds(seed)
env.seed(seed)
# Set Up Logger
logdir = 'dqn_' + exp_name + '_' + env_name + '_' + time.strftime("%d-%m-%Y_%H-%M-%S")
logdir = osp.join('data', logdir)
logdir = osp.join(logdir, '%d'%seed)
logz.configure_output_dir(logdir)
hyperparams = {'exp_name': exp_name, 'env_name': env_name}
logz.save_hyperparams(hyperparams)
expt_dir = '/tmp/hw3_vid_dir2/'
env = wrappers.Monitor(env, osp.join(expt_dir, "gym"), force=True)
env = wrap_deepmind(env)
return env
示例6: get_env
# 需要导入模块: from gym import wrappers [as 别名]
# 或者: from gym.wrappers import Monitor [as 别名]
def get_env(env_name, exp_name, seed):
env = gym.make(env_name)
set_global_seeds(seed)
env.seed(seed)
# Set Up Logger
logdir = 'dqn_' + exp_name + '_' + env_name + '_' + time.strftime("%d-%m-%Y_%H-%M-%S")
logdir = osp.join('data', logdir)
logdir = osp.join(logdir, '%d'%seed)
logz.configure_output_dir(logdir)
hyperparams = {'exp_name': exp_name, 'env_name': env_name}
logz.save_hyperparams(hyperparams)
expt_dir = '/tmp/hw3_vid_dir/'
env = wrappers.Monitor(env, osp.join(expt_dir, "gym"), force=True)
env = wrap_deepmind_ram(env)
return env
示例7: get_env
# 需要导入模块: from gym import wrappers [as 别名]
# 或者: from gym.wrappers import Monitor [as 别名]
def get_env(task, seed):
env = gym.make('PongNoFrameskip-v4')
set_global_seeds(seed)
env.seed(seed)
expt_dir = '/tmp/hw3_vid_dir2/'
env = wrappers.Monitor(env, osp.join(expt_dir, "gym"), force=True)
env = wrap_deepmind(env)
return env
示例8: get_env
# 需要导入模块: from gym import wrappers [as 别名]
# 或者: from gym.wrappers import Monitor [as 别名]
def get_env(seed):
env = gym.make('Pong-ram-v0')
set_global_seeds(seed)
env.seed(seed)
expt_dir = '/tmp/hw3_vid_dir/'
env = wrappers.Monitor(env, osp.join(expt_dir, "gym"), force=True)
env = wrap_deepmind_ram(env)
return env
示例9: lander_stopping_criterion
# 需要导入模块: from gym import wrappers [as 别名]
# 或者: from gym.wrappers import Monitor [as 别名]
def lander_stopping_criterion(num_timesteps):
def stopping_criterion(env, t):
# notice that here t is the number of steps of the wrapped env,
# which is different from the number of steps in the underlying env
return get_wrapper_by_name(env, "Monitor").get_total_steps() >= num_timesteps
return stopping_criterion
示例10: get_env
# 需要导入模块: from gym import wrappers [as 别名]
# 或者: from gym.wrappers import Monitor [as 别名]
def get_env(task, seed):
env_id = task.env_id
env = gym.make(env_id)
set_global_seeds(seed)
env.seed(seed)
expt_dir = '/tmp/hw3_vid_dir2/'
env = wrappers.Monitor(env, osp.join(expt_dir, "gym"), force=True)
env = wrap_deepmind(env)
return env
示例11: test
# 需要导入模块: from gym import wrappers [as 别名]
# 或者: from gym.wrappers import Monitor [as 别名]
def test(self, just_one=True):
""" This is for test-time evaluation. No training is done here. By
default, iterate through every snapshot. If `just_one` is true, this
only runs one set of weights, to ensure that we record right away since
OpenAI will only record subsets and less frequently. Changing the loop
over snapshots is also needed.
"""
os.makedirs(self.args.directory+'/videos')
self.env = wrappers.Monitor(self.env, self.args.directory+'/videos', force=True)
headdir = self.args.directory+'/snapshots/'
snapshots = os.listdir(headdir)
snapshots.sort()
num_rollouts = 10
if just_one:
num_rollouts = 1
for sn in snapshots:
print("\n***** Currently on snapshot {} *****".format(sn))
### Add your own criteria here.
# if "800" not in sn:
# continue
###
with open(headdir+sn, 'rb') as f:
weights = pickle.load(f)
self.sess.run(self.set_params_op,
feed_dict={self.new_weights_v: weights})
returns = []
for i in range(num_rollouts):
returns.append( self._compute_return(test=True) )
print("mean: \t{}".format(np.mean(returns)))
print("std: \t{}".format(np.std(returns)))
print("max: \t{}".format(np.max(returns)))
print("min: \t{}".format(np.min(returns)))
print("returns:\n{}".format(returns))
示例12: main
# 需要导入模块: from gym import wrappers [as 别名]
# 或者: from gym.wrappers import Monitor [as 别名]
def main(env_id, policy_type, policy_file, record, stochastic, extra_kwargs, env_extra_kwargs):
import gym
from gym import wrappers
import tensorflow as tf
import numpy as np
from es.es_distributed import policies
if extra_kwargs:
import json
extra_kwargs = json.loads(extra_kwargs)
if env_extra_kwargs:
import json
env_extra_kwargs = json.loads(env_extra_kwargs)
if env_id == 'osim.env.run:RunEnv':
from osim.env.run import RunEnv
if env_extra_kwargs:
env = RunEnv(True, **env_extra_kwargs)
else:
env = RunEnv(True)
else:
env = gym.make(env_id)
if record:
import uuid
env = wrappers.Monitor(env, '/tmp/' + str(uuid.uuid4()), force=True)
with tf.Session():
policy = getattr(policies, policy_type)
pi = policy.Load(policy_file, extra_kwargs=extra_kwargs)
while True:
rews, t = pi.rollout(env, render=True, random_stream=np.random if stochastic else None)
print('return={:.4f} len={}'.format(rews.sum(), t))
if record:
env.close()
return
示例13: test_no_double_wrapping
# 需要导入模块: from gym import wrappers [as 别名]
# 或者: from gym.wrappers import Monitor [as 别名]
def test_no_double_wrapping():
temp = tempfile.mkdtemp()
try:
env = gym.make("FrozenLake-v0")
env = wrappers.Monitor(env, temp)
try:
env = wrappers.Monitor(env, temp)
except error.DoubleWrapperError:
pass
else:
assert False, "Should not allow double wrapping"
env.close()
finally:
shutil.rmtree(temp)
示例14: eval
# 需要导入模块: from gym import wrappers [as 别名]
# 或者: from gym.wrappers import Monitor [as 别名]
def eval(args):
env = gym.make(args.env_id)
logdir = str(Path(args.logbase_path) / args.env_id)
policy = Policy(env.observation_space.shape[0],env.action_space.shape[0])
### Initialize Parameters
init_op = tf.group(tf.global_variables_initializer(),
tf.local_variables_initializer())
# Training configuration
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.InteractiveSession()
sess.run(init_op)
policy.saver.restore(sess,logdir+'/model.ckpt')
from performance_checker import gen_traj
from gym.wrappers import Monitor
perfs = []
for j in range(args.num_trajs):
if j == 0 and args.video_record:
wrapped = Monitor(env, './video/',force=True)
else:
wrapped = env
perfs.append(gen_traj(wrapped,policy,args.render,args.max_len))
print(logdir, ',', np.mean(perfs), ',', np.std(perfs))
示例15: get_env
# 需要导入模块: from gym import wrappers [as 别名]
# 或者: from gym.wrappers import Monitor [as 别名]
def get_env(task, seed):
env_id = task.env_id
env = gym.make(env_id)
set_global_seeds(seed)
env.seed(seed)
expt_dir = 'tmp/gym-results'
env = wrappers.Monitor(env, expt_dir, force=True)
env = wrap_deepmind(env)
return env