Example #1
0
def hmm_mle(training_set, model):
    """
    Calculates the Maximum Likelihood estimation of the transition and emission probabilities for the standard
        multinomial HMM.

    :param training_set: an iterable sequence of sentences, each containing both the words and the PoS tags
            of the sentence.
    :param model: an initial HMM with the pos2i and word2i mappings among other things.
    :return: a mapping of the transition and emission probabilities.
    """
    transition_prob = np.zeros((model.pos_size, model.pos_size))
    emission_prob = np.zeros((model.pos_size, model.words_size))

    for row in training_set:
        pos_tags = row[0]
        words = row[1]

        for i in range(len(pos_tags) - 1):
            transition_prob[model.pos2i[pos_tags[i]],
                            model.pos2i[pos_tags[i + 1]]] += 1
            emission_prob[model.pos2i[pos_tags[i]],
                          model.word2i[words[i]]] += 1
        emission_prob[model.pos2i[pos_tags[-1]], model.word2i[words[-1]]] += 1

    return utils.normalize_prob(transition_prob), utils.normalize_prob(
        emission_prob)
Example #2
0
def e_step(pt_z, pt_t_z, pf_z, pf_f_z, b_t, b_f):
    """
    :param pt_z: probability distribution of latent vectors for high-time-res plca; shape (n_latent,)
    :param pt_t_z: conditional probability distribution of temporal component for high-time-res plca,
                i.e. temporal basis; shape (n_latent, fft_size)
    :param pf_z: probability distribution of latent vectors for high-spec-res plca; shape (n_latent,)
    :param pf_f_z: conditional probability distribution of spectral component for high-spec-res plca,
                i.e. spectral basis; shape (n_latent, fft_size)
    :param b_t: blurring function for temporal basis
    :param b_f: blurring function for spectral basis
    :return: pt_z_tf, pf_z_tf
            where pt_z_tf conditional distribution of latent components for high-time-res plca;
                shape (n_latent, temporal_size, fft_size)
            and   pf_z_tf conditional distribution of latent components for high-spec-res plca
                shape (n_latent, temporal_size, fft_size)
    """

    pt_z_tf = normalize_prob(
        np.expand_dims(pt_z, (1, 2)) * np.expand_dims(pt_t_z, 2) *
        np.expand_dims(b_f(pf_f_z), 1), 0)
    pf_z_tf = normalize_prob(
        np.expand_dims(pf_z, (1, 2)) * np.expand_dims(b_t(pt_t_z), 2) *
        np.expand_dims(pf_f_z, 1), 0)

    return pt_z_tf, pf_z_tf
Example #3
0
def m_step(pt_z_tf, spec_t, pf_z_tf, spec_f):
    """
    :param pt_z_tf: conditional distribution of latent components for high-time-res plca;
                shape (n_latent, temporal_size, fft_size)
    :param spec_t: high-time-res spectrogram; shape (temporal_size, fft_size)
    :param pf_z_tf: conditional distribution of latent components for high-spec-res plca;
                shape (n_latent, temporal_size, fft_size)
    :param spec_f:  high-spec-res spectrogram; shape (temporal_size, fft_size)
    :return: pt_z, pt_f_z, pf_z, pf_f_z
        pt_z: probability distribution of latent vectors for high-time-res plca; shape (n_latent,)
        pt_f_z: conditional probability distribution of spectral component for high-time-res plca,
                    i.e. spectral basis; shape (n_latent, fft_size)
        pt_t_z: conditional probability distribution of temporal component for high-time-res plca,
                    i.e. temporal basis; shape (n_latent, fft_size)
        pf_z: probability distribution of latent vectors for high-spec-res plca; shape (n_latent,)
    """
    weighted_density_t = np.expand_dims(spec_t, 0) * pt_z_tf
    weighted_density_f = np.expand_dims(spec_f, 0) * pf_z_tf

    pt_z = normalize_prob(np.sum(weighted_density_t, (1, 2)))
    pt_t_z = normalize_prob(np.sum(weighted_density_t, 2), 1)

    pf_z = normalize_prob(np.sum(weighted_density_f, (1, 2)))
    pf_f_z = normalize_prob(np.sum(weighted_density_f, 1), 1)

    return pt_z, pt_t_z, pf_z, pf_f_z
Example #4
0
def build_subword_prob(
    subword_counter,
    normalize_prob=normalize_prob,
    min_prob=None,
    take_root=False,
):
    subword_prob = normalize_prob(subword_counter)
    subword_prob = subword_prob_post_process(
        subword_prob,
        min_prob=min_prob,
        take_root=take_root,
    )
    return Counter(subword_prob)
Example #5
0
def calc_subword_weights(
    w,
    *,
    subword_vocab,
    get_subword_prob=None,
    weight_threshold=None,
):
    subword_weights = {}
    if get_subword_prob:
        p_prefix = calc_prefix_prob(w, get_subword_prob)
        p_suffix = calc_prefix_prob(w, get_subword_prob, backward=True)
        for j in range(1, len(w) + 1):
            for i in range(j):
                sub = w[i:j]
                if sub in subword_vocab:
                    p_sub = get_subword_prob(sub) * p_prefix[i] * p_suffix[j]
                    subword_weights.setdefault(sub, 0)
                    subword_weights[sub] += p_sub
        subword_weights = normalize_prob(subword_weights)
        if weight_threshold:
            subword_weights = {
                k: v
                for k, v in subword_weights.items() if v > weight_threshold
            }
    else:
        for j in range(1, len(w) + 1):
            for i in range(j):
                sub = w[i:j]
                if sub in subword_vocab:
                    subword_weights.setdefault(sub, 0)
                    subword_weights[sub] += 1
        subword_weights = normalize_prob(subword_weights)

    if len(subword_weights) == 0:
        logger.warning(f"no qualified subwords for '{w}'")
        return {}

    return subword_weights
Example #6
0
def init_params(n_latent,
                temporal_size,
                fft_size,
                spec_f=None,
                show_progress=False):
    """
    Initializes probability functions for pcla
    if 'VF' is not None will intitialize the spectral weights using VF

    :returns pt_t_z, pt_z, pf_f_z, pf_z
            pt_t_z is temporal basis for high-temporal-res; shape (n_latent, temporal_size)
            pt_z is latent distribution for high-temporal-res; shape (n_latent)
            pf_t_z is temporal basis for high-spectral-res; shape (n_latent, temporal_size)
            pf_z is latent distribution for high-spectral-res; shape (n_latent)
    """
    if show_progress:
        print("\rInitializing parameters", end='')

    pt_z = np.random.random(n_latent) + 1
    pt_z = pt_z / np.sum(pt_z)
    pt_t_z = np.random.random((n_latent, temporal_size)) + 1
    pt_t_z = normalize_prob(pt_t_z, ax=0)

    pf_z = np.random.random(n_latent) + 1
    pf_z = pf_z / np.sum(pf_z)
    if spec_f is not None:
        n = spec_f.shape[0]
        pf_f_z = spec_f[random_indices(n, n_latent)] + epsilon
    else:
        pf_f_z = np.random.random((n_latent, fft_size)) + 1
    pf_f_z = normalize_prob(pf_f_z, ax=0)
    np.random.random((n_latent, fft_size)) + 1

    if show_progress:
        print("\rDone initializing parameters")

    return pt_z, pt_t_z, pf_z, pf_f_z
Example #7
0
def test_blur_functions_spectrograms():
    spec_t, spec_f = gen_spectrograms_for_testing()
    spec_t = normalize_prob(spec_t, 0)
    spec_f = normalize_prob(spec_f, 1)

    b_t, b_f = make_blur_functions(TEST_WIN_T_SIZE,
                                   TEST_WIN_F_SIZE,
                                   TEST_FFT_SIZE,
                                   TEST_STRIDE,
                                   win_type='hann')

    # show blur filters
    plt.subplot(2, 1, 1)
    plt.plot(b_t.blur_filter.squeeze())
    plt.title('filter-t')
    plt.subplot(2, 1, 2)
    plt.plot(b_f.blur_filter.squeeze())
    plt.title('filter-f')
    plt.show()

    # Show blurred images
    blurred_t = b_t(spec_t)
    blurred_f = b_f(spec_f)
    plt.subplot(2, 2, 1)
    plt.imshow(spec_t[:250])
    plt.title('original t')
    plt.subplot(2, 2, 2)
    plt.imshow(spec_f[:250])
    plt.title('original norm_f')
    plt.subplot(2, 2, 3)
    plt.imshow(blurred_t[:250])
    plt.title('blur-time')
    plt.subplot(2, 2, 4)
    plt.imshow(blurred_f[:250])
    plt.title('blur-spec')
    plt.show()
Example #8
0
def baseline_mle(training_set, model):
    """
    Calculates the Maximum Likelihood estimation of the multinomial and emission probabilities for the baseline model.

    :param training_set: an iterable sequence of sentences, each containing both the words and the PoS tags of the
            sentence.
    :param model: an initial baseline model with the pos2i and word2i mappings among other things.
    :return: a mapping of the multinomial and emission probabilities.
    """
    multinomial_probs = np.zeros(model.pos_size)
    emission_prob = np.zeros((model.pos_size, model.words_size))

    for row in training_set:
        pos_tags = row[0]
        words = row[1]

        for i in range(len(pos_tags)):
            emission_prob[model.pos2i[pos_tags[i]],
                          model.word2i[words[i]]] += 1
            multinomial_probs[model.pos2i[pos_tags[i]]] += 1

    return multinomial_probs / np.sum(multinomial_probs), utils.normalize_prob(
        emission_prob)
Example #9
0
 def __call__(self, spec):
     blurred = convolve2d(spec, self.blur_filter, mode='same')
     if self.norm_axis is not None:
         return normalize_prob(blurred, ax=self.norm_axis)
     else:
         return blurred