新手避坑:RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be

问题分析

在这里插入图片描述

  • 说的是输入的数据是放在了CUDA上,而模型不在CUDA上。
  • 这个问题可能是因为部分模型没有加载到CUDA上面造成的。

代码举例

这里定义了一个模型,先进行卷积,再通过注意力通道,重复三次。

class Model(nn.Module):

    def __init__(self, block_units, width_factor):
        super().__init__()

        self.ca = [CAttention(32), CAttention(128), CAttention(256)]

		self.conv1 = nn.Conv2d(3, 32, 3, 2, 1)
		self.conv2 = nn.Conv2d(32, 128, 3, 2, 1)
		self.conv3 = nn.Conv2d(128, 256, 3, 2, 1)
		
	def forward(self, x):
        x = self.conv1(x)
        x = self.ca[0](x)
        
        x = self.conv2(x)
        x = self.ca[1](x)
        
        x = self.conv2(x)
        x = self.ca[2](x)

可以看到,这里的注意力机制放在了一个List里。之后跑出来就是Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same。

错误解析

	去看大佬的讲解,说是一部分模型未加载到Cuda里,可能有的层没放到init下。但是我把所有的层都放在了主干网络里,还是报错。然后发现是因为放有三个注意力通道的List。
	在__init__中用到List,tuple等对象时,一定要注意不要直接用。可以考虑是否能用nn.ModuleList()。

应改为:

class Model(nn.Module):

    def __init__(self, block_units, width_factor):
        super().__init__()

        self.ca = nn.ModuleList([CAttention(in_planes) for in_planes in (32, 64, 256)])

		self.conv1 = nn.Conv2d(3, 32, 3, 2, 1)
		self.conv2 = nn.Conv2d(32, 128, 3, 2, 1)
		self.conv3 = nn.Conv2d(128, 256, 3, 2, 1)
		
	def forward(self, x):
        x = self.conv1(x)
        x = self.ca[0](x)
        
        x = self.conv2(x)
        x = self.ca[1](x)
        
        x = self.conv2(x)
        x = self.ca[2](x)

参考文章

https://blog.csdn.net/qq_42902997/article/details/122594017
https://blog.csdn.net/weixin_36670529/article/details/105910767

新手,轻喷

你可能感兴趣的:(1024程序员节,深度学习,人工智能,pytorch)