Pytorch中遇到的一些问题

  1. Q: RuntimeError: invalid argument 2: size ‘[-1 x 3 x 174]’ is invalid for input with 174 elements at /pytorch/torch/lib/TH/THStorage.c:37
if self.reshape:
        base_out = base_out.view((-1, self.num_segments) + base_out.size()[1:])
  ... ...
 x = torch.randn([1,3,224,224])
 x = Variable(x)
 tsn.forward('test',x)

A: 造成这一问题的原因是用于view的tensor的shape是1x174的,而view后的尺寸是-1x3x174的,因此可以用来view的input的维度不够reshape成-1x3x174,因此报错。因为我主要是在TSN的models.py中测试网络结构,所以初始化的x的batch size设置为1了,因此input不够reshape成-1x3x174的尺寸。
2. Q: RuntimeError: matrices expected, got 3D, 3D tensors at /pytorch/torch/lib/TH/generic/THTensorMath.c:1429
A:出现这一错误的原因是利用mm将两个高维矩阵相乘了,对于高维矩阵的乘积,应当将其改为用matmul相乘。
3. Q: TypeError: argument 0 is not a Variable
A: 造成这一问题的原因在于我使用的pytorch版本是0.3.1,在将input数据送入module中前应将tensor包装为Variable类型,否则就会报错,但是之后的版本合并了tensor和variable,所以解决这一问题的方法有两种:升级版本,或将tensor包装成Variable。参考链接
4. Q: RuntimeError: invalid argument 1: input is not contiguous at /pytorch/torch/lib/TH/generic/THTensor.c:231
A: 遇到这个问题一般是使用了view或者index_select函数,
解决方法:
data = data.view(bsz, -1).t().contiguous()#只要在view函数后面再加个contiguous()就ok
5. Missing key(s) in state_dict、Unexpected key(s) in state_dict解决
解决办法:
参考:https://www.cnblogs.com/zhengbiqing/p/10434704.htmlPytorch中遇到的一些问题_第1张图片
未完待续。。。

你可能感兴趣的:(python,计算机视觉,神经网络)