-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
51 lines (38 loc) · 1.38 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
import torch
import torch.nn as nn
class QNetwork(nn.Module):
def __init__(self, input_dim, output_dim, hidden_dim) -> None:
"""DQN Network
Args:
input_dim (int): `state` dimension.
`state` is 2-D tensor of shape (n, input_dim)
output_dim (int): Number of actions.
Q_value is 2-D tensor of shape (n, output_dim)
hidden_dim (int): Hidden dimension in fc layer
"""
super(QNetwork, self).__init__()
self.layer1 = torch.nn.Sequential(
torch.nn.Linear(input_dim, hidden_dim),
torch.nn.PReLU()
)
self.layer2 = torch.nn.Sequential(
torch.nn.Linear(hidden_dim, hidden_dim),
torch.nn.PReLU()
)
self.layer3 = torch.nn.Sequential(
torch.nn.Linear(hidden_dim, hidden_dim),
torch.nn.PReLU()
)
self.final = torch.nn.Linear(hidden_dim, output_dim)
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""Returns a Q_value
Args:
x (torch.Tensor): `State` 2-D tensor of shape (n, input_dim)
Returns:
torch.Tensor: Q_value, 2-D tensor of shape (n, output_dim)
"""
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.final(x)
return x