Pytorch踩坑集锦

1. GPU空间充足,但训练和测试同时进行时,报空间不足,即RuntimeError: CUDA out of memory.

答:很多博文建议batch改小,但是可能很多人的错误在于没有固定网络,导致测试集进入网络时保存了大量参数值,因此:


model.eval()
with torch.no_grad():
     for k, test_data in enumerate(test_loader):

  https://blog.csdn.net/xiaoxifei/article/details/84377204

2. soft-argmax梯度消失

def softargmax2d(input, beta=10):
    #   print(input.type)
    *_, h, w = input.shape

    input = beta * input.reshape(*_, h * w)
    input = nn.functional.softmax(input, dim=-1)

    indices_c, indices_r = np.meshgrid(
        np.linspace(0, 1, w),
        np.linspace(0, 1, h),
        indexing='xy'
    )

    indices_r = torch.from_numpy(np.reshape(indices_r, (-1, h * w))).float().cuda()
    indices_c = torch.from_numpy(np.reshape(indices_c, (-1, h * w))).float().cuda()

    result_r = torch.sum((h - 1) * input * indices_r, dim=-1)
    result_c = torch.sum((w - 1) * input * indices_c, dim=-1)
    result = torch.stack([result_r, result_c], dim=-1)

    return result

增大beta能更快得到积分数值解,即概率最大的图像坐标点。但是,在求导过程中,由于soft-argmax的分母存在exp(beta*x),会导致梯度消失。所以,beta不能任意大。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值