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

Split layer in python #497

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
minor changes
Signed-off-by: voropz <[email protected]>
voropz committed Dec 9, 2021
commit 50058c9d78c634dc00358656b7d07e674037768c
31 changes: 20 additions & 11 deletions NeoML/Python/neoml/Dnn/Split.py
Original file line number Diff line number Diff line change
@@ -21,22 +21,18 @@


class SplitLayer(Layer):
"""The base (abstract) class for a split layer.
"""
def __init__(self, classname, input_layer, sizes, name):
assert hasattr(PythonWrapper, classname), 'Incorrect split layer specified: ' + classname

if type(input_layer) is getattr(PythonWrapper, classname):
super().__init__(input_layer)
return

layers, outputs = check_input_layers(input_layer, 1)

s = numpy.array(sizes, dtype=numpy.int32, copy=False)

if s.size > 3:
raise ValueError('The `sizes` must contain not more than 3 elements.')

if numpy.any(s < 0):
raise ValueError('The `sizes` must contain only positive values.')

internal = getattr(PythonWrapper, classname)(str(name), layers[0], int(outputs[0]), s)
internal = getattr(PythonWrapper, classname)(str(name), layers[0], int(outputs[0]), self.__sizes_to_array(sizes))
super().__init__(internal)

@property
@@ -48,8 +44,21 @@ def output_sizes(self):
@output_sizes.setter
def output_sizes(self, value):
"""
"""
self._internal.set_output_counts(value)
"""
self._internal.set_output_counts(self.__sizes_to_array(value))

@staticmethod
def __sizes_to_array(sizes) -> numpy.ndarray:
sizes = numpy.array(sizes, dtype=numpy.int32)
if sizes.ndim != 1 or sizes.size > 3:
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do you have the upper limit of 3 here? It should be up to 31...

Copy link
Contributor

Choose a reason for hiding this comment

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

I see, it was in the original layer. But that's a mistake! It should be 31. (MathEngine can handle split/merge with up to 32 parts).

raise ValueError('The `sizes` must be a one-dimentional sequence containing not more than 3 elements.')

if numpy.any(sizes < 0):
raise ValueError('The `sizes` must contain only positive values.')

return sizes

# ----------------------------------------------------------------------------------------------------------------------


class SplitChannels(SplitLayer):