def predict(f_enc, f_dec, samples, batches, mat, max_len, header):
    '''
    Sample words and compute the prediction error
    '''
    preds = []
    errs = []
    progress = ProgressBar(numpy.sum([len(batch) for batch in batches]), 20,
                           header)
    for batch in batches:
        x, mask_x, y, mask_y = load_batch(samples, batch, mat)
        [prev_h] = f_enc(x, mask_x)

        n_steps = mask_x.sum(0)
        n_samples = x.shape[1]
        sents = numpy.zeros((n_samples, max_len), 'int32')
        # First step - No embedded word is fed into the decoder
        sents[:, 0], prev_h = f_dec(numpy.asarray([-1] * n_samples, 'int32'),
                                    prev_h)
        n_ends = n_steps - (sents[:, 0] == 0)

        for i in range(1, max_len - 1):
            prev_words = sents[:, i - 1]
            if not n_ends.any():
                break

            next_words, prev_h = f_dec(prev_words, prev_h)
            sents[:, i] = next_words * (n_ends > 0)
            n_ends -= (next_words == 0) * (n_ends > 0)

        for i in range(n_samples):
            idx = 0
            while idx < max_len and n_steps[i] > 0:
                if sents[i, idx] == 0:
                    n_steps[i] -= 1
                idx += 1
            preds.append(sents[i, :idx].tolist())

        y = numpy.concatenate(
            [y, numpy.zeros((max_len - len(y), n_samples), 'int32')]).T
        mask_y = numpy.concatenate(
            [mask_y,
             numpy.zeros((max_len - len(mask_y), n_samples), 'int32')]).T
        errs.extend(((sents != y) * mask_y * 1.).sum(1) / mask_y.sum(1))
        progress.disp(errs, ' ERR')

    return preds, numpy.mean(errs)
def predict(f_enc, f_dec, samples, batches, mat, max_len, header):
    '''
    Sample words and compute the prediction error
    '''
    preds = []
    errs = []
    progress = ProgressBar(numpy.sum([len(batch) for batch in batches]), 20, header)
    for batch in batches:
        x, mask_x, y, mask_y = load_batch(samples, batch, mat)
        [prev_h] = f_enc(x, mask_x)

        n_steps = mask_x.sum(0)
        n_samples = x.shape[1]
        sents = numpy.zeros((n_samples, max_len), 'int32')
        # First step - No embedded word is fed into the decoder
        sents[:, 0], prev_h = f_dec(numpy.asarray([-1] * n_samples, 'int32'), prev_h)
        n_ends = n_steps - (sents[:, 0] == 0)

        for i in range(1, max_len - 1):
            prev_words = sents[:, i - 1]
            if not n_ends.any():
                break

            next_words, prev_h = f_dec(prev_words, prev_h)
            sents[:, i] = next_words * (n_ends > 0)
            n_ends -= (next_words == 0) * (n_ends > 0)

        for i in range(n_samples):
            idx = 0
            while idx < max_len and n_steps[i] > 0:
                if sents[i, idx] == 0:
                    n_steps[i] -= 1
                idx += 1
            preds.append(sents[i, : idx].tolist())

        y = numpy.concatenate([y, numpy.zeros((max_len - len(y), n_samples), 'int32')]).T
        mask_y = numpy.concatenate([mask_y, numpy.zeros((max_len - len(mask_y), n_samples), 'int32')]).T
        errs.extend(((sents != y) * mask_y * 1.).sum(1) / mask_y.sum(1))
        progress.disp(errs, ' ERR')

    return preds, numpy.mean(errs)
Exemple #3
0
def predict(f_enc, f_dec, samples, batches, mat, beam_size, max_len, header):
    '''
    Sample words and compute the prediction error
    '''
    preds = []
    errs = []
    progress = ProgressBar(numpy.sum([len(batch) for batch in batches]), 20,
                           header)
    for batch in batches:
        x, mask_x, y, mask_y = load_batch(samples, batch, mat)
        [init_h] = f_enc(x, mask_x)

        n_steps = mask_x.sum(0)
        n_samples = x.shape[1]
        prev_sents = numpy.zeros((beam_size, n_samples, max_len), 'int32')
        # First step - No embedded word is fed into the decoder
        prev_words = numpy.asarray([-1] * n_samples, 'int32')
        prev_sents[:, :, 0], prev_log_prob, prev_h = f_dec(prev_words, init_h)
        prev_h = numpy.tile(prev_h, (beam_size, 1, 1))
        prev_n_ends = n_steps - (prev_sents[:, :, 0] == 0)

        for i in range(1, max_len - 1):
            hypo_sents = [[]] * n_samples
            hypo_log_prob = [[]] * n_samples
            hypo_h = [[]] * n_samples
            hypo_n_ends = [[]] * n_samples
            has_hypos = numpy.asarray([False] * n_samples)
            for j in range(beam_size):
                if not prev_n_ends[j].any():
                    continue

                next_words, next_log_prob, next_h = f_dec(
                    prev_sents[j, :, i - 1], prev_h[j])
                for k in range(n_samples):
                    if prev_n_ends[j, k] > 0:
                        has_hypos[k] = True
                        next_sents = numpy.tile(prev_sents[j, k],
                                                (beam_size, 1))
                        next_sents[:, i] = next_words[:, k]
                        hypo_sents[k].extend(next_sents)
                        hypo_log_prob[k].extend(next_log_prob[:, k] +
                                                prev_log_prob[j, k])
                        hypo_h[k].extend([next_h[k]] * beam_size)
                        hypo_n_ends[k].extend(prev_n_ends[j, k] -
                                              (next_words[:, k] == 0))
                    else:
                        hypo_sents[k].append(prev_sents[j, k].copy())
                        hypo_log_prob[k].append(prev_log_prob[j, k])
                        hypo_h[k].append(prev_h[j, k].copy())
                        hypo_n_ends[k].append(0)

            if not has_hypos.any():
                break

            for j in range(n_samples):
                if not has_hypos[j]:
                    continue

                indices = numpy.argsort(hypo_log_prob[j])[:-beam_size - 1:-1]
                for k in range(beam_size):
                    prev_sents[k, j] = hypo_sents[j][indices[k]]
                    prev_log_prob[k, j] = hypo_log_prob[j][indices[k]]
                    prev_h[k, j] = hypo_h[j][indices[k]]
                    prev_n_ends[k, j] = hypo_n_ends[j][indices[k]]

        sents = prev_sents[prev_log_prob.argmax(0), numpy.arange(n_samples)]
        for i in range(n_samples):
            idx = 0
            while idx < max_len and n_steps[i] > 0:
                if sents[i, idx] == 0:
                    n_steps[i] -= 1
                idx += 1
            preds.append(sents[i, :idx].tolist())

        y = numpy.concatenate(
            [y, numpy.zeros((max_len - len(y), n_samples), 'int32')]).T
        mask_y = numpy.concatenate(
            [mask_y,
             numpy.zeros((max_len - len(mask_y), n_samples), 'int32')]).T
        errs.extend(((sents != y) * mask_y * 1.).sum(1) / mask_y.sum(1))
        progress.disp(errs, ' ERR')

    return preds, numpy.mean(errs)
def predict(f_enc, f_dec, samples, batches, mat, beam_size, max_len, header):
    '''
    Sample words and compute the prediction error
    '''
    preds = []
    errs = []
    progress = ProgressBar(numpy.sum([len(batch) for batch in batches]), 20, header)
    for batch in batches:
        x, mask_x, y, mask_y = load_batch(samples, batch, mat)
        [init_h] = f_enc(x, mask_x)

        n_steps = mask_x.sum(0)
        n_samples = x.shape[1]
        prev_sents = numpy.zeros((beam_size, n_samples, max_len), 'int32')
        # First step - No embedded word is fed into the decoder
        prev_words = numpy.asarray([-1] * n_samples, 'int32')
        prev_sents[:, :, 0], prev_log_prob, prev_h = f_dec(prev_words, init_h)
        prev_h = numpy.tile(prev_h, (beam_size, 1, 1))
        prev_n_ends = n_steps - (prev_sents[:, :, 0] == 0)

        for i in range(1, max_len - 1):
            hypo_sents = [[]] * n_samples
            hypo_log_prob = [[]] * n_samples
            hypo_h = [[]] * n_samples
            hypo_n_ends = [[]] * n_samples
            has_hypos = numpy.asarray([False] * n_samples)
            for j in range(beam_size):
                if not prev_n_ends[j].any():
                    continue

                next_words, next_log_prob, next_h = f_dec(prev_sents[j, :, i - 1], prev_h[j])
                for k in range(n_samples):
                    if prev_n_ends[j, k] > 0:
                        has_hypos[k] = True
                        next_sents = numpy.tile(prev_sents[j, k], (beam_size, 1))
                        next_sents[:, i] = next_words[:, k]
                        hypo_sents[k].extend(next_sents)
                        hypo_log_prob[k].extend(next_log_prob[:, k] + prev_log_prob[j, k])
                        hypo_h[k].extend([next_h[k]] * beam_size)
                        hypo_n_ends[k].extend(prev_n_ends[j, k] - (next_words[:, k] == 0))
                    else:
                        hypo_sents[k].append(prev_sents[j, k].copy())
                        hypo_log_prob[k].append(prev_log_prob[j, k])
                        hypo_h[k].append(prev_h[j, k].copy())
                        hypo_n_ends[k].append(0)

            if not has_hypos.any():
                break

            for j in range(n_samples):
                if not has_hypos[j]:
                    continue

                indices = numpy.argsort(hypo_log_prob[j])[: -beam_size - 1: -1]
                for k in range(beam_size):
                    prev_sents[k, j] = hypo_sents[j][indices[k]]
                    prev_log_prob[k, j] = hypo_log_prob[j][indices[k]]
                    prev_h[k, j] = hypo_h[j][indices[k]]
                    prev_n_ends[k, j] = hypo_n_ends[j][indices[k]]

        sents = prev_sents[prev_log_prob.argmax(0), numpy.arange(n_samples)]
        for i in range(n_samples):
            idx = 0
            while idx < max_len and n_steps[i] > 0:
                if sents[i, idx] == 0:
                    n_steps[i] -= 1
                idx += 1
            preds.append(sents[i, : idx].tolist())

        y = numpy.concatenate([y, numpy.zeros((max_len - len(y), n_samples), 'int32')]).T
        mask_y = numpy.concatenate([mask_y, numpy.zeros((max_len - len(mask_y), n_samples), 'int32')]).T
        errs.extend(((sents != y) * mask_y * 1.).sum(1) / mask_y.sum(1))
        progress.disp(errs, ' ERR')

    return preds, numpy.mean(errs)