-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
57 lines (47 loc) · 1.81 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
import torch
import torch.nn as nn
class DuelingQNetwork(nn.Module):
"""Actor (Policy) Model Using Dueling Architecture
Paper: Dueling Network Architectures for Deep Reinforcement Learning (https://arxiv.org/abs/1511.06581)
"""
def __init__(self, state_size, action_size, seed):
"""Initialize parameters and build model.
Params
======
state_size (int): Dimension of each state
action_size (int): Dimension of each action
seed (int): Random seed; unused if seed value is negative
"""
super(DuelingQNetwork, self).__init__()
if seed >= 0:
self.seed = torch.manual_seed(seed)
# Create the common fully connected layers
self.fc = nn.Sequential(
nn.Linear(state_size, 512),
nn.LeakyReLU(),
nn.Linear(512, 256),
nn.LeakyReLU(),
nn.Linear(256, 256),
nn.LeakyReLU()
)
# Fully connected advantage part (output size: action_size)
self.adv_part = nn.Sequential(
nn.Linear(256, 64),
nn.LeakyReLU(),
nn.Linear(64, action_size)
)
# Fully connected value part (output size: 1)
self.value_part = nn.Sequential(
nn.Linear(256, 64),
nn.LeakyReLU(),
nn.Linear(64, 1)
)
def forward(self, state):
"""Build a network that maps state -> values & advantages"""
# Get the output from the common fully connected layers
x = self.fc(state)
# Pass the output of the common fully connected layers to the advantage and value parts
val = self.value_part(x)
adv = self.adv_part(x)
# Return value according the the dueling paper
return val + (adv - adv.mean(dim=1, keepdim=True))