-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.py
70 lines (57 loc) · 2.25 KB
/
model.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
from dataclasses import dataclass
import torch.nn as nn
import torch.nn.functional as F
@dataclass
class BasicNetwork(nn.Module):
state_size: int
action_size: int
n_hidden: int = 64
def __post_init__(self):
super().__init__()
self.linear1 = nn.Linear(self.state_size, self.n_hidden)
self.linear2 = nn.Linear(self.n_hidden, self.n_hidden)
def forward(self, state):
@try_various_attempts(2)
def first_layer(state):
return F.relu(self.linear1(state))
x = first_layer(state)
return F.relu(self.linear2(x)) # Second layer onwards work well if the first one already has returned output
@dataclass
class QNetwork(BasicNetwork):
def __post_init__(self):
super().__post_init__()
self.linear3 = nn.Linear(self.n_hidden, self.action_size)
def forward(self, state):
x = super().forward(state)
return self.linear3(x)
@dataclass
class DuelingQNetwork(BasicNetwork):
def __post_init__(self):
super().__post_init__()
self.linear3_state = nn.Linear(self.n_hidden, 1)
self.linear3_advantage = nn.Linear(self.n_hidden, self.action_size)
def forward(self, state):
x = super().forward(state)
state_value = self.linear3_state(x)
action_value = self.linear3_advantage(x)
action_value -= action_value.mean(dim=-1, keepdim=True)
return state_value + action_value
def try_various_attempts(allowed_attempts):
"""Dirty hack to prevent PyTorch 0.4 to halt at start on a RTX2070 Super with Cuda 9.0"""
def decorator(func):
def wrapper(*args, **kwargs):
for attempt in range(allowed_attempts):
try:
result = func(*args, **kwargs)
break
except RuntimeError: # When x.is_cuda, it usually just works on second attempt
if attempt + 1 == allowed_attempts:
raise RuntimeError(
f'RuntimeError: {allowed_attempts} successive failed attempts : ' +
f'Probably cublas runtime error'
)
else:
pass
return result
return wrapper
return decorator