-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
127 lines (103 loc) · 4.03 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import torch
import torch.nn as nn
import torch.nn.functional as F
def createblocklist(nu, dropout = None):
'''This function return a list of modules to be implemented as a sequential layer block
Input variables:
nu: array with number of filters (or strides for pooling layers) for different layers of the network
Output:
list of pytorch layers
'''
# Initial conv+relu that takes previous number of filters to the new one
if dropout is None:
modlist = [[nn.Linear(nu[i],nu[i+1]),nn.ReLU()] for i in range(len(nu)-1)]
else:
modlist = [[nn.Linear(nu[i],nu[i+1]),nn.ReLU(), dropout]
for i in range(len(nu)-1)]
modlist = [x for sublist in modlist for x in sublist]
return modlist
class QNetwork(nn.Module):
"""Actor (Policy) Model."""
def __init__(self, state_size, action_size, seed, nu = None, dropout = None):
"""Initialize parameters and build model.
Params
======
state_size (int): Dimension of each state
action_size (int): Dimension of each action
seed (int): Random seed
"""
super(QNetwork, self).__init__()
self.seed = torch.manual_seed(seed)
self.state_size = state_size
self.action_size = action_size
if nu is None:
nu = [32, 64, 128, 512]
self.nu = nu
if dropout is not None:
self.dropout = nn.Dropout(dropout)
else:
self.dropout = None
self.FC0 = nn.Linear(state_size,nu[0])
self.FClist = nn.Sequential(*createblocklist(nu, self.dropout))
self.FCf = nn.Linear(nu[-1], action_size)
self.relu = nn.ReLU()
def forward(self, state):
"""Build a network that maps state -> action values."""
x = self.FC0(state)
x = self.relu(x)
x = self.FClist(x)
x = self.FCf(x)
return x
class DuelQNetwork(nn.Module):
"""Actor (Policy) Model."""
def __init__(self, state_size, action_size, seed,
nu = None, nu_state = None, nu_adv = None, dropout = None):
"""Initialize parameters and build model.
Params
======
state_size (int): Dimension of each state
action_size (int): Dimension of each action
seed (int): Random seed
nu : list of number of units of a sequential list of FC layers
nu_state : similar list for the part of the state value Sv
nu_adv : similar list for the Advantage
nu : number of units
"""
super(DuelQNetwork, self).__init__()
self.seed = torch.manual_seed(seed)
self.state_size = state_size
self.action_size = action_size
if nu is None:
nu = [32, 64, 128, 512]
self.nu = nu
if nu_state is None:
nu_state = [128]
self.nu_state = nu_state
if nu_adv is None:
nu_adv = [128]
self.nu_adv = nu_adv
if dropout is not None:
self.dropout = nn.Dropout(dropout)
else:
self.dropout = None
self.FC0 = nn.Linear(state_size,nu[0])
self.FClist = nn.Sequential(*createblocklist(nu, self.dropout))
nut = [nu[-1], *nu_state]
self.FClstate = nn.Sequential(*createblocklist(nut, self.dropout))
self.FCfstate = nn.Linear(nut[-1], 1)
nut = [nu[-1], *nu_adv]
self.FCladv = nn.Sequential(*createblocklist(nut, self.dropout))
self.FCfadv = nn.Linear(nut[-1], action_size)
self.relu = nn.ReLU()
def forward(self, state):
"""Build a network that maps state -> action values."""
x = self.FC0(state)
x = self.relu(x)
x = self.FClist(x)
state = self.FClstate(x)
state = self.FCfstate(state)
adv = self.FCladv(x)
adv = self.FCfadv(adv)
# Removing ambiguous constant
advmean = adv.mean(1,keepdim=True)
return state+adv-advmean