-
Notifications
You must be signed in to change notification settings - Fork 0
Add FactoredTriphoneBlockV1
#58
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
Open
NeoLegends
wants to merge
8
commits into
main
Choose a base branch
from
moritz-fh-triphone
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7068e4f
Add FactoredDiphoneBlockV2 with right context output for training
NeoLegends d3e3be7
update types
NeoLegends adfcdde
update var names
NeoLegends 647b774
document valid value range for context values
NeoLegends 815934c
Add `FactoredTriphoneBlockV1`
NeoLegends 3de2552
override forward_factored instead
NeoLegends 402ac2e
Resolve joint forwarding ambiguity
NeoLegends 54409a8
Merge branch 'main' into moritz-fh-triphone
NeoLegends File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,13 @@ | ||
| __all__ = ["FactoredDiphoneBlockV1Config", "FactoredDiphoneBlockV1", "BoundaryClassV1"] | ||
| __all__ = [ | ||
| "FactoredDiphoneBlockV1Config", | ||
| "FactoredDiphoneBlockV1", | ||
| "FactoredDiphoneBlockV2Config", | ||
| "FactoredDiphoneBlockV2", | ||
| "FactoredTriphoneBlockV1Config", | ||
| "FactoredTriphoneBlockV1", | ||
| "BoundaryClassV1", | ||
| ] | ||
|
|
||
| from .diphone import * | ||
| from .triphone import * | ||
| from .util import BoundaryClassV1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| __all__ = [ | ||
| "FactoredTriphoneBlockV1Config", | ||
| "FactoredTriphoneBlockV1", | ||
| ] | ||
|
|
||
| from dataclasses import dataclass | ||
| from typing import Tuple | ||
|
|
||
| import torch | ||
| from torch import nn, Tensor | ||
|
|
||
| from .diphone import FactoredDiphoneBlockV1, FactoredDiphoneBlockV2Config | ||
| from .util import get_mlp | ||
|
|
||
|
|
||
| @dataclass | ||
| class FactoredTriphoneBlockV1Config(FactoredDiphoneBlockV2Config): | ||
| """ | ||
| Attributes: | ||
| Same as the FactoredDiphoneBlockV2Config. | ||
| """ | ||
|
|
||
|
|
||
| class FactoredTriphoneBlockV1(FactoredDiphoneBlockV1): | ||
| """ | ||
| Triphone FH model output block. | ||
|
|
||
| Consumes the output h(x) of a main encoder model and computes factored logits/probabilities | ||
| for p(c|l,h(x)), p(l|h(x)) and p(r|c,l,h(x)). | ||
michelwi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
|
|
||
| def __init__(self, cfg: FactoredTriphoneBlockV1Config): | ||
| super().__init__(cfg) | ||
|
|
||
| self.center_state_embedding = nn.Embedding(self.num_center, cfg.center_state_embedding_dim) | ||
| self.right_context_encoder = get_mlp( | ||
| num_input=cfg.num_inputs + cfg.center_state_embedding_dim + cfg.left_context_embedding_dim, | ||
| num_output=self.num_contexts, | ||
| hidden_dim=cfg.context_mix_mlp_dim, | ||
| num_layers=cfg.context_mix_mlp_num_layers, | ||
| dropout=cfg.dropout, | ||
| activation=cfg.activation, | ||
| ) | ||
|
|
||
| def forward( | ||
michelwi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| self, | ||
| features: Tensor, # B, T, F | ||
| contexts_left: Tensor, # B, T | ||
| contexts_center: Tensor, # B, T | ||
| ) -> Tuple[Tensor, Tensor, Tensor, Tensor, Tensor]: | ||
| """ | ||
| :param features: Main encoder output. shape B, T, F. F=num_inputs | ||
| :param contexts_left: The left contexts used to compute p(c|l,x), shape B, T. | ||
| Valid values range from [0, num_contexts). | ||
| :param contexts_center: The center states used to compute p(r|c,l,x), shape B, T. | ||
| Given that the center state also contains the word-end class and HMM state ID, the valid values | ||
| range from [0, num_center_states), where num_center_states >= num_contexts. | ||
| :return: tuple of logits for p(c|l,x), p(l|x), p(r|c,l,x) and the embedded left context and center state values. | ||
michelwi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
|
|
||
| logits_center, logits_left, contexts_left_embedded = super().forward(features, contexts_left) | ||
|
|
||
| # This logic is very similar to FactoredDiphoneBlockV2.forward, but not the same. | ||
| # This class computes `p(r|c,l,h(x))` while FactoredDiphoneBlockV2 computes `p(r|c,h(x))`. | ||
| center_states_embedded = self.center_state_embedding(contexts_center) # B, T, E' | ||
| features_right = torch.cat((features, center_states_embedded, contexts_left_embedded), -1) # B, T, F+E'+E | ||
| logits_right = self.right_context_encoder(features_right) # B, T, C | ||
|
|
||
| return logits_center, logits_left, logits_right, contexts_left_embedded, center_states_embedded | ||
michelwi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.