-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrain_rl.py
More file actions
73 lines (62 loc) · 2.27 KB
/
train_rl.py
File metadata and controls
73 lines (62 loc) · 2.27 KB
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
73
import os
import torch
import argparse
import time
from neuriss.trainer.utils import set_seed, init_logger
from neuriss.env.env import make_env
from neuriss.rl.agents import get_agent
from neuriss.trainer.rl_trainer import RLTrainer
def train_rl(args):
os.environ["CUDA_VISIBLE_DEVICES"] = str(args.gpus)
set_seed(args.seed)
device = torch.device('cuda' if (torch.cuda.is_available() and not args.no_cuda) else 'cpu')
print(f'> Training with {device}')
env = make_env(env_id=args.env, device=torch.device('cpu'))
env.train()
# set up logger
log_path, writer, model_path = init_logger(args.log_path, args.env, args.algo, args.seed, vars(args))
# get agent
agent = get_agent(
algo=args.algo,
state_dims=env.n_dims,
action_dims=env.n_controls,
device=device,
goal_point=env.goal_point,
u_eq=env.u_eq,
state_std=env.state_std,
ctrl_std=torch.ones(sum(env.n_controls), device=device),
env=env
)
# setup RL trainer
trainer = RLTrainer(
env=env,
agent=agent,
writer=writer,
model_dir=model_path,
num_steps=args.steps,
eval_interval=args.steps // 50,
)
print(f'> Training {args.algo.upper()}...')
start_time = time.time()
trainer.train()
print(f'> Done in {time.time() - start_time:.0f}s')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
# custom
parser.add_argument('--env', type=str, required=True,
help='name of the environment')
parser.add_argument('--steps', type=int, required=True,
help='number of training steps')
parser.add_argument('--gpus', type=int, default=0,
help='index of the training gpu')
parser.add_argument('--algo', type=str, default='ppo',
help='name of the algorithm')
# default
parser.add_argument('--seed', type=int, default=0,
help='random seed')
parser.add_argument('--log-path', type=str, default='./logs',
help='path to save training logs')
parser.add_argument('--no-cuda', action='store_true', default=False,
help='disable cuda')
args = parser.parse_args()
train_rl(args)