-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
208 lines (161 loc) · 6.39 KB
/
models.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import torch
from torch.functional import norm
import torch.nn as nn
import torch.nn.functional as F
from torch.distributions import Normal, Categorical
import numpy as np
from math import floor
from typing import Tuple
LOG_SIG_MAX = 2
LOG_SIG_MIN = -20
class Flatten(nn.Module):
def forward(self, x):
return x.view(x.size(0), -1)
def initialize_weights_he(m):
if isinstance(m, nn.Linear) or isinstance(m, nn.Conv2d):
torch.nn.init.kaiming_uniform_(m.weight)
if m.bias is not None:
torch.nn.init.constant_(m.bias, 0)
class BaseNetwork(nn.Module):
def save(self, path):
torch.save(self.state_dict(), path)
def load(self, path):
self.load_state_dict(torch.load(path))
class FullCNN(BaseNetwork):
def __init__(self, in_channels):
super().__init__()
self.net = nn.Sequential(
nn.Conv2d(in_channels, 32, kernel_size=8, stride=4, padding=0),
nn.ReLU(),
nn.Conv2d(32, 64, kernel_size=4, stride=2, padding=0),
nn.ReLU(),
nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=0),
nn.ReLU(),
Flatten(),
).apply(initialize_weights_he)
def forward(self, states):
return self.net(states.permute(0,3,1,2))
@staticmethod
def conv_output_shape(
h_w: Tuple[int, int],
kernel_size: int = 1,
stride: int = 1,
pad: int = 0,
dilation: int = 1,
):
"""
Computes the height and width of the output of a convolution layer.
"""
h = floor(
((h_w[0] + (2 * pad) - (dilation * (kernel_size - 1)) - 1) / stride) + 1
)
w = floor(
((h_w[1] + (2 * pad) - (dilation * (kernel_size - 1)) - 1) / stride) + 1
)
return h, w
class VisualQNetwork(BaseNetwork):
def __init__(self, input_shape, num_actions, hidden_dim, use_conv=False):
super().__init__()
self.use_conv = use_conv
height = input_shape[0]
width = input_shape[1]
in_channels = input_shape[2]
conv1_hw = FullCNN.conv_output_shape((height, width), 8, 4)
conv2_hw = FullCNN.conv_output_shape(conv1_hw, 4, 2)
conv3_hw = FullCNN.conv_output_shape(conv2_hw, 3, 1)
if self.use_conv:
self.conv = FullCNN(in_channels)
self.net = nn.Sequential(
nn.Linear(conv3_hw[0]*conv3_hw[1]*64, hidden_dim),
nn.ReLU(inplace=True),
nn.Linear(hidden_dim, hidden_dim),
nn.ReLU(inplace=True),
nn.Linear(hidden_dim, num_actions)
)
def forward(self, states):
if self.use_conv:
states = self.conv(states)
return self.net(states)
class VisualQNetworkPair(BaseNetwork):
def __init__(self, input_shape, num_actions, hidden_dim, use_conv=False):
super().__init__()
self.q1 = VisualQNetwork(input_shape, num_actions, hidden_dim, use_conv)
self.q2 = VisualQNetwork(input_shape, num_actions, hidden_dim, use_conv)
def forward(self, states):
q1 = self.q1(states)
q2 = self.q2(states)
return q1, q2
class CategoricalPolicy(BaseNetwork):
def __init__(self, input_shape, num_actions, hidden_dim, use_conv=False):
super().__init__()
in_channels = input_shape[2]
height = input_shape[0]
width = input_shape[1]
self.use_conv = use_conv
if self.use_conv:
self.conv = FullCNN(in_channels)
conv1_hw = FullCNN.conv_output_shape((height, width), 8, 4)
conv2_hw = FullCNN.conv_output_shape(conv1_hw, 4, 2)
conv3_hw = FullCNN.conv_output_shape(conv2_hw, 3, 1)
self.net = nn.Sequential(
nn.Linear(conv3_hw[0]*conv3_hw[1]*64, hidden_dim),
nn.ReLU(inplace=True),
nn.Linear(hidden_dim, num_actions)
)
def sample(self, states, epsilon=0.1):
if self.use_conv:
states = self.conv(states)
out = self.net(states)
action_probs = F.softmax(out,dim=1)
action_distro = Categorical(action_probs)
actions = action_distro.sample().view(-1,1)
z = (action_probs == 0.0).float() * 1e-8
log_action_probs = torch.log(action_probs + z)
return actions, action_probs, log_action_probs
def act(self, states, epsilon=0.1):
if self.use_conv:
states = self.conv(states)
action_logits = self.net(states)
action_logits += epsilon * torch.rand(action_logits.shape[0], action_logits.shape[1])
greedy_actions = torch.argmax(
action_logits, dim=1, keepdim=True)
return greedy_actions
class GuassianPolicy(torch.nn.Module):
def __init__(self, num_observations, num_actions, hidden_size, act_limit):
super().__init__()
# 3 layer network, with 2 outputs
self.linear1 = nn.Linear(num_observations, hidden_size)
self.linear2 = nn.Linear(hidden_size, hidden_size)
self.mean_layer = nn.Linear(hidden_size, num_actions)
self.log_stddev_layer = nn.Linear(hidden_size, num_actions)
self.act_limit = act_limit
# apply initial weights
def forward(self, state):
x = F.relu(self.linear1(state))
x = F.relu(self.linear2(x))
mean = self.mean_layer(x)
log_stddev = self.log_stddev_layer(x)
log_stddev = torch.clamp(log_stddev, min=LOG_SIG_MIN, max=LOG_SIG_MAX)
return mean, log_stddev
def sample(self, state):
mean, log_stddev = self.forward(state)
stddev = log_stddev.exp()
normal = Normal(mean, stddev)
print(mean, stddev)
sample = normal.rsample()
log_prob = normal.log_prob(sample).sum(axis=-1)
#log_prob -= (2*(np.log(2) - sample - F.softplus(-2*sample))).sum(axis=0)
action = torch.tanh(sample) * self.act_limit
return action, log_prob
"""
class ActorCritic(nn.Module):
def __init__(self, num_observations, num_actions, hidden_dim, act_limit=1):
super().__init__()
self.policy = GuassianPolicy(num_observations, num_actions, hidden_dim, act_limit)
self.q1 = QNetwork(num_observations, num_actions, hidden_dim)
self.q2 = QNetwork(num_observations, num_actions, hidden_dim)
def act(self, state):
with torch.nograd():
action, _ = self.policy.sample(state)
return action.numpy()
"""