毕设10086个坑(01)

做MS毕设adversarial network的10086个坑记录表

01 cudaErrorIllegalAddress: an illegal memory access was encountered

此处我的错误是没有把模型放入device。eg:

model=Net()
model.to(devie)

02 多增加一个维度

假设现在有一个二维数据,我们希望再多加一个维度(其实也可以多加0维,1维,这里用2举例子)

x=torch.randn((100, 5), dtype=torch.float)
print(x.shape)
<<<torch.Size([100,5])
x=torch.unsqueeze(x, 2)
print(x.shape)
<<<torch.Size([100,5,1])

03 pytorch 自定义binary cross entropy

重点就是不要忘记在后半部分依旧加一个很小的数字(eg. 1e-6),不然loss会变成nan。
难以置信因为这个我竟然研究了得俩小时。。。

def customized_loss(pred, target):
    pred = torch.clamp(output, 1e-6, 1.0)
    loss = -torch.sum(target*torch.log(pred)+(1-target)*torch.log(1-pred+1e-6),axis=1)
          
    loss=torch.sum(loss)/torch.numel(target)

    return loss

04 根据条件修改tensor

官方文档:
https://pytorch.org/docs/stable/generated/torch.where.html#torch-where

torch.where(condition, x, y) → Tensor

condition为True的时候,输出为x;为False的时候输出y。

>>> x = torch.randn(3, 2)
>>> y = torch.ones(3, 2)
>>> x
tensor([[-0.4620,  0.3139],
        [ 0.3898, -0.7197],
        [ 0.0478, -0.1657]])
>>> torch.where(x > 0, x, y)
tensor([[ 1.0000,  0.3139],
        [ 0.3898,  1.0000],
        [ 0.0478,  1.0000]])
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值