-
Notifications
You must be signed in to change notification settings - Fork 0
/
program_generator.py
297 lines (253 loc) · 12.2 KB
/
program_generator.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# Copyright 2017-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the license available at
# https://github.com/facebookresearch/clevr-iep/blob/master/LICENSE
#
# Modifications by David Mascharka to update the code for compatibility with PyTorch >0.1 lead to:
# DISTRIBUTION STATEMENT A. Approved for public release: distribution unlimited.
#
# This material is based upon work supported by the Assistant Secretary of Defense for Research and
# Engineering under Air Force Contract No. FA8721-05-C-0002 and/or FA8702-15-D-0001. Any opinions,
# findings, conclusions or recommendations expressed in this material are those of the author(s) and
# do not necessarily reflect the views of the Assistant Secretary of Defense for Research and
# Engineering.
#
# © 2017 Massachusetts Institute of Technology.
#
# MIT Proprietary, Subject to FAR52.227-11 Patent Rights - Ownership by the contractor (May 2014)
#
# The software/firmware is provided to you on an As-Is basis
#
# Delivered to the U.S. Government with Unlimited Rights, as defined in DFARS Part 252.227-7013 or
# 7014 (Feb 2014). Notwithstanding any copyright notice, U.S. Government rights in this work are
# defined by DFARS 252.227-7013 or DFARS 252.227-7014 as detailed above. Use of this work other than
# as specifically authorized by the U.S. Government may violate any copyrights that exist in this
# work.
import torch
import torch.nn.functional as F
import torch.nn as nn
import numpy as np
import h5py
from pathlib import Path
from torch.distributions import Categorical
class Seq2Seq(nn.Module):
def __init__(self,
encoder_vocab_size=93,
decoder_vocab_size=44,
wordvec_dim=300,
hidden_dim=256,
rnn_num_layers=2,
rnn_dropout=0,
null_token=0,
start_token=1,
end_token=2,
q_learn=False):
super().__init__()
self.encoder_embed = nn.Embedding(encoder_vocab_size, wordvec_dim)
self.encoder_rnn = nn.GRU(wordvec_dim, hidden_dim, rnn_num_layers,
dropout=rnn_dropout, batch_first=True)
self.decoder_embed = nn.Embedding(decoder_vocab_size, wordvec_dim)
self.decoder_rnn = nn.GRU(wordvec_dim + hidden_dim, hidden_dim, rnn_num_layers,
dropout=rnn_dropout, batch_first=True)
self.decoder_linear = nn.Linear(hidden_dim, decoder_vocab_size)
if q_learn:
self.q_linear = nn.Linear(hidden_dim, 1)
self.NULL = null_token
self.START = start_token
self.END = end_token
self.q_learn = q_learn
self.policy_history = []
self.entropy_a, self.entropy_b = [], []
self.q_rewards = []
self.pg_max_len = 30
self.beta = 1
self.reset_param()
def init_policy(self):
self.policy_history = []
self.entropy_a, self.entropy_b = [], []
self.q_rewards = []
def reset_param(self):
for name, param in self.encoder_rnn.named_parameters():
if 'weight' in name:
nn.init.xavier_normal_(param)
if 'bias' in name:
nn.init.constant_(param, 0)
for name, param in self.decoder_rnn.named_parameters():
if 'weight' in name:
nn.init.xavier_normal_(param)
if 'bias' in name:
nn.init.constant_(param, 0)
if self.q_learn:
nn.init.xavier_normal_(self.q_linear.weight)
nn.init.constant_(self.q_linear.bias, 0)
nn.init.xavier_normal_(self.decoder_linear.weight)
nn.init.constant_(self.decoder_linear.bias, 0)
nn.init.kaiming_uniform_(self.encoder_embed.weight)
nn.init.kaiming_uniform_(self.decoder_embed.weight)
def get_dims(self, x=None, y=None):
V_in = self.encoder_embed.num_embeddings
V_out = self.decoder_embed.num_embeddings
D = self.encoder_embed.embedding_dim
H = self.encoder_rnn.hidden_size
L = self.encoder_rnn.num_layers
N = x.size(0) if x is not None else None
N = y.size(0) if N is None and y is not None else N
T_in = x.size(1) if x is not None else None
T_out = y.size(1) if y is not None else None
return V_in, V_out, D, H, L, N, T_in, T_out
def before_rnn(self, x, replace=0):
N, T = x.size()
idx = 0
# Find the last non-null element in each sequence.
x_cpu = x.cpu()
for i in range(N):
for t in range(T - 1):
if x_cpu[i, t] != self.NULL and x_cpu[i, t + 1] == self.NULL:
idx = t
break
return x, idx
def encoder(self, x):
V_in, V_out, D, H, L, N, T_in, T_out = self.get_dims(x=x)
x, idx = self.before_rnn(x)
embed = self.encoder_embed(x)
h0 = torch.zeros(L, N, H).type_as(embed)
out, _ = self.encoder_rnn(embed, h0)
# Pull out the hidden state for the last non-null value in each input
out = out[:, idx, :].view(N, H)
#if out.requires_grad:
# out.register_hook(lambda grad: grad * 100)
return out
def decoder(self, encoded, y, h0=None, c0=None):
V_in, V_out, D, H, L, N, T_in, T_out = self.get_dims(y=y)
if T_out > 1:
y, _ = self.before_rnn(y)
y_embed = self.decoder_embed(y)
encoded_repeat = encoded.view(N, 1, H)
encoded_repeat = encoded_repeat.expand(N, T_out, H)
rnn_input = torch.cat([encoded_repeat, y_embed], 2)
if h0 is None:
h0 = torch.zeros(L, N, H).type_as(encoded)
rnn_output, ht = self.decoder_rnn(rnn_input, h0)
rnn_output_2d = rnn_output.contiguous().view(N * T_out, H)
output_logprobs = self.decoder_linear(rnn_output_2d).view(N, T_out, V_out)
reward_predict = F.relu(self.q_linear(rnn_output_2d)) if self.q_learn else None
return output_logprobs, reward_predict, ht
def forward(self, x, max_length=30, mode='soft'):
N, T = x.size(0), max_length
encoded = self.encoder(x)
cur_input = torch.LongTensor(N, 1).fill_(self.START).cuda()
y_probs = []
policy_hist = []
entropy_a, entropy_b = [], []
q_rew = []
h, c = None, None
for t in range(T):
# Compute logprobs
logprobs, q, h = self.decoder(encoded, cur_input, h0=h)
if self.q_learn:
q_rew.append(torch.squeeze(q))
probs = F.softmax(logprobs.view(N, -1), dim=-1)
if mode == 'soft':
y_probs.append(probs)
_, cur_input = probs.max(1)
elif mode == 'gumbel':
y_probs.append(F.gumbel_softmax(logprobs.view(N, -1), hard=True))
_, cur_input = probs.max(1)
elif 'hard' in mode:
# Distrubution probability
m = Categorical(probs)
entropy_a.append(probs)
entropy_b.append(m.entropy())
# Sampling
action = m.sample()
# Create output vector (one-hot)
y = torch.zeros(1, probs.size(1)).cuda()
y[0, action.item()] = 1
y_probs.append(y)
# Update policy history
policy_hist.append(torch.squeeze(torch.matmul(y, probs.view(-1, 1))))
_, cur_input = probs.max(1)
cur_input = torch.unsqueeze(cur_input, 0)
# Stop if generated token is an end_token
if (cur_input[0].item() == self.END and t >= 2) or t >= max_length - 1:
# Add scene
y = torch.zeros(1, probs.size(1)).cuda()
y[0, 41] = 1
y_probs.append(y)
break
if 'hard' in mode:
self.entropy_a.append(Categorical(torch.sum(torch.stack(entropy_a, 1), 1).div(len(y_probs))).entropy())
self.entropy_b.append(torch.sum(torch.stack(entropy_b, 1), 1).div(len(y_probs)))
self.policy_history.append(torch.stack(policy_hist))
if self.q_learn:
self.q_rewards.append(torch.stack(q_rew))
y_probs = torch.squeeze(torch.stack(y_probs, 1))
return y_probs
def reinforce_penalty(self, reward, penalty):
policy_loss = []
batch_size = len(self.policy_history)
for sample, rew, pen, entropy_a, entropy_b in zip(self.policy_history, reward, penalty, self.entropy_a, self.entropy_b):
gamma = 0.1
seq_len = sample.size(-1)
# More reward if are using end_token
more_rew = 0.2 if seq_len < 30 else 0
# Discount reward in long sequences
rew = rew.add(more_rew).mul(np.power(0.99, seq_len))
# Loss reward
loss_rew = torch.mul(torch.sum(-torch.log(sample.clamp(min=1e-6)), -1), rew)
# Loss penalty
loss_penalty = torch.mul(torch.sum(-torch.log(sample.clamp(min=1e-6)), -1), pen)
self.beta = entropy_a/loss_penalty
# Total Loss = reward + penalty + entropy_program + exploration + branch_factors
loss = (1*loss_rew).add(gamma*loss_penalty).add(-self.beta*entropy_a).add(0.01*-entropy_b)
policy_loss.append(loss)
self.entropy_a = torch.stack(self.entropy_a).sum() / batch_size
self.entropy_b = torch.stack(self.entropy_b).sum() / batch_size
policy_loss = torch.stack(policy_loss).sum() / batch_size
policy_loss.backward()
return policy_loss.data, torch.mean(reward), self.entropy_a, self.entropy_b
def reinforce_reward(self, reward):
policy_loss = []
batch_size = len(self.policy_history)
for sample, rew, entropy_a, entropy_b in zip(self.policy_history, reward, self.entropy_a,
self.entropy_b):
gamma = 0.1
seq_len = sample.size(-1)
# More reward if are using end_token
more_rew = 0.2 if seq_len < 30 else 0
# Discount reward in long sequences
rew = rew.add(more_rew).mul(np.power(0.99, seq_len))
# Loss reward
loss_rew = torch.mul(torch.sum(-torch.log(sample.clamp(min=1e-6)), -1), rew)
# Total Loss = reward + penalty + entropy_program + exploration + branch_factors
loss = (1 * loss_rew).add(-self.beta * entropy_a).add(0.01 * -entropy_b)
policy_loss.append(loss)
self.entropy_a = torch.stack(self.entropy_a).sum() / batch_size
self.entropy_b = torch.stack(self.entropy_b).sum() / batch_size
policy_loss = torch.stack(policy_loss).sum() / batch_size
policy_loss.backward()
return policy_loss.data, torch.mean(reward), self.entropy_a, self.entropy_b
def q_reinforce(self, reward):
policy_loss = []
batch_size = len(self.policy_history)
q_loss = []
for sample, rew, q_rew, entropy_a, entropy_b in zip(self.policy_history,
reward, self.q_rewards, self.entropy_a, self.entropy_b):
# Q function
q_err = (rew - torch.sum(q_rew)).pow(2)
q_loss.append(q_err)
# Discount reward in long sequences
discount = 0.99
for i, r in enumerate(q_rew):
q_rew[i] = r * np.power(discount, i)
# Loss reward
loss_rew = torch.sum(torch.mul(-torch.log(sample.clamp(min=1e-6)), q_rew), -1)
policy_loss.append(loss_rew)
self.entropy_a = torch.stack(self.entropy_a).sum() / batch_size
self.entropy_b = torch.stack(self.entropy_b).sum() / batch_size
policy_loss = torch.stack(policy_loss).sum() / batch_size
q_loss = torch.stack(q_loss).sum() / batch_size
q_loss.backward(retain_graph=True)
policy_loss.backward()
return policy_loss.data, torch.mean(reward), self.entropy_a, self.entropy_b, q_loss.data