MindSpore报错the two input 'input' and 'mask' with shape: [const vector][10] and [const vector][8] can not broadcast.
收藏回复举报
MindSpore报错the two input 'input' and 'mask' with shape: [const vector][10] and [const vector][8] can not broadcast.
发表于2023-01-30 11:32:13
0 查看

1 系统环境

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

MindSpore版本: 1.9.0

执行模式(PyNative/ Graph): Graph

Python版本: Python=3.9.15

操作系统平台: 不限

2 报错信息

2.1  问题描述

在多次调用MindSpore中的ops.masked_时出现报错

2.2  报错信息

ValueError: For 'MaskedSelect', the two input 'input' and 'mask' with shape: [const vector][10] and [const vector][8] can not broadcast.

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

import mindspore as ms
import mindspore.ops as ops
import mindspore.nn as nn
import numpy as np
from mindspore import Tensor

class TestNet(nn.Cell):
    def construct(self, x, y):
        mask = y > 1
        x = ops.masked_select(x, mask[:, None]).reshape(-1, 5)
        y = ops.masked_select(y, mask)

        mask = y > 3
        x = ops.masked_select(x, mask[:, None]).reshape(-1, 5)
        y = ops.masked_select(y, mask)

        mask = y > 5
        x = ops.masked_select(x, mask[:, None]).reshape(-1, 5)
        return x

def main():
    ms.set_context(mode=ms.GRAPH_MODE)
    net = TestNet()

    x = Tensor(np.arange(50).reshape(10, 5))
    y = Tensor(np.arange(10))

    result = net(x, y)
    assert result.shape == (4, 5)

if __name__ == '__main__':
    main()

3 根因分析

******此处由用户补充详细的定位过程******

在动态shape情况下,infer shape的实现中,不应该使用max shape来进行推导,在infer shape的实现中,如果动态shape,则使用-1来推导。

4 解决方案

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

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

支持上传附件

我要发帖子