【问题标题】:why: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same为什么:输入类型(torch.cuda.FloatTensor)和权重类型(torch.FloatTensor)应该相同
【发布时间】:2023-04-10 12:34:02
【问题描述】:
我正在使用 Torch 编写神经网络。这是一个我无法解决的小问题。
我已将网络和网络输入都放入 GPU,但训练时出现错误。
RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same
【问题讨论】:
标签:
neural-network
pytorch
conv-neural-network
【解决方案1】:
我已经解决了这个问题。
查看训练函数后,我确认模型和输入数据已加载到 GPU 上。那么问题一定出在网络模型上。当我检查网络模型时,我发现我没有使用像 List 这样的结构。使用其他自定义网络模型时,很可能模型没有加载到 GPU 中。
于是我检查了网络模型的每一层,也就是将每一层都强制到GPU上(.cuda())。根据错误提示所在的位置,在每层尝试后都发现了问题,即调用自定义模型时模型没有加载到GPU上。
最后,只需在网络定义的forward函数中找到调用自定义模型的位置,强制进入GPU(.cuda())。