ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

pytorch网络转libtorch常见问题

2021-11-15 18:04:34  阅读:663  来源: 互联网

标签:常见问题 torch pytorch len libtorch shape new ModuleList view


目录


一、RuntimeError: all inputs of range must be ints, found Tensor in argument 0:

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

三、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()?

四、Expected integer literal for index


一、RuntimeError: all inputs of range must be ints, found Tensor in argument 0:

问题
参数类型不正确,函数的默认参数是tensor

解决措施
函数传入参数不是tensor需要注明类型
我的问题是传入参数npoint是一个int类型,没有注明会报错,更改如下:

def test(npoint):
  ...

更改为

def test(npoint: int):
  ...

二、RuntimeError: 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

问题和解决方法类似第三个

标签:常见问题,torch,pytorch,len,libtorch,shape,new,ModuleList,view
来源: https://www.cnblogs.com/xiaxuexiaoab/p/15555066.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有