def test_beta(): P = np.array([[0.7, 0.3], [0.5, 0.5], [0.4, 0.6], [0.1, 0.9], [0.7, 0.3], [0.6, 0.4], [0.6, 0.4], [0.5, 0.5], [0.4, 0.6], [0.4, 0.6]]) A = np.array([[0.35, 0.65], [0.45, 0.55]]) D = np.array([[0.3, 0.4, 0.3], [0.2, 0.5, 0.3]]) num_obs, _ = P.shape num_states, _ = A.shape beta = vdhmms.beta(A, P, D) beta_true = np.array([[0.1538, 0.1422], [0.12, 0.12], [1, 1]]) np.testing.assert_almost_equal(beta[-3:], beta_true, decimal=3)
patch = imresize(patch, (h, w)) patch = normalise(patch) descs[i] = patch.flatten() # OK, these are the labels of our observation ! labels = euclidean_distances(descs, voc).argmin(axis=1) # Now, we can compute the emission probability matrices emission_obs = emission[:, labels].T alphas = vdhmms.alpha(transition, emission_obs, occurances, p_init=first_letter.reshape((len(first_letter), ))) betas = vdhmms.beta(transition, emission_obs, occurances, p_init=last_letter.reshape((len(first_letter), ))) show_segments(im, segments) g = alphas * betas chain = g.argmax(axis=1) prob = g.max(axis=1) # careful ! Some labels of the chain "just" correspond to null probabilities letters = 'abcdefghijklmnopqrstuvwxyz' labels = [] for p, l in zip(prob, chain): if p == 0: labels.append('_') else: labels.append(letters[l])
descs = np.zeros((len(patches), voc.shape[1])) for i, patch in enumerate(patches): patch = imresize(patch, (h, w)) patch = normalise(patch) descs[i] = patch.flatten() # OK, these are the labels of our observation ! labels = euclidean_distances(descs, voc).argmin(axis=1) # Now, we can compute the emission probability matrices emission_obs = emission[:, labels].T alphas = vdhmms.alpha(transition, emission_obs, occurances, p_init=first_letter.reshape((len(first_letter), ))) betas = vdhmms.beta(transition, emission_obs, occurances, p_init=last_letter.reshape((len(first_letter), ))) show_segments(im, segments) g = alphas * betas chain = g.argmax(axis=1) prob = g.max(axis=1) # careful ! Some labels of the chain "just" correspond to null probabilities letters = 'abcdefghijklmnopqrstuvwxyz' labels = [] for p, l in zip(prob, chain): if p == 0: labels.append('_') else: labels.append(letters[l])