MindSpore报类型错误:Unsupported parameter type for python primitive..

1 报错描述

1.1 系统环境

*Hardware Environment(Ascend/GPU/CPU): Ascend

Software Environment:

-- MindSpore version (source or binary): 1.6.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 脚本

训练脚本是通过对ReduceMean构建单算子网络,求维度中的所有元素的平均来减少张量的维度。脚本如下:

 01 class Net(nn.Cell):
 02   def __init__(self):
 03     super(Net, self).__init__()
 04     self.reduce_mean = ops.ReduceMean(keep_dims=True)
 05
 06   def construct(self, x):
 07     out = self.reduce_mean(x, axis=(2, 3))
 08     return out
 09
 10 net = Net()
 11 x = Tensor(np.ones((3, 4, 5, 6), dtype=np.float32), mindspore.float32)
 12 out = net(x)
 13 print('out',out.shape)
复制

1.2.2 报错

这里报错信息如下:

Traceback (most recent call last):
 File 'demo.py', line 10, in <module>
​    out = net(x)
…
TypeError: mindspore/ccsrc/pipeline/jit/static_analysis/prim.cc:597 ConvertAbstractToPython] Unsupported parameter type for python primitive, the parameter value is KeywordArg[key : axis, value : (2, 3)]
The function call stack (See file 'rank_0/om/analyze_fail.dat' for more details):
\# 0 In file demo.py(07)
​    out = self.reduce_mean(x, axis=(2, 3))
复制

原因分析

我们看报错信息,在TypeError中,写到*Unsupported parameter type for python primitive, the parameter value is KeywordArg[key : axis, value : (2, 3)],意思是不支持以键值对的方式传参进行初始化,但是你传进去的是一个key为axis, value为(2, 3)的键值对,在之后的out = self.reduce_mean(x, axis=(2, 3))*提示中也指出了报错的代码内容,在官网的用例中也能发现,用例为传值的方式,而不是传键值对。 

2 解决方法

基于上面已知的原因,很容易做出如下修改: 

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

out (3, 4, 1, 1)

3 总结

定位报错问题的步骤:

1、找到报错的用户代码行:out = self.reduce_mean(x, axis=(2, 3)); 2、 根据日志报错信息中的关键字,缩小分析问题的范围:out = self.reduce_mean(x, axis=(2, 3)); 3、需要重点关注变量定义、初始化的正确性。

4 参考文档

4.1 ReduceMean算子API接口

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值