-
Notifications
You must be signed in to change notification settings - Fork 11
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
Added Consensus CNN #162
Open
rahulmohan
wants to merge
3
commits into
dev-v0.1.0
Choose a base branch
from
consensus_cnn
base: dev-v0.1.0
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
Added Consensus CNN #162
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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 |
---|---|---|
|
@@ -143,28 +143,22 @@ def output_ports(self): | |
'output_logit': NeuralType(('B', 'W', 'D'), LogitsType()), | ||
} | ||
|
||
def __init__(self, input_feature_size, num_output_logits, | ||
gru_size=128, gru_layers=2, apply_softmax=False): | ||
def __init__(self, sequence_length, input_feature_size, num_output_logits): | ||
"""Construct an Consensus RNN NeMo instance. | ||
|
||
Args: | ||
sequence_length : Length of sequence to feed into RNN. | ||
input_feature_size : Length of input feature set. | ||
num_output_logits : Number of output classes of classifier. | ||
gru_size : Number of units in RNN | ||
gru_layers : Number of layers in RNN | ||
apply_softmax : Apply softmax to the output of the classifier. | ||
|
||
Returns: | ||
Instance of class. | ||
""" | ||
super().__init__() | ||
self.num_output_logits = num_output_logits | ||
self.apply_softmax = apply_softmax | ||
self.gru_size = gru_size | ||
self.gru_layers = gru_layers | ||
|
||
self.gru = nn.GRU(input_feature_size, gru_size, gru_layers, batch_first=True, bidirectional=True) | ||
self.classifier = nn.Linear(2 * gru_size, self.num_output_logits) # 2* for bidirectional | ||
self.gru = nn.GRU(input_feature_size, 128, 2, batch_first=True, bidirectional=True) | ||
self.classifier = nn.Linear(2 * 128, self.num_output_logits) | ||
|
||
self._device = torch.device( | ||
"cuda" if self.placement == DeviceType.GPU else "cpu") | ||
|
@@ -181,6 +175,70 @@ def forward(self, encoding): | |
""" | ||
encoding, h_n = self.gru(encoding) | ||
encoding = self.classifier(encoding) | ||
if self.apply_softmax: | ||
encoding = F.softmax(encoding, dim=2) | ||
return encoding | ||
|
||
|
||
class ConsensusCNN(TrainableNM): | ||
"""A Neural Module for training a Consensus Attention Model.""" | ||
|
||
@property | ||
@add_port_docs() | ||
def input_ports(self): | ||
"""Return definitions of module input ports. | ||
|
||
Returns: | ||
Module input ports. | ||
""" | ||
return { | ||
"encoding": NeuralType(('B', 'W', 'C'), ChannelType()), | ||
} | ||
|
||
@property | ||
@add_port_docs() | ||
def output_ports(self): | ||
"""Return definitions of module output ports. | ||
|
||
Returns: | ||
Module output ports. | ||
""" | ||
return { | ||
# Variant type | ||
'output_logit': NeuralType(('B', 'W', 'D'), LogitsType()), | ||
} | ||
|
||
def __init__(self, sequence_length, input_feature_size, num_output_logits): | ||
"""Construct an Consensus RNN NeMo instance. | ||
|
||
Args: | ||
sequence_length : Length of sequence to feed into RNN. | ||
input_feature_size : Length of input feature set. | ||
num_output_logits : Number of output classes of classifier. | ||
|
||
Returns: | ||
Instance of class. | ||
""" | ||
super().__init__() | ||
self.num_output_logits = num_output_logits | ||
self.conv1 = nn.Conv1d(input_feature_size, 128, kernel_size=1, padding=0) | ||
self.gru = nn.GRU(128, 16, 1, batch_first=True, bidirectional=True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 16 and 128 should be parameters instead of hard-coded. |
||
self.classifier = nn.Linear(32, self.num_output_logits) | ||
|
||
self._device = torch.device( | ||
"cuda" if self.placement == DeviceType.GPU else "cpu") | ||
self.to(self._device) | ||
|
||
def forward(self, encoding): | ||
"""Abstract function to run the network. | ||
|
||
Args: | ||
encoding : Input sequence to run network on. | ||
|
||
Returns: | ||
Output of forward pass. | ||
""" | ||
encoding = encoding.permute(0, 2, 1) | ||
encoding = self.conv1(encoding) | ||
encoding = encoding.permute(0, 2, 1) | ||
encoding, h_n = self.gru(encoding) | ||
encoding = self.classifier(encoding) | ||
return encoding |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GRU size and layers should remain parameters.