这种情况是模型或者数据有的成功to(cuda)了,而有的隐形地还没有成功.cuda()
大概率是因为model = model.to(device)只对model中实例化在init()中的函数有效,如果在forward中实例化并直接使用则不会将model放置到cuda中,因此很多是不正确地写法,没有成功让layer在init内实例化,导致模型没有成功完全迁移到GPU,但这种情况一般不会直接在该位置报错,因此需要仔细排查。
错误代码实例
class StdConv1d(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, input_shape1d):
super(StdConv1d, self).__init__()
self.conv1 = nn.Conv1d(in_channels,
out_channels,
kernel_size = kernel_size,
stride = 1,
padding='same')
#self.bn1 = nn.BatchNorm1d(out_channels)
self.ln = nn.LayerNorm
self.relu = nn.ReLU()
def forward(self, x):
x = self.conv1(x)
#x = self.bn1(x)
x = self.ln(x)
x = self.relu(x)
return x
class StdConv1d(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, input_shape1d):
super(StdConv1d, self).__init__()
self.conv1 = nn.Conv1d(in_channels,
out_channels,
kernel_size = kernel_size,
stride = 1,
padding='same')
#self.bn1 = nn.BatchNorm1d(out_channels)
#self.ln = nn.LayerNorm
self.relu = nn.ReLU()
def forward(self, x):
x = self.conv1(x)
#x = self.bn1(x)
x = nn.LayerNorm(normalize_shape=x.shape[-1])(x)
x = self.relu(x)
return x