-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmodel.py
191 lines (156 loc) · 5.84 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import torch
import torch.nn as nn
import torch.nn.functional as F
# Convolutional Encording
class ConvEncoder(nn.Module):
"""
Encoder with convolution
Attributes:
conv1 : first convolutional layer
conv2 : second convolutional layer
conv3 : third convolutional layer
flatten : for tensor to be flatten
fc : full connected layer
"""
def __init__(self, units, n_frames, hidden_list=[32, 64, 64], kernel_list=[8, 4, 3], stride_list=[4, 2, 1], flatten_dim=3136):
super(ConvEncoder, self).__init__()
"""
units (int): dim of output
n_frames (int): dim of input
"""
self.conv1 = nn.Conv2d(n_frames, hidden_list[0], kernel_list[0], stride=stride_list[0])
self.conv2 = nn.Conv2d(hidden_list[0], hidden_list[1], kernel_list[1], stride=stride_list[1])
self.conv3 = nn.Conv2d(hidden_list[1], hidden_list[2], kernel_list[2], stride=stride_list[2])
self.flatten = nn.Flatten()
self.fc = nn.Linear(flatten_dim, units)
def forward(self, x):
"""
Args:
x (torch.tensor): input [b, n_frames, 84, 84]
Returns
x (torch.tensor): ouput [b, units]
"""
x = F.relu(self.conv1(x)) # (b, 32, 20, 20)
x = F.relu(self.conv2(x)) # (b, 64, 9, 9)
x = F.relu(self.conv3(x)) # (b, 64, 7, 7)
x = self.flatten(x) # (b, 3136)
x = self.fc(x) # (b, units)
return x
# intrinsic or extrinsic QNetwork
class QNetwork(nn.Module):
"""
Attributes:
action_space (int): dim of action space
num_arms (int): number of arms used in multi-armed bandit problem
conv_encoder : convolutional encoder
lstm : LSTM layer
fc : fully connected layer
fc_adv : fully connected layer to get advantage
fc_v : fully connected layer to get value
"""
def __init__(self, action_space, n_frames, hidden=512, units=512, num_arms=32):
super(QNetwork, self).__init__()
"""
Args:
action_space (int): dim of action space
n_frames (int): number of images to be stacked
"""
self.action_space = action_space
self.num_arms = num_arms
self.conv_encoder = ConvEncoder(units, n_frames)
self.lstm = nn.LSTM(input_size=units+self.action_space+self.num_arms+2,
hidden_size=hidden,
batch_first=False)
self.fc = nn.Linear(hidden, hidden)
self.fc_adv = nn.Linear(hidden, action_space)
self.fc_v = nn.Linear(hidden, 1)
def forward(self, input, states, prev_action, prev_in_rewards, prev_ex_rewards, j):
"""
Args:
input (torch.tensor): state [b, n_frames, 84, 84]
prev_action (torch.tensor): previous action [b]
prev_in_rewards (torch.tensor): previous intrinsic reward [b]
prev_ex_rewards (torch.tensor): previous extrinsic reward [b]
"""
# (b, q_units)
x = F.relu(self.conv_encoder(input))
# (b, action_space)
prev_action_onehot = F.one_hot(prev_action, num_classes=self.action_space)
# (b, num_arms)
j_onehot = F.one_hot(j, num_classes=self.num_arms)
# (b, q_units+action_space+num_arms+2)
x = torch.cat([x, prev_action_onehot, prev_in_rewards[:, None], prev_ex_rewards[:, None], j_onehot], dim=1)
# (1, b, hidden)
x, states = self.lstm(x.unsqueeze(0), states)
# (b, action_space)
A = self.fc_adv(x.squeeze(0))
# (b, 1)
V = self.fc_v(x.squeeze(0))
# (b, action_space)
Q = V.expand(-1, self.action_space) + A - A.mean(1, keepdim=True).expand(-1, self.action_space)
return Q, states
class EmbeddingNet(nn.Module):
"""
Attributes
conv_encoder : convolutional encoder
"""
def __init__(self, n_frames, units=32):
super(EmbeddingNet, self).__init__()
"""
Args:
n_frames (int): number of images to be stacked
"""
self.conv_encoder = ConvEncoder(units, n_frames)
def forward(self, inputs):
"""
Args:
input (torch.tensor): state [b, n_frames, 84, 84]
Returns:
embeded state [b, emebed_units]
"""
return F.relu(self.conv_encoder(inputs))
class EmbeddingClassifer(nn.Module):
"""
Attributes:
fc1 : fully connected layer
fc2 : fully connected layer to get action probability
"""
def __init__(self, action_space, hidden=128):
super(EmbeddingClassifer, self).__init__()
"""
Args:
action_space (int): dim of action space
"""
self.fc1 = nn.Linear(64, hidden)
self.fc2 = nn.Linear(hidden, action_space)
def forward(self, input1, input2):
"""
Args:
embeded state (torch.tensor): state [b, emebed_units]
Returns:
action probability [b, action_space]
"""
x = torch.cat([input1, input2], dim=1)
x = F.relu(self.fc1(x))
x = F.softmax(self.fc2(x), dim=1)
return x
class LifeLongNet(nn.Module):
"""
Attributes
conv_encoder : convolutional encoder
"""
def __init__(self, n_frames, units=128):
super(LifeLongNet, self).__init__()
"""
Args:
n_frames (int): number of images to be stacked
"""
self.conv_encoder = ConvEncoder(units, n_frames)
def forward(self, inputs):
"""
Args:
input (torch.tensor): state [b, n_frames, 84, 84]
Returns:
lifelong state [b, lifelong_units]
"""
return self.conv_encoder(inputs)