pytorch網絡轉libtorch常見問題



### <span id="1">一、All inputs of range must be ints, found Tensor in argument 0:</span>
![](https://img2020.cnblogs.com/blog/1188959/202111/1188959-20211115105734283-880018543.png)

**問題**
<font color="red">參數類型不正確,函數的默認參數是tensor</font>

**解決措施**
<font color="red">函數傳入參數不是tensor需要注明類型</font>
我的問題是傳入參數`npoint`是一個`int`類型,沒有注明會報錯,更改如下:
由
```python
def test(npoint):
  ...
```
更改為
```python
def test(npoint: int):
  ...
```

二、Sliced expression not yet supported for subscripted assignment. File a bug if you want this:

問題
不支持賦值給切片表達式

解決措施
根據自己需求,進行修改,可利用循環替代

我將view_shape[1:] = [1] * (len(view_shape) - 1)更改為

    for i in range(1, len(view_shape)):
        view_shape[i] = 1

三、Tried to access nonexistent attribute or method 'len' of type 'torch.torch.nn.modules.container.ModuleList'. Did you forget to initialize an attribute in init()?

問題
forward函數中好像不支持len(nn.ModuleList())和下標訪問

解決措施
如果是一個ModuleList()可以用enumerate函數,多個同維度的可以用zip函數

我這里有兩個ModuleList(),所以采用zip函數,更改如下:

   for i, conv in enumerate(self.mlp_convs):
      bn = self.mlp_bns[i]
      new_points = F.relu(bn(conv(new_points)))

更改為

    for conv, bn in zip(self.mlp_convs, self.mlp_bns):
        new_points = F.relu(bn(conv(new_points)))

ref: https://github.com/pytorch/pytorch/issues/16123


四、Expected integer literal for index

問題和解決方法類似第三個


五、Arguments for call are not valid. The following variants are available

Expected a value of type 'List[Tensor]' for argument 'indices' but instead found type 'List[Optional[Tensor]]'

問題
賦值類型不對,需求是tensor,但給的是int

解決措施

  • 方法1
    int類型的數Ntorch.tensor(N)代替
mask = sqrdists > radius ** 2
group_idx[mask] = N

變為

mask = sqrdists > radius ** 2
group_idx[mask] = torch.tensor(N)
  • 方法2 (速度較慢)
    for循環替代`
mask = sqrdists > radius ** 2
group_idx[mask] = N

變為

B, rows, cols = sqrdists.shape
ref_redius = radius ** 2
for b in range(B):
    for r in range(rows):
        print("r: ", r)
        for c in range(cols):
            if sqrdists[b][r][c] > ref_redius:
                group_idx[b][r][c] = N


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM