szha commented on a change in pull request #9348: support regex of
collect_params()
URL: https://github.com/apache/incubator-mxnet/pull/9348#discussion_r160778685
##########
File path: python/mxnet/gluon/block.py
##########
@@ -227,13 +228,39 @@ def params(self):
children's parameters)."""
return self._params
- def collect_params(self):
+ def collect_params(self, select=None):
"""Returns a :py:class:`ParameterDict` containing this
:py:class:`Block` and all of its
- children's Parameters."""
+ children's Parameters(default), also can returns the select
:py:class:`ParameterDict`
+ which match some given regular expressions.
+
+ For example, collect the specified parameter in ['conv1_weight',
'conv1_bias', 'fc_weight',
+ 'fc_bias']::
+
+ model.collect_params('conv1_weight|conv1_bias|fc_weight|fc_bias')
+
+ or collect all paramters which their name ends with 'weight' or
'bias', this can be done
+ using regular expressions::
+
+ model.collect_params('.*weight|.*bias')
+
+ Parameters
+ ----------
+ select : str
+ regular expressions
+
+ Returns
+ -------
+ The selected :py:class:`ParameterDict`
+ """
ret = ParameterDict(self._params.prefix)
- ret.update(self.params)
+ if select is None:
+ ret.update(self.params)
+ else:
+ for name in self.params.keys():
+ if re.compile(select).match(name):
+ ret.update({name:self.params[name]})
Review comment:
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.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services