关于人工智能:MindSpore报错-For-‘MirrorPad‘-paddings-must-be-a-Tensor-with

46次阅读

共计 2988 个字符,预计需要花费 8 分钟才能阅读完成。

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

正文完
 0