-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathagent.py
163 lines (116 loc) · 4.54 KB
/
agent.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
155
156
157
158
159
import os
import sys
import torch
import torch.nn as nn
class Agent(object):
def __init__(self, model, optimizer):
self.model = model
self.optimizer = optimizer
def train(self, env):
self.model.train()
# Reset environment
ob = env.reset()
best_reward = len(env.shortest_path(env.agent_pos, env.goal_pos)) + 20 - 2
train_infos = []
for _ in range(env.w + env.h):
# Predict action distribution based on observation
logit = self.model(ob)
self._mask_out_invalid_actions(logit, env)
dist = logit.softmax(dim=0)
# Sample an action
m = torch.distributions.categorical.Categorical(dist)
action = m.sample()
# Take action and receive new observation
ob, reward, feedback, done = env.step(action)
# Store information
train_infos.append({
'ob' : ob,
'logit' : logit,
'reward' : reward,
'feedback': feedback,
'action' : action
})
if done:
break
return train_infos, best_reward, done
def test(self, env, test_id):
self.model.eval()
ob = env.reset(test_id=test_id)
best_reward = len(env.shortest_path(env.agent_pos, env.goal_pos)) + 20 - 2
total_reward = 0
feedbacks = []
with torch.no_grad():
for _ in range(env.w + env.h):
logit = self.model(ob)
self._mask_out_invalid_actions(logit, env)
action = logit.argmax(dim=0)
ob, reward, feedback, done = env.step(action)
total_reward += reward
if done:
return 1, total_reward, best_reward
return 0, total_reward, best_reward
def update(self):
self.model.zero_grad()
self.loss.backward()
self.optimizer.step()
def _mask_out_invalid_actions(self, logit, env):
for idx in range(len(env.action_space)):
if idx not in env.valid_action_indices:
logit[idx] = -float('inf')
class ReinforceAgent(Agent):
def __init__(self, model, optimizer):
super(ReinforceAgent, self).__init__(model, optimizer)
self.loss_fn = nn.CrossEntropyLoss(reduction='none')
def train(self, env):
train_infos, best_reward, done = super(ReinforceAgent, self).train(env)
agent_reward = 0
cum_reward = 0
actor_loss = 0
critic_loss = 0
# Loop reversely, from end point to start point
for info in reversed(train_infos):
agent_reward += info['reward']
cum_reward += info['feedback']
baseline_reward = self.model.predict_baseline(info['ob'])
norm_reward = cum_reward - baseline_reward
logit = info['logit'].unsqueeze(0)
agent_action = info['action'].unsqueeze(0)
# RL loss = cross entropy weighted by (normalized) reward
actor_loss += self.loss_fn(logit, agent_action) * norm_reward.detach()
critic_loss += norm_reward ** 2
self.loss = (actor_loss + critic_loss) / len(train_infos)
# Update model
self.update()
ep_info = {
'done': done,
'best_reward': best_reward,
'agent_reward': agent_reward,
'loss': self.loss.item(),
'actor_loss': actor_loss.item() / len(train_infos),
'critic_loss': critic_loss.item() / len(train_infos)
}
return ep_info
class ImitateAgent(Agent):
def __init__(self, model, optimizer):
super(ImitateAgent, self).__init__(model, optimizer)
self.loss_fn = nn.CrossEntropyLoss()
def train(self, env):
train_infos, best_reward, done = super(ImitateAgent, self).train(env)
self.loss = 0
agent_reward = 0
for info in train_infos:
agent_reward += info['reward']
logit = info['logit'].unsqueeze(0)
reference_action = torch.tensor(info['feedback'], dtype=torch.long).unsqueeze(0)
# IL loss = cross entropy
self.loss += self.loss_fn(logit, reference_action)
self.loss = self.loss / len(train_infos)
# Update model
self.update()
ep_info = {
'done': done,
'best_reward': best_reward,
'agent_reward': agent_reward,
'loss': self.loss.item(),
}
return ep_info