pytorch 学习记录
遇到错误汇总
数据格式不统一
Python中 list, numpy.array, torch.Tensor 格式相互转化
数据维度不一致
pytorch学习 中 torch.squeeze() 和torch.unsqueeze()的用法
数据位置不一致会导致出错
输入和模型权重类型不一样,一个CPU,一个GPU,会报错
解决方法: data.to(device)
一、GPU内存释放方法
错误类型: RuntimeError: CUDA out of memory. Tried to allocate 5.49 GiB (GPU 0; 6.00 GiB total capacity; 388.03 MiB already allocated; 4.15 GiB free; 398.00 MiB reserved in total by PyTorch)
解决方法:
- GPU还有其他进程占用显存,导致本进程无法分配到足够的显存
- 缓存过多,使用torch.cuda.empty_cache()清理缓存
报错行前 加
if hasattr(torch.cuda, 'empty_cache'): #hasattr() 函数用于判断对象是否包含对应的属性。
torch.cuda.empty_cache()
- 显卡内存小
import torch
print(torch.__version__)
print(torch.version.cuda)
print(torch.cuda.is_available())
print(torch.cuda.get_device_name(0))
print(torch.cuda.device_count())
print(torch.cuda.current_device())
print(torch.cuda.is_initialized()) # 查看pytorch的 CUDA 状态是否初始化
print(torch.cuda.get_device_capability(0)) # 查看某一个设备device的计算能力,具体输出结果没看明白
输出如下:
transform函数汇总