Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Multi round nbest rescoring #5

Closed
wants to merge 6 commits into from
Closed

Conversation

pkufool
Copy link
Collaborator

@pkufool pkufool commented Aug 4, 2021

There are several decoding methods in icefall now, their pipelines are demonstrated as the picture below. What I want to do in this pull request locates in the red rectangle, it was proposed here(k2-fsa/snowfall#232) several weeks ago.

image

This is just at the very beginning, just copy the related previous work from k2 & snowfall.

@pkufool pkufool marked this pull request as draft August 9, 2021 08:25
est_scores = 1 - 1/2 * (
1 + torch.erf(
(best_score - path_mean) / torch.sqrt(2 * path_var)
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For finding the best path, instead of trying to use this kind of integral, I would use:
(path_mean - best_score) / torch.sqrt(path_var)
which can be interpreted as the "z score". This has a monotonic relationship with this percentile/integral thingy, and is easier to compute and better behaved numerically.

@pkufool
Copy link
Collaborator Author

pkufool commented Sep 7, 2021

After fixing some errors, now I can get the same WER (as original attention decoder) using nbest attention rescoring, see the table below:

Decoding method WER(%) test-clean WER(%) test-other Notes
rescore with attention decoder (num-paths=500, max-duration=1, lattice-score-scale=0.5) 2.57 5.94 the original attention rescoring method
rescore all nbest path with attention decoder(num-paths=500, max-duration=1, lattice-score-scale=0.5) 2.57 5.98 rescore all the unique nbest path, should be the same with the above row, just to check the function rescore_nbest_with_attention_rescorer is right
rescore topk * 2 nbest path with attention decoder(topk=10,num-paths=500, max-duration=1, lattice-score-scale=0.5) 2.59 5.97 rescore the top2k nbest path (sorted by lattice scores) with attention decoder
rescore topk + topk nbest path with attention decoder(topk=10, num-paths=500, max-duration=1, lattice-score-scale=0.5) 2.59 6.03 rescore topk nbest path (sorted by lattice scores) first, select the other topk path by a small estimation model, then rescore the selected topk nbest path with attention decoder

The future plans are:

  1. Tune the small model that predicting the rescoring score of a path (to select better path).
  2. Support batch processing (only support batch-size=1 now).
  3. Check the code to speed up the decoding (now it spends more time than original attention-decoder method, 29mins vs 20mins or so).

@pkufool pkufool marked this pull request as ready for review September 7, 2021 12:29
self,
path: Path,
model: str,
) -> None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add documentations to the methods/functions in this file?

import glob
import logging
from pathlib import Path
from typing import Tuple, List
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you have a look at
https://icefall.readthedocs.io/en/latest/contributing/code-style.html
to follow the code style?

self.files = files[0: int(len(files) * 0.8)]
elif model == 'dev':
self.files = files[int(len(files) * 0.8): int(len(files) * 0.9)]
elif mode == 'test':
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mode or model?

x = self.embedding(x)
x = self.sigmod(x)
x = self.output(x)
mean, var = x[:, 0], x[:, 1]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var -> log_var?

"--hidden-dim",
type=int,
default=20,
help="Neural number of didden layer.",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
help="Neural number of didden layer.",
help="Neural number of hidden layer.",

hidden_dim = args.hidden_dim
)

model = model.to("cuda")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we support also CPU?

step = 0
model.eval()
for x, y in dev_dataloader:
mean, var = model(x.cuda())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend you to define a variable device, which can be either a CPU or a CUDA device, and use model(x.to(device)).

dev_loss = 0.0
step = 0
model.eval()
for x, y in dev_dataloader:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put the evaluation process in a context disabling gradient computation.

import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove unused imports.
If you have a look at https://icefall.readthedocs.io/en/latest/contributing/code-style.html
and start to use flake8, it will tell you this import is never used.

parser = get_parser()
args = parser.parse_args()
torch.manual_seed(42)
torch.cuda.manual_seed(42)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
torch.cuda.manual_seed(42)

torch.manual_seed(42) already did it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants