This repository has been archived by the owner on Dec 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
chmm_actions.py
executable file
·709 lines (631 loc) · 22.6 KB
/
chmm_actions.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
from __future__ import print_function
from builtins import range
import numpy as np
import numba as nb
from tqdm import trange
import sys
def validate_seq(x, a, n_clones=None):
"""Validate an input sequence of observations x and actions a"""
assert len(x) == len(a) > 0
assert len(x.shape) == len(a.shape) == 1, "Flatten your array first"
assert x.dtype == a.dtype == np.int64
assert 0 <= x.min(), "Number of emissions inconsistent with training sequence"
if n_clones is not None:
assert len(n_clones.shape) == 1, "Flatten your array first"
assert n_clones.dtype == np.int64
assert all(
[c > 0 for c in n_clones]
), "You can't provide zero clones for any emission"
n_emissions = n_clones.shape[0]
assert (
x.max() < n_emissions
), "Number of emissions inconsistent with training sequence"
def datagen_structured_obs_room(
room,
start_r=None,
start_c=None,
no_left=[],
no_right=[],
no_up=[],
no_down=[],
length=10000,
seed=42,
):
"""room is a 2d numpy array. inaccessible locations are marked by -1.
start_r, start_c: starting locations
In addition, there are invisible obstructions in the room
which disallows certain actions from certain states.
no_left:
no_right:
no_up:
no_down:
Each of the above are list of states from which the corresponding action is not allowed.
"""
np.random.seed(seed)
H, W = room.shape
if start_r is None or start_c is None:
start_r, start_c = np.random.randint(H), np.random.randint(W)
actions = np.zeros(length, int)
x = np.zeros(length, int) # observations
rc = np.zeros((length, 2), int) # actual r&c
r, c = start_r, start_c
x[0] = room[r, c]
rc[0] = r, c
count = 0
while count < length - 1:
act_list = [0, 1, 2, 3] # 0: left, 1: right, 2: up, 3: down
if (r, c) in no_left:
act_list.remove(0)
if (r, c) in no_right:
act_list.remove(1)
if (r, c) in no_up:
act_list.remove(2)
if (r, c) in no_down:
act_list.remove(3)
a = np.random.choice(act_list)
# Check for actions taking out of the matrix boundary.
prev_r = r
prev_c = c
if a == 0 and 0 < c:
c -= 1
elif a == 1 and c < W - 1:
c += 1
elif a == 2 and 0 < r:
r -= 1
elif a == 3 and r < H - 1:
r += 1
# Check whether action is taking to inaccessible states.
temp_x = room[r, c]
if temp_x == -1:
r = prev_r
c = prev_c
pass
actions[count] = a
x[count + 1] = room[r, c]
rc[count + 1] = r, c
count += 1
return actions, x, rc
class CHMM(object):
def __init__(self, n_clones, x, a, pseudocount=0.0, dtype=np.float32, seed=42):
"""Construct a CHMM objct. n_clones is an array where n_clones[i] is the
number of clones assigned to observation i. x and a are the observation sequences
and action sequences, respectively."""
np.random.seed(seed)
self.n_clones = n_clones
validate_seq(x, a, self.n_clones)
assert pseudocount >= 0.0, "The pseudocount should be positive"
print("Average number of clones:", n_clones.mean())
self.pseudocount = pseudocount
self.dtype = dtype
n_states = self.n_clones.sum()
n_actions = a.max() + 1
self.C = np.random.rand(n_actions, n_states, n_states).astype(dtype)
self.Pi_x = np.ones(n_states) / n_states
self.Pi_a = np.ones(n_actions) / n_actions
self.update_T()
def update_T(self):
"""Update the transition matrix given the accumulated counts matrix."""
self.T = self.C + self.pseudocount
norm = self.T.sum(2, keepdims=True)
norm[norm == 0] = 1
self.T /= norm
# def update_T(self):
# self.T = self.C + self.pseudocount
# norm = self.T.sum(2, keepdims=True) # old model (conditional on actions)
# norm[norm == 0] = 1
# self.T /= norm
# norm = self.T.sum((0, 2), keepdims=True) # new model (generates actions too)
# norm[norm == 0] = 1
# self.T /= norm
def update_E(self, CE):
"""Update the emission matrix."""
E = CE + self.pseudocount
norm = E.sum(1, keepdims=True)
norm[norm == 0] = 1
E /= norm
return E
def bps(self, x, a):
"""Compute the log likelihood (log base 2) of a sequence of observations and actions."""
validate_seq(x, a, self.n_clones)
log2_lik = forward(self.T.transpose(0, 2, 1), self.Pi_x, self.n_clones, x, a)[0]
return -log2_lik
def bpsE(self, E, x, a):
"""Compute the log likelihood using an alternate emission matrix."""
validate_seq(x, a, self.n_clones)
log2_lik = forwardE(
self.T.transpose(0, 2, 1), E, self.Pi_x, self.n_clones, x, a
)
return -log2_lik
def bpsV(self, x, a):
validate_seq(x, a, self.n_clones)
log2_lik = forward_mp(
self.T.transpose(0, 2, 1), self.Pi_x, self.n_clones, x, a
)[0]
return -log2_lik
def decode(self, x, a):
"""Compute the MAP assignment of latent variables using max-product message passing."""
log2_lik, mess_fwd = forward_mp(
self.T.transpose(0, 2, 1),
self.Pi_x,
self.n_clones,
x,
a,
store_messages=True,
)
states = backtrace(self.T, self.n_clones, x, a, mess_fwd)
return -log2_lik, states
def decodeE(self, E, x, a):
"""Compute the MAP assignment of latent variables using max-product message passing
with an alternative emission matrix."""
log2_lik, mess_fwd = forwardE_mp(
self.T.transpose(0, 2, 1),
E,
self.Pi_x,
self.n_clones,
x,
a,
store_messages=True,
)
states = backtraceE(self.T, E, self.n_clones, x, a, mess_fwd)
return -log2_lik, states
def learn_em_T(self, x, a, n_iter=100, term_early=True):
"""Run EM training, keeping E deterministic and fixed, learning T"""
sys.stdout.flush()
convergence = []
pbar = trange(n_iter, position=0)
log2_lik_old = -np.inf
for it in pbar:
# E
log2_lik, mess_fwd = forward(
self.T.transpose(0, 2, 1),
self.Pi_x,
self.n_clones,
x,
a,
store_messages=True,
)
mess_bwd = backward(self.T, self.n_clones, x, a)
updateC(self.C, self.T, self.n_clones, mess_fwd, mess_bwd, x, a)
# M
self.update_T()
convergence.append(-log2_lik.mean())
pbar.set_postfix(train_bps=convergence[-1])
if log2_lik.mean() <= log2_lik_old:
if term_early:
break
log2_lik_old = log2_lik.mean()
return convergence
def learn_viterbi_T(self, x, a, n_iter=100):
"""Run Viterbi training, keeping E deterministic and fixed, learning T"""
sys.stdout.flush()
convergence = []
pbar = trange(n_iter, position=0)
log2_lik_old = -np.inf
for it in pbar:
# E
log2_lik, mess_fwd = forward_mp(
self.T.transpose(0, 2, 1),
self.Pi_x,
self.n_clones,
x,
a,
store_messages=True,
)
states = backtrace(self.T, self.n_clones, x, a, mess_fwd)
self.C[:] = 0
for t in range(1, len(x)):
aij, i, j = (
a[t - 1],
states[t - 1],
states[t],
) # at time t-1 -> t we go from observation i to observation j
self.C[aij, i, j] += 1.0
# M
self.update_T()
convergence.append(-log2_lik.mean())
pbar.set_postfix(train_bps=convergence[-1])
if log2_lik.mean() <= log2_lik_old:
break
log2_lik_old = log2_lik.mean()
return convergence
def learn_em_E(self, x, a, n_iter=100, pseudocount_extra=1e-20):
"""Run Viterbi training, keeping T fixed, learning E"""
sys.stdout.flush()
n_emissions, n_states = len(self.n_clones), self.n_clones.sum()
CE = np.ones((n_states, n_emissions), self.dtype)
E = self.update_E(CE + pseudocount_extra)
convergence = []
pbar = trange(n_iter, position=0)
log2_lik_old = -np.inf
for it in pbar:
# E
log2_lik, mess_fwd = forwardE(
self.T.transpose(0, 2, 1),
E,
self.Pi_x,
self.n_clones,
x,
a,
store_messages=True,
)
mess_bwd = backwardE(self.T, E, self.n_clones, x, a)
updateCE(CE, E, self.n_clones, mess_fwd, mess_bwd, x, a)
# M
E = self.update_E(CE + pseudocount_extra)
convergence.append(-log2_lik.mean())
pbar.set_postfix(train_bps=convergence[-1])
if log2_lik.mean() <= log2_lik_old:
break
log2_lik_old = log2_lik.mean()
return convergence, E
def sample(self, length):
"""Sample from the CHMM."""
assert length > 0
state_loc = np.hstack(([0], self.n_clones)).cumsum(0)
sample_x = np.zeros(length, dtype=np.int64)
sample_a = np.random.choice(len(self.Pi_a), size=length, p=self.Pi_a)
# Sample
p_h = self.Pi_x
for t in range(length):
h = np.random.choice(len(p_h), p=p_h)
sample_x[t] = np.digitize(h, state_loc) - 1
p_h = self.T[sample_a[t], h]
return sample_x, sample_a
def sample_sym(self, sym, length):
"""Sample from the CHMM conditioning on an inital observation."""
# Prepare structures
assert length > 0
state_loc = np.hstack(([0], self.n_clones)).cumsum(0)
seq = [sym]
alpha = np.ones(self.n_clones[sym])
alpha /= alpha.sum()
for _ in range(length):
obs_tm1 = seq[-1]
T_weighted = self.T.sum(0)
long_alpha = np.dot(
alpha, T_weighted[state_loc[obs_tm1] : state_loc[obs_tm1 + 1], :]
)
long_alpha /= long_alpha.sum()
idx = np.random.choice(np.arange(self.n_clones.sum()), p=long_alpha)
sym = np.digitize(idx, state_loc) - 1
seq.append(sym)
temp_alpha = long_alpha[state_loc[sym] : state_loc[sym + 1]]
temp_alpha /= temp_alpha.sum()
alpha = temp_alpha
return seq
def bridge(self, state1, state2, max_steps=100):
Pi_x = np.zeros(self.n_clones.sum(), dtype=self.dtype)
Pi_x[state1] = 1
log2_lik, mess_fwd = forward_mp_all(
self.T.transpose(0, 2, 1), Pi_x, self.Pi_a, self.n_clones, state2, max_steps
)
s_a = backtrace_all(self.T, self.Pi_a, self.n_clones, mess_fwd, state2)
return s_a
def updateCE(CE, E, n_clones, mess_fwd, mess_bwd, x, a):
timesteps = len(x)
gamma = mess_fwd * mess_bwd
norm = gamma.sum(1, keepdims=True)
norm[norm == 0] = 1
gamma /= norm
CE[:] = 0
for t in range(timesteps):
CE[:, x[t]] += gamma[t]
def forwardE(T_tr, E, Pi, n_clones, x, a, store_messages=False):
"""Log-probability of a sequence, and optionally, messages"""
assert (n_clones.sum(), len(n_clones)) == E.shape
dtype = T_tr.dtype.type
# forward pass
t, log2_lik = 0, np.zeros(len(x), dtype)
j = x[t]
message = Pi * E[:, j]
p_obs = message.sum()
assert p_obs > 0
message /= p_obs
log2_lik[0] = np.log2(p_obs)
if store_messages:
mess_fwd = np.empty((len(x), E.shape[0]), dtype=dtype)
mess_fwd[t] = message
for t in range(1, x.shape[0]):
aij, j = (
a[t - 1],
x[t],
) # at time t-1 -> t we go from observation i to observation j
message = T_tr[aij].dot(message)
message *= E[:, j]
p_obs = message.sum()
assert p_obs > 0
message /= p_obs
log2_lik[t] = np.log2(p_obs)
if store_messages:
mess_fwd[t] = message
if store_messages:
return log2_lik, mess_fwd
else:
return log2_lik
def backwardE(T, E, n_clones, x, a):
"""Compute backward messages."""
assert (n_clones.sum(), len(n_clones)) == E.shape
dtype = T.dtype.type
# backward pass
t = x.shape[0] - 1
message = np.ones(E.shape[0], dtype)
message /= message.sum()
mess_bwd = np.empty((len(x), E.shape[0]), dtype=dtype)
mess_bwd[t] = message
for t in range(x.shape[0] - 2, -1, -1):
aij, j = (
a[t],
x[t + 1],
) # at time t -> t+1 we go from observation i to observation j
message = T[aij].dot(message * E[:, j])
p_obs = message.sum()
assert p_obs > 0
message /= p_obs
mess_bwd[t] = message
return mess_bwd
@nb.njit
def updateC(C, T, n_clones, mess_fwd, mess_bwd, x, a):
state_loc = np.hstack((np.array([0], dtype=n_clones.dtype), n_clones)).cumsum()
mess_loc = np.hstack((np.array([0], dtype=n_clones.dtype), n_clones[x])).cumsum()
timesteps = len(x)
C[:] = 0
for t in range(1, timesteps):
aij, i, j = (
a[t - 1],
x[t - 1],
x[t],
) # at time t-1 -> t we go from observation i to observation j
(tm1_start, tm1_stop), (t_start, t_stop) = (
mess_loc[t - 1 : t + 1],
mess_loc[t : t + 2],
)
(i_start, i_stop), (j_start, j_stop) = (
state_loc[i : i + 2],
state_loc[j : j + 2],
)
q = (
mess_fwd[tm1_start:tm1_stop].reshape(-1, 1)
* T[aij, i_start:i_stop, j_start:j_stop]
* mess_bwd[t_start:t_stop].reshape(1, -1)
)
q /= q.sum()
C[aij, i_start:i_stop, j_start:j_stop] += q
@nb.njit
def forward(T_tr, Pi, n_clones, x, a, store_messages=False):
"""Log-probability of a sequence, and optionally, messages"""
state_loc = np.hstack((np.array([0], dtype=n_clones.dtype), n_clones)).cumsum()
dtype = T_tr.dtype.type
# forward pass
t, log2_lik = 0, np.zeros(len(x), dtype)
j = x[t]
j_start, j_stop = state_loc[j : j + 2]
message = Pi[j_start:j_stop].copy().astype(dtype)
p_obs = message.sum()
assert p_obs > 0
message /= p_obs
log2_lik[0] = np.log2(p_obs)
if store_messages:
mess_loc = np.hstack(
(np.array([0], dtype=n_clones.dtype), n_clones[x])
).cumsum()
mess_fwd = np.empty(mess_loc[-1], dtype=dtype)
t_start, t_stop = mess_loc[t : t + 2]
mess_fwd[t_start:t_stop] = message
else:
mess_fwd = None
for t in range(1, x.shape[0]):
aij, i, j = (
a[t - 1],
x[t - 1],
x[t],
) # at time t-1 -> t we go from observation i to observation j
(i_start, i_stop), (j_start, j_stop) = (
state_loc[i : i + 2],
state_loc[j : j + 2],
)
message = np.ascontiguousarray(T_tr[aij, j_start:j_stop, i_start:i_stop]).dot(
message
)
p_obs = message.sum()
assert p_obs > 0
message /= p_obs
log2_lik[t] = np.log2(p_obs)
if store_messages:
t_start, t_stop = mess_loc[t : t + 2]
mess_fwd[t_start:t_stop] = message
return log2_lik, mess_fwd
@nb.njit
def backward(T, n_clones, x, a):
"""Compute backward messages."""
state_loc = np.hstack((np.array([0], dtype=n_clones.dtype), n_clones)).cumsum()
dtype = T.dtype.type
# backward pass
t = x.shape[0] - 1
i = x[t]
message = np.ones(n_clones[i], dtype) / n_clones[i]
message /= message.sum()
mess_loc = np.hstack((np.array([0], dtype=n_clones.dtype), n_clones[x])).cumsum()
mess_bwd = np.empty(mess_loc[-1], dtype)
t_start, t_stop = mess_loc[t : t + 2]
mess_bwd[t_start:t_stop] = message
for t in range(x.shape[0] - 2, -1, -1):
aij, i, j = (
a[t],
x[t],
x[t + 1],
) # at time t -> t+1 we go from observation i to observation j
(i_start, i_stop), (j_start, j_stop) = (
state_loc[i : i + 2],
state_loc[j : j + 2],
)
message = np.ascontiguousarray(T[aij, i_start:i_stop, j_start:j_stop]).dot(
message
)
p_obs = message.sum()
assert p_obs > 0
message /= p_obs
t_start, t_stop = mess_loc[t : t + 2]
mess_bwd[t_start:t_stop] = message
return mess_bwd
@nb.njit
def forward_mp(T_tr, Pi, n_clones, x, a, store_messages=False):
"""Log-probability of a sequence, and optionally, messages"""
state_loc = np.hstack((np.array([0], dtype=n_clones.dtype), n_clones)).cumsum()
dtype = T_tr.dtype.type
# forward pass
t, log2_lik = 0, np.zeros(len(x), dtype)
j = x[t]
j_start, j_stop = state_loc[j : j + 2]
message = Pi[j_start:j_stop].copy().astype(dtype)
p_obs = message.max()
assert p_obs > 0
message /= p_obs
log2_lik[0] = np.log2(p_obs)
if store_messages:
mess_loc = np.hstack(
(np.array([0], dtype=n_clones.dtype), n_clones[x])
).cumsum()
mess_fwd = np.empty(mess_loc[-1], dtype=dtype)
t_start, t_stop = mess_loc[t : t + 2]
mess_fwd[t_start:t_stop] = message
else:
mess_fwd = None
for t in range(1, x.shape[0]):
aij, i, j = (
a[t - 1],
x[t - 1],
x[t],
) # at time t-1 -> t we go from observation i to observation j
(i_start, i_stop), (j_start, j_stop) = (
state_loc[i : i + 2],
state_loc[j : j + 2],
)
new_message = np.zeros(j_stop - j_start, dtype=dtype)
for d in range(len(new_message)):
new_message[d] = (T_tr[aij, j_start + d, i_start:i_stop] * message).max()
message = new_message
p_obs = message.max()
assert p_obs > 0
message /= p_obs
log2_lik[t] = np.log2(p_obs)
if store_messages:
t_start, t_stop = mess_loc[t : t + 2]
mess_fwd[t_start:t_stop] = message
return log2_lik, mess_fwd
@nb.njit
def rargmax(x):
# return x.argmax() # <- favors clustering towards smaller state numbers
return np.random.choice((x == x.max()).nonzero()[0])
@nb.njit
def backtrace(T, n_clones, x, a, mess_fwd):
"""Compute backward messages."""
state_loc = np.hstack((np.array([0], dtype=n_clones.dtype), n_clones)).cumsum()
mess_loc = np.hstack((np.array([0], dtype=n_clones.dtype), n_clones[x])).cumsum()
code = np.zeros(x.shape[0], dtype=np.int64)
# backward pass
t = x.shape[0] - 1
i = x[t]
t_start, t_stop = mess_loc[t : t + 2]
belief = mess_fwd[t_start:t_stop]
code[t] = rargmax(belief)
for t in range(x.shape[0] - 2, -1, -1):
aij, i, j = (
a[t],
x[t],
x[t + 1],
) # at time t -> t+1 we go from observation i to observation j
(i_start, i_stop), j_start = state_loc[i : i + 2], state_loc[j]
t_start, t_stop = mess_loc[t : t + 2]
belief = (
mess_fwd[t_start:t_stop] * T[aij, i_start:i_stop, j_start + code[t + 1]]
)
code[t] = rargmax(belief)
states = state_loc[x] + code
return states
def backtraceE(T, E, n_clones, x, a, mess_fwd):
"""Compute backward messages."""
assert (n_clones.sum(), len(n_clones)) == E.shape
states = np.zeros(x.shape[0], dtype=np.int64)
# backward pass
t = x.shape[0] - 1
belief = mess_fwd[t]
states[t] = rargmax(belief)
for t in range(x.shape[0] - 2, -1, -1):
aij = a[t] # at time t -> t+1 we go from observation i to observation j
belief = mess_fwd[t] * T[aij, :, states[t + 1]]
states[t] = rargmax(belief)
return states
def forwardE_mp(T_tr, E, Pi, n_clones, x, a, store_messages=False):
"""Log-probability of a sequence, and optionally, messages"""
assert (n_clones.sum(), len(n_clones)) == E.shape
dtype = T_tr.dtype.type
# forward pass
t, log2_lik = 0, np.zeros(len(x), dtype)
j = x[t]
message = Pi * E[:, j]
p_obs = message.max()
assert p_obs > 0
message /= p_obs
log2_lik[0] = np.log2(p_obs)
if store_messages:
mess_fwd = np.empty((len(x), E.shape[0]), dtype=dtype)
mess_fwd[t] = message
for t in range(1, x.shape[0]):
aij, j = (
a[t - 1],
x[t],
) # at time t-1 -> t we go from observation i to observation j
message = (T_tr[aij] * message.reshape(1, -1)).max(1)
message *= E[:, j]
p_obs = message.max()
assert p_obs > 0
message /= p_obs
log2_lik[t] = np.log2(p_obs)
if store_messages:
mess_fwd[t] = message
if store_messages:
return log2_lik, mess_fwd
else:
return log2_lik
def forward_mp_all(T_tr, Pi_x, Pi_a, n_clones, target_state, max_steps):
"""Log-probability of a sequence, and optionally, messages"""
# forward pass
t, log2_lik = 0, []
message = Pi_x
p_obs = message.max()
assert p_obs > 0
message /= p_obs
log2_lik.append(np.log2(p_obs))
mess_fwd = []
mess_fwd.append(message)
T_tr_maxa = (T_tr * Pi_a.reshape(-1, 1, 1)).max(0)
for t in range(1, max_steps):
message = (T_tr_maxa * message.reshape(1, -1)).max(1)
p_obs = message.max()
assert p_obs > 0
message /= p_obs
log2_lik.append(np.log2(p_obs))
mess_fwd.append(message)
if message[target_state] > 0:
break
else:
assert False, "Unable to find a bridging path"
return np.array(log2_lik), np.array(mess_fwd)
def backtrace_all(T, Pi_a, n_clones, mess_fwd, target_state):
"""Compute backward messages."""
states = np.zeros(mess_fwd.shape[0], dtype=np.int64)
actions = np.zeros(mess_fwd.shape[0], dtype=np.int64)
n_states = T.shape[1]
# backward pass
t = mess_fwd.shape[0] - 1
actions[t], states[t] = (
-1,
target_state,
) # last actions is irrelevant, use an invalid value
for t in range(mess_fwd.shape[0] - 2, -1, -1):
belief = (
mess_fwd[t].reshape(1, -1) * T[:, :, states[t + 1]] * Pi_a.reshape(-1, 1)
)
a_s = rargmax(belief.flatten())
actions[t], states[t] = a_s // n_states, a_s % n_states
return actions, states