-
Notifications
You must be signed in to change notification settings - Fork 16
/
gan_train.py
221 lines (188 loc) · 10.3 KB
/
gan_train.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
from __future__ import print_function
import yaml
import time
import os
import sys
import numpy as np
import logging
from argparse import ArgumentParser
import tensorflow as tf
from utils import DataUtil, AttrDict
from model import Model
from cnn_text_discriminator import text_DisCNN
from share_function import deal_generated_samples
from share_function import deal_generated_samples_to_maxlen
from share_function import extend_sentence_to_maxlen
from share_function import prepare_gan_dis_data
from share_function import FlushFile
def gan_train(config):
sess_config = tf.ConfigProto()
sess_config.gpu_options.allow_growth = True
sess_config.allow_soft_placement = True
default_graph=tf.Graph()
with default_graph.as_default():
sess = tf.Session(config=sess_config, graph=default_graph)
logger = logging.getLogger('')
du = DataUtil(config=config)
du.load_vocab(src_vocab=config.generator.src_vocab,
dst_vocab=config.generator.dst_vocab,
src_vocab_size=config.src_vocab_size_a,
dst_vocab_size=config.src_vocab_size_b)
generator = Model(config=config, graph=default_graph, sess=sess)
generator.build_variational_train_model()
generator.init_and_restore(modelFile=config.generator.modelFile)
dis_filter_sizes = [i for i in range(1, config.discriminator.dis_max_len, 4)]
dis_num_filters = [(100 + i * 10) for i in range(1, config.discriminator.dis_max_len, 4)]
discriminator_src = text_DisCNN(
sess=sess,
max_len=config.discriminator.dis_max_len,
num_classes=2,
vocab_size_s=config.dst_vocab_size_a,
batch_size=config.discriminator.dis_batch_size,
dim_word=config.discriminator.dis_dim_word,
filter_sizes=dis_filter_sizes,
num_filters=dis_num_filters,
source_dict=config.discriminator.dis_src_vocab,
gpu_device=config.discriminator.dis_gpu_devices,
s_domain_data=config.discriminator.s_domain_data,
s_domain_generated_data=config.discriminator.s_domain_generated_data,
dev_s_domain_data=config.discriminator.dev_s_domain_data,
dev_s_domain_generated_data=config.discriminator.dev_s_domain_generated_data,
max_epoches=config.discriminator.dis_max_epoches,
dispFreq=config.discriminator.dis_dispFreq,
saveFreq=config.discriminator.dis_saveFreq,
saveto=config.discriminator.dis_saveto,
reload=config.discriminator.dis_reload,
clip_c=config.discriminator.dis_clip_c,
optimizer=config.discriminator.dis_optimizer,
reshuffle=config.discriminator.dis_reshuffle,
scope=config.discriminator.dis_scope
)
discriminator_trg = text_DisCNN(
sess=sess,
max_len=config.discriminator.dis_max_len,
num_classes=2,
vocab_size_s=config.dst_vocab_size_b,
batch_size=config.discriminator.dis_batch_size,
dim_word=config.discriminator.dis_dim_word,
filter_sizes=dis_filter_sizes,
num_filters=dis_num_filters,
source_dict=config.discriminator.dis_dst_vocab,
gpu_device=config.discriminator.dis_gpu_devices,
s_domain_data=config.discriminator.t_domain_data,
s_domain_generated_data=config.discriminator.t_domain_generated_data,
dev_s_domain_data=config.discriminator.dev_t_domain_data,
dev_s_domain_generated_data=config.discriminator.dev_t_domain_generated_data,
max_epoches=config.discriminator.dis_max_epoches,
dispFreq=config.discriminator.dis_dispFreq,
saveFreq=config.discriminator.dis_saveFreq,
saveto=config.discriminator.dis_saveto_trg,
reload=config.discriminator.dis_reload,
clip_c=config.discriminator.dis_clip_c,
optimizer=config.discriminator.dis_optimizer,
reshuffle=config.discriminator.dis_reshuffle,
scope=config.discriminator.dis_scope_trg
)
batch_iter = du.get_training_batches(
set_train_src_path=config.generator.src_path,
set_train_dst_path=config.generator.dst_path,
set_batch_size=config.generator.batch_size,
set_max_length=config.generator.max_length
)
for epoch in range(1, config.gan_iter_num + 1):
for gen_iter in range(config.gan_gen_iter_num):
batch = next(batch_iter)
x, y = batch[0], batch[1]
generate_ab, generate_ba = generator.generate_step(x, y)
logging.info("generate the samples")
generate_ab_dealed, generate_ab_mask = deal_generated_samples(generate_ab, du.dst2idx)
generate_ba_dealed, generate_ba_mask = deal_generated_samples(generate_ba, du.src2idx)
#
#### for debug
##print('the sample is ')
##sample_str=du.indices_to_words(y_sample_dealed, 'dst')
##print(sample_str)
#
x_to_maxlen = extend_sentence_to_maxlen(x)
y_to_maxlen = extend_sentence_to_maxlen(y)
logging.info("calculate the reward")
rewards_ab = generator.get_reward(x=x,
x_to_maxlen=x_to_maxlen,
y_sample=generate_ab_dealed,
y_sample_mask=generate_ab_mask,
rollnum=config.rollnum,
disc=discriminator_trg,
max_len=config.discriminator.dis_max_len,
bias_num=config.bias_num,
data_util=du,
direction='ab')
rewards_ba = generator.get_reward(x=y,
x_to_maxlen=y_to_maxlen,
y_sample=generate_ba_dealed,
y_sample_mask=generate_ba_mask,
rollnum=config.rollnum,
disc=discriminator_src,
max_len=config.discriminator.dis_max_len,
bias_num=config.bias_num,
data_util=du,
direction='ba')
loss_ab = generator.generate_step_and_update(x, generate_ab_dealed, rewards_ab)
loss_ba = generator.generate_step_and_update(y, generate_ba_dealed, rewards_ba)
print("the reward for ab and ba is ", rewards_ab, rewards_ba)
print("the loss is for ab and ba is", loss_ab, loss_ba)
logging.info("save the model into %s" % config.generator.modelFile)
generator.saver.save(generator.sess, config.generator.modelFile)
#### modified to here, next starts from here
logging.info("prepare the gan_dis_data begin")
data_num = prepare_gan_dis_data(
train_data_source=config.generator.src_path,
train_data_target=config.generator.dst_path,
gan_dis_source_data=config.discriminator.s_domain_data,
gan_dis_positive_data=config.discriminator.t_domain_data,
num=config.generate_num,
reshuf=True
)
logging.info("generate and the save t_domain_generated_data in to %s." %config.discriminator.t_domain_generated_data)
generator.generate_and_save(data_util=du,
infile=config.discriminator.s_domain_data,
generate_batch=config.discriminator.dis_batch_size,
outfile=config.discriminator.t_domain_generated_data,
direction='ab'
)
logging.info("generate and the save s_domain_generated_data in to %s." %config.discriminator.s_domain_generated_data)
generator.generate_and_save(data_util=du,
infile=config.discriminator.t_domain_data,
generate_batch=config.discriminator.dis_batch_size,
outfile=config.discriminator.s_domain_generated_data,
direction='ba'
)
logging.info("prepare %d gan_dis_data done!" %data_num)
logging.info("finetuen the discriminator begin")
discriminator_src.train(max_epoch=config.gan_dis_iter_num,
s_domain_data=config.discriminator.s_domain_data,
s_domain_generated_data=config.discriminator.s_domain_generated_data,
)
discriminator_src.saver.save(discriminator_src.sess, discriminator_src.saveto)
discriminator_trg.train(max_epoch=config.gan_dis_iter_num,
s_domain_data=config.discriminator.t_domain_data,
s_domain_generated_data=config.discriminator.t_domain_generated_data,
)
discriminator_trg.saver.save(discriminator_trg.sess, discriminator_trg.saveto)
logging.info("finetune the discrimiantor done!")
logging.info('reinforcement training done!')
if __name__ == '__main__':
sys.stdout = FlushFile(sys.stdout)
parser = ArgumentParser()
parser.add_argument('-c', '--config', dest='config')
args = parser.parse_args()
# Read config
config = AttrDict(yaml.load(open(args.config)))
# Logger
if not os.path.exists(config.logdir):
os.makedirs(config.logdir)
logging.basicConfig(filename=config.logdir+'/train.log', level=logging.INFO)
console = logging.StreamHandler()
console.setLevel(logging.DEBUG)
logging.getLogger('').addHandler(console)
# Train
gan_train(config)