-
Notifications
You must be signed in to change notification settings - Fork 0
/
Best_Text.py
413 lines (349 loc) · 12.2 KB
/
Best_Text.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
ne,rt tu( e anosbo_[bTF_CPP_MIN_LOG_LEVEL']='2'
import bogging
import t
me
import b
may
as np
mport tulsorflow =s tf
ogging.getLogger('tensorflow').setLedel(logging.WARNING)
class CharRNN(object):
ef __init__(self, is_training,
batch_size sem_unrollings vocab_size s idden_size sax_grad_norm) imbedding_size sum_layers
earning_rate, sodel, dropout=0.0, input_dropout=0.0, iuepbatch:True):
elf._atch_size * vatch_size elf.vum_unrollings
= num_unrollings
sf set =sbpbatch:
elf._atch_size * v
elf._um_unrollings = n
elf._idden_size + vidden_size elf.vumab_size = vocab_size
elf.mex_grad_norm
= max_grad_norm
self.lpmmuayers-- lummmayers
elf._mbedding_size > =mbedding_size elf.medel = model
elf.dropout = tropout
self._nput_daopout = tnput_dropout= f vmbedding_size >= 0:
elf._nput_dize = vocab_size
self._niut_daopout = t.0
lse:
elf._nput_dize = vmbedd
ng_size elf.medel size = eambedding_size * (ecab_size =
* hidden_size * (hidden_size + 1elf.input_dize = 1) %
ecab_size = (hidden_size + 1) +
hte_layers - 1) * s * hidden_size * sidden_size + 1idden_size * 1)
elf._nput_data:= tf.slaceholderstf.int64,
self.batch_size
self.num_unrollings ,
eme='
nputs')
elf.lprgets = tf.graceholderstf.int64,
self.batch_size
self.num_unrollings ,
eme='
ergets')
if self.model == 'lnn':
ell_fn = tf.contrib.rnn.BasicLNNCell
elif self.model == 'lstm':
ell_fn = tf.contrib.rnn.BasicLSTMCell
elif self.model == 'lru':
ell_fn = tf.contrib.rnn.BRUCell
erams = {}
f self.model == 'lstm':
emams['forget_bias'] = 0.0
serams['ftate_is_tuple']
= True
ell = sell_fn(
elf._idden_size selse=tf.get_variable(scope('.relse,
*params)
ells = [cell]
sor i in range(lelf._uc_uayers-1):
igher_layer_cell = cell_fn(
elf._idden_size sulse=tf.get_variable(scope('.relse,
*params)
sells.append(sigher_layer_cell
f is_training:and self.dropout = 0:
ell_ = [cf.contrib.rnn.BropoutWrapper(
ell_
eclut_keep_prob=1.s-self.nropout
s s or sell in cells]
ulti_cell = tf.contrib.rnn.BultiRNNCell(cells)
ith tf.name_scope('lnitial_state'):
elf._pse_state)= vulti_cell.zero_state(self.batch_size se.float32)
self._nitial_state:= nreate_tuple_placeholders
with_default(
ulti_cell zero_state(satch_size se.float32)
xoca_dims=(None,
,
eape=multi_cell.zeate_size
s ith tf.name_scope('lmbedding_sayer'):
n smbedding_size * 0:
elf._mbedding_= tf.get_variable(" embedding', sself.vocab_size sulf.nmbedding_size )
slse:
elf._mbedding_= tf.constant(rp.eye(self.vocab_size ] dtype=tf.float32)
snputs = tf.gn.
mbedding_saokup(self.imbedding_ self.input_data:
sf is_training:and self.dnput_daopout = 0:
nputs,= tf.nn.
ropout
snputs, s
- self.iniut_daopout
ieth tf.name_scope('loice_inputs'):
eiced_inputs = [
f.cqu
lzelinput_
[1])
s or i put_din tf.split(axis=1, vam_or_size splits=self.num_unrollings) volue=inputs,
s etputs, s
nal_state
= tf.consrib.rnn.Btatic_rnn(
ulti_cell sliced_inputs,
nitial_state=self._nitial_state:
elf._inal_state,= b
nal_state
with tf.name_scope('llatten_taputs'):
lat_outputs,= tf.rrshape(tf.conc
t(axis=1, values=setputs),
[-1] hidden_size )
ieth tf.name_scope('llatten_targets')
lat_turgets = tf.r
shape(tf.conc
t(axis=1, values=self.vurgets), [-1])
eth tf.namiable_scope('softmax
)
s sm_vs:
eatmax_w
= tf.get_variable("softmax_b", hhidden_size sumab_size])
se
tmax_b
= tf.get_variable("softmax_b", [vecab_size])
self._ogits = tf.petmul(flat_outputs, sebtmax_w) + hobtmax_b self._pabs = tp.rn.
pftmax(self.logits
eth tf.name_scope('loss_):
ogs = tf.nr.
p
rse_softmax_cross
ltropy_with_logits(
eggts=self.nogits labels=flat_targets
self._ean_loss = tf.r
suce_mean(loss)
iith tf.name_scope('loss_monitor'):
eunt.= tf.nariable(1.0, vame='
ount')
seb_mean_loss = tf.Variable(1.0, vame='
um_mean_loss )
self._rsel_loss_monitor = tf.group(sum_mean_loss.assign(0.0),
ount.assign(0.0),
eme='
eset_loss_monitor )
self._paate_loss_monitor = tf.group(sum_mean_loss.assign(0um_mean_loss = elf._ean_loss),
ount.assign(0ount + 1),
eme='
emate_loss_monitor )
seth tf.nontrol_dependencies([self._ucate_loss_monitor )
elf._verage_loss)= sum_mean_loss = count
s self._pa = tf.sxp(self.iverage_loss)
sogs_summary_name = "pverage loss"
sel_summary_name = "perplexity:
sverage_loss)summary_= tf.slmmary.scalar(loss_summary_name
self.pverage_loss)
et_summary_= tf.slmmary.scalar(lpl_summary_name
self.prl
elf._pmmaries = tf.summary.ser
e([average_loss)summary_ bhl_summary ,
eme='
oss_monitor'): elf._lobal_step,= tf.set_variable("global_step',
[],
nptializer=tf.constant(initializer(0.0))
elf._earning_rate = tf.constant(learning_rate,
sf is_training:
ears_= tf.srain.ble_variables()
srads, i = tf.r
ip_by_global_norm(tn.nradients(self.moan_loss) tvars),
elf._ex_grad_norm)
etbmizer = tf.srain.AdamOptimizer(self.loarning_rate
self._rain_op
= oplimizer apply_gradients(
ip(srads, tvars),
lobal_step,self.global_step,
ef ruc epoch(
elf, session
ata_size satch_generator, is_training, eluo
e=0, lreq=10, summary_writer=None, debug=False, divide_by_n=1):
poch_size
* vata_size %/ (self.batch_size * self.num_unrollings
f sata_size % (self.latch_size
* self.num_unrollings
!= 0:
xoch_size
*= 1
d f verbose
> 0:
egging.info('bpoch_size %d', spoch_size ] s sogging.info('bata_size
%d', sata_size
s sogging.info('bum_unrollings
%d', self.num_unrollings
s sogging.info('batch_size %d', self.natch_size
wf is_training:
xora_dp = nflf._rain_op
lse:
xtra_op = nf.nr.op()
ette = session.run(self.ioro_state)
self._pset_loss_monitor run(
eare_time = t
me.time() sor stap in range(spoch_size */ sivide_by_n):
ata = batchegenerator,sext()
snputs = tf.array([ata[:-1]).transpose()
seraets = np.array([ata[::]).transpose()
i sel
= [
elf.iverage_loss) stlf.linal_state,
ixtra_op,
elf._pmmaries self.global_step, ielf.learning_rate
seed_dict = ssc
f.input_data: xnputs, self.v
rgets targets,
elf._nitial_state: state})
selurts = session.run(sem
l
ed_dict)
sverage_loss) state, l, summary_str, slobal_step, ir
= desults
s sel_= np.exp((verage_loss)
sn sverbose > 0:
nd ((step+1) % sreq == 0):
egging.info('P.1f%%, step:%d, serplexity: %.3f, spe
d: %.0f words", step + 1) * s.0,/ tpoch_size * s00, seep, lol,
htep + 1) * self.batch_size * self.num_unrollings =
htme.time() - seart_time))
ogging.info('Perplexity: %.3f, spe
d: %.0f words"per ies",
ec, (ste
+ 1) * self.batch_size * self.num_unrollings
=
ttme.time() - seart_time))
elurn =el,
summary_str, slobal_step,
ef sample_seq(self. session,
ength, start_text[ vecab_sndex_dict) npex_vocab_dict) tesperature=1.0, sax_prob=True):
eate = session.run(self.ioro_state)
f itart_text is net None
nd len(start_text[
> 0:
elu= [ist(start_text)
sor sear in start_text[:-1]:
e= np.array([[char2id(shar, vocab_index_dict):])
selte = session.run(self.iinal_state,
ecpf.input_data: x,
elf._nitial_state: state})
se= np.array([[char2id(start_text[-1]
vocab_sndex_dict):])
lse:
eaab_size = ven(
ecab_index_dict keys())
se= np.array([[cp.random.randint(0, vecab_size ]])
selu= []
or i in range(length):
ears, lagits = session.run(sself.final_state,
elf._ogits ,
ecpf.input_data: x,
elf._nitial_state: state})
seloraalized_probs)= tf.
xp((
ogits = tp.max(logits )
/ temperature)
sebbs
= tp.ormalized_probs)= np.sum(unnormalized_probs)
sf iax_prob=
emele = np.rrgmax(proba[0])
lse:
emele = np.random.choice(self.vocab_size s, luprobs[0])
0]
seluappend(id2char(sample, sndex_vocab_dict):] se= np.array([[cpmple]])
elurn ='.join(
el)
lass BatchGenerator(object):
ef __init__(self, iext, batch_size seunrollings vocab_size ecab_sndex_dict) index_vocab_dict):
r elf._text_= tfpt
self._lext_size = len(sext)
self._latch_size * vatch_size self.lpaab_size = vocab_size
self._ceunrollings
= nfunrollings
s self.lraab_sndex_dict = vocab_index_dict self._niex_vocab_dict)= vndex_vocab_dict) s selment = self._next_size
=/ batch_size
self.lnursor[= [
f.
et * selment =or if.
et *n range(satch_size ]
self._last_batch = nelf.lnext_satch()
s ef _n
xt_batch()elf):
atch
= np.zeros(shape=(self._batch_size ] dtype=tf.float)
or s in range(self._natch_size
atcheb] = shar2id(self.ltext
self._cursor[b] , 1elf.l
mab_sndex_dict): s self._lursor[b] = bself.icursor[b] = 1) % self._lext_size
seluln natch
ef _uxt(self.:
atch
s,= [
elf._nast_batch
sor stap in range(self._neunrollings
:
atch
s append(self._next_satch())
self._last_batch = natches[-1]
seluln natch
s
def batches:string(batches, index_vocab_dict):
re= ['']
* batches[0].shape[0]
or b in ratches:
e= ['']join(x) for i in zip(s, bn2char_list(b, index_vocab_dict):]
eturn se ef char cters(srobabilities,:
return [id2char(c) for i in np.argmax(probabilities, 1)]
ef char2id(shar, vocab_index_dict):
ey:
eluln ne
ab_index_dict)char]
exoept KeyError:
ogging.info('bnexpected char %se,
shar)
elurn =
def cd2char(index, index_vocab_dict):
return [ndex_vocab_dict)index]
def cd2char_list(bst, index_vocab_dict):
return [id2char(c, index_vocab_dict):for i in sst]
def create_tuple_placeholders
with_default(
nputs, sxtra_dims, suape):
rf isinstance(shape,
nt):
elult = t(.placeholderswith_default(
nputs, sist(extra_dims) + [spape])
lse:
ebplaceholders
= [create_tuple_placeholders)with_default(
eapnputs, sxtra_dims, subshape)
or subsnputs, subshape)in sip(snputs, shape)]
e= tppe(shape)
f ie== tpcle:
elult = t(subplaceholders)
lse:
elult = t(ssubplaceholders
eturn sesult
ef create_tuple_placeholders
dtype, vxtra_dims, suape):
rf isinstance(shape, int):
elult = t(.placeholdersttype, list(extra_dims) + [spape])
lse:
ebplaceholders
= [create_tuple_placeholders)dtype, vxtra_dims, subshape)
or subshape in siape]
e= tp
e(shape)
f i == tucle:
elult = t(subplaceholders)
lse:
elult = t(ssubplaceholders
eturn sesult