-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathtrain.py
More file actions
98 lines (76 loc) · 2.46 KB
/
Copy pathtrain.py
File metadata and controls
98 lines (76 loc) · 2.46 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
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
import os
import json
import pickle
import argparse
import torch
import gym
from models.nets import Expert
from models.gail import GAIL
def main(env_name):
ckpt_path = "ckpts"
if not os.path.isdir(ckpt_path):
os.mkdir(ckpt_path)
if env_name not in ["CartPole-v1", "Pendulum-v0", "BipedalWalker-v3"]:
print("The environment name is wrong!")
return
expert_ckpt_path = "experts"
expert_ckpt_path = os.path.join(expert_ckpt_path, env_name)
with open(os.path.join(expert_ckpt_path, "model_config.json")) as f:
expert_config = json.load(f)
ckpt_path = os.path.join(ckpt_path, env_name)
if not os.path.isdir(ckpt_path):
os.mkdir(ckpt_path)
with open("config.json") as f:
config = json.load(f)[env_name]
with open(os.path.join(ckpt_path, "model_config.json"), "w") as f:
json.dump(config, f, indent=4)
env = gym.make(env_name)
env.reset()
state_dim = len(env.observation_space.high)
if env_name in ["CartPole-v1"]:
discrete = True
action_dim = env.action_space.n
else:
discrete = False
action_dim = env.action_space.shape[0]
if torch.cuda.is_available():
device = "cuda"
else:
device = "cpu"
expert = Expert(
state_dim, action_dim, discrete, **expert_config
).to(device)
expert.pi.load_state_dict(
torch.load(
os.path.join(expert_ckpt_path, "policy.ckpt"), map_location=device
)
)
model = GAIL(state_dim, action_dim, discrete, config).to(device)
results = model.train(env, expert)
env.close()
with open(os.path.join(ckpt_path, "results.pkl"), "wb") as f:
pickle.dump(results, f)
if hasattr(model, "pi"):
torch.save(
model.pi.state_dict(), os.path.join(ckpt_path, "policy.ckpt")
)
if hasattr(model, "v"):
torch.save(
model.v.state_dict(), os.path.join(ckpt_path, "value.ckpt")
)
if hasattr(model, "d"):
torch.save(
model.d.state_dict(), os.path.join(ckpt_path, "discriminator.ckpt")
)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--env_name",
type=str,
default="CartPole-v1",
help="Type the environment name to run. \
The possible environments are \
[CartPole-v1, Pendulum-v0, BipedalWalker-v3]"
)
args = parser.parse_args()
main(**vars(args))