[Solved] MindSpore Error: ValueError: For ‘AvgPool’ every dimension of the output shape must be greater than zero

1 Error description

1.1 System Environment

Hardware Environment(Ascend/GPU/CPU): Ascend
Software Environment:
– MindSpore version (source or binary): 1.8.0
– Python version (eg, Python 3.7.5): 3.7.6
– OS platform and distribution (eg, Linux Ubuntu 16.04): Ubuntu 4.15.0-74-generic
– GCC/Compiler version (if compiled from source):

1.2 Basic information

1.2.1 Script

The training script is to perform a two-dimensional average pooling operation on the input multi-dimensional data by constructing a single-operator network of AvgPool. The script is as follows:


 01 class Net(nn.Cell):
 02     def __init__(self):
 03         super(Net, self).__init__()
 04         self.avgpool_op = ops.AvgPool(pad_mode="VALID", kernel_size=32, strides=1)
 05 
 06     def construct(self, x):
 07         result = self.avgpool_op(x)
 08         return result
 09 
 10 x = Tensor(np.arange(128 * 20 * 32 * 65).reshape(65, 32, 20, 128),mindspore.float32)
 11 net = Net()
 12 output = net(x)
 13 print(output)

1.2.2 Error reporting

The error message here is as follows:


Traceback (most recent call last):
  File "avgpool.py", line 17, in <module>
    output = net(x)
  File "/root/archiconda3/envs/lilinjie_high/lib/python3.7/site-packages/mindspore/nn/cell.py", line 573, in __call__
    out = self.compile_and_run(*args)
  File "/root/archiconda3/envs/lilinjie_high/lib/python3.7/site-packages/mindspore/nn/cell.py", line 956, in compile_and_run
    self.compile(*inputs)
  File "/root/archiconda3/envs/lilinjie_high/lib/python3.7/site-packages/mindspore/nn/cell.py", line 929, in compile
    _cell_graph_executor.compile(self, *inputs, phase=self.phase, auto_parallel_mode=self._auto_parallel_mode)
  File "/root/archiconda3/envs/lilinjie_high/lib/python3.7/site-packages/mindspore/common/api.py", line 1063, in compile
    result = self._graph_executor.compile(obj, args_list, phase, self._use_vm_mode())
  File "/root/archiconda3/envs/lilinjie_high/lib/python3.7/site-packages/mindspore/ops/primitive.py", line 575, in __infer__
    out[track] = fn(*(x[track] for x in args))
  File "/root/archiconda3/envs/lilinjie_high/lib/python3.7/site-packages/mindspore/ops/operations/nn_ops.py", line 1572, in infer_shape
    raise ValueError(f"For '{self.name}', the each element of the output shape must be larger than 0, "
ValueError: For 'AvgPool', the each element of the output shape must be larger than 0, but got output shape: [65, 32, -3, 105]. The input shape: [65, 32, 20, 128], kernel size: (1, 1, 24, 24), strides: (1, 1, 1, 1).Please check the official api documents for more information about the output.

 

2 Solutions

For the reasons known above, it is easy to make the following modifications:


 01 class Net(nn.Cell):
 02     def __init__(self):
 03         super(Net, self).__init__()
 04         self.avgpool_op = ops.AvgPool(pad_mode="VALID", kernel_size=15, strides=1)
 05 
 06     def construct(self, x):
 07         result = self.avgpool_op(x)
 08         return result
 09 
 10 x = Tensor(np.arange(128 * 20 * 32 * 65).reshape(65, 32, 20, 128),mindspore.float32)
 11 net = Net()
 12 output = net(x)
 13 print(output)

At this point, the execution is successful, and the output is as follows:

(65, 32, 6, 114)

3 Summary

Steps to locate the error report:

1. Find the line of user code that reports the error: output = net(x) ;

2. According to the keywords in the log error message, narrow the scope of the analysis problem* the each element of the output shape must be larger than 0* ;

3. It is necessary to focus on the correctness of variable definition and initialization.

Read More:

Leave a Reply

Your email address will not be published. Required fields are marked *