-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrainer.py
154 lines (130 loc) · 4.76 KB
/
trainer.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
147
148
149
150
151
152
153
154
import gym
import numpy as np
import seaborn as sns
import torch
from torch.functional import Tensor
from torch.optim.optimizer import Optimizer
from torch.distributions import categorical
import model
def save_heatmap(policy_attn):
attn = policy_attn.detach().numpy()
attn = attn.reshape(-1, 1)
heatmap = sns.heatmap(
attn,
annot=True,
yticklabels=[
'position of cart',
'velocity of cart',
'angle of pole',
'rotation rate of pole'
],
xticklabels=False
)
figure = heatmap.get_figure()
figure.tight_layout()
figure.savefig('policy_attention.png', dpi=400)
class CartPoleTrainer():
def __init__(self):
obs_size = 4
self.policies = model.CartPolePolicies(model.CartPolePoliciesParams(
obs_size=obs_size,
num_actions=2
))
self.policies_selector = model.CartPolePolicySelector(
num_policies=obs_size)
self._obs_size = obs_size
def get_policy_prob_dist(self):
return categorical.Categorical(logits=self.policies_selector.policy_attn)
def train(self, env: gym.Env):
print('Training policies...')
opt = torch.optim.AdamW(list(self.policies.parameters()))
for i in range(self._obs_size):
for _ in range(1000):
self.train_one_eps(
opt, env, torch.tensor(i),
train_policy=True, train_policy_selector=False)
print('Training policy selector...')
opt = torch.optim.AdamW(list(self.policies_selector.parameters()))
for i in range(1000):
policy_id = self.get_policy_prob_dist().sample()
self.train_one_eps(
opt, env, policy_id, train_policy=False, train_policy_selector=True)
save_heatmap(self.policies_selector.policy_attn)
policy_id = torch.argmax(self.policies_selector.policy_attn).item()
self.demo(env, policy_id)
def demo(self, env: gym.Env, policy_id: Tensor):
env = gym.wrappers.RecordVideo(env, './videos', name_prefix=f'policy-{policy_id}')
# env = Monitor(env, f'./videos/{policy_id}', force=True)
obs = env.reset()
done = False
eps_len = 0
while not done:
env.render()
eps_len += 1
policies = self.policies(torch.tensor(obs))
policy_logits = self.policies_selector(policies)
action = torch.argmax(policy_logits[policy_id]).item()
obs, _, done, _ = env.step(action)
def train_one_eps(
self,
opt: Optimizer,
env: gym.Env,
policy_id: Tensor,
train_policy: bool,
train_policy_selector: bool
):
obs = env.reset()
observations = []
actions = []
rewards = []
done = False
eps_len = 0
while not done:
eps_len += 1
policies = self.policies(torch.tensor(obs))
policy_logits = self.policies_selector(policies)
if train_policy:
action_prob_dist = categorical.Categorical(
logits=policy_logits[policy_id])
action = action_prob_dist.sample()
else:
action = torch.argmax(policy_logits[policy_id])
obs, reward, done, _ = env.step(action.item())
actions.append(action)
rewards.append(reward)
observations.append(obs)
observations = torch.as_tensor(np.array(observations))
actions = torch.as_tensor(actions)
# Reward-to-go
rewards_acc = [0]
for r in rewards:
rewards_acc.append(rewards_acc[-1] + r)
total_reward = sum(rewards)
weights = torch.as_tensor([
total_reward - acc for acc in rewards_acc[:-1]
])
# optimize
opt.zero_grad()
policies = self.policies(observations)
policy_logits = self.policies_selector(policies)
if train_policy_selector:
policy_logp = self.get_policy_prob_dist().log_prob(policy_id)
loss = -(policy_logp * weights[0])
else:
loss = torch.tensor(0.0)
if train_policy:
for step, a in enumerate(actions):
action_prob_dist = categorical.Categorical(
logits=policy_logits[policy_id][step])
actions_logp = action_prob_dist.log_prob(a)
loss += -(actions_logp * weights[step])
if train_policy or train_policy_selector:
loss.backward()
opt.step()
return loss.item(), eps_len
def main():
cart_pole_env = gym.make('CartPole-v1')
trainer = CartPoleTrainer()
trainer.train(cart_pole_env)
if __name__ == '__main__':
main()