-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
39 lines (33 loc) · 1.03 KB
/
model.py
File metadata and controls
39 lines (33 loc) · 1.03 KB
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
import torch
import torch.nn as nn
import torch.nn.functional as F
class DQN(nn.Module):
def __init__(
self,
n_observations,
n_actions,
):
""" Initialize Deep Q Network
"""
super().__init__()
self.layer1 = nn.Linear(n_observations, 42, dtype=torch.float32)
self.layer2 = nn.Linear(42, 42, dtype=torch.float32)
self.layer3 = nn.Linear(42, n_actions, dtype=torch.float32)
def forward(self, x: torch.Tensor) -> torch.Tensor:
""" Forward pass
"""
x = F.relu(self.layer1(x))
x = F.relu(self.layer2(x))
return self.layer3(x)
def save(self, filename):
""" Save model
"""
torch.save(self.state_dict(), filename)
def load(self, filename):
""" Load model
"""
self.load_state_dict(torch.load(filename))
def __call__(self, *args, **kwargs) -> torch.Tensor:
""" Call model (redefinition for type hinting)
"""
return super().__call__(*args, **kwargs)