self.SA_modules.append( nn.Sequential( PointnetSAModuleMSG( npoint=cfg.RPN.SA_CONFIG.NPOINTS[k], radii=cfg.RPN.SA_CONFIG.RADIUS[k], nsamples=cfg.RPN.SA_CONFIG.NSAMPLE[k], mlps=mlps, use_xyz=use_xyz, bn=cfg.RPN.USE_BN ), SelfAttention(channel_out) ) )这是SA_modules的定义代码块,而 for i in range(len(self.SA_modules)): li_xyz, li_features = self.SA_modules[i](l_xyz[i], l_features[i]) l_xyz.append(li_xyz) l_features.append(li_features)是SA_modules的调用代码块,运行时程序报错提示: File "/root/autodl-tmp/project/tools/../lib/net/pointnet2_msg.py", line 154, in forward li_xyz, li_features = self.SA_modules[i](l_xyz[i], l_features[i]) File "/root/miniconda3/lib/python3.8/site-packages/torch/nn/modules/module.py", line 727, in _call_impl result = self.forward(*input, **kwargs) TypeError: forward() takes 2 positional arguments but 3 were given,我该如何解决该问题,请给出详细的代码
时间: 2023-11-22 14:52:14 浏览: 63
根据报错信息,你的 `self.SA_modules[i]` 的 forward 方法只接受 2 个位置参数,但是你在调用时传递了 3 个参数。所以你需要检查 `SelfAttention` 类的 forward 方法定义,看看它期望接收几个参数。
如果你的 `SelfAttention` 类确实只接受 2 个参数,那么你需要修改 `nn.Sequential` 的第一个子模块的输出,使其只有 2 个元素,即 `PointnetSAModuleMSG` 的输出只有 `li_features`,而没有 `li_xyz`。可以将以下行代码:
```
li_xyz, li_features = self.SA_modules[i](l_xyz[i], l_features[i])
```
替换为:
```
li_features = self.SA_modules[i](l_xyz[i], l_features[i])
```
这样就只传递了两个参数给 `SelfAttention` 类,解决了报错问题。
完整的修改后的代码如下:
```
self.SA_modules.append(
nn.Sequential(
PointnetSAModuleMSG(
npoint=cfg.RPN.SA_CONFIG.NPOINTS[k],
radii=cfg.RPN.SA_CONFIG.RADIUS[k],
nsamples=cfg.RPN.SA_CONFIG.NSAMPLE[k],
mlps=mlps,
use_xyz=use_xyz,
bn=cfg.RPN.USE_BN
),
SelfAttention(channel_out)
)
)
...
for i in range(len(self.SA_modules)):
li_features = self.SA_modules[i](l_xyz[i], l_features[i])
l_xyz.append(l_xyz[i])
l_features.append(li_features)
```
阅读全文