-
Notifications
You must be signed in to change notification settings - Fork 64
/
policy.py
142 lines (106 loc) · 4.22 KB
/
policy.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
import random
import numpy as np
import torch
import utils
import torch.optim as optim
import torch.nn as nn
# Decide which device we want to run on
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
def calculate_returns(next_value, rewards, masks, gamma=0.99):
R = next_value
returns = []
for step in reversed(range(len(rewards))):
R = rewards[step] + gamma * R * masks[step]
returns.insert(0, R)
return returns
class PositionalMapping(nn.Module):
"""
Positional mapping Layer.
This layer map continuous input coordinates into a higher dimensional space
and enable the prediction to more easily approximate a higher frequency function.
See NERF paper for more details (https://arxiv.org/pdf/2003.08934.pdf)
"""
def __init__(self, input_dim, L=5, scale=1.0):
super(PositionalMapping, self).__init__()
self.L = L
self.output_dim = input_dim * (L*2 + 1)
self.scale = scale
def forward(self, x):
x = x * self.scale
if self.L == 0:
return x
h = [x]
PI = 3.1415927410125732
for i in range(self.L):
x_sin = torch.sin(2**i * PI * x)
x_cos = torch.cos(2**i * PI * x)
h.append(x_sin)
h.append(x_cos)
return torch.cat(h, dim=-1) / self.scale
class MLP(nn.Module):
"""
Multilayer perception with an embedded positional mapping
"""
def __init__(self, input_dim, output_dim):
super().__init__()
self.mapping = PositionalMapping(input_dim=input_dim, L=7)
h_dim = 128
self.linear1 = nn.Linear(in_features=self.mapping.output_dim, out_features=h_dim, bias=True)
self.linear2 = nn.Linear(in_features=h_dim, out_features=h_dim, bias=True)
self.linear3 = nn.Linear(in_features=h_dim, out_features=h_dim, bias=True)
self.linear4 = nn.Linear(in_features=h_dim, out_features=output_dim, bias=True)
self.relu = nn.LeakyReLU(0.2)
def forward(self, x):
# shape x: 1 x m_token x m_state
x = x.view([1, -1])
x = self.mapping(x)
x = self.relu(self.linear1(x))
x = self.relu(self.linear2(x))
x = self.relu(self.linear3(x))
x = self.linear4(x)
return x
class ActorCritic(nn.Module):
"""
RL policy and update rules
"""
def __init__(self, input_dim, output_dim):
super().__init__()
self.output_dim = output_dim
self.actor = MLP(input_dim=input_dim, output_dim=output_dim)
self.critic = MLP(input_dim=input_dim, output_dim=1)
self.softmax = nn.Softmax(dim=-1)
self.optimizer = optim.RMSprop(self.parameters(), lr=5e-5)
def forward(self, x):
# shape x: batch_size x m_token x m_state
y = self.actor(x)
probs = self.softmax(y)
value = self.critic(x)
return probs, value
def get_action(self, state, deterministic=False, exploration=0.01):
state = torch.tensor(state, dtype=torch.float32).unsqueeze(0).to(device)
probs, value = self.forward(state)
probs = probs[0, :]
value = value[0]
if deterministic:
action_id = np.argmax(np.squeeze(probs.detach().cpu().numpy()))
else:
if random.random() < exploration: # exploration
action_id = random.randint(0, self.output_dim - 1)
else:
action_id = np.random.choice(self.output_dim, p=np.squeeze(probs.detach().cpu().numpy()))
log_prob = torch.log(probs[action_id] + 1e-9)
return action_id, log_prob, value
@staticmethod
def update_ac(network, rewards, log_probs, values, masks, Qval, gamma=0.99):
# compute Q values
Qvals = calculate_returns(Qval.detach(), rewards, masks, gamma=gamma)
Qvals = torch.tensor(Qvals, dtype=torch.float32).to(device).detach()
log_probs = torch.stack(log_probs)
values = torch.stack(values)
advantage = Qvals - values
actor_loss = (-log_probs * advantage.detach()).mean()
critic_loss = 0.5 * advantage.pow(2).mean()
ac_loss = actor_loss + critic_loss
network.optimizer.zero_grad()
ac_loss.backward()
network.optimizer.step()