forked from danijar/dreamerv3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
72 lines (58 loc) · 1.92 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import warnings
from functools import partial as bind
import dreamerv3
import embodied
warnings.filterwarnings('ignore', '.*truncated to dtype int32.*')
def main():
config = embodied.Config(dreamerv3.Agent.configs['defaults'])
config = config.update({
**dreamerv3.Agent.configs['size100m'],
'logdir': f'~/logdir/{embodied.timestamp()}-example',
'run.train_ratio': 32,
})
config = embodied.Flags(config).parse()
print('Logdir:', config.logdir)
logdir = embodied.Path(config.logdir)
logdir.mkdir()
config.save(logdir / 'config.yaml')
def make_agent(config):
env = make_env(config)
agent = dreamerv3.Agent(env.obs_space, env.act_space, config)
env.close()
return agent
def make_logger(config):
logdir = embodied.Path(config.logdir)
return embodied.Logger(embodied.Counter(), [
embodied.logger.TerminalOutput(config.filter),
embodied.logger.JSONLOutput(logdir, 'metrics.jsonl'),
embodied.logger.TensorBoardOutput(logdir),
# embodied.logger.WandbOutput(logdir.name, config=config),
])
def make_replay(config):
return embodied.replay.Replay(
length=config.batch_length,
capacity=config.replay.size,
directory=embodied.Path(config.logdir) / 'replay',
online=config.replay.online)
def make_env(config, env_id=0):
import crafter
from embodied.envs import from_gym
env = crafter.Env()
env = from_gym.FromGym(env)
env = dreamerv3.wrap_env(env, config)
return env
args = embodied.Config(
**config.run,
logdir=config.logdir,
batch_size=config.batch_size,
batch_length=config.batch_length,
batch_length_eval=config.batch_length_eval,
replay_context=config.replay_context,
)
embodied.run.train(
bind(make_agent, config),
bind(make_replay, config),
bind(make_env, config),
bind(make_logger, config), args)
if __name__ == '__main__':
main()