AttributeError: Tensor[Int64] object has no attribute: asnumpy
收藏回复举报
AttributeError: Tensor[Int64] object has no attribute: asnumpy
发表于2023-09-14 17:34:10
0 查看

1 系统环境

硬件环境(Ascend/GPU/CPU): Ascend910

MindSpore版本: mindspore=1.10.1

执行模式(PyNative/ Graph):Graph

Python版本: Python=3.9.13

操作系统平台: 不限

2 报错信息

2.1 问题描述

在图模式+Ascend下h_s = int(H_start[idx_H].asnumpy())报错AttributeError: Tensor[Int64] object has no attribute: asnumpy。

具体环境为mindspore-acsend=1.10.1,硬件环境为acsend 910系列

2.2 脚本代码(代码格式,可上传附件)

import mindspore.numpy as np
from mindspore import dtype as mstype
from mindspore import ops, Tensor, context, nn

class AdaptiveAvgPool2d(nn.Cell):
    def init(self, output_size):
    """Initialize AdaptiveAvgPool2d."""
        super(AdaptiveAvgPool2d, self).init()
        self.output_size = output_size

def adaptive_avgpool2d(self, inputs):
    """ NCHW """
    H = self.output_size[0]
    W = self.output_size[1]

    H_start = ops.Cast()(np.arange(start=0, stop=H, dtype=mstype.float32) * (inputs.shape[-2] / H), mstype.int64)
    H_end = ops.Cast()(np.ceil(((np.arange(start=0, stop=H, dtype=mstype.float32)+1) * (inputs.shape[-2] / H))), mstype.int64)

    W_start = ops.Cast()(np.arange(start=0, stop=W, dtype=mstype.float32) * (inputs.shape[-1] / W), mstype.int64)
    W_end = ops.Cast()(np.ceil(((np.arange(start=0, stop=W, dtype=mstype.float32)+1) * (inputs.shape[-1] / W))), mstype.int64)

    pooled2 = []
    for idx_H in range(H):
        pooled1 = []
        for idx_W in range(W):
            h_s = int(H_start[idx_H].asnumpy())
            h_e = int(H_end[idx_H].asnumpy())
            w_s = int(W_start[idx_W].asnumpy())
            w_e = int(W_end[idx_W].asnumpy())
            res = inputs[:, :, h_s:h_e, w_s:w_e]
            pooled1.append(ops.ReduceMean(keep_dims=True)(res, (-2,-1)))
        pooled1 = ops.Concat(-1)(pooled1)
        pooled2.append(pooled1)
    pooled2 = ops.Concat(-2)(pooled2)

    return pooled2

def construct(self, x):
    x = self.adaptive_avgpool2d(x)
    return x

3 根因分析

cke_4310.png

4 解决方案

******此处由用户填写******

包含文字方案和最终脚本代码

请将正确的脚本打包并上传附件

我要发帖子