You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2018/01/07 08:14:31 UTC

[GitHub] tornadomeet closed pull request #9323: support setattr for some specified paramter of ParameterDict

tornadomeet closed pull request #9323: support setattr for some specified paramter of ParameterDict
URL: https://github.com/apache/incubator-mxnet/pull/9323
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/python/mxnet/gluon/parameter.py b/python/mxnet/gluon/parameter.py
index fa38285911..a45c1668a2 100644
--- a/python/mxnet/gluon/parameter.py
+++ b/python/mxnet/gluon/parameter.py
@@ -593,10 +593,10 @@ def reset_ctx(self, ctx):
         for i in self.values():
             i.reset_ctx(ctx)
 
-    def setattr(self, name, value):
-        """Set an attribute to a new value for all Parameters.
+    def setattr(self, name, value, selected_param_names="all"):
+        """Set an attribute to a new value for all(default) or some selected Parameters.
 
-        For example, set grad_req to null if you don't need gradient w.r.t a
+        For example, set grad_req to null if you don't need gradient w.r.t all
         model's Parameters::
 
             model.collect_params().setattr('grad_req', 'null')
@@ -605,15 +605,27 @@ def setattr(self, name, value):
 
             model.collect_params().setattr('lr_mult', 0.5)
 
+        or set grad_req to null if you don't need gradient w.r.t  model's
+        Parameters in ['conv1_weight', 'conv1_bias']::
+
+            model.collect_params().setattr('grad_req', 'null', ['conv1_weight', 'conv1_bias'])
+
         Parameters
         ----------
         name : str
             Name of the attribute.
         value : valid type for attribute name
             The new value for the attribute.
+        selected_param_names : str or list
+            The selected paramters to be setattr.
         """
-        for i in self.values():
-            setattr(i, name, value)
+        if selected_param_names == "all":
+            selected_param_names = self.keys()
+        elif isinstance(selected_param_names, str):
+            selected_param_names = [selected_param_names]
+        for k, v in self.items():
+            if k in selected_param_names:
+                setattr(v, name, value)
 
     def save(self, filename, strip_prefix=''):
         """Save parameters to file.


 

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services