乐趣区

关于tensorflow:MindSpore报错ValueError-For-AvgPool-输出形状的每一维都要大于零

1 报错形容
1.1 零碎环境
Hardware Environment(Ascend/GPU/CPU): Ascend
Software Environment:
– MindSpore version (source or binary): 1.8.0
– Python version (e.g., Python 3.7.5): 3.7.6
– OS platform and distribution (e.g., Linux Ubuntu 16.04): Ubuntu 4.15.0-74-generic
– GCC/Compiler version (if compiled from source):

1.2 根本信息
1.2.1 脚本
训练脚本是通过构建 AvgPool 的单算子网络,对输出的多维数据进行二维均匀池化运算。脚本如下:

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 报错
这里报错信息如下:

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.

起因剖析

咱们看报错信息,在 ValueError 中,写到 For’AvgPool’, the each element of the output shape must be larger than 0, but got output shape: [65, 32, -3, 105]. 意思是输入形态的每一个值都应该大于零,理论呈现了正数,个别输入数据范畴不合理很可能是因为输出没有满足要求导致,因而咱们能够从排查各个输出的有效性着手。认真查看官网 API 形容,再联合咱们输出的数据,strides= 1 在正当范畴内,然而 kernel_size=32 不合理,文档阐明了在 Ascend 环境下,kernel_size 的高度和宽度相乘应小于 256, 而 32*32=1024。因而咱们能够调小 kernel_size 的高度或者宽度来解决这个问题。

2 解决办法
基于下面已知的起因,很容易做出如下批改:

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)

此时执行胜利,输入如下:

(65, 32, 6, 114)

3 总结
定位报错问题的步骤:

1、找到报错的用户代码行:output = net(x);

2、依据日志报错信息中的关键字,放大剖析问题的范畴 the each element of the output shape must be larger than 0 ;

3、须要重点关注变量定义、初始化的正确性。

4 参考文档
4.1 AvgPool 算子 API 接口

退出移动版