def cal_metrics(self, output, input): output_id = output.topk(1)[1].squeeze(-1) pack = Pack() loss = calculate_loss(output, input.tgt_for_metric) assert not t.isinf(loss) output_str = [self.vocab.convert_id2str(i) for i in output_id] tgt_str = [self.vocab.convert_id2str(i) for i in input.tgt_for_metric] cer = sum( [calculate_cer(i[0], i[1]) for i in zip(output_str, tgt_str)]) * 100 / len(output_str) pack.add(loss=loss, cer=t.Tensor([cer])) return pack
def cal_metrics(self, output, input): pack = Pack() pred, gold = output.pred, output.gold loss, n_correct = cal_performance(pred, gold) output_id = pred.topk(1)[1].squeeze(-1) assert not t.isinf(loss) output_str = [self.vocab.convert_id2str(i) for i in output_id] tgt_str = [self.vocab.convert_id2str(i) for i in gold] cer = sum( [calculate_cer(i[0], i[1]) for i in zip(output_str, tgt_str)]) * 100 / len(output_str) pack.add(loss=loss, cer=t.Tensor([cer])) #pack.add(loss=loss) return pack
def forward(self, input): wave, wave_len, text_for_input, text_len = input.wave, input.wave_len, input.tgt_for_input, input.tgt_len """ # Args: # padded_input: N x Ti x D # input_lengths: N # padded_targets: N x To # """ encoder_padded_outputs, *_ = self.encoder(wave, wave_len) # pred is score before softmax pred, gold, *_ = self.decoder(text_for_input, encoder_padded_outputs, text_len) pack = Pack() pack.add(pred=pred, gold=gold) return pack
def __call__(self, batch): pack = Pack() wave = [i[0] for i in batch] tgt_for_input = [i[1] for i in batch] tgt_for_metric = [i[2] for i in batch] wave, wave_len = Padder.pad_tri(wave, 0) tgt_for_input, tgt_for_input_len = Padder.pad_two(tgt_for_input, 0) tgt_for_metric, tgt_for_metric_len = Padder.pad_two(tgt_for_metric, 0) pack.add(wave=wave, tgt_for_input=tgt_for_input.long(), wave_len=t.Tensor(wave_len).long(), tgt_len=t.Tensor(tgt_for_input_len).long()) pack.add(tgt_for_metric=tgt_for_metric.long()) if self.use_cuda: pack = pack.cuda() return pack
def cal_metric(self, model_output): metrics = Pack() metrics.add(loss=model_output.wave.sum()) return metrics, None
def forward(self, pack): output = Pack() wave, wave_length = pack.wave, pack.wave_len output.add(wave=wave) return output