1 系统环境
- 硬件环境(Ascend/GPU/CPU): Atlas 300i
- MindSpore版本: mindspore=2.2.1
- 执行模式(PyNative/ Graph): 不限
- Python版本: Python=3.9
- 操作系统平台: Linux
2 报错信息
2.1 问题描述
在模型推理时,预期输出张量形状应为 [batch_size, 10](10分类问题),但实际获取到的输出形状为 [batch_size, 10, 1, 1],导致后续后处理代码报错。
2.2 脚本信息
# 模型定义关键部分
class CustomNet(nn.Cell):
def __init__(self):
super(CustomNet, self).__init__()
self.conv1 = nn.Conv2d(3, 64, kernel_size=3)
self.pool = nn.MaxPool2d(kernel_size=2)
self.fc = nn.Dense(64 * 54 * 54, 10) # 全连接层
def construct(self, x):
x = self.conv1(x)
x = self.pool(x)
x = x.view(x.shape[0], -1)
x = self.fc(x)
return x
# 推理代码
model = load_checkpoint("model.ckpt")
net = CustomNet()
load_param_into_net(net, model)
input_data = Tensor(np.random.rand(32, 3, 224, 224), dtype=mstype.float32)
output = net(input_data)
print("Output shape:", output.shape) # 期望(32,10),实际得到(32,10,1,1)
2.3 报错信息
ValueError: shapes (32,10) and (32,10,1,1) not aligned
3 根因分析
- 此处由用户填写
4 解决方案
- 此处由用户填写
- 包含文字方案和最终脚本代码 请将正确的脚本打包并上传附件