-
Notifications
You must be signed in to change notification settings - Fork 71
/
train_torch_mpi_norm_load.py
146 lines (123 loc) · 5.3 KB
/
train_torch_mpi_norm_load.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import numpy as np
import gym
import os, sys
from torch_arguments import get_args
import torch
from mpi4py import MPI
from subprocess import CalledProcessError
import time
from spinup_utils.logx import setup_logger_kwargs, colorize
from spinup_utils.logx import EpochLogger
from spinup_utils.print_logger import Logger
# mpi
from spinup_utils.mpi_pytorch import setup_pytorch_for_mpi, sync_params, mpi_avg_grads
from spinup_utils.mpi_tools import mpi_fork, mpi_avg, proc_id, mpi_statistics_scalar, num_procs
from mpi4py import MPI
"""
train the agent, the MPI part code is copy from openai baselines(https://github.com/openai/baselines/blob/master/baselines/her)
but I ignore it~
"""
def obs2state(obs, key_list=['observation', 'desired_goal']):
if type(obs) == dict:
s = np.concatenate(([obs[key] for key in key_list]
))
elif type(obs) == np.ndarray:
s = obs[:]
else:
s = obs[:]
return s
def trainer(net, env, args):
# logger
exp_name = args.exp_name+'_'+args.RL_name+'_'+args.env_name
logger_kwargs = setup_logger_kwargs(exp_name=exp_name,
seed=args.seed,
output_dir=args.output_dir + "/")
logger = EpochLogger(**logger_kwargs)
if proc_id() == 0:
sys.stdout = Logger(logger_kwargs["output_dir"] + "/print.log",
sys.stdout)
logger.save_config(locals(), __file__)
# start running
start_time = time.time()
for i in range(args.n_epochs):
test_ep_reward, logger = net.test_agent(args=args,
env=env,
n=10,
logger=logger,
obs2state=obs2state,
)
logger.store(TestEpRet=test_ep_reward)
logger.log_tabular('Epoch', i)
logger.log_tabular('TestEpRet', average_only=True)
logger.log_tabular('TestSuccess', average_only=True)
logger.dump_tabular()
print(colorize("the experience %s is end" % logger.output_file.name,
'green', bold=True))
net.save_simple_network(logger_kwargs["output_dir"])
net.save_norm(logger_kwargs["output_dir"])
net.save_replay_buffer(logger_kwargs["output_dir"])
def launch(net, args):
env = gym.make(args.env_name)
# Special function to avoid certain slowdowns from PyTorch + MPI combo.
setup_pytorch_for_mpi()
# 确保不同进程的随机种子不同!
seed = args.seed
seed += 10000 * proc_id()
env.seed(seed)
np.random.seed(seed)
try:
s_dim = env.observation_space.shape[0]
except:
s_dim = env.observation_space.spaces['observation'].shape[0] + \
env.observation_space.spaces['desired_goal'].shape[0]
act_dim = env.action_space.shape[0]
a_bound = env.action_space.high[0]
import os
os.environ["CUDA_VISIBLE_DEVICES"] = str(args.gpu_id)
"""
torch1.17.1,gpu_id: 1 device: cuda:0,用的是物理上的0卡;
cuda的序号仍然是按照物理序号;
torch1.3.1,gpu_id: 1 device: cuda:0,用的是物理上的1卡,
torch1.3.1,gpu_id: 1 device: cuda:1,报错:invalid device ordinal;
torch1.3.1,gpu_id: 1,3 device: cuda:1,用的是物理上的3卡,
有点类似于指定GPU-ID后,cuda会重新排序。
"""
device = torch.device("cuda:"+str(0) if torch.cuda.is_available() and args.gpu_id != -1 else 'cpu')
print("gpu_id:", args.gpu_id,
"device:", device)
net = net(act_dim=act_dim,
obs_dim=s_dim,
a_bound=a_bound,
per_flag=args.per,
her_flag=args.her,
action_l2=args.action_l2,
state_norm=args.state_norm,
gamma=args.gamma,
sess_opt=args.sess_opt,
seed=args.seed,
clip_return=args.clip_return,
device=device,
)
restore_path = 'HER_DRLib_Net_Reload/2022-08-12_HER_mpi1_random_TD3Torch_FetchPush-v1/2022-08-12_15-57-53-HER_mpi1_random_TD3Torch_FetchPush-v1_s300/'
net.load_simple_network(restore_path+"actor.pth")
# net.load_replay_buffer(restore_path+"replay.pkl") # 因为文件太大了,我删掉了默认的值
net.load_norm(restore_path+"norm.pkl")
trainer(net, env, args)
if __name__ == '__main__':
# take the configuration for the HER
# os.environ['OMP_NUM_THREADS'] = '1'
# os.environ['MKL_NUM_THREADS'] = '1'
# os.environ['IN_MPI'] = '1'
# get the params
args = get_args()
os.environ['CUDA_VISIBLE_DEVICES'] = str(args.gpu_id)
mpi_fork(args.cpu)
from algos.tf1.td3_sp.TD3_per_her import TD3
from algos.tf1.ddpg_sp.DDPG_per_her import DDPG
from algos.tf1.sac_sp.SAC_per_her import SAC
from algos.tf1.sac_auto.sac_auto_per_her import SAC_AUTO
from algos.pytorch.td3_sp.td3_per_her import TD3Torch
from algos.pytorch.ddpg_sp.ddpg_per_her import DDPGTorch
from algos.pytorch.sac_sp.sac_per_her import SACTorch
RL_list = [TD3, DDPG, SAC, SAC_AUTO, TD3Torch, DDPGTorch, SACTorch]
[launch(net=net, args=args) for net in RL_list if net.__name__ == args.RL_name]