-
Notifications
You must be signed in to change notification settings - Fork 0
/
ensemble.py
50 lines (33 loc) · 976 Bytes
/
ensemble.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
import torch
import torch.nn.functional as F
def qs_function(self, state): # -> models x 1 x actions
with torch.no_grad():
qs = []
for model in self.models:
q = (model(state.unsqueeze(0)) * self.support).sum(2)
qs.append(q)
return torch.stack(qs)
def distrib_qs_function(self, x, log=False): # -> models x 1 x actions x atoms
qs = []
for model in self.models:
q = model(x, log=log)
qs.append(q)
return torch.stack(qs)
# ---
def mean_q(self, state):
self.deterministic()
return qs_function(self, state).mean(0)
# ---
def distrib_mean_q(self, state, log=False):
self.deterministic()
return distrib_qs_function(self, state, log=log).mean(0)
# ---
def forward(self, x, log=False):
return distrib_mean_q(self, x, log=log)
def q_function(self, state):
return mean_q(self, state)
def act(self, state):
if self.args.policy == 'mean':
return self.q(state).argmax(1).item()
else:
raise NotImplementedError