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/03/31 06:28:23 UTC

[GitHub] piiswrong closed pull request #10333: Fix error messages

piiswrong closed pull request #10333: Fix error messages
URL: https://github.com/apache/incubator-mxnet/pull/10333
 
 
   

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/block.py b/python/mxnet/gluon/block.py
index 3571b15ee06..fd95641274d 100644
--- a/python/mxnet/gluon/block.py
+++ b/python/mxnet/gluon/block.py
@@ -226,7 +226,7 @@ def _find_block_in_container(data):
                                   'registered automatically. Make sure to register them using '
                                   'register_child() or switching to '
                                   'nn.Sequential/nn.HybridSequential instead. '
-                                  .format(name=self.__class__.__name__ + "." + k))
+                                  .format(name=self.__class__.__name__ + "." + k), stacklevel=3)
 
     def _alias(self):
         return self.__class__.__name__.lower()
@@ -447,11 +447,11 @@ def _build_cache(self, *args):
         for name, i in input_idx.items():
             if name not in expected_inputs:
                 warnings.warn("The %d-th input to HybridBlock is not used by any "
-                              "computation. Is this intended?"%i)
+                              "computation. Is this intended?"%i, stacklevel=4)
         for name in params:
             if name not in expected_inputs:
                 warnings.warn("Parameter %s is not used by any computation. "
-                              "Is this intended?"%name)
+                              "Is this intended?"%name, stacklevel=4)
 
         self._cached_op_args = [(False, params[name]) if name in params
                                 else (True, input_idx[name])
diff --git a/python/mxnet/gluon/data/vision/transforms.py b/python/mxnet/gluon/data/vision/transforms.py
index a0734676a8d..5e65715e1b2 100644
--- a/python/mxnet/gluon/data/vision/transforms.py
+++ b/python/mxnet/gluon/data/vision/transforms.py
@@ -59,14 +59,17 @@ def __init__(self, transforms):
                 continue
             elif len(hybrid) == 1:
                 self.add(hybrid[0])
+                hybrid = []
             elif len(hybrid) > 1:
                 hblock = HybridSequential()
                 for j in hybrid:
                     hblock.add(j)
+                hblock.hybridize()
                 self.add(hblock)
+                hybrid = []
+
             if i is not None:
                 self.add(i)
-        self.hybridize()
 
 
 class Cast(HybridBlock):
diff --git a/python/mxnet/gluon/model_zoo/vision/resnet.py b/python/mxnet/gluon/model_zoo/vision/resnet.py
index 37eb2cc19b4..5ee67b510a8 100644
--- a/python/mxnet/gluon/model_zoo/vision/resnet.py
+++ b/python/mxnet/gluon/model_zoo/vision/resnet.py
@@ -375,7 +375,12 @@ def get_resnet(version, num_layers, pretrained=False, ctx=cpu(),
     root : str, default '~/.mxnet/models'
         Location for keeping the model parameters.
     """
+    assert num_layers in resnet_spec, \
+        "Invalid number of layers: %d. Options are %s"%(
+            num_layers, str(resnet_spec.keys()))
     block_type, layers, channels = resnet_spec[num_layers]
+    assert version >= 1 and version <= 2, \
+        "Invalid resnet version: %d. Options are 1 and 2."%version
     resnet_class = resnet_net_versions[version-1]
     block_class = resnet_block_versions[version-1][block_type]
     net = resnet_class(block_class, layers, channels, **kwargs)
diff --git a/python/mxnet/gluon/nn/basic_layers.py b/python/mxnet/gluon/nn/basic_layers.py
index 3801c84b56f..f6113cc52b4 100644
--- a/python/mxnet/gluon/nn/basic_layers.py
+++ b/python/mxnet/gluon/nn/basic_layers.py
@@ -81,7 +81,7 @@ def hybridize(self, active=True, **kwargs):
         """
         if self._children and all(isinstance(c, HybridBlock) for c in self._children):
             warnings.warn('All children of this Sequential layer are HybridBlocks. Consider ' \
-                          'using HybridSequential for the best performance.')
+                          'using HybridSequential for the best performance.', stacklevel=2)
         super(Sequential, self).hybridize(active, **kwargs)
 
 
diff --git a/python/mxnet/gluon/parameter.py b/python/mxnet/gluon/parameter.py
index 5a9277b2d63..ab511ab6e42 100644
--- a/python/mxnet/gluon/parameter.py
+++ b/python/mxnet/gluon/parameter.py
@@ -127,7 +127,7 @@ def grad_req(self):
     @grad_req.setter
     def grad_req(self, req):
         assert req in ['write', 'add', 'null'], \
-            "grad_req must be one of write, add, or null, but got %s"%req
+            "grad_req must be one of 'write', 'add', or 'null', but got '%s'"%req
         if not self._differentiable:
             req = 'null'
         if self._grad_req == req:
@@ -165,25 +165,24 @@ def _check_and_get(self, arr_list, ctx):
                     return arr_list[0]
                 else:
                     ctx = context.current_context()
-            if ctx.device_typeid < len(self._ctx_map):
-                ctx_list = self._ctx_map[ctx.device_typeid]
-                if ctx.device_id < len(ctx_list):
-                    idx = ctx_list[ctx.device_id]
-                    if idx is not None:
-                        return arr_list[idx]
+            ctx_list = self._ctx_map[ctx.device_typeid&1]
+            if ctx.device_id < len(ctx_list):
+                idx = ctx_list[ctx.device_id]
+                if idx is not None:
+                    return arr_list[idx]
             raise RuntimeError(
-                "Parameter %s was not initialized on context %s. "
+                "Parameter '%s' was not initialized on context %s. "
                 "It was only initialized on %s."%(
                     self.name, str(ctx), str(self._ctx_list)))
         if self._deferred_init:
             raise DeferredInitializationError(
-                "Parameter %s has not been initialized yet because initialization was " \
+                "Parameter '%s' has not been initialized yet because initialization was " \
                 "deferred. Actual initialization happens during the first forward pass. " \
                 "Please pass one batch of data through the network before accessing Parameters. " \
                 "You can also avoid deferred initialization by specifying in_units, " \
                 "num_features, etc., for network layers."%(self.name))
         raise RuntimeError(
-            "Parameter %s has not been initialized. Note that " \
+            "Parameter '%s' has not been initialized. Note that " \
             "you should initialize parameters and create Trainer " \
             "with Block.collect_params() instead of Block.params " \
             "because the later does not include Parameters of " \
@@ -194,13 +193,13 @@ def _load_init(self, data, ctx):
         if self.shape:
             for self_dim, data_dim in zip(self.shape, data.shape):
                 assert self_dim == 0 or self_dim == data_dim, \
-                    "Failed loading Parameter %s from saved params: " \
+                    "Failed loading Parameter '%s' from saved params: " \
                     "shape incompatible expacted %s vs saved %s"%(
                         self.name, str(self.shape), str(data.shape))
             self.shape = tuple(i if i != 0 else j for i, j in zip(self.shape, data.shape))
         if self.dtype:
             assert np.dtype(self.dtype).type == data.dtype, \
-                "Failed loading Parameter %s from saved params: " \
+                "Failed loading Parameter '%s' from saved params: " \
                 "dtype incompatible expacted %s vs saved %s"%(
                     self.name, str(self.dtype), str(data.dtype))
         if isinstance(ctx, Context):
@@ -208,13 +207,13 @@ def _load_init(self, data, ctx):
         if self._data is None:
             if self._deferred_init:
                 assert set(ctx) == set(self._deferred_init[1]), \
-                    "Failed to load Parameter %s on %s because it was " \
+                    "Failed to load Parameter '%s' on %s because it was " \
                     "previous initialized on %s."%(
                         self.name, str(ctx), str(self.list_ctx()))
             self._init_impl(data, ctx)
         else:
             assert set(ctx) == set(self.list_ctx()), \
-                "Failed to load Parameter %s on %s because it was " \
+                "Failed to load Parameter '%s' on %s because it was " \
                 "previous initialized on %s."%(
                     self.name, str(ctx), str(self.list_ctx()))
             self.set_data(data)
@@ -227,7 +226,7 @@ def _finish_deferred_init(self):
         init, ctx, default_init, data = self._deferred_init
         self._deferred_init = ()
         assert self.shape is not None and np.prod(self.shape) > 0, \
-            "Cannot initialize Parameter %s because it has " \
+            "Cannot initialize Parameter '%s' because it has " \
             "invalid shape: %s. Please specify in_units, " \
             "in_channels, etc for `Block`s."%(
                 self.name, str(self.shape))
@@ -244,11 +243,9 @@ def _finish_deferred_init(self):
     def _init_impl(self, data, ctx_list):
         """Sets data and grad."""
         self._ctx_list = list(ctx_list)
-        self._ctx_map = []
+        self._ctx_map = [[], []]
         for i, ctx in enumerate(self._ctx_list):
-            while len(self._ctx_map) <= ctx.device_typeid:
-                self._ctx_map.append([])
-            dev_list = self._ctx_map[ctx.device_typeid]
+            dev_list = self._ctx_map[ctx.device_typeid&1]
             while len(dev_list) <= ctx.device_id:
                 dev_list.append(None)
             dev_list[ctx.device_id] = i
@@ -317,8 +314,9 @@ def initialize(self, init=None, ctx=None, default_init=initializer.Uniform(),
         <NDArray 2x2 @gpu(1)>
         """
         if self._data is not None and not force_reinit:
-            warnings.warn("Parameter %s is already initialized, ignoring. " \
-                          "Set force_reinit=True to re-initialize."%self.name)
+            warnings.warn("Parameter '%s' is already initialized, ignoring. " \
+                          "Set force_reinit=True to re-initialize."%self.name,
+                          stacklevel=2)
             return
         self._data = self._grad = None
 
@@ -332,7 +330,7 @@ def initialize(self, init=None, ctx=None, default_init=initializer.Uniform(),
             if self._allow_deferred_init:
                 self._deferred_init = (init, ctx, default_init, None)
                 return
-            raise ValueError("Cannot initialize Parameter %s because it has " \
+            raise ValueError("Cannot initialize Parameter '%s' because it has " \
                              "invalid shape: %s."%(self.name, str(self.shape)))
 
         self._deferred_init = (init, ctx, default_init, None)
@@ -357,7 +355,7 @@ def reset_ctx(self, ctx):
             init, _, default_init, data = self._deferred_init
             self._deferred_init = (init, ctx, default_init, data)
         else:
-            raise ValueError("Cannot reset context for Parameter %s because it "
+            raise ValueError("Cannot reset context for Parameter '%s' because it "
                              "has not been initialized."%self.name)
 
 
@@ -367,7 +365,7 @@ def set_data(self, data):
 
         if self._data is None:
             assert self._deferred_init is not None, \
-                "Parameter %s has not been initialized"%self.name
+                "Parameter '%s' has not been initialized"%self.name
             self._deferred_init = self._deferred_init[:3] + (data,)
             return
 
@@ -404,7 +402,7 @@ def grad(self, ctx=None):
         """
         if self._data is not None and self._grad is None:
             raise RuntimeError(
-                "Cannot get gradient array for Parameter %s " \
+                "Cannot get gradient array for Parameter '%s' " \
                 "because grad_req='null'"%(self.name))
         return self._check_and_get(self._grad, ctx)
 
@@ -413,7 +411,7 @@ def list_grad(self):
         as :py:meth:`values`."""
         if self._data is not None and self._grad is None:
             raise RuntimeError(
-                "Cannot get gradient array for Parameter %s " \
+                "Cannot get gradient array for Parameter '%s' " \
                 "because grad_req='null'"%(self.name))
         return self._check_and_get(self._grad, list)
 
@@ -422,7 +420,7 @@ def list_ctx(self):
         if self._data is None:
             if self._deferred_init:
                 return self._deferred_init[1]
-            raise RuntimeError("Parameter %s has not been initialized"%self.name)
+            raise RuntimeError("Parameter '%s' has not been initialized"%self.name)
         return self._ctx_list
 
     def zero_grad(self):
@@ -500,6 +498,14 @@ def _init_weight(self, _, arr):
             init=init_name)
 
 
+def _brief_print_list(lst, limit=7):
+    """Print at most `limit` elements of list."""
+    if len(lst) > limit:
+        return _brief_print_list(lst[:limit//2], limit) + ', ..., ' + \
+            _brief_print_list(lst[-limit//2:], limit)
+    return ', '.join(["'%s'"%str(i) for i in lst])
+
+
 class ParameterDict(object):
     """A dictionary managing a set of parameters.
 
@@ -600,9 +606,9 @@ def get(self, name, **kwargs):
                             continue
 
                     assert v is None or v == existing, \
-                        "Cannot retrieve Parameter %s because desired attribute " \
-                        "does not match with stored for attribute %s: " \
-                        "desired %s vs stored %s."%(
+                        "Cannot retrieve Parameter '%s' because desired attribute " \
+                        "does not match with stored for attribute '%s': " \
+                        "desired '%s' vs stored '%s'."%(
                             name, k, str(v), str(getattr(param, k)))
                 else:
                     setattr(param, k, v)
@@ -631,20 +637,21 @@ def get_constant(self, name, value=None):
         param = self._get_impl(name)
         if param is None:
             if value is None:
-                raise KeyError("No constant named {}. Please specify value " \
+                raise KeyError("No constant named '{}'. Please specify value " \
                                "if you want to create a new constant.".format(
                                    name))
             param = Constant(name, value)
             self._params[name] = param
         elif value is not None:
             assert isinstance(param, Constant), \
-                "Parameter {} already exists but it is not a constant.".format(
+                "Parameter '{}' already exists but it is not a constant.".format(
                     name)
             if isinstance(value, nd.NDArray):
                 value = value.asnumpy()
             assert param.shape == value.shape and \
                 (param.value.asnumpy() == value).all(), \
-                "Constant {} already exists but it's value doesn't match new value"
+                "Constant '{}' already exists but it's value doesn't match new " \
+                "value".format(name)
         return param
 
     def update(self, other):
@@ -653,7 +660,7 @@ def update(self, other):
             if k in self._params:
                 assert self._params[k] is v, \
                     "Cannot update self with other because they have different " \
-                    "Parameters with the same name %s"%k
+                    "Parameters with the same name '%s'"%k
             else:
                 self._params[k] = v
 
@@ -727,8 +734,8 @@ def save(self, filename, strip_prefix=''):
             weight = param._reduce()
             if not param.name.startswith(strip_prefix):
                 raise ValueError(
-                    "Prefix %s is to be striped before saving, but Parameter " \
-                    "%s does not start with %s. If you are using Block.save_params, " \
+                    "Prefix '%s' is to be striped before saving, but Parameter " \
+                    "'%s' does not start with '%s'. If you are using Block.save_params, " \
                     "This may be due to your Block shares parameters from other " \
                     "Blocks or you forgot to use ``with name_scope()`` during init. " \
                     "Consider switching to Block.collect_params.save and " \
@@ -756,8 +763,8 @@ def load(self, filename, ctx, allow_missing=False,
         if restore_prefix:
             for name in self.keys():
                 assert name.startswith(restore_prefix), \
-                    "restore_prefix is %s but Parameters name %s does not start " \
-                    "with %s"%(restore_prefix, name, restore_prefix)
+                    "restore_prefix is '%s' but Parameters name '%s' does not start " \
+                    "with '%s'"%(restore_prefix, name, restore_prefix)
         lprefix = len(restore_prefix)
         loaded = [(k[4:] if k.startswith('arg:') or k.startswith('aux:') else k, v) \
                   for k, v in ndarray.load(filename).items()]
@@ -765,11 +772,15 @@ def load(self, filename, ctx, allow_missing=False,
         if not allow_missing:
             for name in self.keys():
                 assert name in arg_dict, \
-                    "Parameter %s is missing in file %s"%(name[lprefix:], filename)
+                    "Parameter '%s' is missing in file '%s', which contains parameters: %s. " \
+                    "Please make sure source and target networks have the same prefix."%(
+                        name[lprefix:], filename, _brief_print_list(arg_dict.keys()))
         for name in arg_dict:
             if name not in self._params:
                 assert ignore_extra, \
-                    "Parameter %s loaded from file %s is not present in ParameterDict"%(
-                        name[lprefix:], filename)
+                    "Parameter '%s' loaded from file '%s' is not present in ParameterDict, " \
+                    "choices are: %s. Set ignore_extra to True to ignore. " \
+                    "Please make sure source and target networks have the same prefix."%(
+                        name[lprefix:], filename, _brief_print_list(self._params.keys()))
                 continue
             self[name]._load_init(arg_dict[name], ctx)
diff --git a/src/imperative/imperative.cc b/src/imperative/imperative.cc
index fbbaf82d177..c5a47407c0c 100644
--- a/src/imperative/imperative.cc
+++ b/src/imperative/imperative.cc
@@ -193,7 +193,8 @@ void Imperative::RecordOp(
       << "Assigning to NDArrays that are already in a computational graph "
       << "will cause undefined behavior when evaluating gradients. "
       << "Please call backward first to clear the graph or do this out side of "
-      << "a record section. ";
+      << "a record section. Also note that you cannot use inplace operations "
+      << "like +=, *=, relu(x, out=x), etc inside a record section.";
   }
 
   bool need_grad = false;


 

----------------------------------------------------------------
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