コード例 #1
0
def convert_to_dense(feat_matrix: LT) -> DenseFeatureMatrix:
    names = feat_matrix.names
    bs = feat_matrix.size('batch')
    ml = feat_matrix.size('length')
    fm = _g2f[feat_matrix.rename(None)].refine_names(*names)
    dfms = dict()
    for cat in Category:
        e = get_enum_by_cat(cat)
        dfm_idx = fm[..., cat.value]
        dfm = get_zeros(bs, ml, len(e), cpu=True)
        dfm = dfm.scatter(2, dfm_idx.rename(None).unsqueeze(dim=-1), 1.0)
        dfms[cat] = dfm.refine_names('batch', 'length', f'{cat.name}_feat')
    if has_gpus():
        dfms = {k: v.cuda() for k, v in dfms.items()}
    return dfms
コード例 #2
0
    def finish_search(self, lengths: LT):
        last_beam_id = get_zeros(lengths.size('batch'),
                                 g.beam_size).long().rename('batch', 'beam')
        start_beam_id = get_named_range(g.beam_size,
                                        'beam').align_as(last_beam_id)
        samples = list()
        for i, (hyp, beam_id) in enumerate(
                zip(reversed(self.hyps), reversed(self.beam_ids))):
            step = len(self.beam_ids) - i
            start_backtrack = (step == lengths).align_as(beam_id)
            # new_last_beam_id = beam_id.gather('beam', last_beam_id)
            this_beam_id = torch.where(start_backtrack, start_beam_id,
                                       last_beam_id)
            samples.append(hyp.gather('beam', this_beam_id))
            last_beam_id = beam_id.gather('beam', this_beam_id)
        self.samples = torch.stack(samples[::-1], new_name='length')

        hyp_log_probs = torch.stack(self.hyp_log_probs, new_name='length')
        self.sample_log_probs = hyp_log_probs.gather(
            'length', lengths.align_as(hyp_log_probs)).squeeze('length')