-
Notifications
You must be signed in to change notification settings - Fork 27
/
cot.py
210 lines (188 loc) · 8.77 KB
/
cot.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
import numpy as np
import tensorflow as tf
import random
from dataloader import Gen_Data_loader
from generator import Generator
from mediator import Mediator
from target_lstm import TARGET_LSTM
import pickle
#########################################################################################
# Generator Hyper-parameters
######################################################################################
EMB_DIM = 32 # embedding dimension
HIDDEN_DIM = 32 # hidden state dimension of lstm cell
SEQ_LENGTH = 20 # sequence length
START_TOKEN = 0
PRE_EPOCH_NUM = 0 # supervise (maximum likelihood estimation) epochs (not recommended)
SEED = 88
BATCH_SIZE = 64
M_DROPOUT_RATE = 0.5 # Dropout rate of M (optional)
RESTORE = False
#########################################################################################
# Basic Training Parameters
#########################################################################################
TOTAL_BATCH = 200000
positive_file = 'save/real_data.txt'
negative_file = 'save/generator_sample.txt'
eval_file = 'save/eval_file.txt'
generated_num = 10000
def generate_samples(sess, trainable_model, batch_size, generated_num, output_file):
# Generate Samples
generated_samples = []
for _ in range(int(generated_num / batch_size)):
generated_samples.extend(trainable_model.generate(sess))
with open(output_file, 'w') as fout:
for poem in generated_samples:
buffer = ' '.join([str(x) for x in poem]) + '\n'
fout.write(buffer)
def target_loss(sess, target_lstm, data_loader):
# target_loss means the oracle negative log-likelihood tested with the oracle model "target_lstm"
# For more details, please see the Section 4 in https://arxiv.org/abs/1609.05473
nll = []
data_loader.reset_pointer()
for it in range(data_loader.num_batch):
batch = data_loader.next_batch()
g_loss = sess.run(target_lstm.likelihood_loss, {target_lstm.x: batch})
nll.append(g_loss)
return np.mean(nll)
def mle_epoch(sess, trainable_model, data_loader):
# Pre-train the generator using MLE for one epoch
supervised_g_losses = []
data_loader.reset_pointer()
for it in range(data_loader.num_batch):
batch = data_loader.next_batch()
_, g_loss = trainable_model.maximum_likelihood(sess, batch)
supervised_g_losses.append(g_loss)
return np.mean(supervised_g_losses)
def jsd_calculate(sess, generator, oracle, sample_window=200):
real_s = []
fake_s = []
jsd = []
for it in range(sample_window):
real_s.append(oracle.generate(sess))
fake_s.append(generator.generate(sess))
for s in real_s:
p_g = sess.run(generator.g_prediction, feed_dict={generator.x:s})
p_p = sess.run(oracle.g_prediction, feed_dict={oracle.x:s})
p_m = 0.5 * (p_g + p_p)
log_p_p = np.log(p_p)
log_p_m = np.log(p_m)
log_kl_gm = np.mean(np.sum(log_p_p - log_p_m, axis=-1))
jsd.append(log_kl_gm)
for s in fake_s:
p_g = sess.run(generator.g_prediction, feed_dict={generator.x:s})
p_p = sess.run(oracle.g_prediction, feed_dict={oracle.x:s})
p_m = 0.5 * (p_g + p_p)
log_p_g = np.log(p_g)
log_p_m = np.log(p_m)
log_kl_gm = np.mean(np.sum(log_p_g - log_p_m, axis=-1))
jsd.append(log_kl_gm)
jsd = np.mean(jsd)
return jsd
def main():
random.seed(SEED)
np.random.seed(SEED)
assert START_TOKEN == 0
gen_data_loader = Gen_Data_loader(BATCH_SIZE, SEQ_LENGTH)
gan_data_loader = Gen_Data_loader(BATCH_SIZE, SEQ_LENGTH)
val_data_loader = Gen_Data_loader(BATCH_SIZE, SEQ_LENGTH)
likelihood_data_loader = Gen_Data_loader(BATCH_SIZE, SEQ_LENGTH) # For testing
vocab_size = 5000
generator = Generator(vocab_size, BATCH_SIZE, EMB_DIM, HIDDEN_DIM, SEQ_LENGTH, START_TOKEN)
target_params = pickle.load(open('save/target_params_py3.pkl', 'rb'))
target_lstm = TARGET_LSTM(vocab_size, BATCH_SIZE, 32, 32, SEQ_LENGTH, START_TOKEN, target_params) # The oracle model
mediator = Mediator(vocab_size, BATCH_SIZE, EMB_DIM * 2, HIDDEN_DIM * 2, SEQ_LENGTH, START_TOKEN,
name="mediator", dropout_rate=M_DROPOUT_RATE, learning_rate=3e-3,
with_professor_forcing=False)
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)
sess.run(tf.global_variables_initializer())
# First, use the oracle model to provide the positive examples, which are sampled from the oracle data distribution
generate_samples(sess, target_lstm, BATCH_SIZE, generated_num, positive_file)
gen_data_loader.create_batches(positive_file)
gan_data_loader.create_batches(positive_file)
generate_samples(sess, target_lstm, BATCH_SIZE, generated_num, eval_file)
val_data_loader.create_batches(eval_file)
log = open('save/experiment-log.txt', 'w')
log_nll = open('save/experiment-log-nll.txt', 'w')
log_jsd = open('save/experiment-log-jsd.txt', 'w')
# pre-train generator (default 0 epochs)(not recommended)
print('Start pre-training...')
log.write('pre-training...\n')
saver = tf.train.Saver(tf.global_variables())
if RESTORE:
saver.restore(sess, "saved_model/CoT")
for epoch in range(PRE_EPOCH_NUM):
loss = mle_epoch(sess, generator, gen_data_loader)
if epoch % 1 == 0:
generate_samples(sess, generator, BATCH_SIZE, generated_num, negative_file)
likelihood_data_loader.create_batches(negative_file)
test_loss = target_loss(sess, target_lstm, likelihood_data_loader)
print('pre-train epoch ', epoch, 'nll_oracle ', test_loss)
buffer = 'epoch:\t'+ str(epoch) + '\tnll_oracle:\t' + str(test_loss) + '\n'
log_nll.write(buffer)
if epoch % 1 == 0:
test_loss = target_loss(sess, generator, val_data_loader)
print('pre-train epoch ', epoch, 'nll_test ', test_loss)
buffer = 'epoch:\t'+ str(epoch) + '\tnll_test:\t' + str(test_loss) + '\n'
log_nll.write(buffer)
print('#########################################################################')
print('Start Cooperative Training...')
for iter_idx in range(TOTAL_BATCH):
# Train the generator for one step
for it in range(1):
samples = generator.generate(sess)
rewards = mediator.get_reward(sess, samples)
feed = {generator.x: samples, generator.rewards: rewards}
_ = sess.run(generator.g_updates, feed_dict=feed)
# Test
if iter_idx % 100 == 0 or iter_idx == TOTAL_BATCH - 1:
generate_samples(sess, generator, BATCH_SIZE, generated_num, negative_file)
likelihood_data_loader.create_batches(negative_file)
test_loss = target_loss(sess, target_lstm, likelihood_data_loader)
buffer = 'batch:\t' + str(iter_idx) + '\tnll_oracle:\t' + str(test_loss) + '\n'
print('batch: ', iter_idx, 'nll_oracle: ', test_loss)
log_nll.write(buffer)
if iter_idx % 100 == 0:
test_loss = target_loss(sess, generator, val_data_loader)
print('batch:\t', iter_idx, 'nll_test ', test_loss)
buffer = 'batch:\t'+ str(iter_idx) + '\tnll_test:\t' + str(test_loss) + '\n'
log_nll.write(buffer)
# Train the mediator
for _ in range(1):
bnll_ = []
"""
d_loss_ = []
for it in range(3):
feed = {
mediator.x0: gan_data_loader.next_batch(),
mediator.x1: generator.generate(sess)
}
d_loss, _ = sess.run([mediator.d_loss, mediator.d_update], feed)
d_loss_.append(d_loss)
"""
for it in range(1):
feed = {
mediator.x0: gen_data_loader.next_batch(),
mediator.x1: generator.generate(sess)
}
bnll = sess.run(mediator.likelihood_loss, feed)
bnll_.append(bnll)
sess.run(mediator.dropout_on)
_ = sess.run(mediator.likelihood_updates, feed)
sess.run(mediator.dropout_off)
if iter_idx % 10 == 0:
bnll = np.mean(bnll_)
print("mediator cooptrain iter#%d, balanced_nll %f" % (iter_idx, bnll))
log.write("%d\t%f\n" % (iter_idx, bnll))
if iter_idx % gen_data_loader.num_batch == 0:
jsd = jsd_calculate(sess, generator, target_lstm)
print('cooptrain epoch#', iter_idx // gen_data_loader.num_batch, 'jsd ', jsd)
log_jsd.write("%d\t%f\n" % (iter_idx // gen_data_loader.num_batch, jsd))
saver.save(sess, "saved_model/CoT")
log.close()
log_nll.close()
log_jsd.close()
if __name__ == '__main__':
main()