解决RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be...

        导致这个错误的原因是因为输入数据的类型(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函数最好不要直接进行带有权重类型的运算

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值