Pytorch RuntimeError 解决办法
PyTorch 解决办法 RuntimeError
2023-06-13 09:15:24 时间
问题描述
在Pytorch训练自定义数据集中发生如下错误:
RuntimeError: result type Float can't be cast to the desired output type Long
RuntimeError:结果类型 Float 无法转换为所需的输出类型 Long
loss_fn = torch.nn.BCEWithLogitsLoss(pos_weight=torch.tensor([class_weights]))
问题解决
BCEWithLogitsLoss
要求它的目标是一个float
张量,而不是long
。所以应该通过dtype=torch.float32
指定张量的类型。
将上述代码修改如下:
loss_fn = torch.nn.BCEWithLogitsLoss(pos_weight=torch.tensor([class_weights], dtype=torch.float32))
相关文章
- 从零开始学Pytorch(十六)之模型微调
- pytorch 自定义卷积核进行卷积操作[通俗易懂]
- batchnorm pytorch_Pytorch中的BatchNorm
- OpenMMLAB系列框架解读(基于PyTorch)
- PyTorch和Meta「分家」!以后归Linux基金会管了
- pytorch mseloss_pytorch中文手册
- Pytorch创建多任务学习模型(附代码)
- 使用Flask快速部署PyTorch模型
- TensorFlow和PyTorch的实际应用比较
- 使用PyTorch-LSTM进行单变量时间序列预测的示例教程
- PyTorch-24h 01_PyTorch深度学习流程
- PyTorch深度学习(2)
- 利用pytorch实现图像识别demo
- PyTorch 2.0正式版发布!一行代码提速2倍,100%向后兼容
- 用 PyTorch 实现基于字符的循环神经网络