Ejemplo n.º 1
0
int32
subvq_mgau_eval(mgau_model_t * g, subvq_t * vq, int32 m, int32 n,
                int32 * active)
{
    mgau_t *mgau;
    int32 *map;
    int32 i, v, sv_id;
    int32 c;
    int32 *vqdist;
    int32 score;
    int32 last_active;

    float64 f;
    f = 1.0 / log(logmath_get_base(g->logmath));

    vqdist = vq->vqdist[0];
    score = S3_LOGPROB_ZERO;
    mgau = &(g->mgau[m]);
    map = vq->map[m][0];

    if (!active) {
        for (i = 0; i < n; i++) {
            v = 0;
            for (sv_id = 0; sv_id < vq->n_sv; sv_id++) {
                v += vqdist[*(map++)];
            }
            score = logmath_add(g->logmath, score, v + mgau->mixw[i]);
        }
    }
    else {
        last_active = 0;
        for (i = 0; active[i] >= 0; i++) {
            c = active[i];
        }
        for (i = 0; active[i] >= 0; i++) {
            c = active[i];
            map += (c - last_active) * vq->n_sv;
            v = 0;
            for (sv_id = 0; sv_id < vq->n_sv; sv_id++) {
                v += vqdist[*(map++)];
            }

            last_active = c + 1;
            score = logmath_add(g->logmath, score, v + mgau->mixw[i]);
        }
    }

    if (score == S3_LOGPROB_ZERO) {
        E_INFO("Warning!! Score is S3_LOGPROB_ZERO\n");
    }

    return score;

}
Ejemplo n.º 2
0
/*
 * Normalize density values.
 */
int32
gauden_dist_norm(gauden_t * g, int32 n_top, gauden_dist_t *** dist,
                 uint8 * active)
{
    int32 gid, f, t;
    int32 sum, scale;

    if (g->n_mgau > 1) {
        /* Normalize by subtracting max(density values) from each density */
        return (gauden_dist_norm_global(g, n_top, dist, active));
    }

    /* Normalize by subtracting log(sum of density values) from each density */
    gid = 0;
    scale = 0;
    for (f = 0; f < g->n_feat; f++) {
        sum = dist[gid][f][0].dist;
        for (t = 1; t < n_top; t++)
            sum = logmath_add(g->logmath, sum, dist[gid][f][t].dist);

        for (t = 0; t < n_top; t++)
            dist[gid][f][t].dist -= sum;

        scale += sum;
    }

    return scale;               /* Scale factor applied to EVERY senone score */
}
static int32
ngram_model_set_add_ug(ngram_model_t * base, int32 wid, int32 lweight)
{
    ngram_model_set_t *set = (ngram_model_set_t *) base;
    int32 *newwid;
    int32 i, prob;

    /* At this point the word has already been added to the master
       model and we have a new word ID for it.  Add it to active
       submodels and track the word IDs. */
    newwid = ckd_calloc(set->n_models, sizeof(*newwid));
    prob = base->log_zero;
    for (i = 0; i < set->n_models; ++i) {
        int32 wprob, n_hist;

        /* Only add to active models. */
        if (set->cur == -1 || set->cur == i) {
            /* Did this word already exist? */
            newwid[i] = ngram_wid(set->lms[i], base->word_str[wid]);
            if (newwid[i] == NGRAM_INVALID_WID) {
                /* Add it to the submodel. */
                newwid[i] =
                    ngram_model_add_word(set->lms[i], base->word_str[wid],
                                         (float32) logmath_exp(base->lmath,
                                                               lweight));
                if (newwid[i] == NGRAM_INVALID_WID) {
                    ckd_free(newwid);
                    return base->log_zero;
                }
            }
            /* Now get the unigram probability for the new word and either
             * interpolate it or use it (if this is the current model). */
            wprob =
                ngram_ng_prob(set->lms[i], newwid[i], NULL, 0, &n_hist);
            if (set->cur == i)
                prob = wprob;
            else if (set->cur == -1)
                prob =
                    logmath_add(base->lmath, prob,
                                set->lweights[i] + wprob);
        }
        else {
            newwid[i] = NGRAM_INVALID_WID;
        }
    }
    /* Okay we have the word IDs for this in all the submodels.  Now
       do some complicated memory mangling to add this to the
       widmap. */
    set->widmap =
        ckd_realloc(set->widmap, base->n_words * sizeof(*set->widmap));
    set->widmap[0] =
        ckd_realloc(set->widmap[0],
                    base->n_words * set->n_models * sizeof(**set->widmap));
    for (i = 0; i < base->n_words; ++i)
        set->widmap[i] = set->widmap[0] + i * set->n_models;
    memcpy(set->widmap[wid], newwid, set->n_models * sizeof(*newwid));
    ckd_free(newwid);
    return prob;
}
Ejemplo n.º 4
0
int32
interp_cd_ci(interp_t * ip, int32 * senscr, int32 cd, int32 ci)
{
    assert((ci >= 0) && (ci < ip->n_sen));
    assert((cd >= 0) && (cd < ip->n_sen));

    senscr[cd] = logmath_add(ip->logmath,
                             senscr[cd] + ip->wt[cd].cd,
                             senscr[ci] + ip->wt[cd].ci);

    return 0;
}
static int32
ngram_model_set_raw_score(ngram_model_t * base, int32 wid,
                          int32 * history, int32 n_hist, int32 * n_used)
{
    ngram_model_set_t *set = (ngram_model_set_t *) base;
    int32 mapwid;
    int32 score;
    int32 i;

    /* Truncate the history. */
    if (n_hist > base->n - 1)
        n_hist = base->n - 1;

    /* Interpolate if there is no current. */
    if (set->cur == -1) {
        score = base->log_zero;
        for (i = 0; i < set->n_models; ++i) {
            int32 j;
            /* Map word and history IDs for each model. */
            mapwid = set->widmap[wid][i];
            for (j = 0; j < n_hist; ++j) {
                if (history[j] == NGRAM_INVALID_WID)
                    set->maphist[j] = NGRAM_INVALID_WID;
                else
                    set->maphist[j] = set->widmap[history[j]][i];
            }
            score = logmath_add(base->lmath, score,
                                set->lweights[i] +
                                ngram_ng_prob(set->lms[i],
                                              mapwid, set->maphist, n_hist,
                                              n_used));
        }
    }
    else {
        int32 j;
        /* Map word and history IDs (FIXME: do this in a function?) */
        mapwid = set->widmap[wid][set->cur];
        for (j = 0; j < n_hist; ++j) {
            if (history[j] == NGRAM_INVALID_WID)
                set->maphist[j] = NGRAM_INVALID_WID;
            else
                set->maphist[j] = set->widmap[history[j]][set->cur];
        }
        score = ngram_ng_prob(set->lms[set->cur],
                              mapwid, set->maphist, n_hist, n_used);
    }

    return score;
}
Ejemplo n.º 6
0
int32
interp_all(interp_t * ip, int32 * senscr, s3senid_t * cimap,
           int32 n_ci_sen)
{
    int32 ci, cd;

    assert(n_ci_sen <= ip->n_sen);

    for (cd = n_ci_sen; cd < ip->n_sen; cd++) {
        ci = cimap[cd];
        senscr[cd] = logmath_add(ip->logmath,
                                 senscr[cd] + ip->wt[cd].cd,
                                 senscr[ci] + ip->wt[cd].ci);
    }

    return 0;
}
Ejemplo n.º 7
0
int
test_decode(ps_decoder_t *ps)
{
	FILE *rawfh;
	int16 buf[2048];
	size_t nread;
	int16 const *bptr;
	int nfr;
	ps_lattice_t *dag;
	acmod_t *acmod;
	ngram_search_t *ngs;
	int i, j;
	ps_latlink_t *link;
	ps_latnode_t *node;
	latlink_list_t *x;
	int32 norm, post;

	ngs = (ngram_search_t *)ps->search;
	acmod = ps->acmod;

	/* Decode stuff and build a DAG. */
	TEST_ASSERT(rawfh = fopen(DATADIR "/goforward.raw", "rb"));
	TEST_EQUAL(0, acmod_start_utt(acmod));
	ngram_fwdtree_start(ngs);
	while (!feof(rawfh)) {
		nread = fread(buf, sizeof(*buf), 2048, rawfh);
		bptr = buf;
		while ((nfr = acmod_process_raw(acmod, &bptr, &nread, FALSE)) > 0) {
			while (acmod->n_feat_frame > 0) {
				ngram_fwdtree_search(ngs, acmod->output_frame);
				acmod_advance(acmod);
			}
		}
	}
	ngram_fwdtree_finish(ngs);
	printf("FWDTREE: %s\n",
	       ngram_search_bp_hyp(ngs, ngram_search_find_exit(ngs, -1, NULL, NULL)));

	TEST_ASSERT(acmod_end_utt(acmod) >= 0);
	fclose(rawfh);

	dag = ngram_search_lattice(ps->search);
	if (dag == NULL) {
		E_ERROR("Failed to build DAG!\n");
		return -1;
	}

	/* Write lattice to disk. */
	TEST_EQUAL(0, ps_lattice_write(dag, "test_posterior.lat"));

	/* Do a bunch of checks on the DAG generation and traversal code: */
	/* Verify that forward and backward iteration give the same number of edges. */
	i = j = 0;
	for (link = ps_lattice_traverse_edges(dag, NULL, NULL);
	     link; link = ps_lattice_traverse_next(dag, NULL)) {
		++i;
	}
	for (link = ps_lattice_reverse_edges(dag, NULL, NULL);
	     link; link = ps_lattice_reverse_next(dag, NULL)) {
		++j;
	}
	printf("%d forward edges, %d reverse edges\n", i, j);
	TEST_EQUAL(i,j);
	/* Verify that the same links are reachable via entries and exits. */
	for (node = dag->nodes; node; node = node->next) {
		for (x = node->exits; x; x = x->next)
			x->link->alpha = -42;
	}
	for (node = dag->nodes; node; node = node->next) {
		for (x = node->entries; x; x = x->next)
			TEST_EQUAL(x->link->alpha, -42);
	}
	/* Verify that forward iteration is properly ordered. */
	for (link = ps_lattice_traverse_edges(dag, NULL, NULL);
	     link; link = ps_lattice_traverse_next(dag, NULL)) {
		link->alpha = 0;
		for (x = link->from->entries; x; x = x->next) {
			TEST_EQUAL(x->link->alpha, 0);
		}
	}
	/* Verify that backward iteration is properly ordered. */
	for (node = dag->nodes; node; node = node->next) {
		for (x = node->exits; x; x = x->next)
			x->link->alpha = -42;
	}
	for (link = ps_lattice_reverse_edges(dag, NULL, NULL);
	     link; link = ps_lattice_reverse_next(dag, NULL)) {
		link->alpha = 0;
		for (x = link->to->exits; x; x = x->next) {
			TEST_EQUAL(x->link->alpha, 0);
		}
	}
	
	/* Find and print best path. */
	link = ps_lattice_bestpath(dag, ngs->lmset, 1.0, 1.0/20.0);
	printf("BESTPATH: %s\n", ps_lattice_hyp(dag, link));

	/* Calculate betas. */
	post = ps_lattice_posterior(dag, ngs->lmset, 1.0/20.0);
	printf("Best path score: %d\n",
	       link->path_scr + dag->final_node_ascr);
	printf("P(S|O) = %d\n", post);

	/* Verify that sum of final alphas and initial alphas+betas is
	 * sufficiently similar. */
	norm = logmath_get_zero(acmod->lmath);
	for (x = dag->start->exits; x; x = x->next)
		norm = logmath_add(acmod->lmath, norm, x->link->beta + x->link->alpha);
	E_INFO("Sum of final alphas+betas = %d\n", dag->norm);
	E_INFO("Sum of initial alphas+betas = %d\n", norm);
	TEST_EQUAL_LOG(dag->norm, norm);

	/* Print posterior probabilities for each link in best path. */
	while (link) {
		printf("P(%s,%d) = %d = %f\n",
		       dict_wordstr(ps->search->dict, link->from->wid),
		       link->ef,
		       link->alpha + link->beta - dag->norm,
		       logmath_exp(acmod->lmath, link->alpha + link->beta - dag->norm));
		link = link->best_prev;
	}

	return 0;
}
Ejemplo n.º 8
0
int
main(int argc, char *argv[])
{
	logmath_t *lmath;
	int32 rv;

	lmath = logmath_init(1.0001, 0, 1);
	TEST_ASSERT(lmath);
	printf("log(1e-150) = %d\n", logmath_log(lmath, 1e-150));
	TEST_EQUAL_LOG(logmath_log(lmath, 1e-150), -3454050);
	printf("exp(log(1e-150)) = %e\n",logmath_exp(lmath, logmath_log(lmath, 1e-150)));
	TEST_EQUAL_FLOAT(logmath_exp(lmath, logmath_log(lmath, 1e-150)), 1e-150);
	printf("log(1e-48) = %d\n", logmath_log(lmath, 1e-48));
	printf("exp(log(1e-48)) = %e\n",logmath_exp(lmath, logmath_log(lmath, 1e-48)));
	TEST_EQUAL_FLOAT(logmath_exp(lmath, logmath_log(lmath, 1e-48)), 1e-48);
	printf("log(42) = %d\n", logmath_log(lmath, 42));
	TEST_EQUAL_LOG(logmath_log(lmath, 42), 37378);
	printf("exp(log(42)) = %f\n",logmath_exp(lmath, logmath_log(lmath, 42)));
	TEST_EQUAL_FLOAT(logmath_exp(lmath, logmath_log(lmath, 42)), 42);
	printf("log(1e-3 + 5e-3) = %d l+ %d = %d\n",
	       logmath_log(lmath, 1e-3),
	       logmath_log(lmath, 5e-3),
	       logmath_add(lmath, logmath_log(lmath, 1e-3),
			   logmath_log(lmath, 5e-3)));
	printf("log(1e-3 + 5e-3) = %e + %e = %e\n",
	       logmath_exp(lmath, logmath_log(lmath, 1e-3)),
	       logmath_exp(lmath, logmath_log(lmath, 5e-3)),
	       logmath_exp(lmath, logmath_add(lmath, logmath_log(lmath, 1e-3),
					      logmath_log(lmath, 5e-3))));
	TEST_EQUAL_LOG(logmath_add(lmath, logmath_log(lmath, 1e-48),
				   logmath_log(lmath, 5e-48)),
		       logmath_log(lmath, 6e-48));
	TEST_EQUAL_LOG(logmath_add(lmath, logmath_log(lmath, 1e-48),
				   logmath_log(lmath, 42)),
		       logmath_log(lmath, 42));

	rv = logmath_write(lmath, "tmp.logadd");
	TEST_EQUAL(rv, 0);
	logmath_free(lmath);
	lmath = logmath_read("tmp.logadd");
	TEST_ASSERT(lmath);
	printf("log(1e-150) = %d\n", logmath_log(lmath, 1e-150));
	TEST_EQUAL_LOG(logmath_log(lmath, 1e-150), -3454050);
	printf("exp(log(1e-150)) = %e\n",logmath_exp(lmath, logmath_log(lmath, 1e-150)));
	TEST_EQUAL_FLOAT(logmath_exp(lmath, logmath_log(lmath, 1e-150)), 1e-150);
	printf("log(1e-48) = %d\n", logmath_log(lmath, 1e-48));
	printf("exp(log(1e-48)) = %e\n",logmath_exp(lmath, logmath_log(lmath, 1e-48)));
	TEST_EQUAL_FLOAT(logmath_exp(lmath, logmath_log(lmath, 1e-48)), 1e-48);
	printf("log(42) = %d\n", logmath_log(lmath, 42));
	TEST_EQUAL_LOG(logmath_log(lmath, 42), 37378);
	printf("exp(log(42)) = %f\n",logmath_exp(lmath, logmath_log(lmath, 42)));
	TEST_EQUAL_FLOAT(logmath_exp(lmath, logmath_log(lmath, 42)), 41.99);
	printf("log(1e-3 + 5e-3) = %d l+ %d = %d\n",
	       logmath_log(lmath, 1e-3),
	       logmath_log(lmath, 5e-3),
	       logmath_add(lmath, logmath_log(lmath, 1e-3),
			   logmath_log(lmath, 5e-3)));
	printf("log(1e-3 + 5e-3) = %e + %e = %e\n",
	       logmath_exp(lmath, logmath_log(lmath, 1e-3)),
	       logmath_exp(lmath, logmath_log(lmath, 5e-3)),
	       logmath_exp(lmath, logmath_add(lmath, logmath_log(lmath, 1e-3),
					      logmath_log(lmath, 5e-3))));
	TEST_EQUAL_LOG(logmath_add(lmath, logmath_log(lmath, 1e-48),
				   logmath_log(lmath, 5e-48)),
		       logmath_log(lmath, 6e-48));
	TEST_EQUAL_LOG(logmath_add(lmath, logmath_log(lmath, 1e-48),
				   logmath_log(lmath, 42)),
		       logmath_log(lmath, 42));

	return 0;
}