MindSpore报错 For ‘MirrorPad‘, paddings must be a Tensor with *

1 报错描述

1.1 系统环境

Hardware Environment(Ascend/GPU/CPU): CPU
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 脚本

训练脚本是通过构建MirrorPad的单算子网络,使用镜像值填充张量。脚本如下:

01  context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
02  class Net(nn.Cell):
03      def __init__(self):
04          super(Net, self).__init__()
05          self.pad = ops.MirrorPad(mode="REFLECT")
06      def construct(self, x, paddings):
07          return self.pad(x, paddings)
08  
09  x = Tensor(np.random.random(size=(2, 3)).astype(np.float32))
10  paddings = Tensor([[1, 1], [2, 2]])
11  pad = Net()
12  output = pad(x, paddings)
13  print(output.shape)

1.2.2 报错

这里报错信息如下:

Traceback (most recent call last):
  File "99553.py", line 20, in <module>
    output = pad(x, paddings)
  File "/root/miniconda3/envs/high_llj/lib/python3.7/site-packages/mindspore/nn/cell.py", line 573, in __call__
    out = self.compile_and_run(*args)
  File "/root/miniconda3/envs/high_llj/lib/python3.7/site-packages/mindspore/nn/cell.py", line 956, in compile_and_run
    self.compile(*inputs)
  File "/root/miniconda3/envs/high_llj/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/miniconda3/envs/high_llj/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/miniconda3/envs/high_llj/lib/python3.7/site-packages/mindspore/ops/operations/nn_ops.py", line 4189, in __infer__
    raise ValueError(f"For '{self.name}', paddings must be a Tensor with type of int64, "
ValueError: For 'MirrorPad', paddings must be a Tensor with type of int64, but got None.

原因分析

我们看报错信息,在ValueError中,写到ValueError: For 'MirrorPad', paddings must be a Tensor with type of int64, but got None.,意思是paddings必须是一个Tensor[INT64], 但是实际得到的是None, 结合脚本第10和12行发现paddings初始化值明明不是None,这是为什么呢。其实这是因为, 在mindspore的图模式下, 常量输入必须在构图的阶段传入, 否则在执行的时候传入就只能得到None。Ps. 在PYNATIVE_MODE不存在这个问题。

2 解决方法

基于上面已知的原因,可以做出两种修改解决此问题。

# 第一种, 在PYNATIVE_MODE下跑脚本:
from mindspore import context

context.set_context(mode=context.PYNATIVE_MODE, device_target="CPU")
class Net(nn.Cell):
    def __init__(self):
        super(Net, self).__init__()
        self.pad = ops.MirrorPad(mode="REFLECT")
    def construct(self, x, paddings):
        return self.pad(x, paddings)

x = Tensor(np.random.random(size=(2, 3)).astype(np.float32))
paddings = Tensor([[1, 1], [2, 2]])
pad = Net()
output = pad(x, paddings)
print(output shape: output.shape)

此时执行成功,输出如下:

output shape: (4, 7)

# 第二种,将paddings在构图的时候当作常量传入:
class Net(nn.Cell):
    def __init__(self):
        super(Net, self).__init__()
        self.pad = ops.MirrorPad(mode="REFLECT")
        self.paddings = Tensor([[1, 1], [2, 2]])

    def construct(self, x):
        return self.pad(x, self.paddings)

x = Tensor(np.random.random(size=(2, 3)).astype(np.float32))
pad = Net()
output = pad(x)
print("output shape: "output.shape)

此时执行成功,输出如下:

output shape: (4, 7)

3 总结

定位报错问题的步骤:

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

2、 ValueError: For 'MirrorPad', paddings must be a Tensor with type of int64, but got None;

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

4 参考文档

4.1 MirrorPad算子API接口

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值