-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerative.py
995 lines (842 loc) · 34.1 KB
/
generative.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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from gnn import StackedGraphNN
from gnn import readout_mean_max
from flow import init_real_nvp, real_nvp_wrapper
from flow import init_perm_equiv_flow, perm_equiv_flow_wrapper
from util import mlp_mix_diag_normal, mlp_low_rank_normal
from util import mlp_mix_neg_binomial, get_mlp_mix_loc_scale_builder
from util import gated_unit, skip_cond_gated_unit, layer_norm_1d
from util import make_trainable_gmm, identity_diag_normal
import util
import tensorflow as tf
from tensorflow import keras
import tensorflow_probability as tfp
tfd = tfp.distributions
class External(object):
def __init__(self, global_inputs, local_inputs):
self._global_inputs = global_inputs
self._local_inputs = local_inputs
@property
def global_inputs(self):
return self._global_inputs
@property
def local_inputs(self):
return self._local_inputs
class ExternalSequences(External):
def __init__(self, global_inputs, local_inputs):
super(ExternalSequences, self).__init__(
global_inputs, local_inputs
)
def _assert_seq_len(self, sequence, length):
if sequence is None or length is None:
return sequence
with tf.control_dependencies([
tf.assert_equal(tf.shape(sequence)[0], length)
]):
return tf.identity(sequence)
def _index_or_none(self, sequence, t, to=None):
if sequence is None:
return None
if to is None:
return sequence[t]
return sequence[t:to]
def current(self, t, to=None):
global_inputs = self._index_or_none(self.global_inputs, t, to)
local_inputs = self._index_or_none(self.local_inputs, t, to)
if to is None:
return External(global_inputs, local_inputs)
return ExternalSequences(global_inputs, local_inputs)
def truncate(self, length, total_length=None):
global_inputs = self._assert_seq_len(self.global_inputs, total_length)
local_inputs = self._assert_seq_len(self.local_inputs, total_length)
return ExternalSequences(
self._index_or_none(global_inputs, 0, length),
self._index_or_none(local_inputs, 0, length)
)
def _init_zeros(prefix_shape, dim):
prefix_static_shape = prefix_shape
if type(prefix_shape) is tf.TensorShape:
prefix_static_shape = prefix_shape.as_list()
elif type(prefix_shape) is not list:
prefix_shape = tf.unstack(prefix_shape)
prefix_static_shape = [None] * len(prefix_shape)
shape = tf.stack([*prefix_shape, dim])
zeros = tf.zeros(shape)
zeros.set_shape([*prefix_static_shape, dim])
return zeros
def refresh_histories_using_lstm(lstm_cell, histories, states):
assert (
(type(lstm_cell) is keras.layers.LSTMCell) or
(type(lstm_cell) is keras.layers.StackedRNNCells)
)
lstm_states = util.pack_rnn_states(lstm_cell, histories)
_, new_lstm_states = lstm_cell(inputs=states, states=lstm_states)
new_histories = util.concat_rnn_states(new_lstm_states)
return new_histories
class NonMarkovTransition(object):
def __init__(self, refreshed_histories, hidden_conditions,
next_state_dist):
self._refreshed_histories = refreshed_histories
self._hidden_conditions = hidden_conditions
self._next_state_dist = next_state_dist
@property
def refreshed_histories(self):
return self._refreshed_histories
@property
def hidden_conditions(self):
return self._hidden_conditions
@property
def next_state_dist(self):
return self._next_state_dist
class GlobalPrior(NonMarkovTransition):
def __init__(self, prev_local_states,
refreshed_local_histories, refreshed_histories,
hidden_conditions, pre_flow_dist, next_state_dist):
assert next_state_dist.reparameterization_type == \
tfd.FULLY_REPARAMETERIZED
super(GlobalPrior, self).__init__(
refreshed_histories, hidden_conditions, next_state_dist
)
self._refreshed_local_histories = refreshed_local_histories
self._prev_local_states = prev_local_states
self._pre_flow_dist = pre_flow_dist
@property
def refreshed_local_histories(self):
return self._refreshed_local_histories
@property
def prev_local_states(self):
return self._prev_local_states
@property
def pre_flow_dist(self):
return self._pre_flow_dist
class LocalPrior(NonMarkovTransition):
def __init__(self, global_priors, global_states, global_context,
propagated_local_histories, hidden_conditions,
pre_flow_dist, next_state_dist):
assert next_state_dist.reparameterization_type == \
tfd.FULLY_REPARAMETERIZED
super(LocalPrior, self).__init__(
# global_priors.refreshed_local_histories,
propagated_local_histories,
hidden_conditions, next_state_dist
)
self._global_priors = global_priors
self._global_states = global_states
self._global_context = global_context
self._propagated_histories = propagated_local_histories
self._pre_flow_dist = pre_flow_dist
@property
def global_priors(self):
return self._global_priors
@property
def global_states(self):
return self._global_states
@property
def global_context(self):
return self._global_context
@property
def propagated_histories(self):
return self._propagated_histories
@property
def pre_flow_dist(self):
return self._pre_flow_dist
@property
def full_histories(self):
return (
self.global_priors.refreshed_histories,
self.refreshed_histories
)
class FactorizedPrior(object):
def __init__(self, model, graph, external, global_priors):
self._model = model
self._graph = graph
self._external = external
self._global_priors = global_priors
@property
def dim_global_state(self):
return self._model.dim_latent
@property
def dim_local_state(self):
return self._model.dim_latent
@property
def num_nodes(self):
return self._graph.max_num_nodes
def sample(self):
return self._model.sample_latent_states(
self._graph, self._external, self._global_priors
)
def log_prob(self, samples):
global_states, local_states = samples
local_priors = self._model.trans_local(
self._graph, self._external,
self._global_priors, global_states
)
global_prior_dist = self._global_priors.next_state_dist
local_prior_dist = local_priors.next_state_dist
return tf.math.add(
global_prior_dist.log_prob(global_states),
local_prior_dist.log_prob(local_states)
)
class NonMarkovModel(object):
'''
Generative model for the non-Markovian GNN-SSM:
p(X_{0:T}, Z_{0:T}) = p(Z_0)p(X_0|Z_0)
\\prod _{t=1}^{T}{p(Z_t|Z_{1:t-1})p(X_t|Z_t)}
'''
def __init__(
self,
dim_observ, dim_latent, dim_hidden,
dim_mlp, gnn_config,
dim_global_input=0, dim_local_input=0,
const_num_nodes=None,
rnn_num_layers=1,
init_mix_num_components=1,
trans_activation="tanh", trans_layer_norm=False,
trans_mlp_num_layers=1, trans_mix_num_components=1,
trans_scale_activation="softplus",
trans_scale_shift=0.0, trans_scale_identical=False,
trans_ar=False, trans_skip_conn=False,
trans_global_low_rank=0, trans_local_low_rank=0,
trans_global_flow=False, trans_flow_num_layers=0,
trans_flow_mv_factor="qr", trans_flow_skip_conn=True,
emit_neg_binomial=False, emit_loc_scale_type="normal",
emit_non_markov=True, emit_identity=False,
emit_activation="linear", emit_low_rank=0,
emit_mlp_num_layers=1, emit_mix_num_components=1,
emit_scale_activation="softplus",
emit_scale_shift=0.0, emit_scale_identical=True,
name="non_markov_gen_model"
):
assert not (emit_identity and dim_observ > dim_latent)
gnn_config = gnn_config.clone()
gnn_config.readout = None
gnn_config.dim_input = dim_hidden
gnn_config.dim_global_state = dim_latent
dim_readout = 2 * dim_hidden
with tf.variable_scope(name):
if trans_skip_conn:
self._global_skip_conn_layer_norm = layer_norm_1d(
dim_latent, trainable=True,
name="global_skip_conn_layer_norm"
)
self._local_skip_conn_layer_norm = layer_norm_1d(
dim_latent, trainable=True,
name="local_skip_conn_layer_norm"
)
self._trans_gnn = StackedGraphNN(gnn_config, name="trans_gnn")
if init_mix_num_components > 1:
self._global_mixture_prior = make_trainable_gmm(
dim=dim_latent, num_components=init_mix_num_components,
name="trainable_gmm_prior"
)
self._global_rnn_cell = util.make_lstm_cells(
num_layers=rnn_num_layers,
dim_in=(dim_latent + dim_hidden + dim_global_input),
cell_size=dim_hidden,
name="global_rnn_cell"
)
self._local_rnn_cell = util.make_lstm_cells(
num_layers=rnn_num_layers,
dim_in=(
dim_latent + dim_local_input +
(dim_observ if trans_ar else 0)
),
cell_size=dim_hidden,
name="local_rnn_cell"
)
self._global_gated_unit = gated_unit(
dim_i=dim_readout, dim_o=dim_hidden, layer_norm=False,
name="global_gated_unit"
)
self._global_z_plus_h = skip_cond_gated_unit(
dim_i0=dim_latent, dim_i1=dim_hidden,
layer_norm=True, name="global_z_plus_h"
)
trans_params = dict(
dim_in=dim_hidden, dim_hid=dim_mlp, dim_out=dim_latent,
mlp_num_layers=trans_mlp_num_layers,
loc_activation=trans_activation,
loc_layer_norm=trans_layer_norm,
scale_activation=trans_scale_activation,
scale_shift=trans_scale_shift,
scale_identical=trans_scale_identical
)
if trans_global_low_rank > 0:
self._global_trans_dist = mlp_low_rank_normal(
**trans_params, cov_rank=trans_global_low_rank,
name="global_trans_dist"
)
else:
self._global_trans_dist = mlp_mix_diag_normal(
**trans_params,
mix_num_components=trans_mix_num_components,
name="global_trans_dist"
)
if trans_local_low_rank > 0:
self._local_trans_dist = mlp_low_rank_normal(
**trans_params, cov_rank=trans_local_low_rank,
name="local_trans_dist"
)
else:
self._local_trans_dist = mlp_mix_diag_normal(
**trans_params,
mix_num_components=trans_mix_num_components,
name="local_trans_dist"
)
if trans_flow_num_layers > 0:
if trans_global_flow:
self._global_trans_flow_components = init_real_nvp(
num_layers=trans_flow_num_layers,
dim_latent=dim_latent,
dim_context=dim_hidden, dim_mlp=dim_mlp,
conv_1x1_factor=trans_flow_mv_factor,
name="global_trans_flow"
)
self._local_trans_flow_components = init_perm_equiv_flow(
num_layers=trans_flow_num_layers,
dim_latent=dim_latent, dim_context=dim_hidden,
nvp_gnn_config=gnn_config.single_layer,
conv_1x1_factor=trans_flow_mv_factor,
name="local_trans_perm_equiv_flow"
)
assert not (emit_identity and emit_neg_binomial)
assert not (emit_identity and emit_mix_num_components > 1)
assert not (emit_identity and emit_non_markov)
assert not (emit_identity and emit_activation != "linear")
if emit_non_markov:
self._emit_z_plus_h = skip_cond_gated_unit(
dim_i0=(2 * dim_latent), dim_i1=(2 * dim_hidden),
layer_norm=False, name="z_plus_h"
)
emit_dist_params = dict(
dim_in=(2 * dim_latent), dim_hid=dim_mlp, dim_out=dim_observ,
mlp_num_layers=emit_mlp_num_layers,
name="emit_dist"
)
emit_loc_scale_dist_params = dict(
**emit_dist_params,
loc_activation=emit_activation,
loc_layer_norm=False,
scale_activation=emit_scale_activation,
scale_shift=emit_scale_shift,
scale_identical=emit_scale_identical
)
if emit_identity:
self._local_emit_dist = identity_diag_normal(
dim_in=dim_latent, dim_out=dim_observ,
scale_activation=emit_scale_activation,
scale_shift=emit_scale_shift,
name="emit_dist"
)
elif emit_neg_binomial:
assert dim_observ == 1
assert emit_mix_num_components == 1 # TODO
self._local_emit_dist = mlp_mix_neg_binomial(
mix_num_components=emit_mix_num_components,
**emit_dist_params
)
elif emit_low_rank > 0:
self._local_emit_dist = mlp_low_rank_normal(
**emit_loc_scale_dist_params,
cov_rank=emit_low_rank
)
else:
builder = get_mlp_mix_loc_scale_builder(emit_loc_scale_type)
self._local_emit_dist = builder(
**emit_loc_scale_dist_params,
mix_num_components=emit_mix_num_components
)
self._dim_latent = dim_latent
self._dim_observ = dim_observ
self._dim_hidden = dim_hidden
self._dim_global_input = dim_global_input
self._dim_local_input = dim_local_input
self._const_num_nodes = const_num_nodes
self._gnn_config = gnn_config
self._rnn_num_layers = rnn_num_layers
self._num_trans_modes = trans_mix_num_components
self._num_emit_modes = emit_mix_num_components
self._use_mixture_prior = (init_mix_num_components > 1)
self._trans_ar = trans_ar
self._trans_skip_conn = trans_skip_conn
self._trans_global_flow = trans_global_flow
self._trans_flow_num_layers = trans_flow_num_layers
self._trans_flow_skip_conn = trans_flow_skip_conn
self._emit_identity = emit_identity
self._emit_non_markov = emit_non_markov
if emit_identity:
self._state_mask = tf.concat([
tf.zeros([dim_observ]),
tf.ones([dim_latent - dim_observ])
], axis=-1)
else:
self._state_mask = tf.ones([dim_latent])
@property
def dim_observ(self):
return self._dim_observ
@property
def dim_latent(self):
return self._dim_latent
@property
def dim_hidden(self):
return self._dim_hidden
@property
def dim_full_hidden(self):
return 2 * self.rnn_num_layers * self.dim_hidden
@property
def dim_global_input(self):
return self._dim_global_input
@property
def dim_local_input(self):
return self._dim_local_input
@property
def const_num_nodes(self):
assert self._const_num_nodes is not None
return self._const_num_nodes
@property
def gnn_config(self):
return self._gnn_config
@property
def rnn_num_layers(self):
return self._rnn_num_layers
@property
def use_mixture_prior(self):
return self._use_mixture_prior
@property
def trans_ar(self):
return self._trans_ar
@property
def trans_skip_conn(self):
return self._trans_skip_conn
@property
def trans_global_flow(self):
return self._trans_global_flow
@property
def trans_flow_num_layers(self):
return self._trans_flow_num_layers
@property
def trans_flow_skip_conn(self):
return self._trans_flow_skip_conn
@property
def global_trans_flow_components(self):
return self._global_trans_flow_components
@property
def local_trans_flow_components(self):
return self._local_trans_flow_components
@property
def emit_non_markov(self):
return self._emit_non_markov
@property
def emit_identity(self):
return self._emit_identity
@property
def state_mask(self):
return self._state_mask
@property
def num_trans_modes(self):
return self._num_trans_modes
def _concat_with_inputs(self, context, inputs):
if inputs is None:
return context
if inputs.shape.ndims < context.shape.ndims:
inputs = tf.math.add(
inputs, tf.zeros(tf.stack([
*tf.unstack(tf.shape(context)[:-1]), util.dim(inputs)
]))
)
return tf.concat([context, inputs], axis=-1)
def _broadcast_and_concat(self, global_context, local_context):
global_context = tf.expand_dims(global_context, axis=-2)
global_context = tf.math.add(
global_context, tf.zeros(tf.stack([
*tf.unstack(tf.shape(local_context)[:-1]),
util.dim(global_context)
]))
)
return tf.concat([global_context, local_context], axis=-1)
def init_global_histories(self, batch_shape):
'''
Returns:
global_histories: A (..., dH) Tensor.
'''
return _init_zeros(batch_shape, self.dim_full_hidden)
def init_local_histories(self, prefix_shape):
'''
Returns:
local_histories: A (..., N, dH) Tensor.
'''
return _init_zeros(prefix_shape, self.dim_full_hidden)
def extract_rnn_output(self, histories):
return util.extract_rnn_output(
histories, self.rnn_num_layers, util.LSTM
)
def extract_all_rnn_output(self, histories):
return (
self.extract_rnn_output(histories[0]),
self.extract_rnn_output(histories[1])
)
def readout(self, graph, local_histories):
'''
Args:
local_histories: A (..., N, dH) Tensor.
Returns:
summaries: A (..., 2*dh) Tensor.
'''
return self._global_gated_unit(
readout_mean_max(self.gnn_config, graph, local_histories)
)
def refresh_global_histories(self, graph, external,
global_histories, global_states, readouts):
'''
Args:
histories: A (..., dH) Tensor which summrizes Z_{1:t-1}.
new_states: A (..., dz) Tensor.
readouts: A (..., dh) Tensor.
Returns:
new_histories: A (..., dH) Tensor.
'''
return refresh_histories_using_lstm(
self._global_rnn_cell, global_histories,
self._concat_with_inputs(
tf.concat([global_states, readouts], axis=-1),
external.global_inputs
)
)
def refresh_local_histories(self, graph, external,
local_histories, local_states, observations):
'''
Args:
histories: A (..., dH) Tensor that summrizes Z_{1:t-1}.
new_states: A (..., dz) Tensor.
Returns:
new_histories: A (..., dH) Tensor.
'''
if self.trans_ar:
local_states = self._concat_with_inputs(local_states, observations)
return refresh_histories_using_lstm(
self._local_rnn_cell, local_histories,
self._concat_with_inputs(local_states, external.local_inputs)
)
def propagate_local_histories(
self, graph, global_context, local_histories):
'''
Args:
global_context: A (..., dz) Tensor.
local_histories: A (..., N, dH) Tensor.
Returns:
correlated_context: A (..., N, dH) Tensor.
'''
correlated_histories = self._trans_gnn(
graph=graph, states=local_histories,
global_states=global_context
)
mask = tf.expand_dims(graph.center_mask, axis=-1)
return util.select(mask, correlated_histories, local_histories)
def global_prior(self, graph, external, batch_shape):
'''
Returns:
A distribution with batch shape (shape) and event shape (dz).
'''
del graph, external
if self.use_mixture_prior:
return util.broadcast_gmm(self._global_mixture_prior, batch_shape)
full_shape = tf.stack([*tf.unstack(batch_shape), self.dim_latent])
return tfd.MultivariateNormalDiag(
loc=tf.zeros(full_shape), scale_diag=tf.ones(full_shape)
)
def init_global(self, graph, external, num_samples, prefix_shape):
'''
Args:
prefix_shape: A tuple, (..., B, N).
Returns:
global_prior: A GlobalPrior object.
'''
prefix = tf.stack([num_samples, *tf.unstack(prefix_shape)])
global_context = tf.zeros(tf.stack([
*tf.unstack(prefix[:-1]), self.dim_hidden
]))
global_prior_dist = self.global_prior(graph, external, prefix[:-1])
pre_flow_dist = global_prior_dist
if self.trans_global_flow and self.trans_flow_num_layers > 0:
global_prior_dist = real_nvp_wrapper(
components=self.global_trans_flow_components,
context=global_context,
base_dist=global_prior_dist,
skip_conn=self.trans_flow_skip_conn
)
return GlobalPrior(
prev_local_states=tf.zeros(tf.stack([
*tf.unstack(prefix), self.dim_latent
])),
refreshed_local_histories=self.init_local_histories(prefix),
refreshed_histories=self.init_global_histories(prefix[:-1]),
hidden_conditions=global_context,
pre_flow_dist=pre_flow_dist,
next_state_dist=global_prior_dist
)
def trans_global(self, graph, external, histories, states, observations):
'''
Z_{1:t} -> Z_{t+1} <=> (H_{t-1}, Z_t) -> Z_{t+1}
Args:
graph: A RuntimeGraph object.
inputs: Optional. A (..., [N, ]di) Tensor.
histories: A 2-ary tuple:
- global_histories: A (..., dH) Tensor.
- local_histories: A (..., N, dH) Tensor.
states: A 2-ary tuple:
- global_states: A (..., dz) Tensor.
- local_states: A (..., N, dz) Tensor.
observations: A (..., N, dx) Tensor.
Returns:
global_transition: A GlobalTransition object.
'''
global_histories, local_histories = histories
global_states, local_states = states
new_local_histories = self.refresh_local_histories(
graph, external, local_histories, local_states, observations
)
new_global_histories = self.refresh_global_histories(
graph, external, global_histories, global_states,
self.readout(graph, new_local_histories)
)
new_global_context = self.extract_rnn_output(new_global_histories)
new_global_state_dist = self._global_trans_dist(new_global_context)
if self.trans_skip_conn:
assert type(new_global_state_dist) is util.PartialLocScaleDist
new_global_state_dist.loc = self._global_skip_conn_layer_norm(
tf.math.add(new_global_state_dist.loc, global_states)
)
if type(new_global_state_dist) is util.PartialLocScaleDist:
new_global_state_dist = new_global_state_dist.build()
pre_flow_dist = new_global_state_dist
if self.trans_global_flow and self.trans_flow_num_layers > 0:
new_global_state_dist = real_nvp_wrapper(
components=self.global_trans_flow_components,
context=new_global_context,
base_dist=new_global_state_dist,
skip_conn=self.trans_flow_skip_conn
)
return GlobalPrior(
prev_local_states=local_states,
refreshed_local_histories=new_local_histories,
refreshed_histories=new_global_histories,
hidden_conditions=new_global_context,
pre_flow_dist=pre_flow_dist,
next_state_dist=new_global_state_dist,
)
def trans_local(self, graph, external, global_priors, global_states):
'''
Z_{1:t} -> Z_{t+1} <=> (H_{t-1}, Z_t) -> Z_{t+1}
Args:
graph: A RuntimeGraph object.
external: An External Tensor.
global_transition: A GlobalTransition object.
global_states: A (..., dz) Tensor.
Returns:
local_transition: A LocalTransition object.
'''
new_global_context = self._global_z_plus_h(
global_states, global_priors.hidden_conditions
)
correlated_histories = self.propagate_local_histories(
graph, new_global_context,
global_priors.refreshed_local_histories
)
new_local_context = self.extract_rnn_output(correlated_histories)
new_local_state_dist = self._local_trans_dist(new_local_context)
if self.trans_skip_conn:
assert type(new_local_state_dist) is util.PartialLocScaleDist
new_local_state_dist.loc = self._local_skip_conn_layer_norm(
tf.math.add(
global_priors.prev_local_states,
new_local_state_dist.loc
)
)
if type(new_local_state_dist) is util.PartialLocScaleDist:
new_local_state_dist = new_local_state_dist.build()
pre_flow_dist = new_local_state_dist
new_local_state_dist = tfd.Independent(
distribution=new_local_state_dist,
reinterpreted_batch_ndims=1,
name="indep_" + new_local_state_dist.name
)
# TODO: mask
if self.trans_flow_num_layers > 0:
new_local_state_dist = perm_equiv_flow_wrapper(
components=self.local_trans_flow_components,
graph=graph, const_num_nodes=self.const_num_nodes,
global_context=new_global_context,
local_context=new_local_context,
base_dist=new_local_state_dist,
skip_conn=self.trans_flow_skip_conn
)
return LocalPrior(
global_priors=global_priors,
global_states=global_states,
global_context=new_global_context,
propagated_local_histories=correlated_histories,
hidden_conditions=new_local_context,
pre_flow_dist=pre_flow_dist,
next_state_dist=new_local_state_dist
)
def emit(self, histories, states):
'''
Z_{1:t} -> X_t
Args:
histories: A 2-ary tuple:
- global_histories: A (..., dH) Tensor.
- local_histories: A (..., N, dH) Tensor.
states: A 2-ary tuple:
- global_states: A (..., dz) Tensor.
- local_states: A (..., N, dz) Tensor.
Returns:
A distribution with batch shape (..., N) and event shape (dx).
'''
global_histories, local_histories = histories
global_context = self.extract_rnn_output(global_histories)
local_context = self.extract_rnn_output(local_histories)
context = (global_context, local_context)
concat_context = self._broadcast_and_concat(*context)
concat_states = self._broadcast_and_concat(*states)
emit_context = self._emit_z_plus_h(concat_states, concat_context) \
if self.emit_non_markov else concat_states
dist = self._local_emit_dist(emit_context)
if type(dist) is util.PartialLocScaleDist:
dist = dist.build()
return dist
def factorized_prior(self, graph, external, global_priors):
return FactorizedPrior(self, graph, external, global_priors)
def sample_latent_states(self, graph, external, global_priors):
new_global_states = global_priors.next_state_dist.sample(1)[0]
local_priors = self.trans_local(
graph, external, global_priors, new_global_states
)
new_local_states = local_priors.next_state_dist.sample(1)[0]
log_probs = tf.math.add(
global_priors.next_state_dist.log_prob(new_global_states),
local_priors.next_state_dist.log_prob(new_local_states)
)
samples = (new_global_states, new_local_states)
histories = local_priors.full_histories
return samples, histories, log_probs
def recover_histories_if_stale(self, graph, external, global_priors,
histories, states):
if histories is not None:
return histories
global_states, _ = states
local_priors = self.trans_local(
graph, external, global_priors, global_states
)
return local_priors.full_histories
def predict(model, graph, external,
end_histories, end_states, end_observations, horizon):
'''
Args:
model: The generative model.
graph: A RuntimeGraph object.
end_histories: A tuple of Tensors: (S, B, dh) and (S, B, N, dh).
end_states: A tuple of Tensors: (S, B, dz) and (S, B, N, dz).
end_observations: A (B, N, dx) Tensor.
horizon: A scalar.
Returns:
predictions: A (S, H, B, N, dx) Tensor.
log_probs: A (S, H, B) Tensor.
'''
_, end_local_states = end_states
num_samples = tf.shape(end_local_states)[0]
def cond(t, *unused_args):
return tf.less(t, horizon)
def body(t, histories, states, observations, log_probs_acc,
locs_array, scales_array, log_probs_array):
global_priors = model.trans_global(
graph, external.current(t),
histories, states, observations
)
new_global_states = global_priors.next_state_dist.sample(1)[0]
local_priors = model.trans_local(
graph, external.current(t),
global_priors, new_global_states
)
new_local_states = local_priors.next_state_dist.sample(1)[0]
new_log_probs_acc = tf.math.add(
log_probs_acc, tf.math.add(
global_priors.next_state_dist.log_prob(new_global_states),
local_priors.next_state_dist.log_prob(new_local_states)
)
)
new_histories = local_priors.full_histories
new_states = (new_global_states, new_local_states)
likelihood = model.emit(new_histories, new_states)
new_locs_array = locs_array.write(t, likelihood.mean())
new_scales_array = scales_array.write(t, likelihood.stddev())
new_log_probs_array = log_probs_array.write(t, new_log_probs_acc)
return (
t + 1, new_histories, new_states, likelihood.mean(),
new_log_probs_acc,
new_locs_array, new_scales_array, new_log_probs_array
)
broadcast_end_observations = tf.tile(
tf.expand_dims(end_observations, axis=0),
tf.stack([num_samples] + ([1] * end_observations.shape.ndims))
)
t0 = tf.constant(0)
init_log_probs_acc = tf.zeros(tf.shape(end_local_states)[:-2])
init_locs_array = tf.TensorArray(tf.float32, size=horizon)
init_scales_array = tf.TensorArray(tf.float32, size=horizon)
init_log_probs_array = tf.TensorArray(tf.float32, size=horizon)
_, _, _, _, _, locs_array, scales_array, log_probs_array = tf.while_loop(
cond, body,
[
t0, end_histories, end_states, broadcast_end_observations,
init_log_probs_acc,
init_locs_array, init_scales_array, init_log_probs_array
]
)
locs, scales = locs_array.stack(), scales_array.stack()
log_probs = log_probs_array.stack()
assert locs.shape.ndims == 5
assert scales.shape.ndims == 5
assert log_probs.shape.ndims == 3
static_batch_shape = end_local_states.shape.as_list()[:-1]
locs.set_shape([horizon, *static_batch_shape, None])
scales.set_shape([horizon, *static_batch_shape, None])
log_probs.set_shape([horizon, *static_batch_shape[:-1]])
# (H, S, B, N, dx) -> (S, H, B, N, dx)
locs = tf.transpose(locs, perm=[1, 0, 2, 3, 4])
scales = tf.transpose(scales, perm=[1, 0, 2, 3, 4])
# (H, S, B) -> (S, H, B)
log_probs = tf.transpose(log_probs, perm=[1, 0, 2])
return locs, scales, log_probs
# TODO
def preview(model, graph, external, histories, states, num_preview_steps):
for t in range(num_preview_steps - 1):
priors = model.trans(
graph=graph, histories=histories, states=states,
inputs=external.current(t)
)
next_states_dist = priors.next_state_dist
histories = priors.refreshed_histories
states = next_states_dist.sample(1)[0]
return tf.concat([histories, states], axis=-1)
def init_preview_array(
num_time_steps, num_preview_steps,
model, graph, external, initial_states, initial_histories):
preview_array = tf.TensorArray(
tf.float32, size=(num_time_steps + num_preview_steps)
)
zero_previews = tf.concat(
[tf.zeros_like(initial_histories), tf.zeros_like(initial_states)],
axis=-1
)
for k in range(num_preview_steps):
preview_array = preview_array.write(k, zero_previews)
preview_array = preview_array.write(
num_preview_steps,
preview(
model, graph, external, initial_states, initial_histories,
num_preview_steps
)
)
return preview_array