-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
175 lines (132 loc) · 4.28 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
from copy import deepcopy
import torch
import torch.nn as nn
# from torch.distributions import Normal
from torch.distributions import Categorical
device = 'cuda:0' if torch.cuda.is_available() else 'cpu'
n_h = 64
τ = 0.995
class Model:
def __init__(self, model_type, lr, *args, target=False):
self.model = model_type(*args).to(device)
if target:
self.target_model = model_type(*args).to(device)
self.target_model.load_state_dict(self.model.state_dict())
self.optimizer = torch.optim.Adam(self.model.parameters(), lr=lr)
def _optimize(self, loss):
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
def maximize(self, loss):
self._optimize(-loss)
def minimize(self, loss):
self._optimize(loss)
def target(self, *args):
return self.target_model(*args)
def log_prob(self, *args):
return self.model.log_prob(*args)
def __getattr__(self, k):
return getattr(self.model, k)
def __call__(self, *args):
return self.model(*args)
def soft_update_target(self):
for param, target_param in zip(self.model.parameters(), self.target_model.parameters()):
target_param.data.copy_((τ * target_param.data) + ((1 - τ) * param.data))
class CategoricalPolicy(nn.Module):
def __init__(self, n_s, n_a):
super().__init__()
self.main = nn.Sequential(
nn.Linear(n_s, n_h),
nn.Tanh(),
nn.Linear(n_h, n_h),
nn.Tanh(),
nn.Linear(n_h, n_a)
)
# self.sigma = nn.Parameter(torch.zeros(n_a))
def dist(self, s):
# return Normal(self.main(s), self.sigma.exp())
return Categorical(logits=self.main(s))
def forward(self, s):
return self.dist(s).sample()
def log_prob(self, s, a):
return self.dist(s).log_prob(a)
class DeterministicPolicy(nn.Module):
def __init__(self, n_s, n_a, action_space, device):
super().__init__()
self.high = torch.FloatTensor(action_space.high).to(device)
self.main = nn.Sequential(
nn.Linear(n_s, n_h),
nn.Tanh(),
nn.Linear(n_h, n_h),
nn.Tanh(),
nn.Linear(n_h, n_a),
nn.Tanh()
)
def forward(self, s):
return self.main(s) * self.high
class RelativePolicy(nn.Module):
def __init__(self, n_s, n_a):
super().__init__()
self.main = nn.Sequential(
nn.Linear(n_s + n_a, n_h),
nn.Tanh(),
nn.Linear(n_h, n_h),
nn.Tanh(),
nn.Linear(n_h, n_a)
)
def forward(self, s, a):
return self.main(torch.cat([s, a], dim=-1))
class Dynamics(nn.Module):
def __init__(self, n_s, n_a):
super().__init__()
self.main = nn.Sequential(
nn.Linear(n_s + n_a, n_h),
nn.ELU(),
nn.Linear(n_h, n_h),
nn.ELU(),
nn.Linear(n_h, n_s)
)
def forward(self, s, a):
return self.main(torch.cat([s, a], dim=-1))
class Value(nn.Module):
def __init__(self, n_s):
super().__init__()
self.main = nn.Sequential(
nn.Linear(n_s, n_h),
nn.ELU(),
nn.Linear(n_h, n_h),
nn.ELU(),
nn.Linear(n_h, 1)
)
def forward(self, s):
return self.main(s)
class QNetwork(nn.Module):
def __init__(self, n_s, n_a):
super().__init__()
# self.pre_state = nn.Sequential(
# nn.Linear(n_s, n_h),
# nn.ELU(),
# nn.Linear(n_h, n_h // 2),
# nn.ELU()
# )
# self.pre_action = nn.Sequential(
# nn.Linear(n_a, n_h // 2),
# nn.ELU()
# )
# self.main = nn.Sequential(
# nn.Linear(n_h, n_h),
# nn.ELU(),
# nn.Linear(n_h, 1)
# )
self.bruh = nn.Sequential(
nn.Linear(n_s + n_a, n_h),
nn.ELU(),
nn.Linear(n_h, n_h),
nn.ELU(),
nn.Linear(n_h, 1)
)
def forward(self, s, a):
# s = self.pre_state(s)
# a = self.pre_action(a)
# return self.main(torch.cat([s, a], dim=-1))
return self.bruh(torch.cat([s, a], dim=-1))