forked from yufeiwang63/RLlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtorch_networks.py
199 lines (136 loc) · 6.42 KB
/
torch_networks.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.distributions as tds
class NAF_network(nn.Module):
def __init__(self, state_dim, action_dim, action_low, action_high):
super(NAF_network, self).__init__()
self.sharefc1 = nn.Linear(state_dim, 30)
self.sharefc2 = nn.Linear(30, 30)
self.v_fc1 = nn.Linear(30, 1)
self.miu_fc1 = nn.Linear(30, action_dim)
self.L_fc1 = nn.Linear(30, action_dim ** 2)
self.action_dim = action_dim
self.action_low, self.action_high = action_low, action_high
def forward(self, s, a = None):
s = F.relu(self.sharefc1(s))
s = F.relu(self.sharefc2(s))
v = self.v_fc1(s)
miu = self.miu_fc1(s)
# currently could only clip according to the same one single value.
# but different dimensions may mave different high and low bounds
# modify to clip along different action dimension
miu = torch.clamp(miu, self.action_low, self.action_high)
if a is None:
return v, miu
L = self.L_fc1(s)
L = L.view(-1, self.action_dim, self.action_dim)
tril_mask = torch.tril(torch.ones(
self.action_dim, self.action_dim), diagonal=-1).unsqueeze(0)
diag_mask = torch.diag(torch.diag(
torch.ones(self.action_dim, self.action_dim))).unsqueeze(0)
L = L * tril_mask.expand_as(L) + torch.exp(L) * diag_mask.expand_as(L)
P = torch.bmm(L, L.transpose(2, 1))
u_mu = (a - miu).unsqueeze(2)
A = -0.5 * \
torch.bmm(torch.bmm(u_mu.transpose(2, 1), P), u_mu)[:, :, 0]
q = A + v
return q
class DQN_fc_network(nn.Module):
def __init__(self, input_dim, output_dim, hidden_layers):
super(DQN_fc_network, self).__init__()
self.fc_in = nn.Linear(input_dim, 32)
self.fc_hiddens = [nn.Linear(32,32) for i in range(hidden_layers)]
self.fc_out = nn.Linear(32, output_dim)
def forward(self, x):
x = F.relu(self.fc_in(x))
for layer in self.fc_hiddens:
x = F.relu(layer(x))
x = self.fc_out(x)
return x
class DQN_dueling_network(nn.Module):
def __init__(self, input_dim, output_dim, hidden_layers):
super(DQN_dueling_network, self).__init__()
self.fc_in = nn.Linear(input_dim, 32)
self.fc_hiddens = [nn.Linear(32,32) for i in range(hidden_layers - 1)]
self.fca_before = nn.Linear(32, 16)
self.fcv_before = nn.Linear(32, 16)
self.fca = nn.Linear(16, output_dim)
self.fcv = nn.Linear(16, 1)
def forward(self, x):
x = F.relu(self.fc_in(x))
for layer in self.fc_hiddens:
x = F.relu(layer(x))
a = F.relu(self.fca_before(x))
a = self.fca(a)
a -= a.mean()
v = F.relu(self.fcv_before(x))
v = self.fcv(v)
q = a + v
return q
class DDPG_critic_network(nn.Module):
def __init__(self, state_dim, action_dim):
super(DDPG_critic_network, self).__init__()
self.sfc1 = nn.Linear(state_dim, 30)
self.sfc2 = nn.Linear(30,15)
self.afc1 = nn.Linear(action_dim, 30)
self.afc2 = nn.Linear(30,15)
self.sharefc1 = nn.Linear(30,30)
self.sharefc2 = nn.Linear(30,1)
def forward(self, s, a):
s = F.relu(self.sfc1(s))
s = F.relu(self.sfc2(s))
a = F.relu(self.afc1(a))
a = F.relu(self.afc2(a))
qsa = torch.cat((s,a), 1)
qsa = F.relu(self.sharefc1(qsa))
qsa = self.sharefc1(qsa)
return qsa
class DDPG_actor_network(nn.Module):
def __init__(self, state_dim, action_dim, action_low, action_high):
super(DDPG_actor_network, self).__init__()
self.fc1 = nn.Linear(state_dim, 30)
self.fc2 = nn.Linear(30, action_dim)
self.action_low, self.action_high = action_low, action_high
def forward(self, s):
s = F.relu(self.fc1(s))
a = self.fc2(s)
a = a.clamp(self.action_low, self.action_high)
return a
class AC_v_fc_network(nn.Module):
def __init__(self, state_dim):
super(AC_v_fc_network, self).__init__()
self.fc1 = nn.Linear(state_dim, 30)
self.fc2 = nn.Linear(30, 30)
self.fc3 = nn.Linear(30,1)
def forward(self, s):
s = F.relu(self.fc1(s))
v = F.relu(self.fc2(s))
v = self.fc3(v)
return v
class AC_a_fc_network(nn.Module):
def __init__(self, input_dim, output_dim):
super(AC_a_fc_network, self).__init__()
self.fc1 = nn.Linear(input_dim, 30)
self.fc2 = nn.Linear(30, 30)
self.fc3 = nn.Linear(30, output_dim)
def forward(self, x):
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return F.softmax(x, dim = 1)
class CAC_a_fc_network(nn.Module):
def __init__(self, input_dim, output_dim, action_low, action_high):
super(CAC_a_fc_network, self).__init__()
self.fc1 = nn.Linear(input_dim, 32)
self.fc2 = nn.Linear(32, 32)
self.fc3 = nn.Linear(32, output_dim)
self.sigma = torch.ones((output_dim))
self.action_low, self.action_high = action_low, action_high
def forward(self, s):
s = F.relu(self.fc1(s))
s = F.relu(self.fc2(s))
mu = self.fc3(s)
mu = torch.clamp(mu, self.action_low, self.action_high)
m = tds.normal.Normal(loc = mu, scale = self.sigma)
return m