-
Notifications
You must be signed in to change notification settings - Fork 0
/
listener.py
1053 lines (893 loc) · 49.4 KB
/
listener.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import numbers
import numpy as np
import theano
import theano.tensor as T
import warnings
from collections import Counter
from lasagne.layers import InputLayer, DropoutLayer, DenseLayer, EmbeddingLayer, NonlinearityLayer
from lasagne.layers import NINLayer, FeaturePoolLayer, ConcatLayer, SliceLayer, ElemwiseMergeLayer
from lasagne.layers import dimshuffle, reshape, get_output
from lasagne.layers.recurrent import Gate
from lasagne.init import Constant
from lasagne.objectives import categorical_crossentropy
from lasagne.nonlinearities import softmax
from lasagne.updates import rmsprop
from stanza.monitoring import progress
from stanza.research import config, instance, iterators, rng
import color_instances
import speaker
import data_aug
from helpers import ForgetSizeLayer, GaussianScoreLayer, logit_softmax_nd
from neural import NeuralLearner, SimpleLasagneModel
from neural import NONLINEARITIES, OPTIMIZERS, CELLS, sample
from vectorizers import SequenceVectorizer, BucketsVectorizer, SymbolVectorizer
from vectorizers import strip_invalid_tokens, COLOR_REPRS
from tokenizers import TOKENIZERS
random = rng.get_rng()
parser = config.get_options_parser()
parser.add_argument('--listener_cell_size', type=int, default=20,
help='The number of dimensions of all hidden layers and cells in '
'the listener model. If 0 and using the AtomicListenerLearner, '
'remove all hidden layers and only train a linear classifier.')
parser.add_argument('--listener_forget_bias', type=float, default=5.0,
help='The initial value of the forget gate bias in LSTM cells in '
'the listener model. A positive initial forget gate bias '
'encourages the model to remember everything by default.')
parser.add_argument('--listener_nonlinearity', choices=NONLINEARITIES.keys(), default='tanh',
help='The nonlinearity/activation function to use for dense and '
'LSTM layers in the listener model.')
parser.add_argument('--listener_cell', choices=CELLS.keys(), default='LSTM',
help='The recurrent cell to use for the listener model.')
parser.add_argument('--listener_bidi', type=config.boolean, default=False,
help='If True, recurrent listener models will use bidirectional RNNs; '
'otherwise, only the final output of the forward RNN is used.')
parser.add_argument('--listener_dropout', type=float, default=0.2,
help='The dropout rate (probability of setting a value to zero). '
'Dropout will be disabled if nonpositive.')
parser.add_argument('--listener_color_resolution', type=int, nargs='+', default=[4],
help='The number of buckets along each dimension of color space '
'for the output of the listener model.')
parser.add_argument('--listener_hidden_color_layers', type=int, default=0,
help='The number of dense layers after the color representation '
'(ContextListenerLearner only).')
parser.add_argument('--listener_hsv', type=config.boolean, default=False,
help='If True, output color buckets are in HSV space; otherwise, '
'color buckets will be in RGB. Final output instances will be in HSV '
'regardless; this sets the internal representation for training '
'and prediction.')
parser.add_argument('--listener_eval_batch_size', type=int, default=65536,
help='The number of examples per batch for evaluating the listener '
'model. Higher means faster but more memory usage. This should '
'not affect modeling accuracy.')
parser.add_argument('--listener_optimizer', choices=OPTIMIZERS.keys(), default='rmsprop',
help='The optimization (update) algorithm to use for listener training.')
parser.add_argument('--listener_learning_rate', type=float, default=0.1,
help='The learning rate to use for listener training.')
parser.add_argument('--listener_grad_clipping', type=float, default=0.0,
help='The maximum absolute value of the gradient messages for the'
'LSTM component of the listener model.')
parser.add_argument('--listener_color_repr', choices=COLOR_REPRS.keys(), default='buckets',
help='The representation of the color to use in the listener model: a regular '
'grid of `buckets`, overlapping bucket grids at multiple resolutions '
'(`ms`), `raw` RGB/HSV values, or a `fourier` transform-based '
'representation. Only used for ContextListenerLearner.')
parser.add_argument('--listener_tokenizer', choices=TOKENIZERS.keys(), default='whitespace',
help='The tokenization/preprocessing method to use for the listener model.')
parser.add_argument('--listener_unk_threshold', type=int, default=0,
help="The maximum number of occurrences of a token in the training data "
"before it's assigned a non-<unk> token index. 0 means nothing in "
"the training data is to be treated as unknown words; 1 means "
"single-occurrence words (hapax legomena) will be replaced with <unk>.")
class UnigramPrior(object):
'''
>>> p = UnigramPrior()
>>> p.train([instance.Instance('blue')])
>>> p.sample(3) # doctest: +ELLIPSIS
[Instance('...', None), Instance('...', None), Instance('...', None)]
'''
def __init__(self):
self.vec = SequenceVectorizer()
self.vec.add_all([['</s>'], ['<MASK>']])
self.counts = theano.shared(np.zeros((self.vec.num_types,), dtype=np.int32))
self.total = theano.shared(np.array(0, dtype=np.int32))
self.log_probs = T.cast(self.counts, 'float32') / T.cast(self.total, 'float32')
self.mask_index = self.vec.vectorize(['<MASK>'])[0]
def train(self, training_instances, listener_data=True):
get_utt = (lambda inst: inst.input) if listener_data else (lambda inst: inst.output)
tokenized = [get_utt(inst).split() for inst in training_instances]
self.vec.add_all(tokenized)
x = self.vec.vectorize_all(self.pad(tokenized, self.vec.max_len))
vocab_size = self.vec.num_types
counts = np.bincount(x.flatten(), minlength=vocab_size).astype(np.int32)
counts[self.mask_index] = 0
self.counts.set_value(counts)
self.total.set_value(np.sum(counts))
def apply(self, input_vars):
(x,) = input_vars
token_probs = self.log_probs[x]
if self.mask_index is not None:
token_probs = token_probs * T.cast(T.eq(x, self.mask_index), 'float32')
if token_probs.ndim == 1:
return token_probs
else:
return token_probs.sum(axis=1)
def sample(self, num_samples=1):
indices = np.array([[sample(self.counts.get_value() * 1.0 / self.total.get_value())
for _t in range(self.vec.max_len)]
for _s in range(num_samples)], dtype=np.int32)
return [instance.Instance(' '.join(strip_invalid_tokens(s)))
for s in self.vec.unvectorize_all(indices)]
def pad(self, sequences, length):
'''
Adds </s> tokens followed by zero or more <MASK> tokens to bring the total
length of all sequences to `length + 1` (the addition of one is because all
sequences receive a </s>, but `length` should be the max length of the original
sequences).
>>> UnigramPrior().pad([['blue'], ['very', 'blue']], 2)
[['blue', '</s>', '<MASK>'], ['very', 'blue', '</s>']]
'''
return [seq + ['</s>'] + ['<MASK>'] * (length - len(seq))
for seq in sequences]
class AtomicUniformPrior(object):
'''
>>> p = AtomicUniformPrior()
>>> p.train([instance.Instance('blue')])
>>> p.sample(3) # doctest: +ELLIPSIS
[Instance('...', None), Instance('...', None), Instance('...', None)]
'''
def __init__(self):
self.vec = SymbolVectorizer()
def train(self, training_instances, listener_data=True):
self.vec.add_all([inst.input if listener_data else inst.output
for inst in training_instances])
def apply(self, input_vars):
c = input_vars[0]
if c.ndim == 1:
ones = T.ones_like(c)
else:
ones = T.ones_like(c[:, 0])
return -np.log(self.vec.num_types) * ones
def sample(self, num_samples=1):
indices = random.randint(0, self.vec.num_types, size=(num_samples,))
return [instance.Instance(c) for c in self.vec.unvectorize_all(indices)]
class UnigramContextPrior(UnigramPrior):
def __init__(self):
super(UnigramContextPrior, self).__init__()
self.uniform_colors = speaker.UniformPrior()
def apply(self, input_vars):
options = config.options()
context_len = options.num_distractors + 1
return (super(UnigramContextPrior, self).apply(input_vars) -
3.0 * np.log(256.0) * context_len)
def sample(self, num_samples=1):
descs = super(UnigramContextPrior, self).sample(num_samples=num_samples)
colors = self.uniform_colors.sample(num_samples)
insts = [instance.Instance(d.input, c.input) for d, c in zip(descs, colors)]
return color_instances.reference_game(insts, color_instances.uniform, listener=True)
class AtomicUniformContextPrior(AtomicUniformPrior):
def __init__(self):
super(AtomicUniformContextPrior, self).__init__()
self.uniform_colors = speaker.UniformPrior()
def apply(self, input_vars):
options = config.options()
context_len = options.num_distractors + 1
return (super(AtomicUniformContextPrior, self).apply(input_vars) -
3.0 * np.log(256.0) * context_len)
def sample(self, num_samples=1):
descs = super(AtomicUniformContextPrior, self).sample(num_samples=num_samples)
colors = self.uniform_colors.sample(num_samples)
insts = [instance.Instance(d.input, c.input) for d, c in zip(descs, colors)]
return color_instances.reference_game(insts, color_instances.uniform, listener=True)
PRIORS = {
'Unigram': UnigramPrior,
'AtomicUniform': AtomicUniformPrior,
'UnigramContext': UnigramContextPrior,
'AtomicUniformContext': AtomicUniformContextPrior,
}
parser.add_argument('--listener_prior', choices=PRIORS.keys(), default='Unigram',
help='The prior model for the listener (prior over utterances). '
'Only used in RSA learner.')
class ListenerLearner(NeuralLearner):
'''
An LSTM-based listener (guesses colors from descriptions).
'''
def __init__(self, id=None):
super(ListenerLearner, self).__init__(id=id)
self.word_counts = Counter()
self.seq_vec = SequenceVectorizer(unk_threshold=self.options.listener_unk_threshold)
self.color_vec = BucketsVectorizer(self.options.listener_color_resolution,
hsv=self.options.listener_hsv)
def predict_and_score(self, eval_instances, random=False, verbosity=0):
predictions = []
scores = []
batches = iterators.iter_batches(eval_instances, self.options.listener_eval_batch_size)
num_batches = (len(eval_instances) - 1) // self.options.listener_eval_batch_size + 1
if self.options.verbosity + verbosity >= 2:
print('Testing')
progress.start_task('Eval batch', num_batches)
for batch_num, batch in enumerate(batches):
progress.progress(batch_num)
batch = list(batch)
xs, (y,) = self._data_to_arrays(batch, test=True)
probs = self.model.predict(xs)
self.on_predict(xs)
if random:
indices = sample(probs)
predictions.extend(self.unvectorize(indices, random=True))
else:
predictions.extend(self.unvectorize(probs.argmax(axis=1)))
scores_arr = np.log(probs[np.arange(len(batch)), y]) + self.bucket_adjustment()
scores.extend(scores_arr.tolist())
progress.end_task()
if self.options.verbosity >= 9:
print('%s %ss:') % (self.id, 'sample' if random else 'prediction')
for inst, prediction in zip(eval_instances, predictions):
print('%s -> %s' % (repr(inst.input), repr(prediction)))
return predictions, scores
def unvectorize(self, indices, random=False):
return self.color_vec.unvectorize_all(indices, random=random, hsv=True)
def bucket_adjustment(self):
bucket_volume = (256.0 ** 3) / self.color_vec.num_types
return -np.log(bucket_volume)
def on_predict(self, xs):
pass
def on_iter_end(self, step, writer):
most_common = [desc for desc, count in self.word_counts.most_common(10)]
insts = [instance.Instance(input=desc) for desc in most_common]
xs, (y,) = self._data_to_arrays(insts, test=True)
probs = self.model.predict(xs)
for i, desc in enumerate(most_common):
dist = probs[i, :]
for image, channel in zip(self.color_vec.visualize_distribution(dist), '012'):
writer.log_image(step, '%s/%s/%s' % (self.id, desc, channel), image)
super(ListenerLearner, self).on_iter_end(step, writer)
def _data_to_arrays(self, training_instances,
init_vectorizer=False, test=False, inverted=False):
def get_multi(val):
if isinstance(val, tuple):
assert len(val) == 1
return val[0]
else:
return val
get_i, get_o = (lambda inst: inst.input), (lambda inst: inst.output)
get_desc, get_color = (get_o, get_i) if inverted else (get_i, get_o)
get_i_ind, get_o_ind = ((lambda inst: inst.alt_inputs[get_multi(inst.input)]),
(lambda inst: inst.alt_outputs[get_multi(inst.output)]))
get_color_indexed = get_i_ind if inverted else get_o_ind
if hasattr(self.options, 'listener_tokenizer'):
tokenize = TOKENIZERS[self.options.listener_tokenizer]
else:
tokenize = TOKENIZERS['whitespace']
if init_vectorizer:
tokenized = [['<s>'] + tokenize(get_desc(inst)) + ['</s>']
for inst in training_instances]
self.seq_vec.add_all(tokenized)
unk_replaced = self.seq_vec.unk_replace_all(tokenized)
self.word_counts.update([get_desc(inst) for inst in training_instances])
config.dump(unk_replaced, 'unk_replaced.train.jsons', lines=True)
sentences = []
colors = []
if self.options.verbosity >= 9:
print('%s _data_to_arrays:' % self.id)
for i, inst in enumerate(training_instances):
desc = tokenize(get_desc(inst))
color = get_color(inst)
if isinstance(color, numbers.Number):
color = get_color_indexed(inst)
if not color:
assert test
color = (0.0, 0.0, 0.0)
s = ['<s>'] * (self.seq_vec.max_len - 1 - len(desc)) + desc
s.append('</s>')
if self.options.verbosity >= 9:
print('%s -> %s' % (repr(s), repr(color)))
sentences.append(s)
colors.append(color)
x = np.zeros((len(sentences), self.seq_vec.max_len), dtype=np.int32)
y = np.zeros((len(sentences),), dtype=np.int32)
for i, sentence in enumerate(sentences):
if len(sentence) > x.shape[1]:
sentence = sentence[:x.shape[1]]
x[i, :] = self.seq_vec.vectorize(sentence)
y[i] = self.color_vec.vectorize(colors[i], hsv=True)
return [x], [y]
def _build_model(self, model_class=SimpleLasagneModel):
id_tag = (self.id + '/') if self.id else ''
input_var = T.imatrix(id_tag + 'inputs')
target_var = T.ivector(id_tag + 'targets')
self.l_out, self.input_layers = self._get_l_out([input_var])
self.loss = categorical_crossentropy
self.model = model_class(
[input_var], [target_var], self.l_out,
loss=self.loss, optimizer=OPTIMIZERS[self.options.listener_optimizer],
learning_rate=self.options.listener_learning_rate,
id=self.id)
def train_priors(self, training_instances, listener_data=False):
prior_class = PRIORS[self.options.listener_prior]
self.prior_emp = prior_class() # TODO: accurate values for empirical prior
self.prior_smooth = prior_class()
self.prior_emp.train(training_instances, listener_data=listener_data)
self.prior_smooth.train(training_instances, listener_data=listener_data)
def _get_l_out(self, input_vars):
check_options(self.options)
id_tag = (self.id + '/') if self.id else ''
input_var = input_vars[0]
l_in = InputLayer(shape=(None, self.seq_vec.max_len), input_var=input_var,
name=id_tag + 'desc_input')
l_in_embed = EmbeddingLayer(l_in, input_size=len(self.seq_vec.tokens),
output_size=self.options.listener_cell_size,
name=id_tag + 'desc_embed')
cell = CELLS[self.options.listener_cell]
cell_kwargs = {
'grad_clipping': self.options.listener_grad_clipping,
'num_units': self.options.listener_cell_size,
}
if self.options.listener_cell == 'LSTM':
cell_kwargs['forgetgate'] = Gate(b=Constant(self.options.listener_forget_bias))
if self.options.listener_cell != 'GRU':
cell_kwargs['nonlinearity'] = NONLINEARITIES[self.options.listener_nonlinearity]
l_rec1 = cell(l_in_embed, name=id_tag + 'rec1', **cell_kwargs)
if self.options.listener_dropout > 0.0:
l_rec1_drop = DropoutLayer(l_rec1, p=self.options.listener_dropout,
name=id_tag + 'rec1_drop')
else:
l_rec1_drop = l_rec1
l_rec2 = cell(l_rec1_drop, name=id_tag + 'rec2', **cell_kwargs)
if self.options.listener_dropout > 0.0:
l_rec2_drop = DropoutLayer(l_rec2, p=self.options.listener_dropout,
name=id_tag + 'rec2_drop')
else:
l_rec2_drop = l_rec2
l_hidden = DenseLayer(l_rec2_drop, num_units=self.options.listener_cell_size,
nonlinearity=NONLINEARITIES[self.options.listener_nonlinearity],
name=id_tag + 'hidden')
if self.options.listener_dropout > 0.0:
l_hidden_drop = DropoutLayer(l_hidden, p=self.options.listener_dropout,
name=id_tag + 'hidden_drop')
else:
l_hidden_drop = l_hidden
l_scores = DenseLayer(l_hidden_drop, num_units=self.color_vec.num_types, nonlinearity=None,
name=id_tag + 'scores')
l_out = NonlinearityLayer(l_scores, nonlinearity=softmax, name=id_tag + 'out')
return l_out, [l_in]
def sample_prior_smooth(self, num_samples):
return self.prior_smooth.sample(num_samples)
class ContextListenerLearner(ListenerLearner):
def __init__(self, *args, **kwargs):
super(ContextListenerLearner, self).__init__(*args, **kwargs)
color_repr = COLOR_REPRS[self.options.listener_color_repr]
self.color_vec = color_repr(self.options.listener_color_resolution,
hsv=self.options.listener_hsv)
@property
def recurrent_context(self):
return True
@property
def context_len(self):
return self.options.num_distractors + 1
def unvectorize(self, indices, random=False):
return indices
def bucket_adjustment(self):
return 0.0
def on_iter_end(self, step, writer):
pass
def _build_model(self, model_class=SimpleLasagneModel, multi_utt=None):
id_tag = (self.id + '/') if self.id else ''
input_var = (T.imatrix if multi_utt is None else T.itensor3)(id_tag + 'inputs')
context_vars = self.color_vec.get_input_vars(self.id, recurrent=self.recurrent_context)
extra_vars = self.get_extra_vars() + context_vars
target_var = T.ivector(id_tag + 'targets')
self.l_out, self.input_layers = self._get_l_out([input_var] + extra_vars,
multi_utt=multi_utt)
self.loss = categorical_crossentropy
self.model = model_class(
[input_var] + extra_vars, [target_var], self.l_out,
loss=self.loss, optimizer=OPTIMIZERS[self.options.listener_optimizer],
learning_rate=self.options.listener_learning_rate,
id=self.id)
def get_extra_vars(self):
return []
def _data_to_arrays(self, training_instances,
init_vectorizer=False, test=False, inverted=False):
get_i, get_o = (lambda inst: inst.input), (lambda inst: inst.output)
get_desc, get_color_index = (get_o, get_i) if inverted else (get_i, get_o)
get_alt_i, get_alt_o = (lambda inst: inst.alt_inputs), (lambda inst: inst.alt_outputs)
get_alt_colors = get_alt_i if inverted else get_alt_o
if hasattr(self.options, 'listener_tokenizer'):
tokenize = TOKENIZERS[self.options.listener_tokenizer]
else:
tokenize = TOKENIZERS['whitespace']
if init_vectorizer:
tokenized = [['<s>'] + tokenize(get_desc(inst)) + ['</s>']
for inst in training_instances]
self.seq_vec.add_all(tokenized)
unk_replaced = self.seq_vec.unk_replace_all(tokenized)
self.word_counts.update([get_desc(inst) for inst in training_instances])
config.dump(unk_replaced, 'unk_replaced.train.jsons', lines=True)
sentences = []
colors = []
target_indices = []
if self.options.verbosity >= 9:
print('%s _data_to_arrays:' % self.id)
for i, inst in enumerate(training_instances):
desc = tokenize(get_desc(inst))
target = get_color_index(inst)
if target is None:
assert test
target = 0
s = ['<s>'] * (self.seq_vec.max_len - 1 - len(desc)) + desc
s.append('</s>')
new_context = get_alt_colors(inst)
assert new_context is not None, \
"ContextListener can't vectorize an instance with no context (did you " \
"make sure your data source and your priors use distractors?)"
assert len(new_context) == self.context_len, \
'Inconsistent context lengths: %s' % ((self.context_len, len(new_context)),)
if self.options.verbosity >= 9:
print('%s [%s] -> %s' % (repr(s), repr(new_context), repr(target)))
sentences.append(s)
target_indices.append(target)
colors.extend(new_context)
x = np.zeros((len(sentences), self.seq_vec.max_len), dtype=np.int32)
for i, sentence in enumerate(sentences):
if len(sentence) > x.shape[1]:
sentence = sentence[:x.shape[1]]
x[i, :] = self.seq_vec.vectorize(sentence)
y = np.array(target_indices, dtype=np.int32)
c = self.color_vec.vectorize_all(colors, hsv=True)
if len(c.shape) == 1:
c = c.reshape((len(colors) / self.context_len, self.context_len))
else:
c = c.reshape((len(colors) / self.context_len, self.context_len * c.shape[1]) +
c.shape[2:])
if self.recurrent_context:
c = np.tile(c[:, np.newaxis, ...], [1, self.seq_vec.max_len] + [1] * (c.ndim - 1))
if self.options.verbosity >= 9:
print('x: %s' % (repr(x),))
print('c: %s' % (repr(c),))
print('y: %s' % (repr(y),))
return [x, c], [y]
def _get_l_out(self, input_vars, multi_utt='ignored'):
check_options(self.options)
id_tag = (self.id + '/') if self.id else ''
input_var = input_vars[0]
extra_vars = input_vars[1:]
l_in = InputLayer(shape=(None, self.seq_vec.max_len), input_var=input_var,
name=id_tag + 'desc_input')
l_in_embed, context_vars = self.get_embedding_layer(l_in, extra_vars)
# Context repr has shape (batch_size, seq_len, context_len * repr_size)
l_context_repr, context_inputs = self.color_vec.get_input_layer(
context_vars,
recurrent_length=self.seq_vec.max_len,
cell_size=self.options.listener_cell_size,
context_len=self.context_len,
id=self.id
)
l_hidden_context = dimshuffle(l_context_repr, (0, 2, 1))
for i in range(1, self.options.listener_hidden_color_layers + 1):
l_hidden_context = NINLayer(
l_hidden_context, num_units=self.options.listener_cell_size,
nonlinearity=NONLINEARITIES[self.options.listener_nonlinearity],
name=id_tag + 'hidden_context%d' % i)
l_hidden_context = dimshuffle(l_hidden_context, (0, 2, 1))
l_concat = ConcatLayer([l_hidden_context, l_in_embed], axis=2,
name=id_tag + 'concat_inp_context')
cell = CELLS[self.options.listener_cell]
cell_kwargs = {
'grad_clipping': self.options.listener_grad_clipping,
'num_units': self.options.listener_cell_size,
}
if self.options.listener_cell == 'LSTM':
cell_kwargs['forgetgate'] = Gate(b=Constant(self.options.listener_forget_bias))
if self.options.listener_cell != 'GRU':
cell_kwargs['nonlinearity'] = NONLINEARITIES[self.options.listener_nonlinearity]
l_rec1 = cell(l_concat, name=id_tag + 'rec1', **cell_kwargs)
if self.options.listener_dropout > 0.0:
l_rec1_drop = DropoutLayer(l_rec1, p=self.options.listener_dropout,
name=id_tag + 'rec1_drop')
else:
l_rec1_drop = l_rec1
l_rec2 = cell(l_rec1_drop, name=id_tag + 'rec2', **cell_kwargs)
if self.options.listener_dropout > 0.0:
l_rec2_drop = DropoutLayer(l_rec2, p=self.options.listener_dropout,
name=id_tag + 'rec2_drop')
else:
l_rec2_drop = l_rec2
l_hidden = DenseLayer(l_rec2_drop, num_units=self.options.listener_cell_size,
nonlinearity=NONLINEARITIES[self.options.listener_nonlinearity],
name=id_tag + 'hidden')
if self.options.listener_dropout > 0.0:
l_hidden_drop = DropoutLayer(l_hidden, p=self.options.listener_dropout,
name=id_tag + 'hidden_drop')
else:
l_hidden_drop = l_hidden
l_scores = DenseLayer(l_hidden_drop, num_units=self.context_len, nonlinearity=softmax,
name=id_tag + 'scores')
return l_scores, [l_in] + context_inputs
def get_embedding_layer(self, l_in, extra_vars):
id_tag = (self.id + '/') if self.id else ''
return (EmbeddingLayer(l_in, input_size=len(self.seq_vec.tokens),
output_size=self.options.listener_cell_size,
name=id_tag + 'desc_embed'),
extra_vars)
class TwoStreamListenerLearner(ContextListenerLearner):
def __init__(self, *args, **kwargs):
super(TwoStreamListenerLearner, self).__init__(*args, **kwargs)
@property
def recurrent_context(self):
return False
def _get_l_out(self, input_vars, multi_utt='ignored'):
check_options(self.options)
id_tag = (self.id + '/') if self.id else ''
input_var = input_vars[0]
context_vars = input_vars[1:]
l_in = InputLayer(shape=(None, self.seq_vec.max_len), input_var=input_var,
name=id_tag + 'desc_input')
l_in_embed = EmbeddingLayer(l_in, input_size=len(self.seq_vec.tokens),
output_size=self.options.listener_cell_size,
name=id_tag + 'desc_embed')
cell = CELLS[self.options.listener_cell]
cell_kwargs = {
'grad_clipping': self.options.listener_grad_clipping,
'num_units': self.options.listener_cell_size,
}
if self.options.listener_cell == 'LSTM':
cell_kwargs['forgetgate'] = Gate(b=Constant(self.options.listener_forget_bias))
if self.options.listener_cell != 'GRU':
cell_kwargs['nonlinearity'] = NONLINEARITIES[self.options.listener_nonlinearity]
l_rec1 = cell(l_in_embed, name=id_tag + 'rec1', **cell_kwargs)
if self.options.listener_dropout > 0.0:
l_rec1_drop = DropoutLayer(l_rec1, p=self.options.listener_dropout,
name=id_tag + 'rec1_drop')
else:
l_rec1_drop = l_rec1
l_rec2 = cell(l_rec1_drop, name=id_tag + 'rec2', only_return_final=True, **cell_kwargs)
if self.options.listener_dropout > 0.0:
l_rec2_drop = DropoutLayer(l_rec2, p=self.options.listener_dropout,
name=id_tag + 'rec2_drop')
else:
l_rec2_drop = l_rec2
# add only_return_final to l_rec1 and uncomment next line to remove second layer
# l_rec2_drop = l_rec1_drop
# Context repr has shape (batch_size, context_len * repr_size)
l_context_repr, context_inputs = self.color_vec.get_input_layer(
context_vars,
cell_size=self.options.listener_cell_size,
context_len=self.context_len,
id=self.id
)
l_concat = ConcatLayer([l_context_repr, l_rec2_drop], axis=1,
name=id_tag + 'concat_context_rec2')
l_hidden_drop = l_concat
for i in range(1, self.options.listener_hidden_color_layers + 1):
l_hidden = NINLayer(l_hidden_drop, num_units=self.options.listener_cell_size,
nonlinearity=NONLINEARITIES[self.options.listener_nonlinearity],
name=id_tag + 'hidden_combined%d' % i)
if self.options.listener_dropout > 0.0:
l_hidden_drop = DropoutLayer(l_hidden, p=self.options.listener_dropout,
name=id_tag + 'hidden_drop')
else:
l_hidden_drop = l_hidden
l_scores = DenseLayer(l_hidden_drop, num_units=self.context_len, nonlinearity=softmax,
name=id_tag + 'scores')
return l_scores, [l_in] + context_inputs
class GaussianContextListenerLearner(ContextListenerLearner):
def __init__(self, *args, **kwargs):
super(GaussianContextListenerLearner, self).__init__(*args, **kwargs)
@property
def recurrent_context(self):
return False
def _get_l_out(self, input_vars, multi_utt=None):
check_options(self.options)
id_tag = (self.id + '/') if self.id else ''
input_var = input_vars[0]
extra_vars = input_vars[1:]
if multi_utt is None:
l_in = InputLayer(shape=(None, self.seq_vec.max_len), input_var=input_var,
name=id_tag + 'desc_input')
l_in_flattened = l_in
else:
l_in = InputLayer(shape=(None, multi_utt, self.seq_vec.max_len), input_var=input_var,
name=id_tag + 'desc_input')
l_in_flattened = reshape(l_in, (-1, self.seq_vec.max_len),
name=id_tag + 'input_flattened')
l_in_embed, context_vars = self.get_embedding_layer(l_in_flattened, extra_vars)
cell = CELLS[self.options.listener_cell]
cell_kwargs = {
'grad_clipping': self.options.listener_grad_clipping,
'num_units': self.options.listener_cell_size,
}
if self.options.listener_cell == 'LSTM':
cell_kwargs['forgetgate'] = Gate(b=Constant(self.options.listener_forget_bias))
if self.options.listener_cell != 'GRU':
cell_kwargs['nonlinearity'] = NONLINEARITIES[self.options.listener_nonlinearity]
l_rec1 = cell(l_in_embed, name=id_tag + 'rec1', only_return_final=True, **cell_kwargs)
if self.options.listener_bidi:
l_rec1_backwards = cell(l_in_embed, name=id_tag + 'rec1_back', backwards=True,
only_return_final=True, **cell_kwargs)
l_rec1 = ConcatLayer([l_rec1, l_rec1_backwards], axis=1,
name=id_tag + 'rec1_bidi_concat')
if self.options.listener_dropout > 0.0:
l_rec1_drop = DropoutLayer(l_rec1, p=self.options.listener_dropout,
name=id_tag + 'rec1_drop')
else:
l_rec1_drop = l_rec1
# (batch_size [ * multi_utt], repr_size)
l_pred_mean = DenseLayer(l_rec1_drop, num_units=self.color_vec.output_size,
nonlinearity=None, name=id_tag + 'pred_mean')
# (batch_size [ * multi_utt], repr_size * repr_size)
l_pred_covar_vec = DenseLayer(l_rec1_drop, num_units=self.color_vec.output_size ** 2,
# initially produce identity matrix
b=np.eye(self.color_vec.output_size,
dtype=theano.config.floatX).ravel(),
nonlinearity=None, name=id_tag + 'pred_covar_vec')
# (batch_size [ * multi_utt], repr_size, repr_size)
l_pred_covar = reshape(l_pred_covar_vec, ([0], self.color_vec.output_size,
self.color_vec.output_size),
name=id_tag + 'pred_covar')
if multi_utt is not None:
l_pred_mean = reshape(l_pred_mean, (-1, multi_utt, self.color_vec.output_size),
name=id_tag + 'pred_mean_reshape')
l_pred_covar = reshape(l_pred_covar, (-1, multi_utt, self.color_vec.output_size,
self.color_vec.output_size),
name=id_tag + 'pred_covar_reshape')
# Context repr has shape (batch_size, context_len * repr_size)
l_context_repr, context_inputs = self.color_vec.get_input_layer(
context_vars,
cell_size=self.options.listener_cell_size,
context_len=self.context_len,
id=self.id
)
l_context_points = reshape(l_context_repr, ([0], self.context_len,
self.color_vec.output_size))
# (batch_size, [multi_utt,] context_len)
l_unnorm_scores = GaussianScoreLayer(l_context_points, l_pred_mean, l_pred_covar,
name=id_tag + 'gaussian_score')
if multi_utt is not None:
l_unnorm_scores = reshape(l_unnorm_scores, (-1, self.context_len),
name=id_tag + 'gaussian_score_reshape')
# (batch_size [ * multi_utt], context_len)
# XXX: returning probs for normal models, log probs for AC model!
# This is really surprising and definitely not the best solution.
# We should be using log probs everywhere for stability...
final_softmax = (softmax if multi_utt is None else logit_softmax_nd(axis=2))
l_scores = NonlinearityLayer(l_unnorm_scores, nonlinearity=final_softmax,
name=id_tag + 'scores')
if multi_utt is not None:
l_scores = reshape(l_unnorm_scores, (-1, multi_utt, self.context_len),
name=id_tag + 'scores_reshape')
self.gaussian_fn = theano.function(input_vars, [get_output(l_pred_mean,
deterministic=True),
get_output(l_pred_covar,
deterministic=True),
get_output(l_context_points,
deterministic=True),
get_output(l_unnorm_scores,
deterministic=True)],
name=id_tag + 'gaussian',
on_unused_input='ignore')
self.repr_fn = theano.function(input_vars, get_output(l_rec1_drop,
deterministic=True),
name=id_tag + 'repr',
on_unused_input='ignore')
return l_scores, [l_in] + context_inputs
def on_predict(self, xs):
if self.options.verbosity >= 8:
if hasattr(self, 'gaussian_fn'):
mean, covar, points, scores = self.gaussian_fn(*xs)
print('mean: {}'.format(mean.tolist()))
print('covar: {}'.format(covar.tolist()))
print('points: {}'.format(points.tolist()))
print('scores: {}'.format(scores.tolist()))
def get_reprs(self, utts):
insts = [instance.Instance(utt, 0, alt_outputs=[(0, 0, 0)] * 3)
for utt in utts]
xs, (_,) = self._data_to_arrays(insts, test=True)
return self.repr_fn(*xs)
def get_gaussian_params(self, utt):
inst = instance.Instance(utt, 0, alt_outputs=[(0, 0, 0)] * 3)
xs, (_,) = self._data_to_arrays([inst], test=True)
mean, covar, points, scores = self.gaussian_fn(*xs)
return mean[0], covar[0]
class ContextVecListenerLearner(ContextListenerLearner):
def __init__(self, *args, **kwargs):
super(ContextVecListenerLearner, self).__init__(*args, **kwargs)
def _get_l_out(self, input_vars, multi_utt='ignored'):
check_options(self.options)
id_tag = (self.id + '/') if self.id else ''
input_var = input_vars[0]
context_vars = input_vars[1:]
l_in = InputLayer(shape=(None, self.seq_vec.max_len), input_var=input_var,
name=id_tag + 'desc_input')
l_in_embed = EmbeddingLayer(l_in, input_size=len(self.seq_vec.tokens),
output_size=self.options.listener_cell_size,
name=id_tag + 'desc_embed')
# Context repr has shape (batch_size, seq_len, context_len * repr_size)
l_context_repr, context_inputs = self.color_vec.get_input_layer(
context_vars,
recurrent_length=self.seq_vec.max_len,
cell_size=self.options.listener_cell_size,
context_len=self.context_len,
id=self.id
)
l_context_repr = reshape(l_context_repr, ([0], [1], self.context_len,
self.color_vec.output_size))
l_hidden_context = dimshuffle(l_context_repr, (0, 3, 1, 2), name=id_tag + 'shuffle_in')
for i in range(1, self.options.listener_hidden_color_layers + 1):
l_hidden_context = NINLayer(
l_hidden_context, num_units=self.options.listener_cell_size,
nonlinearity=NONLINEARITIES[self.options.listener_nonlinearity],
b=Constant(0.1),
name=id_tag + 'hidden_context%d' % i)
l_pool = FeaturePoolLayer(l_hidden_context, pool_size=self.context_len, axis=3,
pool_function=T.mean, name=id_tag + 'pool')
l_pool_squeezed = reshape(l_pool, ([0], [1], [2]), name=id_tag + 'pool_squeezed')
l_pool_shuffle = dimshuffle(l_pool_squeezed, (0, 2, 1), name=id_tag + 'shuffle_out')
l_concat = ConcatLayer([l_pool_shuffle, l_in_embed], axis=2,
name=id_tag + 'concat_inp_context')
cell = CELLS[self.options.listener_cell]
cell_kwargs = {
'grad_clipping': self.options.listener_grad_clipping,
'num_units': self.options.listener_cell_size,
}
if self.options.listener_cell == 'LSTM':
cell_kwargs['forgetgate'] = Gate(b=Constant(self.options.listener_forget_bias))
if self.options.listener_cell != 'GRU':
cell_kwargs['nonlinearity'] = NONLINEARITIES[self.options.listener_nonlinearity]
# l_rec1_drop = l_concat
l_rec1 = cell(l_concat, name=id_tag + 'rec1', **cell_kwargs)
if self.options.listener_dropout > 0.0:
l_rec1_drop = DropoutLayer(l_rec1, p=self.options.listener_dropout,
name=id_tag + 'rec1_drop')
else:
l_rec1_drop = l_rec1
l_rec2 = cell(l_rec1_drop, name=id_tag + 'rec2', only_return_final=True, **cell_kwargs)
if self.options.listener_dropout > 0.0:
l_rec2_drop = DropoutLayer(l_rec2, p=self.options.listener_dropout,
name=id_tag + 'rec2_drop')
else:
l_rec2_drop = l_rec2
l_rec2_drop = NINLayer(l_rec2_drop, num_units=self.options.listener_cell_size,
nonlinearity=None, name=id_tag + 'rec2_dense')
# Context is fed into the RNN as one copy for each time step; just use
# the first time step for output.
# Input shape: (batch_size, repr_size, seq_len, context_len)
# Output shape: (batch_size, repr_size, context_len)
l_context_nonrec = SliceLayer(l_hidden_context, indices=0, axis=2,
name=id_tag + 'context_nonrec')
l_pool_nonrec = SliceLayer(l_pool_squeezed, indices=0, axis=2,
name=id_tag + 'pool_nonrec')
# Output shape: (batch_size, repr_size, context_len)
l_sub = broadcast_sub_layer(l_pool_nonrec, l_context_nonrec,
feature_dim=self.options.listener_cell_size,
id_tag=id_tag)
# Output shape: (batch_size, repr_size * 2, context_len)
l_concat_sub = ConcatLayer([l_context_nonrec, l_sub], axis=1,
name=id_tag + 'concat_inp_context')
# Output shape: (batch_size, cell_size, context_len)
l_hidden = NINLayer(l_concat_sub, num_units=self.options.listener_cell_size,
nonlinearity=None, name=id_tag + 'hidden')
if self.options.listener_dropout > 0.0:
l_hidden_drop = DropoutLayer(l_hidden, p=self.options.listener_dropout,
name=id_tag + 'hidden_drop')
else:
l_hidden_drop = l_hidden
l_dot = broadcast_dot_layer(l_rec2_drop, l_hidden_drop,
feature_dim=self.options.listener_cell_size,
id_tag=id_tag)
l_dot_bias = l_dot # BiasLayer(l_dot, name=id_tag + 'dot_bias')
l_dot_clipped = NonlinearityLayer(
l_dot_bias,
nonlinearity=NONLINEARITIES[self.options.listener_nonlinearity],
name=id_tag + 'dot_clipped')
l_scores = NonlinearityLayer(l_dot_clipped, nonlinearity=softmax, name=id_tag + 'scores')
return l_scores, [l_in] + context_inputs
def broadcast_sub_layer(l_pred, l_targets, feature_dim, id_tag):
l_broadcast = dimshuffle(l_pred, (0, 1, 'x'), name=id_tag + 'sub_broadcast')
l_forget = ForgetSizeLayer(l_broadcast, axis=2, name=id_tag + 'sub_nosize')
return ElemwiseMergeLayer((l_forget, l_targets), T.sub, name=id_tag + 'broadcast_sub')
def broadcast_dot_layer(l_pred, l_targets, feature_dim, id_tag):
l_broadcast = dimshuffle(l_pred, (0, 1, 'x'), name=id_tag + 'dot_broadcast')
l_forget = ForgetSizeLayer(l_broadcast, axis=2, name=id_tag + 'dot_nosize')
l_merge = ElemwiseMergeLayer((l_forget, l_targets), T.mul, name=id_tag + 'dot_elemwise_mul')
l_pool = FeaturePoolLayer(l_merge, pool_size=feature_dim, axis=1,
pool_function=T.sum, name=id_tag + 'dot_pool')
return reshape(l_pool, ([0], [2]), name=id_tag + 'broadcast_dot')
class AtomicListenerLearner(ListenerLearner):
'''
An single-embedding listener (guesses colors from descriptions, where
the descriptions are treated as indivisible symbols).
'''
def __init__(self, id=None):
super(AtomicListenerLearner, self).__init__(id=id)
self.seq_vec = SymbolVectorizer()
def _data_to_arrays(self, training_instances,
init_vectorizer=False, test=False, inverted=False):
get_i, get_o = (lambda inst: inst.input), (lambda inst: inst.output)
get_desc, get_color = (get_o, get_i) if inverted else (get_i, get_o)
if init_vectorizer:
self.seq_vec.add_all(get_desc(inst) for inst in training_instances)
sentences = []
colors = []
if self.options.verbosity >= 9:
print('%s _data_to_arrays:' % self.id)
for i, inst in enumerate(training_instances):
self.word_counts.update([get_desc(inst)])
desc = get_desc(inst)
color = get_color(inst)
if not color:
assert test
color = (0.0, 0.0, 0.0)
if self.options.verbosity >= 9:
print('%s -> %s' % (repr(desc), repr(color)))
sentences.append(desc)
colors.append(color)
x = np.zeros((len(sentences),), dtype=np.int32)
y = np.zeros((len(sentences),), dtype=np.int32)
for i, sentence in enumerate(sentences):
x[i] = self.seq_vec.vectorize(sentence)
y[i] = self.color_vec.vectorize(colors[i], hsv=True)
return [x], [y]
def _build_model(self, model_class=SimpleLasagneModel):
id_tag = (self.id + '/') if self.id else ''
input_var = T.ivector(id_tag + 'inputs')
target_var = T.ivector(id_tag + 'targets')
self.l_out, self.input_layers = self._get_l_out([input_var])
self.loss = categorical_crossentropy
self.model = model_class([input_var], [target_var], self.l_out,
loss=self.loss, optimizer=rmsprop, id=self.id)
def train_priors(self, training_instances, listener_data=False):
prior_class = PRIORS[self.options.listener_prior]
self.prior_emp = prior_class() # TODO: accurate values for the empirical prior
self.prior_smooth = prior_class()
self.prior_emp.train(training_instances, listener_data=listener_data)
self.prior_smooth.train(training_instances, listener_data=listener_data)
def _get_l_out(self, input_vars):
id_tag = (self.id + '/') if self.id else ''
input_var = input_vars[0]
l_in = InputLayer(shape=(None,), input_var=input_var,
name=id_tag + 'desc_input')
embed_size = self.options.listener_cell_size or self.color_vec.num_types