You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by jx...@apache.org on 2018/03/31 06:28:26 UTC

[incubator-mxnet] branch master updated: Fix error messages (#10333)

This is an automated email from the ASF dual-hosted git repository.

jxie pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-mxnet.git


The following commit(s) were added to refs/heads/master by this push:
     new 73273cf  Fix error messages (#10333)
73273cf is described below

commit 73273cf18d88caaa1dcfeef1168a33d01a051948
Author: Eric Junyuan Xie <pi...@users.noreply.github.com>
AuthorDate: Fri Mar 30 23:28:21 2018 -0700

    Fix error messages (#10333)
    
    * Fix error messages
    
    * fix bugs
---
 python/mxnet/gluon/block.py                   |  6 +-
 python/mxnet/gluon/data/vision/transforms.py  |  5 +-
 python/mxnet/gluon/model_zoo/vision/resnet.py |  5 ++
 python/mxnet/gluon/nn/basic_layers.py         |  2 +-
 python/mxnet/gluon/parameter.py               | 93 +++++++++++++++------------
 src/imperative/imperative.cc                  |  3 +-
 6 files changed, 67 insertions(+), 47 deletions(-)

diff --git a/python/mxnet/gluon/block.py b/python/mxnet/gluon/block.py
index 3571b15..fd95641 100644
--- a/python/mxnet/gluon/block.py
+++ b/python/mxnet/gluon/block.py
@@ -226,7 +226,7 @@ class Block(object):
                                   '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 @@ class HybridBlock(Block):
         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 a073467..5e65715 100644
--- a/python/mxnet/gluon/data/vision/transforms.py
+++ b/python/mxnet/gluon/data/vision/transforms.py
@@ -59,14 +59,17 @@ class Compose(Sequential):
                 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 37eb2cc..5ee67b5 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 3801c84..f6113cc 100644
--- a/python/mxnet/gluon/nn/basic_layers.py
+++ b/python/mxnet/gluon/nn/basic_layers.py
@@ -81,7 +81,7 @@ class Sequential(Block):
         """
         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 5a9277b..ab511ab 100644
--- a/python/mxnet/gluon/parameter.py
+++ b/python/mxnet/gluon/parameter.py
@@ -127,7 +127,7 @@ class Parameter(object):
     @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 @@ class Parameter(object):
                     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 @@ class Parameter(object):
         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 @@ class Parameter(object):
         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 @@ class Parameter(object):
         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 @@ class Parameter(object):
     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 @@ class Parameter(object):
         <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 @@ class Parameter(object):
             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 @@ class Parameter(object):
             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 @@ class Parameter(object):
 
         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 @@ class Parameter(object):
         """
         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 @@ class Parameter(object):
         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 @@ class Parameter(object):
         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 @@ class Constant(Parameter):
             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 @@ class ParameterDict(object):
                             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 @@ class ParameterDict(object):
         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 @@ class ParameterDict(object):
             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 @@ class ParameterDict(object):
             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 @@ class ParameterDict(object):
         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 @@ class ParameterDict(object):
         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 fbbaf82..c5a4740 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;

-- 
To stop receiving notification emails like this one, please contact
jxie@apache.org.