RuntimeError: Input type and weight type should be the same

RuntimeError: Input type and weight type should be the same

我正在尝试将我当前模型中的 resNet 块与 resNext 块交换。一切正常,我什至用 resNet 块训练了 1000 多个时期的模型,但是当我将以下 class 添加到模型时,它返回了这个错误。 (运行 在我的本地 CPU 中没有错误,但在 colab 中 运行 时出现错误)

已添加 Class :

class GroupConv1D(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, padding, stride, groups):
    super(GroupConv1D, self).__init__()

    if not in_channels % groups == 0:
        raise ValueError("The input channels must be divisible by the no. of groups")
    if not out_channels % groups == 0:
        raise ValueError("The output channels must be divisible by the no. of groups")

    self.kernel_size = kernel_size
    self.stride = stride
    self.padding = padding
    self.groups = groups

    self.group_in_num = in_channels // groups
    self.group_out_num = out_channels // groups
    self.conv_list = []

    for i in range(self.groups):
        self.conv_list.append(
            nn.Conv1d(
                in_channels=self.group_out_num,
                out_channels=self.group_out_num,
                kernel_size=kernel_size,
                stride=stride,
                padding=padding)
        )

def forward(self, inputs):
    feature_map_list = []
    for i in range(self.groups):
        x_i = self.conv_list[i](
            inputs[:, i * self.group_in_num: (i + 1) * self.group_in_num]
        )
        feature_map_list.append(x_i)

    out = torch.concat(feature_map_list, dim=1)
    return out

错误:

Traceback (most recent call last):
File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main
  "__main__", mod_spec)
File "/usr/lib/python3.7/runpy.py", line 85, in _run_code
  exec(code, run_globals)
File "/content/drive/MyDrive/FYPprototypeTest2/train.py", line 268, in <module>
  cycleGAN.trainModel()
File "/content/drive/MyDrive/FYPprototypeTest2/train.py", line 140, in trainModel
  B_fake = self.A_generator_B(A_real, A_mask)
File "/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py", line 1102, in 
_call_impl
  return forward_call(*input, **kwargs)
File "/content/drive/MyDrive/FYPprototypeTest2/model.py", line 235, in forward
  resnet_block_1 = self.resnet_block_1(conv2d_conv1d)
File "/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py", line 1102, in 
_call_impl
  return forward_call(*input, **kwargs)
File "/content/drive/MyDrive/FYPprototypeTest2/model.py", line 88, in forward
  group_layer = self.groupConv_1(layer_one_GLU)
File "/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py", line 1102, in 
_call_impl
  return forward_call(*input, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/torch/nn/modules/container.py", line 141, in 
forward
  input = module(input)
File "/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py", line 1102, in 
_call_impl
  return forward_call(*input, **kwargs)
File "/content/drive/MyDrive/FYPprototypeTest2/model.py", line 46, in forward
  inputs[:, i * self.group_in_num: (i + 1) * self.group_in_num]
File "/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py", line 1102, in 
_call_impl
  return forward_call(*input, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/torch/nn/modules/conv.py", line 301, in forward
  return self._conv_forward(input, self.weight, self.bias)
File "/usr/local/lib/python3.7/dist-packages/torch/nn/modules/conv.py", line 298, in 
_conv_forward
  self.padding, self.dilation, self.groups)
RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should 
be the same

非常感谢您的帮助。

你在新 class GroupConv1D 中的问题是你将所有卷积模块存储在 常规 python 列表 self.conv_list 而不是使用 nn Containers.
所有影响 nn.Module 的方法(例如,.to(device).eval() 等)都递归地应用于“根”的所有 相关 成员nn.Module.
但是,pytorch 如何判断哪些是 relevant 成员?
为此,您有 containers:它们将 sub-modules、寄存器和参数组合在一起,以便 pytorch 可以递归地将所有相关的 nn.Module 方法应用于它们。

参见,例如,