导致这个错误的原因是因为输入数据的类型(torch.cuda.FloatTensor)和模型权重的类型(torch.FloatTensor)不一致,由于模型参数没有被正确地移到GPU上。但是在main函数中我已经将对象model.cuda()了,但还是报错说我模型参数没有在GPU上。
解决办法:
原本代码中我将卷积Conv1d函数写在了forward前向传播中了
class bottleneck(nn.Module):
def __init__(self, input_channels, hidden_channel1, hidden_channel2):
super(bottleneck, self).__init__()
...
def forward(self, x):
...
x_shortcut1 = weight_norm(nn.Conv1d(self.input_channels, self.hidden_channel2, 1, 2, 0, bias=True)(x))
...
return x
将上面代码改为:
class bottleneck(nn.Module):
def __init__(self, input_channels, hidden_channel1, hidden_channel2):
super(bottleneck, self).__init__()
...
self.shortcut_conv = weight_norm(nn.Conv1d(self.input_channels, self.hidden_channel2, 1, 2, 0, bias=True)(x))
def forward(self, x):
...
x_shortcut1 = self.shortcut_conv(x)
...
return x
将卷积定义在init函数中即可,forward函数最好不要直接进行带有权重类型的运算