Pytorch数据使用列表的卷积层时报错及解决-RuntimeError: Input type (torch.cuda.HalfTensor) and weight type (torch.FloatTensor) should be same

错误信息

  • RuntimeError: Input type (torch.cuda.HalfTensor) and weight type (torch.FloatTensor) should be same

场景描述

  • 数据调用 list 里的卷积层时出现数据不匹配错误。

  • self.proj = [nn.Conv2d(self.in_chans, self.dim[i], kernel_size=self.patch_size,\
                         stride=self.patch_size) for i in range(len(self.dim))]

解决方法

  • list 改为 nn.ModuleListnn.ModuleList 使用和 list 类似,具体说明见文档

  • self.proj = nn.ModuleList([nn.Conv2d(self.in_chans, self.dim[i], kernel_size=self.patch_size,\
                                       stride=self.patch_size) for i in range(len(self.dim))])
  • 说实话,当初写的时候,Github copilot 提醒我了,即便它看起来就很像用于 PyTorch 的列表,但我还是没用…

You may also like...

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

CAPTCHAis initialing...