这种情况是模型或者数据有的成功to(cuda)了,而有的隐形地还没有成功.cuda()

    大概率是因为model = model.to(device)只对model中实例化在init()中的函数有效,如果在forward中实例化并直接使用则不会将model放置到cuda中,因此很多是不正确地写法,没有成功让layer在init内实例化,导致模型没有成功完全迁移到GPU,但这种情况一般不会直接在该位置报错,因此需要仔细排查。
    错误代码实例


      1. class StdConv1d(nn.Module):
      2. def __init__(self, in_channels, out_channels, kernel_size, input_shape1d):
      3. super(StdConv1d, self).__init__()
      4. self.conv1 = nn.Conv1d(in_channels,
      5. out_channels,
      6. kernel_size = kernel_size,
      7. stride = 1,
      8. padding='same')
      9. #self.bn1 = nn.BatchNorm1d(out_channels)
      10. self.ln = nn.LayerNorm
      11. self.relu = nn.ReLU()
      12. def forward(self, x):
      13. x = self.conv1(x)
      14. #x = self.bn1(x)
      15. x = self.ln(x)
      16. x = self.relu(x)
      17. return x

      1. class StdConv1d(nn.Module):
      2. def __init__(self, in_channels, out_channels, kernel_size, input_shape1d):
      3. super(StdConv1d, self).__init__()
      4. self.conv1 = nn.Conv1d(in_channels,
      5. out_channels,
      6. kernel_size = kernel_size,
      7. stride = 1,
      8. padding='same')
      9. #self.bn1 = nn.BatchNorm1d(out_channels)
      10. #self.ln = nn.LayerNorm
      11. self.relu = nn.ReLU()
      12. def forward(self, x):
      13. x = self.conv1(x)
      14. #x = self.bn1(x)
      15. x = nn.LayerNorm(normalize_shape=x.shape[-1])(x)
      16. x = self.relu(x)
      17. return x