-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Conversation
python/mxnet/gluon/block.py
Outdated
@@ -227,13 +228,37 @@ def params(self): | |||
children's parameters).""" | |||
return self._params | |||
|
|||
def collect_params(self): | |||
def collect_params(self, select=['.*']): |
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.
select should be None by default.
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.
ok, will change this.
python/mxnet/gluon/block.py
Outdated
Parameters | ||
---------- | ||
select : list of str | ||
List of name or regular expressions |
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.
Only support 1 regex, not a list
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.
Done. @piiswrong
another, if user has a python list to select parameter, like ['a', 'b', 'c'], t hen he/she should first convert select='a|b|c' first using some code, any better way to avoid this?
python/mxnet/gluon/block.py
Outdated
ret = ParameterDict(self._params.prefix) | ||
ret.update(self.params) | ||
if select is None: |
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.
don't do any regex matching if select is None. Its very slow
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.
@piiswrong Done.
wonderful. |
python/mxnet/gluon/block.py
Outdated
else: | ||
for name in self.params.keys(): | ||
if re.compile(select).match(name): | ||
ret.update({name:self.params[name]}) |
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.
pattern = re.compile(select)
ret.update({name: value for name, value in self.params.items() if pattern.match(name)}
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.
Compiling a pattern that don't change can be expensive especially inside the for loop. I wonder if we should cache the compiled object and pass it to the recursive calls.
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.
@szha Done.
python/mxnet/gluon/block.py
Outdated
@@ -253,7 +253,7 @@ def collect_params(self, select=None): | |||
The selected :py:class:`ParameterDict` | |||
""" | |||
ret = ParameterDict(self._params.prefix) | |||
if select is None: | |||
if not select: |
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.
if select = []
or {}
or ''
or ()
then not select will be True, may be wrong here.
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.
You're right, and it should be fine as long as both empty string and None returns true.
* support regex of collect_params() * fix pylint * change default value && make select as a sigle reg * fix if select is None, then will not do any regex matching * update regex compile && add test * Update block.py * support regex of collect_params() * fix pylint * change default value && make select as a sigle reg * fix if select is None, then will not do any regex matching * update regex compile && add test
* support regex of collect_params() * fix pylint * change default value && make select as a sigle reg * fix if select is None, then will not do any regex matching * update regex compile && add test * Update block.py * support regex of collect_params() * fix pylint * change default value && make select as a sigle reg * fix if select is None, then will not do any regex matching * update regex compile && add test
* support regex of collect_params() * fix pylint * change default value && make select as a sigle reg * fix if select is None, then will not do any regex matching * update regex compile && add test * Update block.py * support regex of collect_params() * fix pylint * change default value && make select as a sigle reg * fix if select is None, then will not do any regex matching * update regex compile && add test
* support regex of collect_params() * fix pylint * change default value && make select as a sigle reg * fix if select is None, then will not do any regex matching * update regex compile && add test * Update block.py * support regex of collect_params() * fix pylint * change default value && make select as a sigle reg * fix if select is None, then will not do any regex matching * update regex compile && add test
Description
#9323
Checklist
Essentials
make lint
)@szha
select
name and function comments may be improved. if these change is ok, i'll add unit test here.