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

add any axis type #373

Merged
merged 1 commit into from
Feb 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion nemo/core/neural_types/axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class AxisKind(AxisKindAbstract):
"""This Enum represents what does varying axis dimension mean.
For example, does this dimension correspond to width, batch, time, etc.
The "Dimension" and "Channel" kinds are the same and used to represent
a general axis.
a general axis. "Any" axis will accept any axis kind fed to it.
"""

Batch = 0
Expand All @@ -43,6 +43,7 @@ class AxisKind(AxisKindAbstract):
Channel = 2
Width = 3
Height = 4
Any = 5

def __str__(self):
return str(self.name).lower()
Expand All @@ -61,6 +62,8 @@ def from_str(label):
return AxisKind.Width
elif _label == "h" or _label == "height":
return AxisKind.Height
elif _label == "any":
return AxisKind.Any
else:
raise ValueError(f"Can't create AxisKind from {label}")

Expand Down
4 changes: 3 additions & 1 deletion nemo/core/neural_types/neural_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,9 @@ def __compare_axes(axes_a, axes_b) -> int:
for axis_a, axis_b in zip(axes_a, axes_b):
kinds_a[axis_a.kind] = axis_a.size
kinds_b[axis_b.kind] = axis_b.size
if (
if axis_a.kind == AxisKind.Any:
same = True
elif (
axis_a.kind != axis_b.kind
or axis_a.is_list != axis_b.is_list
or (axis_a.size != axis_b.size and axis_a.size is not None)
Expand Down
10 changes: 10 additions & 0 deletions tests/core/test_neural_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,13 @@ def test_unspecified_dimensions(self):
t1 = NeuralType(('B', 'T', 'C'), SpectrogramType())
self.assertEqual(t1.compare(t0), NeuralTypeComparisonResult.SAME)
self.assertEqual(t0.compare(t1), NeuralTypeComparisonResult.DIM_INCOMPATIBLE)

def test_any_axis(self):
t0 = NeuralType(('B', 'Any', 'Any'), VoidType())
t1 = NeuralType(('B', 'Any', 'Any'), SpectrogramType())
t2 = NeuralType(('B', 'T', 'C'), SpectrogramType())
self.assertEqual(t0.compare(t1), NeuralTypeComparisonResult.SAME)
self.assertEqual(t0.compare(t2), NeuralTypeComparisonResult.SAME)
self.assertEqual(t1.compare(t2), NeuralTypeComparisonResult.SAME)
self.assertEqual(t2.compare(t1), NeuralTypeComparisonResult.INCOMPATIBLE)
self.assertEqual(t1.compare(t0), NeuralTypeComparisonResult.INCOMPATIBLE)